From 4f8645d1be6a4dd9d2feb43d1ee69e8b24cea7a1 Mon Sep 17 00:00:00 2001 From: TinaMor Date: Tue, 10 Jun 2025 02:32:38 +0300 Subject: [PATCH 1/3] Fix premature exit in uninstall command --- containers-toolkit/Public/BuildkitTools.psm1 | 59 ++++++++----------- .../Public/ContainerNetworkTools.psm1 | 2 +- .../Public/ContainerdTools.psm1 | 44 +++++++------- containers-toolkit/Public/NerdctlTools.psm1 | 27 +++++---- 4 files changed, 65 insertions(+), 67 deletions(-) diff --git a/containers-toolkit/Public/BuildkitTools.psm1 b/containers-toolkit/Public/BuildkitTools.psm1 index 2caa245..7f42009 100644 --- a/containers-toolkit/Public/BuildkitTools.psm1 +++ b/containers-toolkit/Public/BuildkitTools.psm1 @@ -46,7 +46,7 @@ function Install-Buildkit { [Parameter(ParameterSetName = 'Setup')] [Parameter(HelpMessage = "Path to Windows CNI plugin. Defaults to `$ENV:ProgramFiles\Containerd\cni")] - [string]$WinCNIPath="$ENV:ProgramFiles\Containerd\cni", + [string]$WinCNIPath = "$ENV:ProgramFiles\Containerd\cni", [Parameter(ParameterSetName = 'Install')] [Parameter(ParameterSetName = 'Setup')] @@ -140,7 +140,7 @@ function Install-Buildkit { } } catch { - Write-Warning "Failed to registed and start Buildkitd service. $_" + Write-Warning "Failed to setup Buildkitd service. $_" } if ($showCommands) { @@ -213,10 +213,10 @@ function Register-BuildkitdService { )] param( [parameter(HelpMessage = "Windows CNI plugin path. Defaults to `$ENV:ProgramFiles\Containerd\cni")] - [String]$WinCNIPath= "$ENV:ProgramFiles\Containerd\cni", + [String]$WinCNIPath = "$ENV:ProgramFiles\Containerd\cni", [parameter(HelpMessage = "Buildkit path. Defaults to `$ENV:ProgramFiles\Buildkit")] - [String]$BuildKitPath= "$ENV:ProgramFiles\Buildkit", + [String]$BuildKitPath = "$ENV:ProgramFiles\Buildkit", [parameter(HelpMessage = "Specify to start Buildkitd service after registration is complete")] [Switch]$Start, @@ -285,7 +285,7 @@ function Register-BuildkitdService { Write-Debug "CNI conf path: $cniConfPath" # Register buildkit service - $command = "$buildkitdExecutable --register-service --debug --containerd-worker=true --containerd-cni-config-path=`"$cniConfPath`" --containerd-cni-binary-dir=`"$cniBinDir`" --service-name buildkitd" + $arguments = "--register-service --debug --containerd-worker=true --containerd-cni-config-path=`"$cniConfPath`" --containerd-cni-binary-dir=`"$cniBinDir`" --service-name buildkitd" if (Test-ConfFileEmpty -Path $cniConfPath) { $consent = $force @@ -295,7 +295,7 @@ function Register-BuildkitdService { if ($consent) { Write-Warning "Containerd conf file not found at $cniConfPath. Buildkit service will be registered without Containerd cni configurations." - $command = "$buildkitdExecutable --register-service --debug --containerd-worker=true --service-name buildkitd" + $arguments = "--register-service --debug --containerd-worker=true --service-name buildkitd" } else { Write-Error "Failed to register buildkit service. Containerd conf file not found at $cniConfPath.`n`t1. Ensure that the required CNI plugins are installed or you can install them using 'Install-WinCNIPlugin'.`n`t2. Create the file to resolve this issue .`n`t3. Rerun this command 'Register-BuildkitdService'" @@ -303,12 +303,8 @@ function Register-BuildkitdService { } } - # remove the executable extension from the command - $escapedPath = [regex]::Escape($buildkitdExecutable) - $arguments = ($command -replace "$escapedPath", "").Trim() - # Register the service - $output = Invoke-ExecutableCommand -Executable $buildkitdExecutable -Arguments $arguments + $output = Invoke-ExecutableCommand -Executable "$buildkitdExecutable" -Arguments $arguments if ($output.ExitCode -ne 0) { Throw "Failed to register buildkitd service. $($output.StandardError.ReadToEnd())" } @@ -386,10 +382,10 @@ function Uninstall-Buildkit { process { if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) { - if (Test-EmptyDirectory -Path "$path") { - Write-Output "$tool does not exist at '$Path' or the directory is empty" - return - } + # if (Test-EmptyDirectory -Path "$path") { + # Write-Output "$tool does not exist at '$Path' or the directory is empty" + # return + # } $consent = $force if (!$ENV:PESTER) { @@ -426,23 +422,20 @@ function Uninstall-BuildkitHelper { [Switch] $Purge ) - if (Test-EmptyDirectory -Path "$Path") { - Write-Error "Buildkit does not exist at '$Path' or the directory is empty." - return - } - - try { - if (Test-ServiceRegistered -Service 'Buildkitd') { - Stop-BuildkitdService - Unregister-Buildkitd -BuildkitPath "$Path" + if (-not (Test-EmptyDirectory -Path "$Path")) { + try { + if (Test-ServiceRegistered -Service 'Buildkitd') { + Stop-BuildkitdService + Unregister-Buildkitd -BuildkitPath "$Path" + } + } + catch { + Throw "Could not stop or unregister buildkitd service. $_" } - } - catch { - Throw "Could not stop or unregister buildkitd service. $_" - } - # Remove the folder where buildkit is installed and related folders - Remove-Item -Path "$Path" -Recurse -Force + # Remove the folder where buildkit is installed and related folders + Remove-Item -Path "$Path" -Recurse -Force + } if ($Purge) { Write-Output "Purging Buildkit program data" @@ -494,10 +487,10 @@ function Unregister-Buildkitd($buildkitPath) { } # Unregister buildkit service - $buildkitdExecutable = "$buildkitPath\bin\buildkitd.exe" + $buildkitdExecutable = (Get-ChildItem -Path "$buildkitPath" -Recurse -Filter "buildkitd.exe").FullName | Select-Object -First 1 - Write-Debug "Buildkitd path: $buildkitdExecutable " - $output = Invoke-ExecutableCommand -Executable $buildkitdExecutable -Arguments "--unregister-service" + Write-Debug "Buildkitd path: '$buildkitdExecutable'" + $output = Invoke-ExecutableCommand -Executable "$buildkitdExecutable" -Arguments "--unregister-service" if ($output.ExitCode -ne 0) { Throw "Could not unregister buildkitd service. $($output.StandardError.ReadToEnd())" } diff --git a/containers-toolkit/Public/ContainerNetworkTools.psm1 b/containers-toolkit/Public/ContainerNetworkTools.psm1 index f3a6a60..cea2ca5 100644 --- a/containers-toolkit/Public/ContainerNetworkTools.psm1 +++ b/containers-toolkit/Public/ContainerNetworkTools.psm1 @@ -383,7 +383,7 @@ function Import-HNSModule { $ModuleName = 'HNS' # https://www.powershellgallery.com/packages/HNS/0.2.4 if (Get-Module -ListAvailable -Name $ModuleName) { - Import-Module -Name $ModuleName -DisableNameChecking -Force:$Force + Get-Module -ListAvailable -Name "$ModuleName" | Import-Module -DisableNameChecking -Force:$force return } diff --git a/containers-toolkit/Public/ContainerdTools.psm1 b/containers-toolkit/Public/ContainerdTools.psm1 index 2476038..4165cea 100644 --- a/containers-toolkit/Public/ContainerdTools.psm1 +++ b/containers-toolkit/Public/ContainerdTools.psm1 @@ -242,13 +242,13 @@ function Register-ContainerdService { Write-Output "Configuring containerd service" - Add-MpPreference -ExclusionProcess $containerdExecutable + Add-MpPreference -ExclusionProcess "$containerdExecutable" # Get default containerd config and write to file $containerdConfigFile = "$ContainerdPath\config.toml" Write-Debug "Containerd config file: $containerdConfigFile" - $output = Invoke-ExecutableCommand -Executable $containerdExecutable -Arguments "config default" + $output = Invoke-ExecutableCommand -Executable "$containerdExecutable" -Arguments "config default" $output.StandardOutput.ReadToEnd() | Out-File -FilePath $containerdConfigFile -Encoding ascii -Force # Check config file is not empty @@ -260,7 +260,7 @@ function Register-ContainerdService { Write-Output "Review containerd configutations at $containerdConfigFile" # Register containerd service - $output = Invoke-ExecutableCommand -Executable $containerdExecutable -Arguments "--register-service --log-level debug --service-name containerd --log-file `"$env:TEMP\containerd.log`"" + $output = Invoke-ExecutableCommand -Executable "$containerdExecutable" -Arguments "--register-service --log-level debug --service-name containerd --log-file `"$env:TEMP\containerd.log`"" if ($output.ExitCode -ne 0) { Throw "Failed to register containerd service. $($output.StandardError.ReadToEnd())" } @@ -331,10 +331,10 @@ function Uninstall-Containerd { process { if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) { - if (Test-EmptyDirectory -Path $path) { - Write-Output "$tool does not exist at $Path or the directory is empty" - return - } + # if (Test-EmptyDirectory -Path $path) { + # Write-Output "$tool does not exist at $Path or the directory is empty" + # return + # } $consent = $force if (!$ENV:PESTER) { @@ -372,23 +372,23 @@ function Uninstall-ContainerdHelper { [Switch] $Purge ) - if (Test-EmptyDirectory -Path "$Path") { - Write-Error "Containerd does not exist at $Path or the directory is empty." - return - } + if (-not (Test-EmptyDirectory -Path "$Path")) { + # Write-Error "Containerd does not exist at $Path or the directory is empty." + # return - try { - if (Test-ServiceRegistered -Service 'containerd') { - Stop-ContainerdService - Unregister-Containerd -ContainerdPath "$Path" + try { + if (Test-ServiceRegistered -Service 'containerd') { + Stop-ContainerdService + Unregister-Containerd -ContainerdPath "$Path" + } + } + catch { + Throw "Could not stop or unregister containerd service. $_" } - } - catch { - Throw "Could not stop or unregister containerd service. $_" - } - # Remove the folder where containerd is installed and related folders - Remove-Item -Path "$Path" -Recurse -Force + # Remove the folder where containerd is installed and related folders + Remove-Item -Path "$Path" -Recurse -Force + } if ($Purge) { Write-Output "Purging Containerd program data" @@ -417,7 +417,7 @@ function Unregister-Containerd ($containerdPath) { # Unregister containerd service $containerdExecutable = (Get-ChildItem -Path "$ContainerdPath" -Recurse -Filter "containerd.exe").FullName | Select-Object -First 1 - $output = Invoke-ExecutableCommand -Executable $containerdExecutable -Arguments "--unregister-service" + $output = Invoke-ExecutableCommand -Executable "$containerdExecutable" -Arguments "--unregister-service" if ($output.ExitCode -ne 0) { Throw "Could not unregister containerd service. $($output.StandardError.ReadToEnd())" } diff --git a/containers-toolkit/Public/NerdctlTools.psm1 b/containers-toolkit/Public/NerdctlTools.psm1 index 8e46a32..74a04a9 100644 --- a/containers-toolkit/Public/NerdctlTools.psm1 +++ b/containers-toolkit/Public/NerdctlTools.psm1 @@ -224,10 +224,11 @@ function Uninstall-Nerdctl { process { if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) { - if (Test-EmptyDirectory -Path "$path") { - Write-Output "$tool does not exist at '$Path' or the directory is empty" - return - } + + # if (Test-EmptyDirectory -Path "$path") { + # Write-Output "$tool does not exist at '$Path' or the directory is empty" + # return + # } $consent = $force if (!$ENV:PESTER) { @@ -239,11 +240,15 @@ function Uninstall-Nerdctl { return } + $executablePath = (Get-ChildItem -Path "$path" -Filter "nerdctl.exe" -Recurse | Select-Object -First 1).FullName + Write-Debug "nerdctl executable path: $executablePath" + Write-Warning "Uninstalling preinstalled $tool at the path '$path'.`n$WhatIfMessage" try { - if ($Purge) { + if ($executablePath -and $Purge) { # Remove all unused images, not just dangling ones - $cmdOutput = Invoke-ExecutableCommand -Executable "$path\nerdctl.exe" -Arguments "system prune --all" + Write-Debug "Pruning all unused images..." + $cmdOutput = Invoke-ExecutableCommand -Executable "$executablePath" -Arguments "system prune --all --force" if ($cmdOutput.ExitCode -ne 0) { Write-Warning "Couldn't prune images. $($cmdOutput.StandardError.ReadToEnd())" } @@ -275,12 +280,12 @@ function Uninstall-NerdctlHelper { ) if (Test-EmptyDirectory -Path "$Path") { - Write-Error "nerdctl does not exist at '$Path' or the directory is empty." - return - } + # Write-Error "nerdctl does not exist at '$Path' or the directory is empty." + # return - # Remove the folder where nerdctl is installed and related folders - Remove-Item -Path "$Path" -Recurse -Force + # Remove the folder where nerdctl is installed and related folders + Remove-Item -Path "$Path" -Recurse -Force + } if ($Purge) { Write-Output "Purging nerdctl program data" From 87e0c5004fe7960820172b385ba0ce91f675bc36 Mon Sep 17 00:00:00 2001 From: TinaMor Date: Mon, 16 Jun 2025 15:03:29 +0300 Subject: [PATCH 2/3] Fix failing tests --- Tests/BuildkitTools.Tests.ps1 | 26 ++++++++++++------- Tests/ContainerNetworkTools.Tests.ps1 | 13 +++++++--- Tests/NerdctlTools.Tests.ps1 | 21 +++++++++++++-- containers-toolkit/Public/BuildkitTools.psm1 | 5 ---- .../Public/ContainerNetworkTools.psm1 | 8 +++--- .../Public/ContainerdTools.psm1 | 8 ------ containers-toolkit/Public/NerdctlTools.psm1 | 11 +------- 7 files changed, 50 insertions(+), 42 deletions(-) diff --git a/Tests/BuildkitTools.Tests.ps1 b/Tests/BuildkitTools.Tests.ps1 index 187aad8..e558d35 100644 --- a/Tests/BuildkitTools.Tests.ps1 +++ b/Tests/BuildkitTools.Tests.ps1 @@ -28,20 +28,27 @@ Describe "BuildkitTools.psm1" { # Mock functions function Test-ServiceRegistered { } Mock Test-ServiceRegistered -ModuleName 'BuildkitTools' -MockWith { return $true } + + function CleanupTestDrive { + Get-ChildItem -Path "$TestDrive" -Force -ErrorAction SilentlyContinue | ` + Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue + Get-ChildItem -Path "TestDrive:\" -Recurse -Force -ErrorAction SilentlyContinue | ` + Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue + } } BeforeEach { - Remove-Item -Path "$TestDrive" -Re -Force -ErrorAction Ignore + CleanupTestDrive } AfterEach { $ENV:PESTER = $false - Remove-Item -Path "$TestDrive" -Re -Force -ErrorAction Ignore + + CleanupTestDrive } AfterAll { - Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore - Remove-Module -Name "$ModuleParentPath\Public\BuildkitTools.psm1" -Force -ErrorAction Ignore + CleanupTestDrive } Context "Install-Buildkit" -Tag "Install-Buildkit" { @@ -63,7 +70,7 @@ Describe "BuildkitTools.psm1" { } AfterEach { - Remove-Item -Path "TestDrive:\" -Force -ErrorAction Ignore + CleanupTestDrive } It 'Should not process on implicit request for validation (WhatIfPreference)' { @@ -130,10 +137,10 @@ Describe "BuildkitTools.psm1" { Should -Invoke Register-BuildkitdService -Times 1 -Exactly -Scope It -ModuleName 'BuildkitTools' ` -ParameterFilter { - $BuildKitPath -eq "$Env:ProgramFiles\Buildkit" -and - $WinCNIPath -eq "$ENV:ProgramFiles\Containerd\cni" - $Start -eq $true - } + $BuildKitPath -eq "$Env:ProgramFiles\Buildkit" -and + $WinCNIPath -eq "$ENV:ProgramFiles\Containerd\cni" + $Start -eq $true + } } It "Should uninstall tool if it is already installed" { @@ -222,7 +229,6 @@ Describe "BuildkitTools.psm1" { $expectedExecutablePath = "$MockBuildKitPath\bin\buildkitd.exe" $expectedCommandArguments = "--register-service --debug --containerd-worker=true --containerd-cni-config-path=`"$MockCniConfPath`" --containerd-cni-binary-dir=`"$MockCniBinDir`" --service-name buildkitd" - Write-Host "'$expectedCommandArguments'" -ForegroundColor Magenta Should -Invoke Invoke-ExecutableCommand -Times 1 -Scope It -ModuleName "BuildkitTools" ` -ParameterFilter { ($Executable -eq $expectedExecutablePath ) -and diff --git a/Tests/ContainerNetworkTools.Tests.ps1 b/Tests/ContainerNetworkTools.Tests.ps1 index 0c728bf..272cabf 100644 --- a/Tests/ContainerNetworkTools.Tests.ps1 +++ b/Tests/ContainerNetworkTools.Tests.ps1 @@ -228,17 +228,24 @@ Describe "ContainerNetworkTools.psm1" { It "Should use HNS module if HostNetworkingService is not installed" { Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' } - Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{} } + Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { + return @{Name = 'HNS'; Path = "TestDrive:\PowerShell\Modules\HNS\HNS.psm1" } + } Initialize-NatNetwork -Force Should -Invoke Import-Module -Times 0 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' } - Should -Invoke Import-Module -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } + Should -Invoke Get-Module -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } + + # NOTE: Because we are piping the HNS module info from Get-Module, Pester's Should -Invoke + # often struggle to intercept commands invoked via the pipeline, especially when parameter + # binding happens implicitly or when the function relies on parameter value from a piped object. + Should -Invoke Import-Module -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' } It "Should throw an error when importing HNS module fails" { Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' } - Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{} } + Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{ Name = 'HNS' } } Mock Import-Module -ModuleName 'ContainerNetworkTools' -MockWith { Throw 'Error message.' } { Initialize-NatNetwork -Force } | Should -Throw "Could not import HNS module. Error message." diff --git a/Tests/NerdctlTools.Tests.ps1 b/Tests/NerdctlTools.Tests.ps1 index e203913..4e62bf8 100644 --- a/Tests/NerdctlTools.Tests.ps1 +++ b/Tests/NerdctlTools.Tests.ps1 @@ -183,7 +183,24 @@ Describe "NerdctlTools.psm1" { } It "Should successfully purge program data" { - Uninstall-Nerdctl -Path 'TestDrive:\Program Files\nerdctl' -Confirm:$false -Force -Purge + $mockPath = 'TestDrive:\Program Files\nerdctl' + Mock Get-ChildItem -ModuleName 'NerdctlTools' -ParameterFilter { $Path -eq "$mockPath" } -MockWith { + return @( + New-MockObject -Type 'System.IO.DirectoryInfo' -Properties @{ + FullName = "$mockPath\nerdctl.exe" + Name = 'nerdctl.exe' + Length = 27259904 + DirectoryName = "$mockPath" + Directory = "$mockPath" + PSIsContainer = $false + } + ) + } + + $mockProcess = New-MockObject -Type 'System.Diagnostics.Process' -Properties @{ ExitCode = 0 } + Mock Invoke-ExecutableCommand -ModuleName "NerdctlTools" -MockWith { return $mockProcess } + + Uninstall-Nerdctl -Path "$mockPath" -Confirm:$false -Force -Purge # Should purge program data Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "NerdctlTools" ` @@ -194,7 +211,7 @@ Describe "NerdctlTools.psm1" { -ParameterFilter { $Feature -eq "nerdctl" } Should -Invoke Invoke-ExecutableCommand -Time 1 -Scope It -ModuleName "NerdctlTools" -ParameterFilter { $executable -eq "TestDrive:\Program Files\nerdctl\nerdctl.exe" -and - $arguments -eq "system prune --all" + $arguments -eq "system prune --all --force" } } diff --git a/containers-toolkit/Public/BuildkitTools.psm1 b/containers-toolkit/Public/BuildkitTools.psm1 index 7f42009..66e2811 100644 --- a/containers-toolkit/Public/BuildkitTools.psm1 +++ b/containers-toolkit/Public/BuildkitTools.psm1 @@ -382,11 +382,6 @@ function Uninstall-Buildkit { process { if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) { - # if (Test-EmptyDirectory -Path "$path") { - # Write-Output "$tool does not exist at '$Path' or the directory is empty" - # return - # } - $consent = $force if (!$ENV:PESTER) { $consent = $force -or $PSCmdlet.ShouldContinue($env:COMPUTERNAME, "Are you sure you want to uninstall Buildkit from '$path'?") diff --git a/containers-toolkit/Public/ContainerNetworkTools.psm1 b/containers-toolkit/Public/ContainerNetworkTools.psm1 index cea2ca5..bb020c4 100644 --- a/containers-toolkit/Public/ContainerNetworkTools.psm1 +++ b/containers-toolkit/Public/ContainerNetworkTools.psm1 @@ -380,10 +380,10 @@ function Import-HNSModule { return } - $ModuleName = 'HNS' - # https://www.powershellgallery.com/packages/HNS/0.2.4 - if (Get-Module -ListAvailable -Name $ModuleName) { - Get-Module -ListAvailable -Name "$ModuleName" | Import-Module -DisableNameChecking -Force:$force + # Check and import HostNetworkingService module + $hnsModule = Get-Module -ListAvailable -Name 'HNS' -ErrorAction SilentlyContinue + if ($hnsModule) { + $hnsModule | Import-Module -DisableNameChecking -Force:$force return } diff --git a/containers-toolkit/Public/ContainerdTools.psm1 b/containers-toolkit/Public/ContainerdTools.psm1 index 4165cea..09d7a86 100644 --- a/containers-toolkit/Public/ContainerdTools.psm1 +++ b/containers-toolkit/Public/ContainerdTools.psm1 @@ -331,11 +331,6 @@ function Uninstall-Containerd { process { if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) { - # if (Test-EmptyDirectory -Path $path) { - # Write-Output "$tool does not exist at $Path or the directory is empty" - # return - # } - $consent = $force if (!$ENV:PESTER) { $consent = $force -or $PSCmdlet.ShouldContinue($Path, 'Are you sure you want to uninstall Containerd?') @@ -373,9 +368,6 @@ function Uninstall-ContainerdHelper { ) if (-not (Test-EmptyDirectory -Path "$Path")) { - # Write-Error "Containerd does not exist at $Path or the directory is empty." - # return - try { if (Test-ServiceRegistered -Service 'containerd') { Stop-ContainerdService diff --git a/containers-toolkit/Public/NerdctlTools.psm1 b/containers-toolkit/Public/NerdctlTools.psm1 index 74a04a9..d4c8e38 100644 --- a/containers-toolkit/Public/NerdctlTools.psm1 +++ b/containers-toolkit/Public/NerdctlTools.psm1 @@ -224,12 +224,6 @@ function Uninstall-Nerdctl { process { if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) { - - # if (Test-EmptyDirectory -Path "$path") { - # Write-Output "$tool does not exist at '$Path' or the directory is empty" - # return - # } - $consent = $force if (!$ENV:PESTER) { $consent = $force -or $PSCmdlet.ShouldContinue($Path, 'Are you sure you want to uninstall nerdctl?') @@ -279,10 +273,7 @@ function Uninstall-NerdctlHelper { [Switch] $Purge ) - if (Test-EmptyDirectory -Path "$Path") { - # Write-Error "nerdctl does not exist at '$Path' or the directory is empty." - # return - + if (-not (Test-EmptyDirectory -Path "$Path")) { # Remove the folder where nerdctl is installed and related folders Remove-Item -Path "$Path" -Recurse -Force } From 2fbc4c7257acab17f98f06f7b911d11d8309ed3a Mon Sep 17 00:00:00 2001 From: TinaMor Date: Mon, 16 Jun 2025 15:39:24 +0300 Subject: [PATCH 3/3] Use $TestDrive instead of TestDtivr:\ in Buildkit Tests --- Tests/BuildkitTools.Tests.ps1 | 28 +++++++++----------- containers-toolkit/Public/BuildkitTools.psm1 | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Tests/BuildkitTools.Tests.ps1 b/Tests/BuildkitTools.Tests.ps1 index e558d35..62b1368 100644 --- a/Tests/BuildkitTools.Tests.ps1 +++ b/Tests/BuildkitTools.Tests.ps1 @@ -32,8 +32,6 @@ Describe "BuildkitTools.psm1" { function CleanupTestDrive { Get-ChildItem -Path "$TestDrive" -Force -ErrorAction SilentlyContinue | ` Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue - Get-ChildItem -Path "TestDrive:\" -Recurse -Force -ErrorAction SilentlyContinue | ` - Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue } } @@ -111,10 +109,10 @@ Describe "BuildkitTools.psm1" { } It "Should call function with user-specified values" { - $customPath = "TestDrive:\Downloads\buildkit-v0.2.3.windows-amd64.tar.gz" + $customPath = "$TestDrive\Downloads\buildkit-v0.2.3.windows-amd64.tar.gz" Mock Get-InstallationFile -ModuleName 'BuildkitTools' -MockWith { return $customPath } - Install-Buildkit -Version '0.2.3' -InstallPath 'TestDrive:\BuildKit' -DownloadPath 'TestDrive:\Downloads' -OSArchitecture "arm64" -Force -Confirm:$false + Install-Buildkit -Version '0.2.3' -InstallPath "$TestDrive\BuildKit" -DownloadPath "$TestDrive\Downloads" -OSArchitecture "arm64" -Force -Confirm:$false Should -Invoke Uninstall-Buildkit -ModuleName 'BuildkitTools' -Times 0 -Exactly -Scope It Should -Invoke Get-InstallationFile -ModuleName 'BuildkitTools' -ParameterFilter { @@ -123,9 +121,9 @@ Describe "BuildkitTools.psm1" { } Should -Invoke Install-RequiredFeature -ModuleName 'BuildkitTools' -ParameterFilter { $Feature -eq "Buildkit" -and - $InstallPath -eq 'TestDrive:\BuildKit' -and + $InstallPath -eq "$TestDrive\BuildKit" -and $SourceFile -eq "$customPath" -and - $EnvPath -eq 'TestDrive:\Buildkit\bin' -and + $EnvPath -eq "$TestDrive\Buildkit\bin" -and $cleanup -eq $true } } @@ -310,7 +308,7 @@ Describe "BuildkitTools.psm1" { Context "Uninstall-Buildkit" -Tag "Uninstall-Buildkit" { BeforeAll { - Mock Get-DefaultInstallPath -ModuleName 'BuildkitTools' -MockWith { return 'TestDrive:\Program Files\Buildkit' } + Mock Get-DefaultInstallPath -ModuleName 'BuildkitTools' -MockWith { return "$TestDrive\Program Files\Buildkit" } Mock Test-EmptyDirectory -ModuleName 'BuildkitTools' -MockWith { return $false } Mock Stop-BuildkitdService -ModuleName 'BuildkitTools' Mock Unregister-Buildkitd -ModuleName 'BuildkitTools' @@ -320,7 +318,7 @@ Describe "BuildkitTools.psm1" { } It "Should successfully uninstall Buildkit" { - Uninstall-Buildkit -Path 'TestDrive:\Custom\Buildkit\' -Confirm:$false -Force + Uninstall-Buildkit -Path "$TestDrive\Custom\Buildkit\" -Confirm:$false -Force # Should stop and deregister the buildkitd service Should -Invoke Stop-BuildkitdService -Times 1 -Scope It -ModuleName "BuildkitTools" @@ -328,7 +326,7 @@ Describe "BuildkitTools.psm1" { # Should remove buildkit dir Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "BuildkitTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Custom\Buildkit\bin' } + -ParameterFilter { $Path -eq "$TestDrive\Custom\Buildkit\bin" } # Should not purge program data Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "BuildkitTools" ` @@ -343,15 +341,15 @@ Describe "BuildkitTools.psm1" { Uninstall-Buildkit -Confirm:$false -Force Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "BuildkitTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Buildkit\bin' } + -ParameterFilter { $Path -eq "$TestDrive\Program Files\Buildkit\bin" } } It "Should successfully purge program data" { - Uninstall-Buildkit -Path 'TestDrive:\Program Files\Buildkit' -Confirm:$false -Force -Purge + Uninstall-Buildkit -Path "$TestDrive\Program Files\Buildkit" -Confirm:$false -Force -Purge # Should purge program data Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "BuildkitTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Buildkit' } + -ParameterFilter { $Path -eq "$TestDrive\Program Files\Buildkit" } Should -Invoke Uninstall-ProgramFiles -Times 1 -Scope It -ModuleName "BuildkitTools" ` -ParameterFilter { $Path -eq "$ENV:ProgramData\Buildkit" } Should -Invoke Remove-FeatureFromPath -Times 1 -Scope It -ModuleName "BuildkitTools" ` @@ -360,7 +358,7 @@ Describe "BuildkitTools.psm1" { It "Should do nothing if user does not consent to uninstalling Buildkit" { $ENV:PESTER = $true - Uninstall-Buildkit -Path 'TestDrive:\Program Files\Buildkit' -Confirm:$false -Force:$false + Uninstall-Buildkit -Path "$TestDrive\Program Files\Buildkit" -Confirm:$false -Force:$false # Should NOT stop and deregister the buildkit service Should -Invoke Stop-BuildkitdService -Times 0 -Scope It -ModuleName "BuildkitTools" @@ -373,7 +371,7 @@ Describe "BuildkitTools.psm1" { It "Should do nothing if buildkit is not installed at specified path" { Mock Test-EmptyDirectory -ModuleName 'BuildkitTools' -MockWith { return $true } - Uninstall-Buildkit -Path 'TestDrive:\Program Files\Buildkit' -Confirm:$false + Uninstall-Buildkit -Path "$TestDrive\Program Files\Buildkit" -Confirm:$false Should -Invoke Stop-BuildkitdService -Times 0 -Scope It -ModuleName "BuildkitTools" Should -Invoke Unregister-Buildkitd -Times 0 -Scope It -ModuleName "BuildkitTools" @@ -383,7 +381,7 @@ Describe "BuildkitTools.psm1" { It "Should throw an error if buildkitd service stop or unregister was unsuccessful" { Mock Stop-BuildkitdService -ModuleName 'BuildkitTools' -MockWith { Throw 'Error' } - { Uninstall-Buildkit -Path 'TestDrive:\Program Files\Buildkit' -Confirm:$false -Force -Purge } | Should -Throw "*Could not stop or unregister buildkitd service.*" + { Uninstall-Buildkit -Path "$TestDrive\Program Files\Buildkit" -Confirm:$false -Force -Purge } | Should -Throw "*Could not stop or unregister buildkitd service.*" Should -Invoke Unregister-Buildkitd -Times 0 -Scope It -ModuleName "BuildkitTools" Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "BuildkitTools" Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "BuildkitTools" diff --git a/containers-toolkit/Public/BuildkitTools.psm1 b/containers-toolkit/Public/BuildkitTools.psm1 index 66e2811..6b93576 100644 --- a/containers-toolkit/Public/BuildkitTools.psm1 +++ b/containers-toolkit/Public/BuildkitTools.psm1 @@ -140,7 +140,7 @@ function Install-Buildkit { } } catch { - Write-Warning "Failed to setup Buildkitd service. $_" + Write-Warning "Failed to set up Buildkitd service. $_" } if ($showCommands) {