|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +#Requires -Version 7 |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | +Seeds (mode seed) or verifies (mode assert) the file fixture for the startup |
| 7 | +downloaded-installer sweep scenario (sc-106111). |
| 8 | +
|
| 9 | +.DESCRIPTION |
| 10 | +Three control files are placed in the agent's updates directory: |
| 11 | +
|
| 12 | + installer-900000001.bin matches the name pattern the updater downloads under |
| 13 | + and is aged past the sweep threshold, so it must be |
| 14 | + reclaimed |
| 15 | + installer-900000002.bin matches the pattern but keeps the current timestamp, |
| 16 | + standing in for the installer of the update that is |
| 17 | + still running - the file the sweep must never take |
| 18 | + vendor-installer.bin is aged but is not a name the agent ever creates, so |
| 19 | + the sweep must leave it alone however stale it is |
| 20 | +
|
| 21 | +The files are sized in megabytes rather than bytes, so the reclaimed space is |
| 22 | +visible in the directory listing the fixture prints: the leak this scenario |
| 23 | +covers is measured in tens of megabytes per update, not in file counts. |
| 24 | +
|
| 25 | +The updates directory is created if it does not exist. An installation that has |
| 26 | +never completed an update has never created it, which is the normal state on a |
| 27 | +CI runner - the scenario exercises the sweep, not the download. |
| 28 | +
|
| 29 | +Ageing is the only thing simulated here: it is exactly what a long-lived install |
| 30 | +does by staying up. The production 24h threshold stays in play, so no test-only |
| 31 | +override is needed in the agent. |
| 32 | +
|
| 33 | +Symlink handling is asserted by the unit tests rather than here. Aging a symlink |
| 34 | +without following it needs a different call on every platform, and an un-aged |
| 35 | +link would survive on the age check alone - making the assertion pass without |
| 36 | +proving the sweep declined to follow it. |
| 37 | +#> |
| 38 | + |
| 39 | +param( |
| 40 | + [Parameter(Mandatory)][ValidateSet('seed', 'assert')][string]$Mode, |
| 41 | + [Parameter(Mandatory)][string]$UpdatesDir, |
| 42 | + [int]$StaleAgeHours = 48 |
| 43 | +) |
| 44 | + |
| 45 | +$ErrorActionPreference = 'Stop' |
| 46 | + |
| 47 | +$stale = Join-Path $UpdatesDir 'installer-900000001.bin' |
| 48 | +$fresh = Join-Path $UpdatesDir 'installer-900000002.bin' |
| 49 | +$foreign = Join-Path $UpdatesDir 'vendor-installer.bin' |
| 50 | + |
| 51 | +function Show-UpdatesDir { |
| 52 | + Write-Output "Contents of ${UpdatesDir}:" |
| 53 | + Get-ChildItem -LiteralPath $UpdatesDir -Force | |
| 54 | + Select-Object Name, Length, LastWriteTime | |
| 55 | + Format-Table | |
| 56 | + Out-String | |
| 57 | + Write-Output |
| 58 | +} |
| 59 | + |
| 60 | +if ($Mode -eq 'seed') { |
| 61 | + if (-not (Test-Path -LiteralPath $UpdatesDir)) { |
| 62 | + New-Item -ItemType Directory -Path $UpdatesDir -Force | Out-Null |
| 63 | + Write-Output "Created updates directory $UpdatesDir" |
| 64 | + } |
| 65 | + |
| 66 | + # 4 MiB each: small enough to write quickly, large enough that the listing |
| 67 | + # shows the sweep reclaiming real space rather than empty files. |
| 68 | + $payload = [byte[]]::new(4MB) |
| 69 | + foreach ($path in @($stale, $fresh, $foreign)) { |
| 70 | + [System.IO.File]::WriteAllBytes($path, $payload) |
| 71 | + } |
| 72 | + |
| 73 | + $aged = (Get-Date).AddHours(-$StaleAgeHours) |
| 74 | + foreach ($path in @($stale, $foreign)) { |
| 75 | + (Get-Item -LiteralPath $path -Force).LastWriteTime = $aged |
| 76 | + Write-Output "Aged $path to $aged" |
| 77 | + } |
| 78 | + |
| 79 | + Show-UpdatesDir |
| 80 | + exit 0 |
| 81 | +} |
| 82 | + |
| 83 | +if (-not (Test-Path -LiteralPath $UpdatesDir)) { |
| 84 | + Write-Error "Updates directory does not exist: $UpdatesDir" |
| 85 | + exit 1 |
| 86 | +} |
| 87 | + |
| 88 | +$failed = $false |
| 89 | + |
| 90 | +if (Test-Path -LiteralPath $stale) { |
| 91 | + Write-Output "FAIL: stale installer file was not swept: $stale" |
| 92 | + $failed = $true |
| 93 | +} |
| 94 | +else { |
| 95 | + Write-Output "OK: stale installer file was swept: $stale" |
| 96 | +} |
| 97 | + |
| 98 | +foreach ($path in @($fresh, $foreign)) { |
| 99 | + if (Test-Path -LiteralPath $path) { |
| 100 | + Write-Output "OK: file the sweep must not touch survived: $path" |
| 101 | + } |
| 102 | + else { |
| 103 | + Write-Output "FAIL: sweep removed a file it must not touch: $path" |
| 104 | + $failed = $true |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +Show-UpdatesDir |
| 109 | + |
| 110 | +if ($failed) { |
| 111 | + Write-Error 'Startup installer sweep did not behave as expected' |
| 112 | + exit 1 |
| 113 | +} |
| 114 | + |
| 115 | +Write-Output 'Startup installer sweep reclaimed the stale downloaded installer and nothing else' |
0 commit comments