Skip to content

Commit

Permalink
Refactor scripts to use common tool invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
mthalman committed Apr 29, 2024
1 parent 665f1d2 commit aee8a79
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 44 deletions.
24 changes: 8 additions & 16 deletions container/check-image.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@ $ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
Set-StrictMode -Version 2.0

function LogMessage ($Message) {
Write-Output $Message | Out-File $env:HOME/log.txt -Append
}

$expr = "dredge image compare layers --output json $BaseImage $TargetImage --os linux --arch $Architecture"
LogMessage "Invoke: $expr"
$result = Invoke-Expression $expr
$dredgeExitCode = $LASTEXITCODE
LogMessage "Result: $result"
if ($dredgeExitCode -ne 0) {
throw "dredge image compare failed"
}

$result = $result | ConvertFrom-Json

$imageUpToDate = [bool]$($result.summary.targetIncludesAllBaseLayers)
Import-Module $PSScriptRoot/common.psm1

$cmd = "dredge image compare layers --output json $BaseImage $TargetImage --os linux --arch $Architecture"
$layerComparisonStr = $(InvokeTool $cmd "dredge image compare failed")

$layerComparison = $layerComparisonStr | ConvertFrom-Json

$imageUpToDate = [bool]$($layerComparison.summary.targetIncludesAllBaseLayers)
$sendDispatch = ([string](-not $imageUpToDate)).ToLower()

LogMessage "Send dispatch: $sendDispatch"
Expand Down
24 changes: 24 additions & 0 deletions container/common.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
Set-StrictMode -Version 2.0

function LogMessage ([string] $Message) {
Write-Output $Message | Out-File $env:HOME/log.txt -Append
}

function InvokeTool([string]$ToolCommand, [string] $ErrorMessage) {
LogMessage "Invoke: $ToolCommand"

# Reset $LASTEXITCODE in case it was tripped somewhere
# See https://github.com/pester/Pester/issues/1616
$Global:LASTEXITCODE = 0

$result = Invoke-Expression $ToolCommand
$exitCode = $LASTEXITCODE
LogMessage "Result: $result"
if ($exitCode -ne 0) {
throw $ErrorMessage
}

return $result
}
4 changes: 2 additions & 2 deletions container/entrypoint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ if (-not $BaseImage) {
$BaseImage = $(& $PSScriptRoot/get-base-image.ps1 -DockerfilePath $DockerfilePath -BaseStageName $BaseStageName)
}

$triggerWorkflow = $(& $PSScriptRoot/check-image.ps1 -TargetImage $targetImage -BaseImage $BaseImage -Architecture $Architecture)
$result = $(& $PSScriptRoot/check-image.ps1 -TargetImage $targetImage -BaseImage $BaseImage -Architecture $Architecture)

return $triggerWorkflow
return $result
14 changes: 3 additions & 11 deletions container/get-base-image.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ $ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
Set-StrictMode -Version 2.0

function LogMessage ($Message) {
Write-Output $Message | Out-File $env:HOME/log.txt -Append
}
Import-Module $PSScriptRoot/common.psm1

