@@ -14,8 +14,8 @@ Describe "NerdctlTools.psm1" {
1414 $RootPath = Split-Path - Parent $PSScriptRoot
1515 $ModuleParentPath = Join-Path - Path $RootPath - ChildPath ' Containers-Toolkit'
1616 Import-Module - Name " $ModuleParentPath \Private\CommonToolUtilities.psm1" - Force
17- Import-Module - Name " $ModuleParentPath \Public\NerdctlTools.psm1"
1817 Import-Module - Name " $ModuleParentPath \Public\ContainerdTools.psm1"
18+ Import-Module - Name " $ModuleParentPath \Public\BuildkitTools.psm1"
1919 Import-Module - Name " $ModuleParentPath \Public\ContainerNetworkTools.psm1"
2020 Import-Module - Name " $ModuleParentPath \Public\NerdctlTools.psm1" - Force
2121 }
@@ -26,8 +26,8 @@ Describe "NerdctlTools.psm1" {
2626
2727 AfterAll {
2828 Remove-Module - Name " $ModuleParentPath \Private\CommonToolUtilities.psm1" - Force - ErrorAction Ignore
29- Remove-Module - Name " $ModuleParentPath \Public\BuildkitTools.psm1" - Force - ErrorAction Ignore
3029 Remove-Module - Name " $ModuleParentPath \Public\ContainerdTools.psm1" - Force - ErrorAction Ignore
30+ Remove-Module - Name " $ModuleParentPath \Public\BuildkitTools.psm1" - Force - ErrorAction Ignore
3131 Remove-Module - Name " $ModuleParentPath \Public\ContainerNetworkTools.psm1" - Force - ErrorAction Ignore
3232 Remove-Module - Name " $ModuleParentPath \Public\NerdctlTools.psm1" - Force - ErrorAction Ignore
3333 }
@@ -49,6 +49,17 @@ Describe "NerdctlTools.psm1" {
4949 Mock Install-WinCNIPlugin - ModuleName ' NerdctlTools'
5050 Mock Install-Nerdctl - ModuleName ' NerdctlTools'
5151 Mock Remove-Item - ModuleName ' NerdctlTools'
52+
53+ # Mock for Invoke-ExecutableCommand- "nerdctl --version"
54+ $mockExecutablePath = " $TestDrive \Program Files\nerdctl\nerdctl.exe"
55+ $mockConfigStdOut = New-MockObject - Type ' System.IO.StreamReader' - Methods @ { ReadToEnd = { return " nerdctl version v7.9.8" } }
56+ $mockProcess = New-MockObject - Type ' System.Diagnostics.Process' - Properties @ {
57+ StandardOutput = $mockConfigStdOut
58+ ExitCode = 0
59+ }
60+ Mock Invoke-ExecutableCommand - ModuleName " NerdctlTools" - MockWith { return $mockProcess } - ParameterFilter {
61+ $Executable -eq " $mockExecutablePath " -and
62+ $Arguments -eq " --version" }
5263 }
5364
5465 It ' Should not process on implicit request for validation (WhatIfPreference)' {
@@ -67,6 +78,7 @@ Describe "NerdctlTools.psm1" {
6778 It " Should use defaults" {
6879 Install-Nerdctl - Force - Confirm:$false
6980
81+ Should - Invoke Get-NerdctlLatestVersion - ModuleName ' NerdctlTools' - Times 1 - Exactly - Scope It
7082 Should - Invoke Uninstall-Nerdctl - ModuleName ' NerdctlTools' - Times 0 - Exactly - Scope It
7183 Should - Invoke Get-InstallationFile - ModuleName ' NerdctlTools' - ParameterFilter {
7284 $fileParameters [0 ].Feature -eq " nerdctl" -and
@@ -117,11 +129,35 @@ Describe "NerdctlTools.psm1" {
117129 Should - Invoke Install-WinCNIPlugin - ModuleName ' NerdctlTools' - Times 0 - Exactly - Scope It
118130 }
119131
132+ It " Should not reinstall tool if version already exists and force is not specified" {
133+ Mock Test-EmptyDirectory - ModuleName ' ContainerdTools' - MockWith { return $false }
134+
135+ # Mock for Get-ChildItem - "nerdctl.exe"
136+ Mock Get-ChildItem - ModuleName ' NerdctlTools' - ParameterFilter {
137+ $Path -eq " $Env: ProgramFiles \nerdctl" -and
138+ $Recurse -eq $true
139+ $Filter -eq " nerdctl.exe"
140+ } - MockWith { return @ {FullName = " $mockExecutablePath " } }
141+
142+ Install-Nerdctl - Confirm:$false
143+ Should - Invoke Uninstall-Nerdctl - ModuleName ' NerdctlTools' - Times 0
144+ Should - Invoke Install-RequiredFeature - ModuleName ' NerdctlTools' - Times 0
145+ }
146+
120147 It " Should uninstall tool if it is already installed" {
121148 Mock Test-EmptyDirectory - ModuleName ' NerdctlTools' - MockWith { return $false }
122149
150+ # Mock for Get-ChildItem - "nerdctl.exe"
151+ Mock Get-ChildItem - ModuleName ' NerdctlTools' - ParameterFilter {
152+ $Path -eq " $Env: ProgramFiles \nerdctl" -and
153+ $Recurse -eq $true
154+ $Filter -eq " nerdctl.exe"
155+ } - MockWith { return @ {FullName = " $mockExecutablePath " } }
156+
123157 Install-Nerdctl - Force - Confirm:$false
124158
159+ Should - Invoke Invoke-ExecutableCommand - ModuleName " NerdctlTools" `
160+ - ParameterFilter { ($Executable -eq $mockExecutablePath ) -and ($Arguments -eq " --version" ) }
125161 Should - Invoke Uninstall-Nerdctl - ModuleName ' NerdctlTools' - Times 1 - Exactly - Scope It `
126162 - ParameterFilter { $Path -eq " $Env: ProgramFiles \nerdctl" }
127163 }
@@ -130,19 +166,26 @@ Describe "NerdctlTools.psm1" {
130166 Mock Test-EmptyDirectory - ModuleName ' NerdctlTools' - MockWith { return $false }
131167 Mock Uninstall-Nerdctl - ModuleName ' NerdctlTools' - MockWith { throw ' Error' }
132168
133- { Install-Nerdctl - Confirm:$false } | Should - Throw " nerdctl installation failed. Error"
169+ # Mock for Get-ChildItem - "nerdctl.exe"
170+ Mock Get-ChildItem - ModuleName ' NerdctlTools' - ParameterFilter {
171+ $Path -eq " $Env: ProgramFiles \nerdctl" -and
172+ $Recurse -eq $true
173+ $Filter -eq " nerdctl.exe"
174+ } - MockWith { return @ {FullName = " $mockExecutablePath " } }
175+
176+ { Install-Nerdctl - Confirm:$false - Force } | Should - Throw " nerdctl installation failed. Error"
134177 }
135178
136179 It " Should install all dependencies if 'All' is specified" {
137- Install-Nerdctl - Dependencies ' All' - Confirm:$false
180+ Install-Nerdctl - Dependencies ' All' - Confirm:$false - Force
138181
139182 Should - Invoke Install-Containerd - ModuleName ' NerdctlTools' - Times 1 - Exactly - Scope It
140183 Should - Invoke Install-Buildkit - ModuleName ' NerdctlTools' - Times 1 - Exactly - Scope It
141184 Should - Invoke Install-WinCNIPlugin - ModuleName ' NerdctlTools' - Times 1 - Exactly - Scope It
142185 }
143186
144187 It " Should install specified dependencies" {
145- Install-Nerdctl - Dependencies ' containerd' - Confirm:$false
188+ Install-Nerdctl - Dependencies ' containerd' - Confirm:$false - Force
146189
147190 Should - Invoke Install-Containerd - ModuleName ' NerdctlTools' - Times 1 - Exactly - Scope It
148191 Should - Invoke Install-Buildkit - ModuleName ' NerdctlTools' - Times 0 - Exactly - Scope It
0 commit comments