Files
management-scripts/tests/Update-EmberkomCloudBackup.Tests.ps1
T

195 lines
11 KiB
PowerShell
Raw Normal View History

2026-11-17 00:13:07 -05:00
#Requires -Version 5.1
# Run with Windows PowerShell. All downloads, plan checks and installs are fixtures.
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$fixtureDir = Join-Path ([IO.Path]::GetTempPath()) ('CloudBackupTest-' + [guid]::NewGuid())
$null = New-Item -ItemType Directory -Path $fixtureDir
function Read-TestAst($Path) {
$parseErrors = $null
$tree = [Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$parseErrors)
if ($parseErrors) { throw ($parseErrors | Out-String) }
return $tree
}
function Find-TestFunction($Tree, $Name) {
$fn = $Tree.Find({param($n) $n -is [Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $Name}, $false)
if (!$fn) { throw "Missing $Name" }
return $fn
}
try {
$core = Read-TestAst (Join-Path $repo 'Update-EmberkomCloudBackup.ps1')
$windowFn = Find-TestFunction $core 'Test-CloudBackupUpdateWindow'
. ([scriptblock]::Create($windowFn.Extent.Text))
foreach ($case in @(
@('23:30-00:30','23:30',$true), @('23:30-00:30','00:29',$true),
@('23:30-00:30','00:30',$false), @('12:00-13:00','11:59',$false),
@('12:00-13:00','12:00',$true), @('12:00-13:00','13:00',$false), @('','02:00',$true)
)) {
if ((Test-CloudBackupUpdateWindow $case[0] ([datetime]::ParseExact($case[1],'HH:mm',$null))) -ne $case[2]) {
throw "Window boundary failed for $($case[0]) at $($case[1])"
}
}
# Substitute only discovery of the installed product in the downloaded core.
2026-11-17 00:36:59 -05:00
$discovery = Find-TestFunction $core 'Get-CloudBackupInstallation'
$coreSource = $core.Extent.Text.Replace($discovery.Extent.Text, 'function Get-CloudBackupInstallation { Get-FixtureInstallation }')
2026-11-17 00:13:07 -05:00
$toolsAst = Read-TestAst (Join-Path $repo 'Tools.ps1')
2026-11-17 00:36:59 -05:00
$shimTools = (@('Run-Script','Get-EmberkomCloudBackupReleases','Get-EmberkomCloudBackupRelease','Save-EmberkomCloudBackupInstaller') |
2026-11-17 00:13:07 -05:00
ForEach-Object { (Find-TestFunction $toolsAst $_).Extent.Text }) -join [Environment]::NewLine
$fixtureBytes = [byte[]](1,2,3)
$sha = [Security.Cryptography.SHA256]::Create()
try { $hash = [BitConverter]::ToString($sha.ComputeHash($fixtureBytes)).Replace('-','') } finally { $sha.Dispose() }
$manifest = Get-Content -Raw -LiteralPath (Join-Path $repo 'resources/EmberkomCloudBackup.json') | ConvertFrom-Json
foreach ($kind in 'Windows','VM') {
if ($manifest.$kind.SHA256 -notmatch '^[a-f0-9]{64}$' -or $manifest.$kind.Url -notlike 'https://*') { throw "Bad $kind release" }
$manifest.$kind.SHA256 = $hash
$manifest.$kind.Version = '8.10.1.177'
}
function IsURLValid { param($URL) $true }
function Download-File {
param($URL)
if ($URL -notlike '*/Update-EmberkomCloudBackup.ps1') { throw "Unexpected script request" }
$path = Join-Path $fixtureDir 'downloaded.ps1'
[IO.File]::WriteAllText($path, $coreSource)
return $path
}
function New-Object {
param($TypeName)
if ($TypeName -ne 'Net.WebClient') { throw "Unexpected construction of $TypeName" }
$client = [pscustomobject]@{}
$client | Add-Member ScriptMethod DownloadString { param($url)
if ($url -like '*/Tools.ps1') { return $shimTools }
if ($url -like '*/Update-EmberkomCloudBackup.ps1') { return $coreSource }
2026-11-17 00:36:59 -05:00
if ($url -like '*/resources/EmberkomCloudBackup.json') {
$record.ManifestReads++
return ($manifest | ConvertTo-Json)
}
2026-11-17 00:13:07 -05:00
throw "Unexpected URL"
}
$client | Add-Member ScriptMethod Dispose {}
return $client
}
2026-11-17 01:01:10 -05:00
function Invoke-WebRequest {
param([switch]$UseBasicParsing,$Uri,$OutFile,$TimeoutSec,$ErrorAction)
$record.Downloads++
if ($Uri -cne $manifest.$kind.Url -or !$UseBasicParsing -or $TimeoutSec -ne 240) { throw 'Unexpected installer request' }
if (Test-Path -LiteralPath $OutFile) { throw 'Partial installer was not removed before retry' }
$record.Staged = $OutFile
[IO.File]::WriteAllBytes($OutFile, $fixtureBytes)
if ($scenario -eq 'corrupt') { [IO.File]::AppendAllText($OutFile,'x') }
if ($scenario -eq 'download failure') { throw [Net.WebException]::new('Certificate validation failed',[Net.WebExceptionStatus]::TrustFailure) }
if ($scenario -eq 'persistent timeout' -or ($scenario -in @('timeout then success','plan during retry') -and $record.Downloads -eq 1)) {
throw [Net.WebException]::new('The operation has timed out',[Net.WebExceptionStatus]::Timeout)
}
}
function Start-Sleep { param($Seconds) if ($Seconds -ne 3) { throw 'Unexpected retry delay' } }
2026-11-17 00:36:59 -05:00
function Get-FixtureInstallation {
2026-11-17 00:13:07 -05:00
if ($scenario -eq 'absent') { return }
$v = '8.9.1.177'
2026-11-17 00:36:59 -05:00
if ($scenario -in @('current','only VM newer','VM needs update','incomplete Windows')) { $v = '8.10.1.177' }
if ($record.Installs -and $scenario -ne 'unverified') { $v = $manifest.$kind.Version }
2026-11-17 00:13:07 -05:00
if ($scenario -eq 'newer') { $v = '8.11.0.1' }
if ($scenario -eq 'already updated' -and $record.Downloads) { $v = '8.10.1.177' }
2026-11-17 00:36:59 -05:00
[pscustomobject]@{ Version=[version]$v; Cli='Get-FixtureCli' }
}
function Get-FixtureCli($Command) {
if ($Command -ne 'ver') { throw 'Unexpected agent command' }
$record.CliCalls++
$global:LASTEXITCODE = 0
if ($kind -eq 'VM') { 'Backup Agent Edition: VMware' } else { 'Backup Agent Edition: DesktopIBB' }
'Service name: Fixture Service'
2026-11-17 00:13:07 -05:00
}
function Get-Date {
if ($scenario -eq 'window ended' -and $record.Downloads) { return [datetime]'2026-01-01T13:00' }
return [datetime]'2026-01-01T12:30'
}
function Get-CimInstance {
param($ClassName,$Filter,$Property,$ErrorAction)
2026-11-17 00:36:59 -05:00
$record.PlanChecks++
2026-11-17 00:13:07 -05:00
if ($ClassName -ne 'Win32_Process' -or $Filter -ne "Name = 'CBBackupPlan.exe'") { throw 'Unexpected worker query' }
if ($scenario -eq 'query failure') { throw 'Cannot enumerate processes' }
2026-11-17 01:01:10 -05:00
if ($scenario -eq 'busy' -or ($record.Downloads -and $scenario -in @('late plan','plan during retry'))) { [pscustomobject]@{ ProcessId=42 } }
2026-11-17 00:13:07 -05:00
}
function Start-Process {
param($FilePath,$ArgumentList,$WindowStyle,[switch]$Wait,[switch]$PassThru,$ErrorAction)
$record.Installs++
if ($FilePath -ne $record.Staged -or $ArgumentList -cne '/S' -or !$Wait -or !$PassThru) { throw 'Unexpected setup invocation' }
$code = switch ($scenario) { reboot {3010} failure {1603} default {0} }
[pscustomobject]@{ ExitCode=$code }
}
function Get-Service {
param($Name,$ErrorAction)
if ($Name -ne 'Fixture Service') { throw 'Unexpected service name' }
$service = [pscustomobject]@{}
$service | Add-Member ScriptMethod WaitForStatus { param($state,$timeout)
if ($state -ne 'Running' -or $scenario -eq 'service down') { throw 'Backup service unavailable' }
}
return $service
}
foreach ($case in @(
@('absent',0,0,0), @('current',0,0,0), @('newer',0,0,0),
2026-11-17 00:36:59 -05:00
@('only VM newer',0,0,0), @('VM needs update',0,1,1), @('incomplete Windows',1,0,0),
2026-11-17 00:13:07 -05:00
@('busy',0,0,0), @('late plan',0,1,0), @('query failure',1,0,0),
@('download failure',1,1,0), @('already updated',0,1,0), @('window ended',0,1,0),
2026-11-17 01:01:10 -05:00
@('timeout then success',0,2,1), @('plan during retry',0,2,0), @('persistent timeout',1,2,0),
2026-11-17 00:13:07 -05:00
@('bad window',1,0,0), @('corrupt',1,1,0), @('success',0,1,1),
@('VM',0,1,1), @('failure',1,1,1), @('unverified',1,1,1),
@('service down',1,1,1), @('reboot',3010,1,1)
)) {
$scenario,$expected,$expectedDownloads,$expectedInstalls = $case
2026-11-17 00:36:59 -05:00
$kind = if ($scenario -in @('VM','VM needs update')) { 'VM' } else { 'Windows' }
$manifest.VM.Version = if ($scenario -in @('only VM newer','VM needs update')) { '8.11.1.177' } else { '8.10.1.177' }
$manifest.Windows.SHA256 = if ($scenario -eq 'incomplete Windows') { '' } else { $hash }
2026-11-17 00:13:07 -05:00
$MSP360UpdateWindow = switch ($scenario) {
'bad window' { '25:00-26:00' }
'window ended' { '12:00-13:00' }
default { '' }
}
2026-11-17 00:36:59 -05:00
$record = @{ Downloads=0; Installs=0; Staged=$null; ManifestReads=0; CliCalls=0; PlanChecks=0 }
2026-11-17 00:13:07 -05:00
$messages = & (Join-Path $repo 'rmm/Update-EmberkomCloudBackup.ps1') *>&1
if ($LASTEXITCODE -ne $expected -or $record.Downloads -ne $expectedDownloads -or $record.Installs -ne $expectedInstalls) {
throw "$scenario failed. Exit=$LASTEXITCODE Downloads=$($record.Downloads) Installs=$($record.Installs). $messages"
}
2026-11-17 00:36:59 -05:00
if ($scenario -in @('current','newer','only VM newer')) {
$expectedCliCalls = if ($scenario -eq 'only VM newer') { 1 } else { 0 }
if ($record.ManifestReads -ne 1 -or $record.CliCalls -ne $expectedCliCalls -or $record.PlanChecks -ne 0 -or
($messages -join ' ') -notmatch 'No update needed') { throw "$scenario did not take the expected early exit" }
}
2026-11-17 00:13:07 -05:00
if ($record.Staged -and (Test-Path -LiteralPath $record.Staged)) { throw "$scenario left staged media" }
2026-11-17 01:01:10 -05:00
if ($scenario -eq 'persistent timeout') {
$output = $messages -join ' '
if ($output -notmatch 'attempt 2 of 2.*Timeout.*3 bytes saved' -or $output -match 'urlParams=') {
throw 'Persistent timeout must report the failed attempt and byte count without the signed URL.'
}
if (!$output.Contains($record.Staged) -or !$output.Contains($hash) -or
$output -notmatch "Z $([regex]::Escape($env:COMPUTERNAME)) - MSP360 installer download") {
throw 'Missing endpoint, UTC time, installer path or hash for endpoint security correlation.'
}
}
2026-11-17 00:13:07 -05:00
Write-Host "PASS $scenario"
}
# Check the provisioner's existing edition mapping at the new release-file boundary.
& {
function Import-MSP360Module {}
function Get-MBSAgent { $false }
function Get-EmberkomCloudBackupRelease($Build) {
$record.Selected = $Build
$record.AgentEdition = $AgentEdition
throw 'Stopped at release lookup'
}
foreach ($pair in @(@('desktop','Windows','desktop'), @('server','Windows','baremetal'), @('vm','VM','vmedition'))) {
$Edition = $pair[0]
$record = @{}
try { & (Join-Path $repo 'Install-EmberkomCloudBackup.ps1') }
catch { if ($_.Exception.Message -ne 'Stopped at release lookup') { throw } }
if ($record.Selected -ne $pair[1] -or $record.AgentEdition -ne $pair[2]) { throw "Wrong provisioning package for $Edition" }
Write-Host "PASS existing $Edition installer release selection"
}
}
} finally {
$fixtureCore = Join-Path $fixtureDir 'downloaded.ps1'
if (Test-Path -LiteralPath $fixtureCore) { Remove-Item -LiteralPath $fixtureCore -Force }
Remove-Item -LiteralPath $fixtureDir -Force
}