Skip to content

Commit 7f71097

Browse files
committed
Refactor Get-LatestToolVersion
Store the repo names in the CommonToolUtilities.psm1 to make them reusable across the module.
1 parent 7cb550b commit 7f71097

File tree

7 files changed

+73
-18
lines changed

7 files changed

+73
-18
lines changed

Tests/CommonToolUtilities.Tests.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ Describe "CommonToolUtilities.psm1" {
8484
}
8585

8686
Context "Get-LatestToolVersion" -Tag "Get-LatestToolVersion" {
87+
BeforeEach {
88+
$expectedUri = "https://api.github.com/repos/containerd/containerd/releases/latest"
89+
}
90+
8791
It "Should return the latest version for a tool" {
8892
$sampleOutput = @{
8993
StatusCode = 200
@@ -98,17 +102,20 @@ Describe "CommonToolUtilities.psm1" {
98102
}
99103
Mock Invoke-WebRequest { $sampleOutput } -ModuleName "CommonToolUtilities"
100104

101-
$result = Get-LatestToolVersion -Repository "test/tool"
105+
$result = Get-LatestToolVersion -Tool "containerd"
102106

103-
$expectedUri = "https://api.github.com/repos/test/tool/releases/latest"
104107
Should -Invoke Invoke-WebRequest -ParameterFilter { $Uri -eq $expectedUri } -Exactly 1 -Scope It -ModuleName "CommonToolUtilities"
105108
$result | Should -Be '0.12.3'
106109
}
107110

111+
It "Should throw an error if invalid tool name is provided" {
112+
{ Get-LatestToolVersion -Tool "invalid-tool" } | Should -Throw "Couldn't get latest invalid-tool version. Invalid tool name: 'invalid-tool'."
113+
}
114+
108115
It "Should throw an error if API call fails" {
109116
$errorMessage = "Response status code does not indicate success: 404 (Not Found)."
110117
Mock Invoke-WebRequest -MockWith { Throw $errorMessage } -ModuleName "CommonToolUtilities"
111-
{ Get-LatestToolVersion -Repository "test/tool" } | Should -Throw "Could not get tool latest version. $errorMessage"
118+
{ Get-LatestToolVersion -Tool "containerd" } | Should -Throw "Couldn't get containerd latest version from $expectedUri. $errorMessage"
112119
}
113120
}
114121

Tests/ContainerNetworkTools.Tests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ Describe "ContainerNetworkTools.psm1" {
3131
Remove-Module -Name "$RootPath\Tests\TestData\MockClasses.psm1" -Force -ErrorAction Ignore
3232
}
3333

34+
Context "Get-WinCNILatestVersion" -Tag "Get-WinCNILatestVersion" {
35+
BeforeEach {
36+
Mock Get-LatestToolVersion -ModuleName 'ContainerNetworkTools'
37+
}
38+
39+
It "Should return the latest version of Windows CNI plugin" {
40+
Get-WinCNILatestVersion
41+
Should -Invoke Get-LatestToolVersion -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Tool -eq 'wincniplugin' }
42+
}
43+
44+
It "Should return the latest version of Cloud Native CNI plugin" {
45+
Get-WinCNILatestVersion -Repo 'containernetworking/plugins'
46+
Should -Invoke Get-LatestToolVersion -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Tool -eq 'cloudnativecni' }
47+
}
48+
}
49+
3450
Context "Install-WinCNIPlugin" -Tag "Install-WinCNIPlugin" {
3551
BeforeAll {
3652
$Script:ToolName = 'WinCNIPlugin'

containers-toolkit/Private/CommonToolUtilities.psm1

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,38 @@ $HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA1|SHA256|SHA384|SHA512|MD5
7272
$NERDCTL_CHECKSUM_FILE_PATTERN = "(?<hashfunction>(?:^({0})))" -f ($HASH_FUNCTIONS -join '|')
7373
$NERDCTL_FILTER_SCRIPTBLOCK_STR = { (("{0}" -match "$NERDCTL_CHECKSUM_FILE_PATTERN") -and "{0}" -notmatch ".*.asc$") }.ToString()
7474

75-
function Get-LatestToolVersion($repository) {
75+
76+
Set-Variable -Option AllScope -scope Global -Visibility Public -Name "CONTAINERD_REPO" -Value "containerd/containerd" -Force
77+
Set-Variable -Option AllScope -scope Global -Visibility Public -Name "BUILDKIT_REPO" -Value "moby/buildkit" -Force
78+
Set-Variable -Option AllScope -scope Global -Visibility Public -Name "NERDCTL_REPO" -Value "containerd/nerdctl" -Force
79+
Set-Variable -Option AllScope -scope Global -Visibility Public -Name "WINCNI_PLUGIN_REPO" -Value "microsoft/windows-container-networking" -Force
80+
Set-Variable -Option AllScope -scope Global -Visibility Public -Name "CLOUDNATIVE_CNI_REPO" -Value "containernetworking/plugins" -Force
81+
82+
83+
function Get-LatestToolVersion($tool) {
84+
# Get the repository based on the tool
85+
$repository = switch ($tool.ToLower()) {
86+
"containerd" { $CONTAINERD_REPO }
87+
"buildkit" { $BUILDKIT_REPO }
88+
"nerdctl" { $NERDCTL_REPO }
89+
"wincniplugin" { $WINCNI_PLUGIN_REPO }
90+
"cloudnativecni" { $CLOUDNATIVE_CNI_REPO }
91+
Default { Throw "Couldn't get latest $tool version. Invalid tool name: '$tool'." }
92+
}
93+
94+
# Get the latest release version URL string
95+
$uri = "https://api.github.com/repos/$repository/releases/latest"
96+
97+
Write-Debug "Getting the latest $tool version from $uri"
98+
99+
# Get the latest release version
76100
try {
77-
$uri = "https://api.github.com/repos/$repository/releases/latest"
78101
$response = Invoke-WebRequest -Uri $uri -UseBasicParsing
79102
$version = ($response.content | ConvertFrom-Json).tag_name
80103
return $version.TrimStart("v")
81104
}
82105
catch {
83-
$tool = ($repository -split "/")[1]
84-
Throw "Could not get $tool latest version. $($_.Exception.Message)"
106+
Throw "Couldn't get $tool latest version from $uri. $($_.Exception.Message)"
85107
}
86108
}
87109

@@ -889,7 +911,7 @@ function Invoke-ExecutableCommand {
889911
return $p
890912
}
891913

892-
914+
Export-ModuleMember -Variable CONTAINERD_REPO, BUILDKIT_REPO, NERDCTL_REPO, WINCNI_PLUGIN_REPO, CLOUDNATIVE_CNI_REPO
893915
Export-ModuleMember -Function Get-LatestToolVersion
894916
Export-ModuleMember -Function Get-DefaultInstallPath
895917
Export-ModuleMember -Function Test-EmptyDirectory

containers-toolkit/Public/BuildkitTools.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot
1313
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1414

1515
function Get-BuildkitLatestVersion {
16-
$latestVersion = Get-LatestToolVersion -Repository "moby/buildkit"
16+
$latestVersion = Get-LatestToolVersion -Tool "buildkit"
1717
return $latestVersion
1818
}
1919

@@ -96,7 +96,7 @@ function Install-Buildkit {
9696
# Download files
9797
$downloadParams = @{
9898
ToolName = "Buildkit"
99-
Repository = "moby/buildkit"
99+
Repository = "$BUILDKIT_REPO"
100100
Version = $Version
101101
OSArchitecture = $OSArchitecture
102102
DownloadPath = $DownloadPath

containers-toolkit/Public/ContainerNetworkTools.psm1

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@ using module "..\Private\CommonToolUtilities.psm1"
1212
$ModuleParentPath = Split-Path -Parent $PSScriptRoot
1313
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1414

15+
$WINCNI_PLUGIN_REPO = "microsoft/windows-container-networking"
16+
$CLOUDNATIVE_CNI_REPO = "containernetworking/plugins"
17+
1518
function Get-WinCNILatestVersion {
16-
$latestVersion = Get-LatestToolVersion -Repository "microsoft/windows-container-networking"
19+
param (
20+
[String]$repo = "microsoft/windows-container-networking"
21+
)
22+
$tool = switch ($repo.ToLower()) {
23+
$WINCNI_PLUGIN_REPO { "wincniplugin" }
24+
$CLOUDNATIVE_CNI_REPO { "cloudnativecni" }
25+
Default { Throw "Invalid repository. Supported repositories are $WINCNI_PLUGIN_REPO and $CLOUDNATIVE_CNI_REPO" }
26+
}
27+
$latestVersion = Get-LatestToolVersion -Tool $tool
1728
return $latestVersion
1829
}
1930

@@ -80,7 +91,7 @@ function Install-WinCNIPlugin {
8091
# Get Windows CNI plugins version to install
8192
if (!$WinCNIVersion) {
8293
# Get default version
83-
$WinCNIVersion = Get-WinCNILatestVersion
94+
$WinCNIVersion = Get-WinCNILatestVersion -Repo $SourceRepo
8495
}
8596
$WinCNIVersion = $WinCNIVersion.TrimStart('v')
8697
Write-Output "Downloading CNI plugin version $WinCNIVersion at $WinCNIPath"
@@ -385,8 +396,7 @@ function Install-MissingPlugin {
385396
$consent = ([ActionConsent](Get-Host).UI.PromptForChoice($title, $question, $choices, 1)) -eq [ActionConsent]::Yes
386397

387398
if (-not $consent) {
388-
$downloadPath = "https://github.com/microsoft/windows-container-networking"
389-
Throw "Windows CNI plugins have not been installed. To install, run the command `"Install-WinCNIPlugin`" or download from $downloadPath."
399+
Throw "Windows CNI plugins have not been installed. To install, run the command `"Install-WinCNIPlugin`"."
390400
}
391401
}
392402

containers-toolkit/Public/ContainerdTools.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1313

1414

1515
function Get-ContainerdLatestVersion {
16-
$latestVersion = Get-LatestToolVersion -Repository "containerd/containerd"
16+
$latestVersion = Get-LatestToolVersion -Tool "containerd"
1717
return $latestVersion
1818
}
1919

@@ -85,7 +85,7 @@ function Install-Containerd {
8585
# Download files
8686
$downloadParams = @{
8787
ToolName = "Containerd"
88-
Repository = "containerd/containerd"
88+
Repository = "$CONTAINERD_REPO"
8989
Version = $version
9090
OSArchitecture = $OSArchitecture
9191
DownloadPath = $DownloadPath

containers-toolkit/Public/NerdctlTools.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot
1212
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1313

1414
function Get-NerdctlLatestVersion {
15-
$latestVersion = Get-LatestToolVersion -Repository "containerd/nerdctl"
15+
$latestVersion = Get-LatestToolVersion -Tool "nerdctl"
1616
return $latestVersion
1717
}
1818

@@ -133,7 +133,7 @@ function Install-Nerdctl {
133133
# Download files
134134
$downloadParams = @{
135135
ToolName = "nerdctl"
136-
Repository = "containerd/nerdctl"
136+
Repository = "$NERDCTL_REPO"
137137
Version = $version
138138
OSArchitecture = $OSArchitecture
139139
DownloadPath = $DownloadPath

0 commit comments

Comments
 (0)