Skip to content

Commit 7cb550b

Browse files
authored
Refactor 'Install-WinCNIPlugin' to use Install-RequiredFeature` (#44)
1 parent 030ef50 commit 7cb550b

File tree

4 files changed

+40
-41
lines changed

4 files changed

+40
-41
lines changed

Tests/ContainerNetworkTools.Tests.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Describe "ContainerNetworkTools.psm1" {
3333

3434
Context "Install-WinCNIPlugin" -Tag "Install-WinCNIPlugin" {
3535
BeforeAll {
36+
$Script:ToolName = 'WinCNIPlugin'
3637
$Script:WinCNIRepo = 'https://github.com/microsoft/windows-container-networking/releases/download'
3738
$Script:MockZipFileName = "windows-container-networking-cni-amd64-v1.0.0.zip"
3839
$Script:TestDownloadPath = "$HOME\Downloads\$Script:MockZipFileName"
@@ -47,8 +48,7 @@ Describe "ContainerNetworkTools.psm1" {
4748
Mock Test-EmptyDirectory -ModuleName 'ContainerNetworkTools' -MockWith { return $true }
4849
Mock Install-WinCNIPlugin -ModuleName 'ContainerNetworkTools'
4950
Mock Test-Path -ModuleName 'ContainerNetworkTools' -MockWith { return $true }
50-
Mock Invoke-ExecutableCommand -ModuleName 'ContainerNetworkTools' -MockWith {
51-
return New-MockObject -Type 'System.Diagnostics.Process' -Properties @{ ExitCode = 0 } }
51+
Mock Install-RequiredFeature -ModuleName 'ContainerNetworkTools'
5252
}
5353

5454
It 'Should not process on implicit request for validation (WhatIfPreference)' {
@@ -69,16 +69,20 @@ Describe "ContainerNetworkTools.psm1" {
6969

7070
Should -Invoke Uninstall-WinCNIPlugin -ModuleName 'ContainerNetworkTools' -Times 0 -Exactly -Scope It
7171
Should -Invoke Get-InstallationFile -ModuleName 'ContainerNetworkTools' -ParameterFilter {
72-
$fileParameters[0].Feature -eq "WinCNIPlugin" -and
72+
$fileParameters[0].Feature -eq "$Script:ToolName" -and
7373
$fileParameters[0].Repo -eq "microsoft/windows-container-networking" -and
7474
$fileParameters[0].Version -eq 'latest' -and
7575
$fileParameters[0].DownloadPath -eq "$HOME\Downloads"
7676
[string]::IsNullOrWhiteSpace($fileParameters.ChecksumSchemaFile) -and
7777
$fileParameters[0].FileFilterRegEx -eq $null
7878
}
79-
Should -Invoke Invoke-ExecutableCommand -ModuleName 'ContainerNetworkTools' -ParameterFilter {
80-
$executable -eq "tar.exe" -and
81-
$arguments -eq "-xf `"$Script:TestDownloadPath`" -C `"C:\Program Files\Containerd\cni\bin`""
79+
Should -Invoke Install-RequiredFeature -ModuleName 'ContainerNetworkTools' -ParameterFilter {
80+
$Feature -eq "$Script:ToolName" -and
81+
$InstallPath -eq "$Env:ProgramFiles\Containerd\cni\bin" -and
82+
$SourceFile -eq "$Script:TestDownloadPath" -and
83+
$EnvPath -eq $null -and
84+
$cleanup -eq $true -and
85+
$UpdateEnvPath -eq $false
8286
}
8387
}
8488

