Skip to content

Commit

Permalink
Refactor Get-LatestToolVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaMor committed Feb 5, 2025
1 parent 7cb550b commit b405616
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
13 changes: 10 additions & 3 deletions Tests/CommonToolUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Describe "CommonToolUtilities.psm1" {
}

Context "Get-LatestToolVersion" -Tag "Get-LatestToolVersion" {
BeforeEach {
$expectedUri = "https://api.github.com/repos/containerd/containerd/releases/latest"
}

It "Should return the latest version for a tool" {
$sampleOutput = @{
StatusCode = 200
Expand All @@ -98,17 +102,20 @@ Describe "CommonToolUtilities.psm1" {
}
Mock Invoke-WebRequest { $sampleOutput } -ModuleName "CommonToolUtilities"

$result = Get-LatestToolVersion -Repository "test/tool"
$result = Get-LatestToolVersion -Tool "containerd"

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

It "Should throw an error if invalid tool name is provided" {
{ Get-LatestToolVersion -Tool "invalid-tool" } | Should -Throw "Couldn't get latest invalid-tool version. Invalid tool name: 'invalid-tool'."
}

It "Should throw an error if API call fails" {
$errorMessage = "Response status code does not indicate success: 404 (Not Found)."
Mock Invoke-WebRequest -MockWith { Throw $errorMessage } -ModuleName "CommonToolUtilities"
{ Get-LatestToolVersion -Repository "test/tool" } | Should -Throw "Could not get tool latest version. $errorMessage"
{ Get-LatestToolVersion -Tool "containerd" } | Should -Throw "Couldn't get containerd latest version from $expectedUri. $errorMessage"
}
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/ContainerNetworkTools.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ Describe "ContainerNetworkTools.psm1" {
Remove-Module -Name "$RootPath\Tests\TestData\MockClasses.psm1" -Force -ErrorAction Ignore
}

Context "Get-WinCNILatestVersion" -Tag "Get-WinCNILatestVersion" {
BeforeEach {
Mock Get-LatestToolVersion -ModuleName 'ContainerNetworkTools'
}

It "Should return the latest version of Windows CNI plugin" {
Get-WinCNILatestVersion
Should -Invoke Get-LatestToolVersion -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Tool -eq 'wincniplugin' }
}

It "Should return the latest version of Cloud Native CNI plugin" {
Get-WinCNILatestVersion -Repo 'containernetworking/plugins'
Should -Invoke Get-LatestToolVersion -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Tool -eq 'cloudnativecni' }
}
}

Context "Install-WinCNIPlugin" -Tag "Install-WinCNIPlugin" {
BeforeAll {
$Script:ToolName = 'WinCNIPlugin'
Expand Down
29 changes: 25 additions & 4 deletions containers-toolkit/Private/CommonToolUtilities.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,37 @@ $HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA1|SHA256|SHA384|SHA512|MD5
$NERDCTL_CHECKSUM_FILE_PATTERN = "(?<hashfunction>(?:^({0})))" -f ($HASH_FUNCTIONS -join '|')
$NERDCTL_FILTER_SCRIPTBLOCK_STR = { (("{0}" -match "$NERDCTL_CHECKSUM_FILE_PATTERN") -and "{0}" -notmatch ".*.asc$") }.ToString()

function Get-LatestToolVersion($repository) {
$CONTAINERD_REPO = "containerd/containerd"
$BUILDKIT_REPO = "moby/buildkit"
$NERDCTL_REPO = "containerd/nerdctl"
$WINCNI_PLUGIN_REPO = "microsoft/windows-container-networking"
$CLOUDNATIVE_CNI_REPO = "containernetworking/plugins"


function Get-LatestToolVersion($tool) {
# Get the repository based on the tool
$repository = switch ($tool.ToLower()) {
"containerd" { $CONTAINERD_REPO }
"buildkit" { $BUILDKIT_REPO }
"nerdctl" { $NERDCTL_REPO }
"wincniplugin" { $WINCNI_PLUGIN_REPO }
"cloudnativecni" { $CLOUDNATIVE_CNI_REPO }
Default { Throw "Couldn't get latest $tool version. Invalid tool name: '$tool'." }
}

# Get the latest release version URL string
$uri = "https://api.github.com/repos/$repository/releases/latest"

Write-Debug "Getting the latest $tool version from $uri"

# Get the latest release version
try {
$uri = "https://api.github.com/repos/$repository/releases/latest"
$response = Invoke-WebRequest -Uri $uri -UseBasicParsing
$version = ($response.content | ConvertFrom-Json).tag_name
return $version.TrimStart("v")
}
catch {
$tool = ($repository -split "/")[1]
Throw "Could not get $tool latest version. $($_.Exception.Message)"
Throw "Couldn't get $tool latest version from $uri. $($_.Exception.Message)"
}
}

Expand Down
2 changes: 1 addition & 1 deletion containers-toolkit/Public/BuildkitTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force

function Get-BuildkitLatestVersion {
$latestVersion = Get-LatestToolVersion -Repository "moby/buildkit"
$latestVersion = Get-LatestToolVersion -Tool "buildkit"
return $latestVersion
}

Expand Down
19 changes: 15 additions & 4 deletions containers-toolkit/Public/ContainerNetworkTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ using module "..\Private\CommonToolUtilities.psm1"
$ModuleParentPath = Split-Path -Parent $PSScriptRoot
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force

$WINCNI_PLUGIN_REPO = "microsoft/windows-container-networking"
$CLOUDNATIVE_CNI_REPO = "containernetworking/plugins"

function Get-WinCNILatestVersion {
$latestVersion = Get-LatestToolVersion -Repository "microsoft/windows-container-networking"
param (
[String]$repo = "microsoft/windows-container-networking"
)
$tool = switch ($repo.ToLower()) {
$WINCNI_PLUGIN_REPO { "wincniplugin" }
$CLOUDNATIVE_CNI_REPO { "cloudnativecni" }
Default { Throw "Invalid repository. Supported repositories are $WINCNI_PLUGIN_REPO and $CLOUDNATIVE_CNI_REPO" }
}
$latestVersion = Get-LatestToolVersion -Tool $tool
return $latestVersion
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion containers-toolkit/Public/ContainerdTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force


function Get-ContainerdLatestVersion {
$latestVersion = Get-LatestToolVersion -Repository "containerd/containerd"
$latestVersion = Get-LatestToolVersion -Tool "containerd"
return $latestVersion
}

Expand Down
2 changes: 1 addition & 1 deletion containers-toolkit/Public/NerdctlTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force

function Get-NerdctlLatestVersion {
$latestVersion = Get-LatestToolVersion -Repository "containerd/nerdctl"
$latestVersion = Get-LatestToolVersion -Tool "nerdctl"
return $latestVersion
}

Expand Down

0 comments on commit b405616

Please sign in to comment.