Skip to content

Commit ddfdf97

Browse files
committed
Fix failing tests
1 parent 7c32ebe commit ddfdf97

File tree

7 files changed

+47
-42
lines changed

7 files changed

+47
-42
lines changed

Tests/BuildkitTools.Tests.ps1

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,27 @@ Describe "BuildkitTools.psm1" {
2828
# Mock functions
2929
function Test-ServiceRegistered { }
3030
Mock Test-ServiceRegistered -ModuleName 'BuildkitTools' -MockWith { return $true }
31+
32+
function CleanupTestDrive {
33+
Get-ChildItem -Path "$TestDrive" -Force -ErrorAction SilentlyContinue | `
34+
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue
35+
Get-ChildItem -Path "TestDrive:\" -Recurse -Force -ErrorAction SilentlyContinue | `
36+
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue
37+
}
3138
}
3239

3340
BeforeEach {
34-
Remove-Item -Path "$TestDrive" -Re -Force -ErrorAction Ignore
41+
CleanupTestDrive
3542
}
3643

3744
AfterEach {
3845
$ENV:PESTER = $false
39-
Remove-Item -Path "$TestDrive" -Re -Force -ErrorAction Ignore
46+
47+
CleanupTestDrive
4048
}
4149

4250
AfterAll {
43-
Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore
44-
Remove-Module -Name "$ModuleParentPath\Public\BuildkitTools.psm1" -Force -ErrorAction Ignore
51+
CleanupTestDrive
4552
}
4653