@@ -99,9 +103,12 @@ Describe "ContainerNetworkTools.psm1" {
99103
$fileParameters[0].OSArchitecture -eq '386' -and
100104
$fileParameters[0].FileFilterRegEx -eq ".*tgz(.SHA512)?$"
101105
}
102-
Should -Invoke Invoke-ExecutableCommand -ModuleName 'ContainerNetworkTools' -ParameterFilter {
103-
$executable -eq "tar.exe" -and
104-
$arguments -eq "-xf `"$HOME\Downloads\$MockZipFileName`" -C `"TestDrive:\WinCNI\bin`""
106+
Should -Invoke Install-RequiredFeature -ModuleName 'ContainerNetworkTools' -ParameterFilter {
107+
$Feature -eq "$Script:ToolName" -and
108+
$InstallPath -eq 'TestDrive:\WinCNI\bin' -and
109+
$SourceFile -eq $MockDownloadFilePath -and
110+
$cleanup -eq $true -and
111+
$UpdateEnvPath -eq $false
105112
}
106113
}
107114

containers-toolkit/Private/CommonToolUtilities.psm1

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ function Get-ReleaseAssets {
164164
# Filter assets by OS (windows) and architecture
165165
# In the "zip|tar.gz" regex, we do not add the "$" at the end to allow for checksum files to be included
166166
# The checksum files end with eg: ".tar.gz.sha256sum"
167-
($_.name -match "(windows(.+)$OSArch)") -or
167+
($_.name -match "(windows(.+)$OSArch)") -or
168168

169169
# nerdctl checksum files are named "SHA256SUMS".
170-
(& ([ScriptBlock]::Create($NERDCTL_FILTER_SCRIPTBLOCK_STR -f $_.name)))
170+
(& ([ScriptBlock]::Create($NERDCTL_FILTER_SCRIPTBLOCK_STR -f $_.name)))
171171
)
172172
} |
173173
ForEach-Object {
@@ -320,7 +320,7 @@ function Get-InstallationFile {
320320

321321
# Buildkit checksum files are named ending with ".provenance.json" or ".sbom.json"
322322
# We only need the ".sbom.json" file
323-
($_.asset_name -match ".*sbom.json$") -or
323+
($_.asset_name -match ".sbom.json$") -or
324324

325325
# nerdctl checksum files are named "SHA256SUMS". Check file names that have such a format.
326326
(& ([ScriptBlock]::Create($NERDCTL_FILTER_SCRIPTBLOCK_STR -f $_.asset_name)))
@@ -702,7 +702,10 @@ function Install-RequiredFeature {
702702
[string] $InstallPath,
703703
[string[]] $SourceFile,
704704
[string] $EnvPath,
705-
[boolean] $cleanup
705+
[boolean] $cleanup,
706+
707+
# Use by WinCNI plugin to avoid updating the environment path
708+
[boolean] $UpdateEnvPath = $true
706709
)
707710
# Create the directory to untar to
708711
Write-Information -InformationAction Continue -MessageData "Extracting $Feature to $InstallPath"
@@ -730,12 +733,14 @@ function Install-RequiredFeature {
730733
}
731734

732735
# Add to env path
733-
Add-FeatureToPath -Feature $Feature -Path $EnvPath
736+
if ($UpdateEnvPath -and -not [string]::IsNullOrWhiteSpace($envPath)) {
737+
Add-FeatureToPath -Feature $feature -Path $envPath
738+
}
734739

735740
# Clean up
736741
if ($CleanUp) {
737742
Write-Output "Cleanup to remove downloaded files"
738-
if (Test-Path -Path $SourceFile) {
743+
if (Test-Path -Path $SourceFile -ErrorAction SilentlyContinue) {
739744
Remove-Item -Path $SourceFile -Force -ErrorAction Ignore
740745
}
741746
}

containers-toolkit/Public/AllToolsUtilities.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Show-ContainerTools {
2424
$installedTools += (Get-InstalledVersion -Feature $tool -Latest:$Latest)
2525
}
2626

27-
$registerCommands = (Get-Command -Name "*-*Service" | Where-Object { $_.Source -eq 'Containers-Toolkit' }).Name -join ', '
27+
$registerCommands = (Get-Command -Name "*-*Service" -Module 'Containers-Toolkit').Name -join ', '
2828
$message = "For unregistered services/daemons, check the tool's help or register the service using `n`t$registerCommands"
2929
Write-Information -MessageData $message -Tags "Instructions" -InformationAction Continue
3030
return $installedTools
@@ -131,7 +131,7 @@ function Install-ContainerTools {
131131
$completedInstalls += $task.Name
132132
}
133133
catch {
134-
Write-Error "$($task.Name) Installation failed. $_"
134+
Write-Error "$($task.Name) installation failed. $_"
135135
$failedInstalls += $task.Name
136136
}
137137
}

containers-toolkit/Public/ContainerNetworkTools.psm1

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ function Install-WinCNIPlugin {
4343
)
4444

4545
begin {
46+
$ToolName = 'WinCNIPlugin'
4647
if (!$WinCNIPath) {
4748
$containerdPath = Get-DefaultInstallPath -Tool "containerd"
4849
$WinCNIPath = "$containerdPath\cni"
4950
}
5051
$WinCNIPath = $WinCNIPath -replace '(\\bin)$', ''
5152

52-
# Check if Containerd is alread installed
53+
# Check if WinCNI plugins are installed
5354
$isInstalled = -not (Test-EmptyDirectory -Path $WinCNIPath)
5455

5556
$plugin = "Windows CNI plugins"
@@ -86,10 +87,6 @@ function Install-WinCNIPlugin {
8687

8788
New-Item -Path $WinCNIPath -ItemType Directory -Force -ErrorAction Ignore | Out-Null
8889

89-
# NOTE: We download plugins from https://github.com/microsoft/windows-container-networking
90-
# instead of https://github.com/containernetworking/plugins/releases.
91-
# The latter causes an error in nerdctl: "networking setup error has occurred. incompatible CNI versions"
92-
9390
Write-Debug ("Downloading Windows CNI plugins from {0}" -f $SourceRepo)
9491

9592
# File filter for Windows CNI plugins
@@ -104,7 +101,7 @@ function Install-WinCNIPlugin {
104101

105102
# Download file from repo
106103
$downloadParams = @{
107-
ToolName = "WinCNIPlugin"
104+
ToolName = "$ToolName"
108105
Repository = $SourceRepo
109106
Version = $WinCNIVersion
110107
OSArchitecture = $OSArchitecture
@@ -123,25 +120,15 @@ function Install-WinCNIPlugin {
123120
)
124121
$sourceFile = Get-InstallationFile -FileParameters $downloadParamsProperties
125122

126-
if (-not (Test-Path -Path $sourceFile)) {
127-
Throw "Couldn't find the downloaded file $sourceFile"
128-
}
129-
130-
$WinCNIBin = "$WinCNIPath\bin"
131-
if (-not (Test-Path -Path $WinCNIBin)) {
132-
New-Item -Path $WinCNIBin -ItemType Directory -Force -ErrorAction Ignore | Out-Null
133-
}
134-
135-
# Expand archive file
136-
$cmdOutput = Invoke-ExecutableCommand -executable "tar.exe" -arguments "-xf `"$sourceFile`" -C `"$WinCNIBin`"" -timeout (60 * 2)
137-
if ($cmdOutput.ExitCode -ne 0) {
138-
Throw "Failed to expand archive `"$sourceFile`" at `"$WinCNIBin`". Exit code: $($cmdOutput.ExitCode). $($cmdOutput.StandardError.ReadToEnd())"
139-
}
140-
141-
if (Test-Path -Path $sourceFile) {
142-
# Remove the downloaded file
143-
Remove-Item -Path "$sourceFile" -Force -ErrorAction SilentlyContinue
123+
# Untar and install tool
124+
$params = @{
125+
Feature = "$ToolName"
126+
InstallPath = "$WinCNIPath\bin"
127+
SourceFile = $sourceFile
128+
cleanup = $true
129+
UpdateEnvPath = $false
144130
}
131+
Install-RequiredFeature @params
145132

146133
Write-Output "CNI plugin version $WinCNIVersion ($sourceRepo) successfully installed at $WinCNIPath"
147134
}

0 commit comments

Comments
 (0)