158 lines
8.2 KiB
PowerShell
158 lines
8.2 KiB
PowerShell
#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.
|
|
$discovery = Find-TestFunction $core 'Get-CloudBackupAgent'
|
|
$coreSource = $core.Extent.Text.Replace($discovery.Extent.Text, 'function Get-CloudBackupAgent { Get-FixtureAgent }')
|
|
$toolsAst = Read-TestAst (Join-Path $repo 'Tools.ps1')
|
|
$shimTools = (@('Run-Script','Get-EmberkomCloudBackupRelease','Save-EmberkomCloudBackupInstaller') |
|
|
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 }
|
|
if ($url -like '*/resources/EmberkomCloudBackup.json') { return ($manifest | ConvertTo-Json) }
|
|
throw "Unexpected URL"
|
|
}
|
|
$client | Add-Member ScriptMethod DownloadFile { param($url,$path)
|
|
$record.Downloads++
|
|
if ($url -cne $manifest.$kind.Url) { throw 'Downloaded the wrong edition' }
|
|
$record.Staged = $path
|
|
[IO.File]::WriteAllBytes($path, $fixtureBytes)
|
|
if ($scenario -eq 'corrupt') { [IO.File]::AppendAllText($path,'x') }
|
|
if ($scenario -eq 'download failure') { throw 'Transfer interrupted' }
|
|
}
|
|
$client | Add-Member ScriptMethod Dispose {}
|
|
return $client
|
|
}
|
|
function Get-FixtureAgent {
|
|
if ($scenario -eq 'absent') { return }
|
|
$v = '8.9.1.177'
|
|
if ($scenario -eq 'current' -or ($record.Installs -and $scenario -ne 'unverified')) { $v = '8.10.1.177' }
|
|
if ($scenario -eq 'newer') { $v = '8.11.0.1' }
|
|
if ($scenario -eq 'already updated' -and $record.Downloads) { $v = '8.10.1.177' }
|
|
[pscustomobject]@{ Version=[version]$v; Build=$kind; Service='Fixture Service' }
|
|
}
|
|
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)
|
|
if ($ClassName -ne 'Win32_Process' -or $Filter -ne "Name = 'CBBackupPlan.exe'") { throw 'Unexpected worker query' }
|
|
if ($scenario -eq 'query failure') { throw 'Cannot enumerate processes' }
|
|
if ($scenario -eq 'busy' -or ($record.Downloads -and $scenario -eq 'late plan')) { [pscustomobject]@{ ProcessId=42 } }
|
|
}
|
|
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),
|
|
@('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),
|
|
@('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
|
|
$kind = if ($scenario -eq 'VM') { 'VM' } else { 'Windows' }
|
|
$MSP360UpdateWindow = switch ($scenario) {
|
|
'bad window' { '25:00-26:00' }
|
|
'window ended' { '12:00-13:00' }
|
|
default { '' }
|
|
}
|
|
$record = @{ Downloads=0; Installs=0; Staged=$null }
|
|
$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"
|
|
}
|
|
if ($record.Staged -and (Test-Path -LiteralPath $record.Staged)) { throw "$scenario left staged media" }
|
|
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
|
|
}
|