add checks for free disk space

This commit is contained in:
2026-10-22 16:57:31 -04:00
parent 0030be0c7b
commit c8eec20391
3 changed files with 177 additions and 2 deletions
+88 -1
View File
@@ -37,7 +37,38 @@ try {
# Exercise the real bounded retry loops without delaying offline tests.
function Start-Sleep { param([int]$Seconds) $script:testSleeps += $Seconds }
function Get-FileHash { throw 'Office deployment must not scan file checksums.' }
$diskSpaceCheck = ${function:Assert-OfficeDiskSpace}
# Staging/orchestration fixtures must not depend on this test computer's free space.
function Assert-OfficeDiskSpace { param($Workspace,[switch]$DownloadingMedia) }
$script:testSleeps = @()
& {
function Get-CimInstance {
param($ClassName,$Filter)
Assert-OfficeTest ($ClassName -eq 'Win32_LogicalDisk') 'Disk checks must use read-only logical-disk inventory.'
$script:diskQueries += $Filter
if ($script:diskReadFails) { throw 'Synthetic disk inventory failure' }
if ($Filter -match "DeviceID='([^']+)'") { [pscustomobject]@{FreeSpace=$script:freeBytes[$Matches[1]]} }
}
$script:diskReadFails=$false; $script:diskQueries=@()
$script:freeBytes=@{'C:'=16GB; 'D:'=6GB; 'E:'=10GB}
$output = @(& $diskSpaceCheck -Workspace 'C:\Temp\Office' -InstallPaths @('C:\Windows','C:\Program Files') -DownloadingMedia)
Assert-OfficeTest ($output.Count -eq 0 -and $script:diskQueries.Count -eq 1) 'A shared staging/install volume must combine reserves once without leaking log messages.'
$script:freeBytes['C:']=16GB-1
Assert-OfficeThrows { & $diskSpaceCheck -Workspace 'C:\Temp\Office' -InstallPaths @('C:\Windows') -DownloadingMedia } 'Insufficient disk space.*before downloading media.*16 GB.*Existing apps have not been removed'
$script:freeBytes['C:']=10GB
$null = & $diskSpaceCheck -Workspace 'C:\Temp\Office' -InstallPaths @('C:\Windows')
$script:freeBytes['C:']=1701MB
Assert-OfficeThrows { & $diskSpaceCheck -Workspace 'C:\Temp\Office' -InstallPaths @('C:\Windows') } 'Insufficient disk space.*after staging, before removal'
$script:freeBytes['C:']=10GB; $script:diskQueries=@()
$null = & $diskSpaceCheck -Workspace 'D:\Temp\Office' -InstallPaths @('C:\Windows','E:\Program Files') -DownloadingMedia
Assert-OfficeTest ($script:diskQueries.Count -eq 3) 'Separate staging, Windows, and application drives must each be checked.'
$script:freeBytes['D:']=5GB
Assert-OfficeThrows { & $diskSpaceCheck -Workspace 'D:\Temp\Office' -InstallPaths @('C:\Windows') -DownloadingMedia } 'Insufficient disk space on D:.*6 GB'
$script:freeBytes['C:']=$null
Assert-OfficeThrows { & $diskSpaceCheck -Workspace 'D:\Temp\Office' -InstallPaths @('C:\Windows') } 'Could not determine free space'
$script:diskReadFails=$true
Assert-OfficeThrows { & $diskSpaceCheck -Workspace 'D:\Temp\Office' -InstallPaths @('C:\Windows') } 'Synthetic disk inventory failure'
}
& {
$script:probeCalls=0; $script:testSleeps=@()
$result = Wait-OfficeResult { $script:probeCalls++; if ($script:probeCalls -eq 3) { [pscustomobject]@{Version='ready'} } } 'delayed fixture registration'
@@ -50,6 +81,12 @@ try {
Assert-OfficeTest ($script:probeCalls -eq 1) 'Failed inventory reads must not be treated as absent installations.'
}
& {
function Write-OfficeSetupDiagnostics {
param($SinceUtc)
$script:diagnosticCalls++
Assert-OfficeTest ($SinceUtc -is [DateTime]) 'ODT diagnostics must use the actual process start time.'
if ($script:diagnosticFails) { throw 'Synthetic locked diagnostic log' }
}
function Start-Process {
param($FilePath,$ArgumentList,$WorkingDirectory,[switch]$Wait,[switch]$PassThru,$WindowStyle,$RedirectStandardOutput,$RedirectStandardError)
$script:processCalls++; $script:processLogs += $RedirectStandardOutput
@@ -68,6 +105,43 @@ try {
Assert-OfficeTest ($code -eq $expectedCode -and $script:processCalls -eq $expectedCalls -and $script:testSleeps.Count -eq ($expectedCalls - 1)) "Incorrect retry behavior for $mode."
Assert-OfficeTest (@($script:processLogs | Select-Object -Unique).Count -eq $expectedCalls) 'MSI retry output logs must not overwrite earlier attempts.'
}
foreach ($script:diagnosticFails in @($false, $true)) {
$script:processExit=17004; $script:diagnosticCalls=0
$output = @(Invoke-OfficeProcess (Join-Path $testRoot 'setup.exe') '/configure inert.xml' (Join-Path $testRoot 'odt-failed'))
Assert-OfficeTest ($output.Count -eq 1 -and $output[0] -eq 17004 -and $script:diagnosticCalls -eq 1) 'ODT native diagnostic collection must retain the original exit code and not pollute output, even when collection fails.'
}
$script:diagnosticFails=$false
foreach ($script:processExit in @(0,3010)) {
$script:diagnosticCalls=0
$null = Invoke-OfficeProcess (Join-Path $testRoot 'setup.exe') '/configure inert.xml' (Join-Path $testRoot 'odt-success')
Assert-OfficeTest ($script:diagnosticCalls -eq 0) 'Successful ODT operations must not search unrelated native logs.'
}
}
& {
$nativeRoot = Join-Path $testRoot 'native-logs'; $null = [IO.Directory]::CreateDirectory($nativeRoot)
$since = [DateTime]::UtcNow.AddMinutes(-1)
$recent = Join-Path $nativeRoot ($env:COMPUTERNAME + '-20261022-test.log')
[IO.File]::WriteAllLines($recent, @('Normal setup entry', 'Click-To-Run Error {"ErrorCode":12345,"ErrorMessage":"Synthetic setup reason"}'))
$old = Join-Path $nativeRoot 'Office-old.log'; [IO.File]::WriteAllText($old, 'Error Old unrelated failure'); [IO.File]::SetLastWriteTimeUtc($old, $since.AddHours(-1))
$unrelated = Join-Path $nativeRoot 'OtherApplication.log'; [IO.File]::WriteAllText($unrelated, 'Error Unrelated app failure')
$empty = Join-Path $nativeRoot 'Office-empty.log'; [IO.File]::WriteAllText($empty, '')
$nested = Join-Path $nativeRoot 'nested'; $null = [IO.Directory]::CreateDirectory($nested)
[IO.File]::WriteAllText((Join-Path $nested 'Office-nested.log'), 'Error Nested unrelated failure')
$output = @(Write-OfficeSetupDiagnostics -SinceUtc $since -SearchRoots @($nativeRoot,$nativeRoot))
Assert-OfficeTest ($output.Count -eq 0) 'Native diagnostics must write through Tools logging without leaking return values.'
$log = Get-Content -LiteralPath $Global:LogFile -Raw
Assert-OfficeTest ($log -match 'Synthetic setup reason' -and $log.Contains($recent)) 'The management log must include the native filename and actual error detail.'
Assert-OfficeTest (([regex]::Matches($log, 'Native Office detail:.*Synthetic setup reason')).Count -eq 1) 'Duplicate search roots must not duplicate diagnostics.'
Assert-OfficeTest ($log -notmatch 'Old unrelated failure|Unrelated app failure|Nested unrelated failure|Office-empty.log') 'Diagnostics must exclude old, unrelated, empty, and nested logs.'
Assert-OfficeTest ((Get-Content -LiteralPath $recent -Raw) -match 'Normal setup entry') 'Collecting native diagnostics must not modify the original log.'
[IO.File]::WriteAllLines($recent, @(1..30 | ForEach-Object { "Error Bounded native detail $_" }))
$null = Write-OfficeSetupDiagnostics -SinceUtc $since -SearchRoots @($nativeRoot)
$log = Get-Content -LiteralPath $Global:LogFile -Raw
Assert-OfficeTest (([regex]::Matches($log, 'Native Office detail: Error Bounded native detail')).Count -eq (Get-OfficeDeploymentDefinitions).SetupDiagnosticErrorLines) 'Native error excerpts must be bounded.'
$null = Write-OfficeSetupDiagnostics -SinceUtc ([DateTime]::UtcNow.AddMinutes(1)) -SearchRoots @($nativeRoot)
Assert-OfficeTest ((Get-Content -LiteralPath $Global:LogFile -Raw) -match 'No recent nonempty native Office logs found') 'Missing native logs must be reported without inventing a diagnosis.'
function Get-Content { throw 'Synthetic native log read failure' }
Assert-OfficeTest (@(Write-OfficeSetupDiagnostics -SinceUtc $since -SearchRoots @($nativeRoot)).Count -eq 0) 'A locked native log must not throw or alter the installer result.'
}
& {
function Invoke-WebRequest {
@@ -336,6 +410,12 @@ try {
Assert-OfficeTest (([xml](Get-Content -LiteralPath $media.Config -Raw)).Configuration.Add.Version -eq $media.Version) 'Windows 10 installation must retain the compatible target.'
}
}
$mediaWorkspace = Join-Path $testRoot 'low-space-before-download'; $null = [IO.Directory]::CreateDirectory($mediaWorkspace)
$options = Get-OfficeDeploymentOptions @{OfficeSubscription='Microsoft 365 Business Standard'; OfficeArchitecture='64'}
$script:mediaProcesses=@()
function Assert-OfficeDiskSpace { param($Workspace,[switch]$DownloadingMedia) Assert-OfficeTest $DownloadingMedia.IsPresent 'A fresh download must include staging space.'; throw 'Synthetic insufficient download space' }
Assert-OfficeThrows { Initialize-OfficeMedia $options $mediaWorkspace } 'Synthetic insufficient download space'
Assert-OfficeTest (!$script:mediaProcesses.Count -and !(Test-Path -LiteralPath (Join-Path $mediaWorkspace 'odt.exe'))) 'Low space must stop before downloading or launching ODT.'
}
& {
$script:staged = @(); $script:verified = @()
@@ -763,6 +843,7 @@ try {
$options = Get-OfficeDeploymentOptions @{OfficeSubscription='Microsoft/Office 365 E3/E5 (no Teams)'; OfficeArchitecture='64'; COMPANY='New Company'; OfficeAppShutdown='Force'; ValidateBeforeChanges='false'}
$script:rejectCacheSignature = $false; $script:helperDownloads = 0
function Get-CimInstance { }
function Assert-OfficeDiskSpace { throw 'Cache reuse must not request fresh-download disk reserve; the main workflow checks installation space afterward.' }
function Assert-OfficeMicrosoftSignature { param($Path) if ($script:rejectCacheSignature) { throw 'Untrusted cache signature' }; Assert-OfficeTest (Test-Path -LiteralPath $Path) 'Cache signature target was missing.' }
function Save-OfficeDownload { param($Uri,$Path) if ([IO.Path]::GetFileName($Path) -ne 'Office-UserCleanup.ps1') { throw 'Unexpected network download on cache hit' }; $script:helperDownloads++; Set-Content -LiteralPath $Path -Value ('# ' + (Get-OfficeDeploymentDefinitions).HelperVersion) }
function Invoke-OfficeProcess { throw 'A cached deployment must not redownload Office.' }
@@ -892,6 +973,7 @@ try {
throw "Unexpected object requested by orchestration fixture: $TypeName"
}
function Assert-OfficeDeploymentHost { }
function Assert-OfficeDiskSpace { param($Workspace,[switch]$DownloadingMedia) if ($script:mode -eq 'disk-space') { throw 'Insufficient disk space after staging' } }
function Complete-OfficeDeploymentFiles { param($Workspace,$UserWorkspace,$PreviousRoot) $script:artifactCleanupRuns++; if ($script:mode -eq 'cleanup-warning') { return ,@('Synthetic file in use') }; return ,@() }
function New-OfficeUserWorkspace { param($Media,$Workspace) $Workspace }
function Register-ScheduledTask { throw 'Unexpected task registration in orchestration fixture.' }
@@ -920,7 +1002,7 @@ try {
function Install-OfficeCompanionApps { param($Media,$Workspace,[bool]$InstallTeams) $script:teamsRequested=$InstallTeams; $script:events += 'install-companions'; if ($script:mode -eq 'install-companions') { throw 'companion install failure' }; [pscustomobject]@{ExitCode=$(if ($script:mode -eq 'companion-reboot') {3010} else {0}); Versions=@{NewOutlook='1'; OneDrive='1'}} }
$script:removalStillRegistered=$false
$options = Get-OfficeDeploymentOptions @{OfficeSubscription='Microsoft 365 Business Premium'; OfficeAppShutdown='Force'; ValidateBeforeChanges='false'; DeferReboot='false'}
foreach ($script:mode in @('success','download','companions','remove-office','identity','user','phase','install','install-companions','pending','reboot','office-reboot','companion-reboot')) {
foreach ($script:mode in @('success','download','disk-space','companions','remove-office','identity','user','phase','install','install-companions','pending','reboot','office-reboot','companion-reboot')) {
$script:artifactCleanupRuns = 0
$script:events = @(); $errorResult = $null; $result = $null
try { $result = Invoke-OfficeDeployment $options } catch { $errorResult = $_ }
@@ -932,10 +1014,15 @@ try {
if ($script:mode -notin @('success','pending','install','install-companions','companion-reboot')) { Assert-OfficeTest ('install' -notin $script:events) "Install ran after $script:mode failure/reboot." }
if ($script:mode -notin @('success','pending','install-companions','companion-reboot')) { Assert-OfficeTest ('install-companions' -notin $script:events) "Companion install ran after $script:mode failure/reboot." }
if ($script:mode -eq 'download') { Assert-OfficeTest ('companions' -notin $script:events -and 'remove-office' -notin $script:events) 'Removal ran after download failed.' }
if ($script:mode -eq 'disk-space') { Assert-OfficeTest (($script:events -join ',') -eq 'download') 'Low space after staging must stop before app shutdown, removal, identity cleanup, and installation.' }
Assert-OfficeTest ($script:artifactCleanupRuns -eq $(if ($script:mode -in @('success','pending','companion-reboot')) {1} else {0})) "Media cleanup ran before successful installation, or was skipped after success: $script:mode"
if ($script:mode -eq 'office-reboot') { Assert-OfficeTest ($result.OfficeDeploymentStatus -eq 'RestartRequiredBeforeInstall' -and 'identity' -notin $script:events -and 'user' -notin $script:events) 'Office removal restart must stop before user cleanup and replacement installation.' }
if ($script:mode -eq 'success') { Assert-OfficeTest (($script:events -join ',') -eq 'download,apps,companions,remove-office,identity,user,phase,install,install-companions') 'Deployment order changed.' }
}
$script:mode='disk-space'; $script:events=@(); $options.ValidateBeforeChanges=$true
Assert-OfficeThrows { Invoke-OfficeDeployment $options } 'Insufficient disk space after staging'
Assert-OfficeTest (($script:events -join ',') -eq 'download') 'Validation must not report success or mutate apps with insufficient installation space.'
$options.ValidateBeforeChanges=$false
$script:mode='cleanup-warning'; $script:events=@()
$result = @(Invoke-OfficeDeployment $options)
Assert-OfficeTest ($result.Count -eq 1 -and $result[0].ArtifactCleanupPending -and $result[0].ExitCode -eq 0 -and $result[0].OfficeDeploymentStatus -eq 'Installed') 'Artifact cleanup problems corrupted the receipt or failed a verified installation.'