if (-not (Test-Path $DockerfilePath)) {
throw "Dockerfile path '$DockerfilePath' does not exist."
Expand All @@ -36,14 +34,8 @@ if (-not $BaseStageName) {

$dfspyArgsString = $dfspyArgs -join ' '

$expr = "dfspy $dfspyArgsString"
LogMessage "Invoke: $expr"
$fromOutput = Invoke-Expression $expr
$dfspyExitCode = $LASTEXITCODE
LogMessage "Result: $fromOutput"
if ($dfspyExitCode -ne 0) {
throw "dfspy failed"
}
$cmd = "dfspy $dfspyArgsString"
$fromOutput = $(InvokeTool $cmd "dfspy failed")
$fromOutput = $fromOutput | ConvertFrom-Json

if ($BaseStageName) {
Expand Down
15 changes: 9 additions & 6 deletions container/tests/check-image.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
BeforeAll {
Import-Module -Force $PSScriptRoot/../common.psm1

BeforeAll {
$global:LASTEXITCODE = 0
Mock Invoke-Expression { $global:LASTEXITCODE = 0 } -ParameterFilter { $Command -like "dotnet *" }
Mock Out-File { }
Mock Out-File { } -ModuleName common

$targetImage = "foo"
$baseImage = "bar"
Expand All @@ -19,7 +20,8 @@ Describe 'Get result' {
}
}
$result | ConvertTo-Json
} -ParameterFilter { $Command -eq "dredge image compare layers --output json $baseImage $targetImage --os linux --arch $architecture" }
} -ParameterFilter { $Command -eq "dredge image compare layers --output json $baseImage $targetImage --os linux --arch $architecture" } `
-ModuleName common
& $targetScript -TargetImage $targetImage -BaseImage $baseImage -Architecture $architecture | Should -Be "false"
}

Expand All @@ -31,13 +33,14 @@ Describe 'Get result' {
}
}
$result | ConvertTo-Json
} -ParameterFilter { $Command -eq "dredge image compare layers --output json $baseImage $targetImage --os linux --arch $architecture" }
} -ParameterFilter { $Command -eq "dredge image compare layers --output json $baseImage $targetImage --os linux --arch $architecture" } `
-ModuleName common

& $targetScript -TargetImage $targetImage -BaseImage $baseImage -Architecture $architecture | Should -Be "true"
}

It 'Given a failed dredge command, it throws an error' {
Mock Invoke-Expression { $global:LASTEXITCODE = 1 } -ParameterFilter { $Command -like "dredge *" }
Mock Invoke-Expression { $global:LASTEXITCODE = 1 } -ParameterFilter { $Command -like "dredge *" } -ModuleName common

{ & $targetScript -TargetImage $targetImage -BaseImage $baseImage -Architecture $architecture } | Should -Throw "dredge image compare failed"
}
Expand Down
25 changes: 16 additions & 9 deletions container/tests/get-base-image.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Import-Module -Force $PSScriptRoot/../common.psm1

BeforeAll {
$global:LASTEXITCODE = 0
Mock Invoke-Expression { $global:LASTEXITCODE = 0 } -ParameterFilter { $Command -like "dotnet *" }
Mock Test-Path { $true }
Mock Out-File {}
Mock Out-File {} -ModuleName common

$targetScript = "$PSScriptRoot/../get-base-image.ps1"
}
Expand All @@ -19,7 +20,8 @@ Describe 'Get base image' {
}
)
$fromOutput | ConvertTo-Json
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile --layout graph" }
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile --layout graph" } `
-ModuleName common

& $targetScript -DockerfilePath 'Dockerfile' | Should -Be "foo"
}
Expand Down Expand Up @@ -47,7 +49,8 @@ Describe 'Get base image' {
}
)
$fromOutput | ConvertTo-Json -Depth 10
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile --layout graph" }
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile --layout graph" } `
-ModuleName common

& $targetScript -DockerfilePath 'Dockerfile' | Should -Be "foo3"
}
Expand Down Expand Up @@ -81,7 +84,8 @@ Describe 'Get base image' {
}
)
$fromOutput | ConvertTo-Json -Depth 10
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile --layout graph" }
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile --layout graph" } `
-ModuleName common

& $targetScript -DockerfilePath 'Dockerfile' | Should -Be "foo4"
}
Expand All @@ -97,7 +101,8 @@ Describe 'Get base image' {
}
)
$fromOutput | ConvertTo-Json
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile" }
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile" } `
-ModuleName common

& $targetScript -DockerfilePath 'Dockerfile' -BaseStageName stage1 | Should -Be "foo"
}
Expand All @@ -118,7 +123,8 @@ Describe 'Get base image' {
}
)
$fromOutput | ConvertTo-Json -Depth 10
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile" }
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile" } `
-ModuleName common

& $targetScript -DockerfilePath 'Dockerfile' -BaseStageName stage1 | Should -Be "foo3"
}
Expand All @@ -131,7 +137,7 @@ Describe 'Get base image' {
}

It 'Given a failed dfspy command, it throws an error' {
Mock Invoke-Expression { $global:LASTEXITCODE = 1 } -ParameterFilter { $Command -like "dfspy *" }
Mock Invoke-Expression { $global:LASTEXITCODE = 1 } -ParameterFilter { $Command -like "dfspy *" } -ModuleName common

{ & $targetScript -DockerfilePath 'Dockerfile' } | Should -Throw "dfspy failed"
}
Expand All @@ -146,7 +152,8 @@ Describe 'Get base image' {
}
)
$fromOutput | ConvertTo-Json
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile" }
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile" } `
-ModuleName common

{ & $targetScript -DockerfilePath 'Dockerfile' -BaseStageName stage2 } | Should -Throw "Could not find stage with name 'stage2'."
}
Expand Down

0 comments on commit aee8a79

Please sign in to comment.