4754
Context "Install-Buildkit" -Tag "Install-Buildkit" {
@@ -63,7 +70,7 @@ Describe "BuildkitTools.psm1" {
6370
}
6471

6572
AfterEach {
66-
Remove-Item -Path "TestDrive:\" -Force -ErrorAction Ignore
73+
CleanupTestDrive
6774
}
6875

6976
It 'Should not process on implicit request for validation (WhatIfPreference)' {
@@ -130,10 +137,10 @@ Describe "BuildkitTools.psm1" {
130137

131138
Should -Invoke Register-BuildkitdService -Times 1 -Exactly -Scope It -ModuleName 'BuildkitTools' `
132139
-ParameterFilter {
133-
$BuildKitPath -eq "$Env:ProgramFiles\Buildkit" -and
134-
$WinCNIPath -eq "$ENV:ProgramFiles\Containerd\cni"
135-
$Start -eq $true
136-
}
140+
$BuildKitPath -eq "$Env:ProgramFiles\Buildkit" -and
141+
$WinCNIPath -eq "$ENV:ProgramFiles\Containerd\cni"
142+
$Start -eq $true
143+
}
137144
}
138145

139146
It "Should uninstall tool if it is already installed" {
@@ -222,7 +229,6 @@ Describe "BuildkitTools.psm1" {
222229

223230
$expectedExecutablePath = "$MockBuildKitPath\bin\buildkitd.exe"
224231
$expectedCommandArguments = "--register-service --debug --containerd-worker=true --containerd-cni-config-path=`"$MockCniConfPath`" --containerd-cni-binary-dir=`"$MockCniBinDir`" --service-name buildkitd"
225-
Write-Host "'$expectedCommandArguments'" -ForegroundColor Magenta
226232
Should -Invoke Invoke-ExecutableCommand -Times 1 -Scope It -ModuleName "BuildkitTools" `
227233
-ParameterFilter {
228234
($Executable -eq $expectedExecutablePath ) -and

Tests/ContainerNetworkTools.Tests.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,24 @@ Describe "ContainerNetworkTools.psm1" {
228228

229229
It "Should use HNS module if HostNetworkingService is not installed" {
230230
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' }
231-
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{} }
231+
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith {
232+
return @{Name = 'HNS'; Path = "TestDrive:\PowerShell\Modules\HNS\HNS.psm1" }
233+
}
232234

233235
Initialize-NatNetwork -Force
234236

235237
Should -Invoke Import-Module -Times 0 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' }
236-
Should -Invoke Import-Module -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }
238+
Should -Invoke Get-Module -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }
239+
240+
# NOTE: Because we are piping the HNS module info from Get-Module, Pester's Should -Invoke
241+
# often struggle to intercept commands invoked via the pipeline, especially when parameter
242+
# binding happens implicitly or when the function relies on parameter value from a piped object.
243+
Should -Invoke Import-Module -Times 1 -Scope It -ModuleName 'ContainerNetworkTools'
237244
}
238245

239246
It "Should throw an error when importing HNS module fails" {
240247
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' }
241-
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{} }
248+
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{ Name = 'HNS' } }
242249
Mock Import-Module -ModuleName 'ContainerNetworkTools' -MockWith { Throw 'Error message.' }
243250

244251
{ Initialize-NatNetwork -Force } | Should -Throw "Could not import HNS module. Error message."

Tests/NerdctlTools.Tests.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,21 @@ Describe "NerdctlTools.psm1" {
183183
}
184184

185185
It "Should successfully purge program data" {
186-
Uninstall-Nerdctl -Path 'TestDrive:\Program Files\nerdctl' -Confirm:$false -Force -Purge
186+
$mockPath = 'TestDrive:\Program Files\nerdctl'
187+
Mock Get-ChildItem -ModuleName 'NerdctlTools' -ParameterFilter { $Path -eq "$mockPath" } -MockWith {
188+
return @(
189+
New-MockObject -Type 'System.IO.DirectoryInfo' -Properties @{
190+
FullName = "$mockPath\nerdctl.exe"
191+
Name = 'nerdctl.exe'
192+
Length = 27259904
193+
DirectoryName = "$mockPath"
194+
Directory = "$mockPath"
195+
PSIsContainer = $false
196+
}
197+
)
198+
}
199+
200+
Uninstall-Nerdctl -Path "$mockPath" -Confirm:$false -Force -Purge
187201

188202
# Should purge program data
189203
Should -Invoke Remove-Item -Times 1 -Scope It -ModuleName "NerdctlTools" `
@@ -194,7 +208,7 @@ Describe "NerdctlTools.psm1" {
194208
-ParameterFilter { $Feature -eq "nerdctl" }
195209
Should -Invoke Invoke-ExecutableCommand -Time 1 -Scope It -ModuleName "NerdctlTools" -ParameterFilter {
196210
$executable -eq "TestDrive:\Program Files\nerdctl\nerdctl.exe" -and
197-
$arguments -eq "system prune --all"
211+
$arguments -eq "system prune --all --force"
198212
}
199213
}
200214

containers-toolkit/Public/BuildkitTools.psm1

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,6 @@ function Uninstall-Buildkit {
382382

383383
process {
384384
if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) {
385-
# if (Test-EmptyDirectory -Path "$path") {
386-
# Write-Output "$tool does not exist at '$Path' or the directory is empty"
387-
# return
388-
# }
389-
390385
$consent = $force
391386
if (!$ENV:PESTER) {
392387
$consent = $force -or $PSCmdlet.ShouldContinue($env:COMPUTERNAME, "Are you sure you want to uninstall Buildkit from '$path'?")

containers-toolkit/Public/ContainerNetworkTools.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ function Import-HNSModule {
380380
return
381381
}
382382

383-
$ModuleName = 'HNS'
384-
# https://www.powershellgallery.com/packages/HNS/0.2.4
385-
if (Get-Module -ListAvailable -Name $ModuleName) {
386-
Get-Module -ListAvailable -Name "$ModuleName" | Import-Module -DisableNameChecking -Force:$force
383+
# Check and import HostNetworkingService module
384+
$hnsModule = Get-Module -ListAvailable -Name 'HNS' -ErrorAction SilentlyContinue
385+
if ($hnsModule) {
386+
$hnsModule | Import-Module -DisableNameChecking -Force:$force
387387
return
388388
}
389389

containers-toolkit/Public/ContainerdTools.psm1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,6 @@ function Uninstall-Containerd {
331331

332332
process {
333333
if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) {
334-
# if (Test-EmptyDirectory -Path $path) {
335-
# Write-Output "$tool does not exist at $Path or the directory is empty"
336-
# return
337-
# }
338-
339334
$consent = $force
340335
if (!$ENV:PESTER) {
341336
$consent = $force -or $PSCmdlet.ShouldContinue($Path, 'Are you sure you want to uninstall Containerd?')
@@ -373,9 +368,6 @@ function Uninstall-ContainerdHelper {
373368
)
374369

375370
if (-not (Test-EmptyDirectory -Path "$Path")) {
376-
# Write-Error "Containerd does not exist at $Path or the directory is empty."
377-
# return
378-
379371
try {
380372
if (Test-ServiceRegistered -Service 'containerd') {
381373
Stop-ContainerdService

containers-toolkit/Public/NerdctlTools.psm1

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,6 @@ function Uninstall-Nerdctl {
224224

225225
process {
226226
if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) {
227-
228-
# if (Test-EmptyDirectory -Path "$path") {
229-
# Write-Output "$tool does not exist at '$Path' or the directory is empty"
230-
# return
231-
# }
232-
233227
$consent = $force
234228
if (!$ENV:PESTER) {
235229
$consent = $force -or $PSCmdlet.ShouldContinue($Path, 'Are you sure you want to uninstall nerdctl?')
@@ -279,10 +273,7 @@ function Uninstall-NerdctlHelper {
279273
[Switch] $Purge
280274
)
281275

282-
if (Test-EmptyDirectory -Path "$Path") {
283-
# Write-Error "nerdctl does not exist at '$Path' or the directory is empty."
284-
# return
285-
276+
if (-not (Test-EmptyDirectory -Path "$Path")) {
286277
# Remove the folder where nerdctl is installed and related folders
287278
Remove-Item -Path "$Path" -Recurse -Force
288279
}

0 commit comments

Comments
 (0)