Skip to content

Backward compatibility for validating JSON files using Test-JSON command #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ You can find documentation for these functions here: [Containers-Toolkit Documen

1. PowerShell: Minimum Version 7

> NOTE: While this module is developed and tested with PowerShell 7, it may work with PowerShell 5.1, but it is not guaranteed. PowerShell 7 is recommended for the best experience.

1. `HNS` module

To install the HNS module, follow the [instructions here](./docs/FAQs.md#2-new-hnsnetwork-command-does-not-exist)
Expand Down
23 changes: 21 additions & 2 deletions Tests/CommonToolUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,15 @@ Describe "CommonToolUtilities.psm1" {
-ParameterFilter { $Path -eq $Script:ChecksumFile }
}

It "should skip JSON validation when Test-Json is not available" {
Mock Get-Command -ModuleName "CommonToolUtilities" -MockWith { return $null } -ParameterFilter {
$Name -eq 'Test-Json'
}

{ & $Script:FunctionToCall } | Should -Not -Throw
Should -Invoke Test-Json -Times 0 -Scope It -ModuleName "CommonToolUtilities"
}

It "should throw an error if checksum file does not exist" {
Mock Test-Path -ModuleName "CommonToolUtilities" -MockWith { return $false } -ParameterFilter { $Path -eq $Script:ChecksumFile }

Expand Down Expand Up @@ -576,14 +585,24 @@ Describe "CommonToolUtilities.psm1" {
{ & $Script:FunctionToCall } | Should -Throw "Invalid schema file: $Script:SchemaFile. Schema file is empty."
}

It "should throw an error if the JSON file is not valid" {
It "should throw an error if Test-Json fails" {
Mock Get-Content -ModuleName "CommonToolUtilities" -MockWith { return "Test data" }

# Test-Json returns true if the JSON is valid, otherwise it throws an error
Mock Test-Json -ModuleName "CommonToolUtilities" -MockWith { Throw "Error" }

# Test the function
{ & $Script:FunctionToCall } | Should -Throw "Invalid JSON format in checksum file. Error"
{ & $Script:FunctionToCall } | Should -Throw "Error validating JSON checksum file. Error"
}

It "should throw an error if the JSON file is not valid" {
Mock Get-Content -ModuleName "CommonToolUtilities" -MockWith { return "Test data" }

# Test-Json returns true if the JSON is valid, otherwise it throws an error
Mock Test-Json -ModuleName "CommonToolUtilities" -MockWith { return $false }

# Test the function
{ & $Script:FunctionToCall } | Should -Throw "Checksum file validation failed for $Script:ChecksumFile. Please check the file format and schema."
}

It "should throw an error if script block throws an error" {
Expand Down
14 changes: 11 additions & 3 deletions containers-toolkit/Private/CommonToolUtilities.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public enum ActionConsent {
}
'@

$HASH_FUNCTIONS = @("SHA1", "SHA256", "SHA384", "SHA512", "MD5")
$HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA1|SHA256|SHA384|SHA512|MD5
$HASH_FUNCTIONS = @("SHA256", "SHA384", "SHA512")
$HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA256|SHA384|SHA512
$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()

Expand Down Expand Up @@ -579,6 +579,9 @@ function Test-JSONChecksum {
# Validate the checksum file
$isJsonValid = ValidateJSONChecksumFile -ChecksumFilePath $checksumFile -SchemaFile $SchemaFile
Write-Debug "Checksum JSON file validation status. {success: $isJsonValid}"
if ($isJsonValid -eq $false) {
Throw "Checksum file validation failed for $checksumFile. Please check the file format and schema."
}

if ($null -eq $ExtractDigestScriptBlock) {
Write-Debug "Using default JSON checksum extraction script block"
Expand Down Expand Up @@ -650,11 +653,16 @@ function ValidateJSONChecksumFile {

# Test JSON checksum file is valid
try {
# Check if Test-Json cmdlet is available
if (-not (Get-Command -Name Test-Json -ErrorAction SilentlyContinue)) {
Write-Warning "Couldn't validate JSON checksum file. Test-Json cmdlet is not available. Upgrade to PowerShell 6.1 or later to use this feature."
return $null
}
$isValidJSON = Test-Json -Json "$(Get-Content -Path $ChecksumFilePath -Raw)" -Schema "$schemaFileContent"
return $isValidJSON
}
catch {
Throw "Invalid JSON format in checksum file. $_"
Throw "Error validating JSON checksum file. $($_.Exception.Message)"
}
}

Expand Down