@@ -30,26 +30,121 @@ Describe "AllToolsUtilities.psm1" {
3030
3131 Context " Show-ContainerTools" - Tag " Show-ContainerTools" {
3232 BeforeAll {
33- Mock Get-InstalledVersion - ModuleName ' AllToolsUtilities'
33+ # Mock get version
34+ $mockConfigStdOut = New-MockObject - Type ' System.IO.StreamReader' - Methods @ { ReadToEnd = { return " tool version v1.0.1" } }
35+ $mockConfigProcess = New-MockObject - Type ' System.Diagnostics.Process' - Properties @ {
36+ ExitCode = 0
37+ StandardOutput = $mockConfigStdOut
38+ }
39+ Mock Invoke-ExecutableCommand - ModuleName " AllToolsUtilities" `
40+ - ParameterFilter { $Arguments -eq " --version" } `
41+ - MockWith { return $mockConfigProcess }
42+ }
43+
44+ It " Should get containerd version" {
45+ $executablePath = " TestDrive:\Program Files\Containerd\bin\containerd.exe"
46+ Mock Get-Command - ModuleName ' AllToolsUtilities' - MockWith { @ { Name = ' containerd.exe' ; Source = $executablePath } }
47+ Mock Get-Service - ModuleName ' AllToolsUtilities'
48+
49+ $containerdVersion = Show-ContainerTools - ToolName ' containerd'
50+
51+ # Check the output
52+ $expectedOutput = [PSCustomObject ]@ {
53+ Tool = ' containerd'
54+ Path = $executablePath
55+ Installed = $true
56+ Version = ' v1.0.1'
57+ Daemon = ' containerd'
58+ DaemonStatus = ' Unregistered'
59+ }
60+ # $containerdVersion | Should -Be $expectedOutput
61+ # HACK: Should -Be does not work with PSCustomObject in PSv5.
62+ # However PSv6 has support for this. To be investigated further.
63+ foreach ($key in $expectedOutput.Keys ) {
64+ $expectedValue = $expectedOutput [$key ]
65+ $actualValue = $containerdVersion .$key
66+ $actualValue | Should - Be $expectedValue
67+ }
68+
69+ # Check the invocation
70+ Should - Invoke Get-Command - ModuleName ' AllToolsUtilities' `
71+ - Times 1 - Exactly - Scope It - ParameterFilter { $Name -eq ' containerd.exe' }
72+ }
73+
74+ It " Should get buildkit version" {
75+ $executablePath = " TestDrive:\Program Files\Buildkit\bin\buildkitd.exe"
76+ $buildctlPath = " TestDrive:\Program Files\Buildkit\bin\buildctl.exe"
77+
78+ Mock Get-Service - ModuleName ' AllToolsUtilities' - MockWith { @ { Status = " Running" } }
79+ Mock Get-Command - ModuleName ' AllToolsUtilities' - MockWith { @ (
80+ @ { Name = ' buildkitd.exe' ; Source = $executablePath }
81+ @ { Name = ' buildctl.exe' ; Source = $buildctlPath }
82+ ) }
83+
84+ $buildkitVersion = Show-ContainerTools - ToolName ' buildkit'
85+
86+ # Check the output
87+ $expectedOutput = [PSCustomObject ]@ {
88+ Tool = ' buildkit'
89+ Path = $executablePath
90+ Installed = $true
91+ Version = ' v1.0.1'
92+ Daemon = ' buildkitd'
93+ DaemonStatus = ' Running'
94+ BuildctlPath = $buildctlPath
95+ }
96+ foreach ($key in $expectedOutput.Keys ) {
97+ $expectedValue = $expectedOutput [$key ]
98+ $actualValue = $buildkitVersion .$key
99+ $actualValue | Should - Be $expectedValue
100+ }
101+
102+ # Check the invocation
103+ Should - Invoke Get-Command - ModuleName ' AllToolsUtilities' `
104+ - Times 1 - Exactly - Scope It - ParameterFilter { $Name -eq " build*.exe" }
34105 }
35106
36- It " Should list all container tools and their install status " {
37- Show-ContainerTools
107+ It " Should return basic info if the tool is not installed " {
108+ Mock Get-Command - ModuleName ' AllToolsUtilities '
38109
39- @ (" containerd" , " buildkit" , " nerdctl" ) | ForEach-Object {
40- Should - Invoke Get-InstalledVersion - ModuleName ' AllToolsUtilities' `
41- - Times 1 - Exactly - Scope It `
42- - ParameterFilter { $Feature -eq $_ }
110+ $toolInfo = Show-ContainerTools
111+
112+ # Check the output
113+ $expectedOutput = @ (
114+ [PSCustomObject ]@ { Tool = ' containerd' ; Installed = $false ; Daemon = ' containerd' ; DaemonStatus = ' Unregistered' }
115+ [PSCustomObject ]@ { Tool = ' buildkit' ; Installed = $false ; Daemon = ' buildkitd' ; DaemonStatus = ' Unregistered' }
116+ [PSCustomObject ]@ { Tool = ' nerdctl' ; Installed = $false }
117+ )
118+ $expectedOutput | ForEach-Object {
119+ $tool = $_.Tool
120+ $actualOutput = $toolInfo | Where-Object { $_.Tool -eq $tool }
121+ foreach ($key in $_.Keys ) {
122+ $expectedValue = $_ [$key ]
123+ $actualValue = $actualOutput .$key
124+ $actualValue | Should - Be $expectedValue
125+ }
43126 }
44127 }
45128
46- It " Should list the latest available version for each tool" {
47- Show-ContainerTools - Latest
129+ It " Should return latest version if Latest flag is specified" {
130+ Mock Get-Command - ModuleName ' AllToolsUtilities'
131+
132+ $toolInfo = Show-ContainerTools - Latest
48133
49- @ (" containerd" , " buildkit" , " nerdctl" ) | ForEach-Object {
50- Should - Invoke Get-InstalledVersion - ModuleName ' AllToolsUtilities' `
51- - Times 1 - Exactly - Scope It `
52- - ParameterFilter { $Feature -eq $_ -and $Latest -eq $true }
134+ # Check the output
135+ $expectedOutput = @ (
136+ [PSCustomObject ]@ { Tool = ' containerd' ; Installed = $false ; Daemon = ' buildkitd' ; DaemonStatus = ' Unregistered' ; LatestVersion = ' v1.0.1' }
137+ [PSCustomObject ]@ { Tool = ' buildkit' ; Installed = $false ; Daemon = ' buildkitd' ; DaemonStatus = ' Unregistered' ; LatestVersion = ' v1.0.1' }
138+ [PSCustomObject ]@ { Tool = ' nerdctl' ; Installed = $false ; LatestVersion = ' v1.0.1' }
139+ )
140+ $expectedOutput | ForEach-Object {
141+ $tool = $_.Tool
142+ $actualOutput = $toolInfo | Where-Object { $_.Tool -eq $tool }
143+ foreach ($key in $_.Keys ) {
144+ $expectedValue = $_ [$key ]
145+ $actualValue = $actualOutput .$key
146+ $actualValue | Should - Be $expectedValue
147+ }
53148 }
54149 }
55150 }
0 commit comments