From 93bd27de37a98972ea144ef839a963730be1b990 Mon Sep 17 00:00:00 2001 From: TinaMor Date: Thu, 30 Jan 2025 16:59:45 +0300 Subject: [PATCH] Add unit tests for purge functionality and bug fixes --- Tests/BuildkitTools.Tests.ps1 | 60 +++++++++++-------- Tests/ContainerNetworkTools.Tests.ps1 | 29 ++++----- Tests/ContainerdTools.Tests.ps1 | 59 ++++++++++-------- Tests/NerdctlTools.Tests.ps1 | 52 ++++++++-------- containers-toolkit/Public/BuildkitTools.psm1 | 10 +++- .../Public/ContainerNetworkTools.psm1 | 16 +++-- .../Public/ContainerdTools.psm1 | 8 ++- containers-toolkit/Public/NerdctlTools.psm1 | 3 +- 8 files changed, 131 insertions(+), 106 deletions(-) diff --git a/Tests/BuildkitTools.Tests.ps1 b/Tests/BuildkitTools.Tests.ps1 index 369d3bc..31e83b6 100644 --- a/Tests/BuildkitTools.Tests.ps1 +++ b/Tests/BuildkitTools.Tests.ps1 @@ -302,35 +302,36 @@ Describe "BuildkitTools.psm1" { } It "Should successfully uninstall Buildkit" { - Mock Uninstall-BuildkitHelper -ModuleName 'BuildkitTools' + Uninstall-Buildkit -Path 'TestDrive:\Custom\Buildkit\' -Confirm:$false -Force - Uninstall-Buildkit -Path 'TestDrive:\Program Files\Buildkit' -Confirm:$false -Force + # Should stop and deregister the buildkitd service + Should -Invoke Stop-BuildkitdService -Times 1 -Scope It -ModuleName "BuildkitTools" + Should -Invoke Unregister-Buildkitd -Times 1 -Scope It -ModuleName "BuildkitTools" - Should -Invoke Uninstall-BuildkitHelper -Times 1 -Scope It -ModuleName "BuildkitTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Buildkit' } + # Should remove buildkit dir + Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "BuildkitTools" ` + -ParameterFilter { $Path -eq 'TestDrive:\Custom\Buildkit\bin' } + + # Should not purge program data + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "BuildkitTools" ` + -ParameterFilter { $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\buildkit' } + Should -Invoke Uninstall-ProgramFiles -Times 0 -Scope It -ModuleName "BuildkitTools" ` + -ParameterFilter { $Path -eq "$ENV:ProgramData\Buildkit" } + Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "BuildkitTools" ` + -ParameterFilter { $Feature -eq "buildkit" } } It "Should successfully uninstall Buildkit from default path" { - Mock Uninstall-BuildkitHelper -ModuleName 'BuildkitTools' - Uninstall-Buildkit -Confirm:$false -Force - Should -Invoke Uninstall-BuildkitHelper -Times 1 -Scope It -ModuleName "BuildkitTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Buildkit' } + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "BuildkitTools" ` + -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Buildkit\bin' } } - It "Should throw an error if user does not consent to uninstalling Buildkit" { - $ENV:PESTER = $true - { Uninstall-Buildkit -Path 'TestDrive:\Program Files\Buildkit' -Confirm:$false -Force:$false } | Should -Throw "Buildkit uninstallation cancelled." - } - - It "Should successfully call uninstall Buildkit helper function" { - Uninstall-BuildkitHelper -Path 'TestDrive:\Program Files\Buildkit' + It "Should successfully purge program data" { + Uninstall-Buildkit -Path 'TestDrive:\Program Files\Buildkit' -Confirm:$false -Force -Purge - Should -Invoke Stop-BuildkitdService -Times 1 -Scope It -ModuleName "BuildkitTools" - Should -Invoke Unregister-Buildkitd -Times 1 -Scope It -ModuleName "BuildkitTools" - Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "BuildkitTools" ` - -ParameterFilter { $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\buildkit' } + # Should purge program data Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "BuildkitTools" ` -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Buildkit' } Should -Invoke Uninstall-ProgramFiles -Times 1 -Scope It -ModuleName "BuildkitTools" ` @@ -339,26 +340,35 @@ Describe "BuildkitTools.psm1" { -ParameterFilter { $Feature -eq "buildkit" } } + 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 + + # Should NOT stop and deregister the buildkit service + Should -Invoke Stop-BuildkitdService -Times 0 -Scope It -ModuleName "BuildkitTools" + Should -Invoke Unregister-Buildkitd -Times 0 -Scope It -ModuleName "BuildkitTools" + + # Should NOT remove buildkit binaries/dir + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "BuildkitTools" + } + It "Should do nothing if buildkit is not installed at specified path" { Mock Test-EmptyDirectory -ModuleName 'BuildkitTools' -MockWith { return $true } - Uninstall-BuildkitHelper -Path 'TestDrive:\Program Files\Buildkit' + 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" Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "BuildkitTools" - Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "BuildkitTools" - - $Error[0].Exception.Message | Should -BeExactly 'Buildkit does not exist at TestDrive:\Program Files\Buildkit or the directory is empty.' } It "Should throw an error if buildkitd service stop or unregister was unsuccessful" { Mock Stop-BuildkitdService -ModuleName 'BuildkitTools' -MockWith { Throw 'Error' } - { Uninstall-BuildkitHelper -Path 'TestDrive:\Program Files\Buildkit' } | 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" } } -} \ No newline at end of file +} diff --git a/Tests/ContainerNetworkTools.Tests.ps1 b/Tests/ContainerNetworkTools.Tests.ps1 index abf3ac8..5a158df 100644 --- a/Tests/ContainerNetworkTools.Tests.ps1 +++ b/Tests/ContainerNetworkTools.Tests.ps1 @@ -252,42 +252,33 @@ Describe "ContainerNetworkTools.psm1" { } It "Should successfully uninstall WinCNI plugins" { - Mock Uninstall-WinCNIPluginHelper -ModuleName 'ContainerNetworkTools' + Uninstall-WinCNIPlugin -Path 'TestDrive:\Program Files' -Confirm:$false -Force - Uninstall-WinCNIPlugin -Confirm:$false -Path 'TestDrive:\Program Files\cni' -Force - - Should -Invoke Uninstall-WinCNIPluginHelper -Times 1 -Scope It -ModuleName "ContainerNetworkTools" ` + # Should remove containerd/cni dir + Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "ContainerNetworkTools" ` -ParameterFilter { $Path -eq 'TestDrive:\Program Files\cni' } } It "Should successfully uninstall WinCNI plugins from default path" { - Mock Uninstall-WinCNIPluginHelper -ModuleName 'ContainerNetworkTools' - Uninstall-WinCNIPlugin -Confirm:$false -Force - Should -Invoke Uninstall-WinCNIPluginHelper -Times 1 -Scope It -ModuleName "ContainerNetworkTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Containerd\cni' } + Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "ContainerNetworkTools" ` + -ParameterFilter { $Path -eq "$ENV:ProgramFiles\Containerd\cni" } } - It "Should throw an error if user does not consent to uninstalling WinCNIPlugin" { + It "Should do nothing if user does not consent to uninstalling WinCNIPlugin" { $ENV:PESTER = $true - { Uninstall-WinCNIPlugin -Confirm:$false -Path 'TestDrive:\Program Files\cni' -Force:$false } | Should -Throw "Windows CNI plugins uninstallation cancelled." - } + Uninstall-WinCNIPlugin -Confirm:$false -Force:$false - It "Should successfully call uninstall WinCNIPlugin helper function" { - Uninstall-WinCNIPluginHelper -Path 'TestDrive:\TestDir\cni' - - Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "ContainerNetworkTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\TestDir\cni' } + # Should NOT remove WinCNIPlugin binaries/dir + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "ContainerNetworkTools" } It "Should do nothing if WinCNI plugins is not installed at specified path" { Mock Test-EmptyDirectory -ModuleName 'ContainerNetworkTools' -MockWith { return $true } - Uninstall-WinCNIPluginHelper -Path 'TestDrive:\TestDir\cni' + Uninstall-WinCNIPlugin -Path 'TestDrive:\TestDir\cni' -Confirm:$false Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "ContainerNetworkTools" - - $Error[0].Exception.Message | Should -Be 'Windows CNI plugin does not exist at TestDrive:\TestDir\cni or the directory is empty.' } } } \ No newline at end of file diff --git a/Tests/ContainerdTools.Tests.ps1 b/Tests/ContainerdTools.Tests.ps1 index fd3893b..bda6c42 100644 --- a/Tests/ContainerdTools.Tests.ps1 +++ b/Tests/ContainerdTools.Tests.ps1 @@ -279,35 +279,37 @@ Describe "ContainerdTools.psm1" { } It "Should successfully uninstall Containerd" { - Mock Uninstall-ContainerdHelper -ModuleName 'ContainerdTools' + Uninstall-Containerd -Path 'TestDrive:\Custom\Containerd\' -Confirm:$false -Force - Uninstall-Containerd -Path 'TestDrive:\Program Files\Containerd' -Confirm:$false -Force + # Should stop and deregister the containerd service + Should -Invoke Stop-ContainerdService -Times 1 -Scope It -ModuleName "ContainerdTools" + Should -Invoke Unregister-Containerd -Times 1 -Scope It -ModuleName "ContainerdTools" - Should -Invoke Uninstall-ContainerdHelper -Times 1 -Scope It -ModuleName "ContainerdTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Containerd' } + # Should remove containerd binaries only not the entire dir + # The containerd dir contains cni binaries and config.toml + Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "ContainerdTools" ` + -ParameterFilter { $Path -eq 'TestDrive:\Custom\Containerd\bin' } + + # Should not purge program data + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "ContainerdTools" ` + -ParameterFilter { $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\containerd' } + Should -Invoke Uninstall-ProgramFiles -Times 0 -Scope It -ModuleName "ContainerdTools" ` + -ParameterFilter { $Path -eq "$ENV:ProgramData\Containerd" } + Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "ContainerdTools" ` + -ParameterFilter { $Feature -eq "containerd" } } It "Should successfully uninstall Containerd from default path" { - Mock Uninstall-ContainerdHelper -ModuleName 'ContainerdTools' - Uninstall-Containerd -Confirm:$false -Force - Should -Invoke Uninstall-ContainerdHelper -Times 1 -Scope It -ModuleName "ContainerdTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Containerd' } - } - - It "Should throw an error if user does not consent to uninstalling Containerd" { - $ENV:PESTER = $true - { Uninstall-Containerd -Path 'TestDrive:\Program Files\Containerd' -Confirm:$false -Force:$false } | Should -Throw "Containerd uninstallation cancelled." + Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "ContainerdTools" ` + -ParameterFilter { $Path -eq "$ENV:ProgramFiles\Containerd\bin" } } - It "Should successfully call uninstall Containerd helper function" { - Uninstall-ContainerdHelper -Path 'TestDrive:\Program Files\Containerd' + It "Should successfully purge program data" { + Uninstall-Containerd -Path 'TestDrive:\Program Files\Containerd' -Confirm:$false -Force -Purge - Should -Invoke Stop-ContainerdService -Times 1 -Scope It -ModuleName "ContainerdTools" - Should -Invoke Unregister-Containerd -Times 1 -Scope It -ModuleName "ContainerdTools" - Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "ContainerdTools" ` - -ParameterFilter { $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\containerd' } + # Should purge program data Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "ContainerdTools" ` -ParameterFilter { $Path -eq 'TestDrive:\Program Files\Containerd' } Should -Invoke Uninstall-ProgramFiles -Times 1 -Scope It -ModuleName "ContainerdTools" ` @@ -316,23 +318,32 @@ Describe "ContainerdTools.psm1" { -ParameterFilter { $Feature -eq "containerd" } } + It "Should do nothing if user does not consent to uninstalling Containerd" { + $ENV:PESTER = $true + Uninstall-Containerd -Path 'TestDrive:\Program Files\Containerd' -Confirm:$false -Force:$false + + # Should NOT stop and deregister the containerd service + Should -Invoke Stop-ContainerdService -Times 0 -Scope It -ModuleName "ContainerdTools" + Should -Invoke Unregister-Containerd -Times 0 -Scope It -ModuleName "ContainerdTools" + + # Should NOT remove containerd binaries/dir + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "ContainerdTools" + } + It "Should do nothing if containerd is not installed at specified path" { Mock Test-EmptyDirectory -ModuleName 'ContainerdTools' -MockWith { return $true } - Uninstall-ContainerdHelper -Path 'TestDrive:\Program Files\Containerd' + Uninstall-Containerd -Path 'TestDrive:\Program Files\Containerd' -Confirm:$false Should -Invoke Stop-ContainerdService -Times 0 -Scope It -ModuleName "ContainerdTools" Should -Invoke Unregister-Containerd -Times 0 -Scope It -ModuleName "ContainerdTools" Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "ContainerdTools" - Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "ContainerdTools" - - $Error[0].Exception.Message | Should -BeExactly 'Containerd does not exist at TestDrive:\Program Files\Containerd or the directory is empty.' } It "Should throw an error if containerd service stop or unregister was unsuccessful" { Mock Stop-ContainerdService -ModuleName 'ContainerdTools' -MockWith { Throw 'Error' } - { Uninstall-ContainerdHelper -Path 'TestDrive:\Program Files\Containerd' } | Should -Throw "Could not stop or unregister containerd service.*" + { Uninstall-Containerd -Path 'TestDrive:\Program Files\Containerd' -Confirm:$false -Force -Purge } | Should -Throw "*Could not stop or unregister containerd service.*" Should -Invoke Unregister-Containerd -Times 0 -Scope It -ModuleName "ContainerdTools" Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "ContainerdTools" Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "ContainerdTools" diff --git a/Tests/NerdctlTools.Tests.ps1 b/Tests/NerdctlTools.Tests.ps1 index 75e3f52..0738de0 100644 --- a/Tests/NerdctlTools.Tests.ps1 +++ b/Tests/NerdctlTools.Tests.ps1 @@ -160,39 +160,30 @@ Describe "NerdctlTools.psm1" { } It "Should successfully uninstall nerdctl" { - Mock Uninstall-NerdctlHelper -ModuleName 'NerdctlTools' + Uninstall-Nerdctl -Path 'TestDrive:\Custom\nerdctl\' -Confirm:$false -Force - Uninstall-Nerdctl -Path 'TestDrive:\Program Files\nerdctl' -Confirm:$false -Force + # Should remove nerdctl dir + Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "NerdctlTools" ` + -ParameterFilter { $Path -eq 'TestDrive:\Custom\nerdctl\' } - Should -Invoke Uninstall-NerdctlHelper -Times 1 -Scope It -ModuleName "NerdctlTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\nerdctl' } + # Should not purge program data + Should -Invoke Uninstall-ProgramFiles -Times 0 -Scope It -ModuleName "NerdctlTools" ` + -ParameterFilter { $Path -eq "$ENV:ProgramData\nerdctl" } + Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "NerdctlTools" ` + -ParameterFilter { $Feature -eq "nerdctl" } } It "Should successfully uninstall nerdctl from default path" { - Mock Uninstall-NerdctlHelper -ModuleName 'NerdctlTools' - Uninstall-Nerdctl -Confirm:$false -Force - Should -Invoke Uninstall-NerdctlHelper -Times 1 -Scope It -ModuleName "NerdctlTools" ` - -ParameterFilter { $Path -eq 'TestDrive:\Program Files\nerdctl' } - } - - It "Should throw an error if user does not consent to uninstalling nerdctl" { - $ENV:PESTER = $true - { Uninstall-Nerdctl -Confirm:$false -Path 'TestDrive:\Program Files\nerdctl'-Force:$false } | Should -Throw 'nerdctl uninstallation cancelled.' - } - - It "Should do nothing if nerdctl is not installed at specified path" { - Mock Test-EmptyDirectory -ModuleName 'NerdctlTools' -MockWith { return $true } - - Uninstall-Nerdctl -Confirm:$false -Force - Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "NerdctlTools" - Should -Invoke Remove-FeatureFromPath -Times 0 -Scope It -ModuleName "NerdctlTools" + Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "NerdctlTools" ` + -ParameterFilter { $Path -eq "$ENV:ProgramFiles\nerdctl" } } - It "Should successfully call uninstall nerdctl helper function" { - Uninstall-NerdctlHelper -Path 'TestDrive:\Program Files\nerdctl' + It "Should successfully purge program data" { + Uninstall-Nerdctl -Path 'TestDrive:\Program Files\nerdctl' -Confirm:$false -Force -Purge + # Should purge program data Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "NerdctlTools" ` -ParameterFilter { $Path -eq 'TestDrive:\Program Files\nerdctl' } Should -Invoke Uninstall-ProgramFiles -Times 1 -Scope It -ModuleName "NerdctlTools" ` @@ -201,12 +192,19 @@ Describe "NerdctlTools.psm1" { -ParameterFilter { $Feature -eq "nerdctl" } } - It "Should write an error if nerdctl is not installed at specified path" { - Mock Test-EmptyDirectory -ModuleName 'NerdctlTools' -MockWith { return $true } + It "Should do nothing if user does not consent to uninstalling nerdctl" { + $ENV:PESTER = $true + Uninstall-Nerdctl -Path 'TestDrive:\Program Files\nerdctl' -Confirm:$false -Force:$false - Uninstall-NerdctlHelper -Path 'TestDrive:\Program Files\nerdctl' + # Should NOT remove nerdctl binaries/dir + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "NerdctlTools" + } + + It "Should do nothing if nerdctl is not installed at specified path" { + Mock Test-EmptyDirectory -ModuleName 'NerdctlTools' -MockWith { return $true } - $Error[0].Exception.Message | Should -BeExactly 'nerdctl does not exist at TestDrive:\Program Files\nerdctl or the directory is empty.' + Uninstall-Nerdctl -Path 'TestDrive:\Program Files\nerdctl' -Confirm:$false + Should -Invoke Remove-Item -Times 0 -Scope It -ModuleName "NerdctlTools" } } } diff --git a/containers-toolkit/Public/BuildkitTools.psm1 b/containers-toolkit/Public/BuildkitTools.psm1 index d280832..b54d406 100644 --- a/containers-toolkit/Public/BuildkitTools.psm1 +++ b/containers-toolkit/Public/BuildkitTools.psm1 @@ -354,6 +354,13 @@ function Uninstall-Buildkit { $Path = Get-DefaultInstallPath -Tool $tool } + # If we are not purging, we are uninstalling from the bin directory + # that contains the buildkit binaries, buildkit/bin + $path = $path.TrimEnd("\") + if (-not $Purge -and (-not $path.EndsWith("\bin"))) { + $path = $path.Trim() + "\bin" + } + $WhatIfMessage = "Buildkit will be uninstalled from '$path' and buildkitd service will be stopped and unregistered." if ($Purge) { $WhatIfMessage += " Buildkit program data will also be removed." @@ -376,7 +383,8 @@ function Uninstall-Buildkit { } if (!$consent) { - Throw "$tool uninstallation cancelled." + Write-Warning "$tool uninstallation cancelled." + return } Write-Warning "Uninstalling preinstalled $tool at the path '$path'.`n$WhatIfMessage" diff --git a/containers-toolkit/Public/ContainerNetworkTools.psm1 b/containers-toolkit/Public/ContainerNetworkTools.psm1 index 84fde05..49230db 100644 --- a/containers-toolkit/Public/ContainerNetworkTools.psm1 +++ b/containers-toolkit/Public/ContainerNetworkTools.psm1 @@ -285,12 +285,15 @@ function Uninstall-WinCNIPlugin { begin { $tool = 'WinCNIPlugin' + # Get the default path if (!$Path) { - $ContainerdPath = Get-DefaultInstallPath -Tool "containerd" - $Path = "$ContainerdPath\cni" + $path = Get-DefaultInstallPath -Tool "containerd" } - $Path = $Path -replace '(\\bin\\?)$', '' + # Only delete the /cni dir + if (-not $path.EndsWith("\cni")) { + $path = Join-Path -Path "$path" -ChildPath "cni" + } $WhatIfMessage = "Windows CNI plugins will be uninstalled from $path" } @@ -309,7 +312,8 @@ function Uninstall-WinCNIPlugin { } if (!$consent) { - Throw "Windows CNI plugins uninstallation cancelled." + Write-Warning "$tool uninstallation cancelled." + return } Write-Warning "Uninstalling preinstalled Windows CNI plugin at the path $path" @@ -332,7 +336,7 @@ function Uninstall-WinCNIPluginHelper { param( [ValidateNotNullOrEmpty()] [parameter(HelpMessage = "Windows CNI plugin path")] - [String]$Path="$ENV:ProgramFiles\Containerd\cni" + [String]$Path ) Write-Output "Uninstalling Windows CNI plugin" @@ -342,7 +346,7 @@ function Uninstall-WinCNIPluginHelper { } # Remove the folder where WinCNI plugins are installed - Remove-Item $Path -Recurse -Force -ErrorAction Ignore + Remove-Item $Path -Recurse -Force -ErrorAction Continue Write-Output "Successfully uninstalled Windows CNI plugin." } diff --git a/containers-toolkit/Public/ContainerdTools.psm1 b/containers-toolkit/Public/ContainerdTools.psm1 index f52ab55..5722fbe 100644 --- a/containers-toolkit/Public/ContainerdTools.psm1 +++ b/containers-toolkit/Public/ContainerdTools.psm1 @@ -313,9 +313,10 @@ function Uninstall-Containerd { } # If we are not purging, we are uninstalling from the bin directory - # that contains the containerd executables + # that contains the containerd binaries, containerd/bin + $path = $path.TrimEnd("\") if (-not $Purge -and (-not $path.EndsWith("\bin"))) { - $path = $path.TrimEnd("\").Trim() + "\bin" + $path = $path.Trim() + "\bin" } $WhatIfMessage = "Containerd will be uninstalled from '$path' and containerd service will be stopped and unregistered." @@ -340,7 +341,8 @@ function Uninstall-Containerd { } if (!$consent) { - Throw "$tool uninstallation cancelled." + Write-Warning "$tool uninstallation cancelled." + return } Write-Warning "Uninstalling preinstalled $tool at the path '$path'.`n$WhatIfMessage" diff --git a/containers-toolkit/Public/NerdctlTools.psm1 b/containers-toolkit/Public/NerdctlTools.psm1 index 7e5a096..ed52d6c 100644 --- a/containers-toolkit/Public/NerdctlTools.psm1 +++ b/containers-toolkit/Public/NerdctlTools.psm1 @@ -236,7 +236,8 @@ function Uninstall-Nerdctl { } if (!$consent) { - Throw "$tool uninstallation cancelled." + Write-Warning "$tool uninstallation cancelled." + return } Write-Warning "Uninstalling preinstalled $tool at the path '$path'.`n$WhatIfMessage"