add retry logic to Cloud Backup download

This commit is contained in:
2026-11-17 01:01:10 -05:00
parent bfdf8f5eae
commit 2bbb07c05e
3 changed files with 69 additions and 12 deletions
+26 -9
View File
@@ -66,17 +66,23 @@ try {
}
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 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' } }
function Get-FixtureInstallation {
if ($scenario -eq 'absent') { return }
$v = '8.9.1.177'
@@ -102,7 +108,7 @@ try {
$record.PlanChecks++
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 } }
if ($scenario -eq 'busy' -or ($record.Downloads -and $scenario -in @('late plan','plan during retry'))) { [pscustomobject]@{ ProcessId=42 } }
}
function Start-Process {
param($FilePath,$ArgumentList,$WindowStyle,[switch]$Wait,[switch]$PassThru,$ErrorAction)
@@ -125,6 +131,7 @@ try {
@('only VM newer',0,0,0), @('VM needs update',0,1,1), @('incomplete Windows',1,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),
@('timeout then success',0,2,1), @('plan during retry',0,2,0), @('persistent timeout',1,2,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)
@@ -149,6 +156,16 @@ try {
($messages -join ' ') -notmatch 'No update needed') { throw "$scenario did not take the expected early exit" }
}
if ($record.Staged -and (Test-Path -LiteralPath $record.Staged)) { throw "$scenario left staged media" }
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.'
}
}
Write-Host "PASS $scenario"
}