more fixes for scrubber
This commit is contained in:
@@ -918,6 +918,85 @@ try {
|
||||
$log=Get-Content -LiteralPath $Global:LogFile -Raw
|
||||
Assert-OfficeTest ($log.Contains('[truncated; see full stream log]') -and $log.Contains('Get Help stderr: no text captured.')) 'Oversized lines and empty streams must be explicit in the management log.'
|
||||
}
|
||||
# Restore runtime syntax detection instead of trusting stale published switches.
|
||||
& {
|
||||
$usage='GetHelpCmd.exe -S OfficeScrubScenario -AcceptEula'
|
||||
Assert-OfficeTest ((Get-OfficeScrubArguments 0 $usage) -eq '-S OfficeScrubScenario -AcceptEula') 'Current tool usage must omit unsupported OfficeVersion.'
|
||||
Assert-OfficeTest ((Get-OfficeScrubArguments 0 ($usage + "`n -OfficeVersion <version>")) -eq '-S OfficeScrubScenario -AcceptEula -OfficeVersion All') 'A tool advertising OfficeVersion must request all versions.'
|
||||
Assert-OfficeThrows { Get-OfficeScrubArguments 1 $usage } 'Could not validate'
|
||||
Assert-OfficeThrows { Get-OfficeScrubArguments 0 ($usage + "`nError: Invalid command line arguments") } 'Could not validate'
|
||||
Assert-OfficeThrows { Get-OfficeScrubArguments 0 'No OfficeScrubScenario example' } 'Could not validate'
|
||||
}
|
||||
& {
|
||||
function Get-CimInstance {
|
||||
param($ClassName,$Filter)
|
||||
Assert-OfficeTest ($ClassName -eq 'Win32_Process' -and $Filter.Contains("Name='powershell.exe'")) 'Worker monitoring must inspect detached script hosts.'
|
||||
[pscustomobject]@{Name='powershell.exe';ProcessId=$PID;CommandLine='self'}
|
||||
[pscustomobject]@{Name='powershell.exe';ProcessId=9701;CommandLine='powershell.exe -File C:\Temp\GetHelpElevated\abc\Run.ps1'}
|
||||
[pscustomobject]@{Name='msiexec.exe';ProcessId=9702;CommandLine='msiexec.exe /V'}
|
||||
[pscustomobject]@{Name='OfficeClickToRun.exe';ProcessId=9703;CommandLine='OfficeClickToRun.exe /service'}
|
||||
[pscustomobject]@{Name='OfficeClickToRun.exe';ProcessId=9704;CommandLine='OfficeClickToRun.exe /user'}
|
||||
[pscustomobject]@{Name='msiexec.exe';ProcessId=9705;CommandLine='msiexec.exe /x {fixture}'}
|
||||
}
|
||||
$workers=@(Get-OfficeScrubWorkers)
|
||||
Assert-OfficeTest ($workers.Count -eq 2 -and 9701 -in $workers.ProcessId -and 9705 -in $workers.ProcessId) 'Detached cleanup and active MSI workers matter; the caller and idle service hosts do not.'
|
||||
}
|
||||
# Verify actual process-handle retention using only an inert child, never a scrubber.
|
||||
& {
|
||||
$encoded=[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes('Start-Sleep -Milliseconds 1500; exit 7'))
|
||||
$probe=Start-Process -FilePath (Join-Path $PSHOME 'powershell.exe') -ArgumentList "-NoProfile -NonInteractive -EncodedCommand $encoded" -WindowStyle Hidden -PassThru
|
||||
$held=$null
|
||||
try {
|
||||
$identity=Get-CimInstance Win32_Process -Filter "ProcessId=$($probe.Id)" -ErrorAction Stop
|
||||
$held=Open-OfficeScrubWorkerHandle $identity
|
||||
Assert-OfficeTest ($held.WaitForExit(10000) -and $held.ExitCode -eq 7) 'A held handle must preserve a failed worker exit after its PID disappears.'
|
||||
Assert-OfficeThrows { Open-OfficeScrubWorkerHandle ([pscustomobject]@{ProcessId=$PID;CreationDate=[datetime]'2000-01-01'}) } 'Process identity changed'
|
||||
} finally { if ($held) { $held.Dispose() }; $probe.Dispose() }
|
||||
}
|
||||
# A background launch is pending. Workers must finish successfully and removal
|
||||
# state must stay clean for the entire quiet interval; closing early is not success.
|
||||
& {
|
||||
$existing=[pscustomobject]@{Name='powershell.exe';ProcessId=8001;CreationDate=[datetime]'2026-10-29T12:00:00';CommandLine='powershell.exe -NoProfile'}
|
||||
$fixtureScrubWorker=[pscustomobject]@{Name='powershell.exe';ProcessId=9701;CreationDate=[datetime]'2026-10-29T16:00:00';CommandLine='powershell.exe -File C:\Temp\GetHelpElevated\abc\Run.ps1'}
|
||||
function Get-OfficeScrubWorkers {
|
||||
$script:waitPoll++
|
||||
if ($script:waitMode -eq 'process-error') { throw 'Process inventory unreadable' }
|
||||
$existing
|
||||
if ($script:waitMode -ne 'no-worker' -and ($script:waitPoll -le 2 -or $script:waitMode -eq 'keypress')) { $fixtureScrubWorker }
|
||||
if ($script:waitMode -eq 'quiet-reset' -and $script:waitPoll -eq 5) { [pscustomobject]@{Name='setup.exe';ProcessId=9702;CreationDate=$fixtureScrubWorker.CreationDate;CommandLine='setup.exe'} }
|
||||
}
|
||||
function Open-OfficeScrubWorkerHandle {
|
||||
param($Worker)
|
||||
$script:openCalls++
|
||||
if ($script:waitMode -eq 'lost-handle') { throw 'Could not monitor scrub worker PID 9701' }
|
||||
$handle=[pscustomobject]@{ExitCode=$(if ($script:waitMode -eq 'closed') {-1073741510} elseif ($script:waitMode -eq 'failed') {1} elseif ($script:waitMode -eq 'restart') {3010} else {0})}
|
||||
$handle | Add-Member ScriptProperty HasExited { $script:waitPoll -gt 2 -and $script:waitMode -ne 'keypress' }
|
||||
$handle | Add-Member ScriptMethod Dispose { $script:disposed++ }
|
||||
$handle
|
||||
}
|
||||
function Get-OfficeSuiteInventory {
|
||||
if ($script:waitMode -eq 'inventory-error') { throw 'Office inventory unreadable' }
|
||||
if ($script:waitMode -eq 'remaining' -or ($script:waitMode -eq 'delayed' -and $script:waitPoll -le 4)) { [pscustomobject]@{DisplayName='Old Office'} }
|
||||
}
|
||||
function Get-OfficeClickToRunState {
|
||||
if ($script:waitMode -eq 'state-error') { throw 'Servicing state unreadable' }
|
||||
[pscustomobject]@{SourceReferences=@($(if ($script:waitMode -eq 'stale') {'old-download'}));WorkingConfiguration=@()}
|
||||
}
|
||||
foreach ($script:waitMode in @('success','restart','delayed','quiet-reset','closed','failed','remaining','stale','no-worker','keypress','process-error','inventory-error','state-error','lost-handle')) {
|
||||
$script:waitPoll=0; $script:disposed=0; $script:openCalls=0; $script:testSleeps=@()
|
||||
if ($script:waitMode -in @('success','restart','delayed','quiet-reset')) {
|
||||
$result=@(Wait-OfficeScrubRemoval @($existing))
|
||||
$expectedPolls=switch ($script:waitMode) { 'delayed' {11}; 'quiet-reset' {12}; default {9} }
|
||||
Assert-OfficeTest ($result.Count -eq 0 -and $script:waitPoll -eq $expectedPolls) 'Background verification must wait for workers, delayed removal, and a continuous 60-second quiet interval without leaking output.'
|
||||
} else {
|
||||
$pattern=switch ($script:waitMode) { 'closed' {'exited unsuccessfully.*-1073741510'}; 'failed' {'exited unsuccessfully.*exit 1'}; 'process-error' {'Process inventory unreadable'}; 'inventory-error' {'Office inventory unreadable'}; 'state-error' {'Servicing state unreadable'}; 'lost-handle' {'Could not monitor scrub worker'}; default {'background removal did not finish verification'} }
|
||||
Assert-OfficeThrows { Wait-OfficeScrubRemoval @($existing) } $pattern
|
||||
if ($script:waitMode -in @('remaining','stale','no-worker','keypress')) { Assert-OfficeTest (($script:testSleeps | Measure-Object -Sum).Sum -eq 1800) 'Unfinished background removal must stop after the restored 30-minute wait.' }
|
||||
}
|
||||
Assert-OfficeTest ($script:disposed -eq $(if ($script:waitMode -in @('process-error','no-worker','lost-handle')) {0} else {1})) 'Every acquired worker handle must be released on success and failure.'
|
||||
Assert-OfficeTest ($script:openCalls -le 1) 'Polling must retain the same worker handle rather than reopening a possibly reused PID.'
|
||||
}
|
||||
}
|
||||
# A scrub requires both a successful scenario/process result and independently
|
||||
# verified removal. Every attempt is bounded to one and cleanup retains its logs.
|
||||
& {
|
||||
@@ -930,13 +1009,26 @@ try {
|
||||
}
|
||||
function Invoke-OfficeProcess {
|
||||
param($FilePath,$Arguments,$LogPrefix)
|
||||
if ($Arguments -eq '-?') {
|
||||
$script:usageCalls++
|
||||
Set-Content -LiteralPath "$LogPrefix.stdout.log" -Value $(if ($script:scrubMode -eq 'bad-help') {'Usage unavailable'} else {'GetHelpCmd.exe -S OfficeScrubScenario -AcceptEula'})
|
||||
Set-Content -LiteralPath "$LogPrefix.stderr.log" -Value ''
|
||||
return 0
|
||||
}
|
||||
$script:scrubLaunches++
|
||||
Assert-OfficeTest ($Arguments -eq '-S OfficeScrubScenario -AcceptEula' -and (Split-Path -Leaf $LogPrefix) -eq 'office-scrub') 'Scrub must use the current tool-supported arguments, omitting the rejected OfficeVersion option.'
|
||||
Set-Content -LiteralPath "$LogPrefix.stdout.log" -Value $script:scrubOutput
|
||||
Set-Content -LiteralPath "$LogPrefix.stderr.log" -Value ''
|
||||
$script:scrubExit
|
||||
}
|
||||
function Get-OfficeScrubWorkers { if ($script:scrubMode -eq 'preexisting-worker') { [pscustomobject]@{Name='powershell.exe';ProcessId=8000;CommandLine='powershell.exe -File C:\Temp\GetHelpElevated\old\Run.ps1'} } }
|
||||
function Wait-OfficeScrubRemoval {
|
||||
param($ExistingWorkers)
|
||||
$script:backgroundWaits++
|
||||
if ($script:scrubMode -eq 'background-no-worker') { throw 'Get Help background removal did not finish verification within the allowed time.' }
|
||||
}
|
||||
function Get-CimInstance {
|
||||
if ($script:scrubMode -eq 'preexisting-worker') { Get-OfficeScrubWorkers }
|
||||
if ($script:scrubMode -eq 'running') { [pscustomobject]@{Name='GetHelpCmd.exe'} }
|
||||
if ($script:scrubMode -eq 'background') { [pscustomobject]@{Name='powershell.exe';ProcessId=7064;CommandLine='powershell.exe -File C:\Windows\SystemTemp\GetHelpElevated\d509ee113e5346fa82eded7e01f47f69\Run.ps1'} }
|
||||
}
|
||||
@@ -945,9 +1037,9 @@ try {
|
||||
if ($script:scrubMode -eq 'inventory-error') { throw 'Registry unreadable' }
|
||||
}
|
||||
function Assert-OfficeFreshInstallState { if ($script:scrubMode -eq 'stale') { throw 'Previous Click-to-Run state remains' } }
|
||||
foreach ($script:scrubMode in @('success','restart','download','failure','not-found-66','not-found-68','empty','invalid-arguments','usage','background','background-no-worker','background-with-00','generic-success','contradictory','bad-exit','running','remaining','inventory-error','stale')) {
|
||||
foreach ($script:scrubMode in @('success','restart','download','bad-help','preexisting-worker','failure','not-found-66','not-found-68','empty','invalid-arguments','usage','background','background-no-worker','background-with-00','generic-success','text-error','background-bad-exit','background-error','contradictory','bad-exit','running','remaining','inventory-error','stale')) {
|
||||
$workspace=Join-Path $testRoot ('scrub-result-' + $script:scrubMode); $null=[IO.Directory]::CreateDirectory($workspace)
|
||||
$media=[pscustomobject]@{}; $script:scrubExit=0; $script:scrubOutput='00: Successfully completed this scenario.'
|
||||
$media=[pscustomobject]@{}; $script:scrubExit=0; $script:scrubOutput='00: Successfully completed this scenario.'; $script:backgroundWaits=0; $script:usageCalls=0
|
||||
switch ($script:scrubMode) {
|
||||
'restart' { $script:scrubExit=3010 }
|
||||
'failure' { $script:scrubOutput='09: Failure to remove Office.' }
|
||||
@@ -958,24 +1050,29 @@ try {
|
||||
'usage' { $script:scrubOutput="Usage: GetHelpCmd.exe [options]`n00: Success" }
|
||||
{ $_ -like 'background*' } { $script:scrubOutput="Starting Microsoft GetHelp Command Line Tool`nDiagnostic action completed.`nScenario completed successfully.`nExecutionResultDetails: Uninstall office is running in the background."; if ($_ -eq 'background-with-00') { $script:scrubOutput += "`n00: Success" } }
|
||||
'generic-success' { $script:scrubOutput='Scenario completed successfully.' }
|
||||
'text-error' { $script:scrubOutput="Error: Removal failed`nScenario completed successfully." }
|
||||
'background-error' { $script:scrubOutput += "`nError: Removal failed" }
|
||||
'background-bad-exit' { $script:scrubExit=1 }
|
||||
'contradictory' { $script:scrubOutput="09: Failure`n00: Success" }
|
||||
'bad-exit' { $script:scrubExit=1 }
|
||||
}
|
||||
if ($script:scrubMode -in @('success','restart')) {
|
||||
if ($script:scrubMode -in @('success','restart','generic-success','background-with-00')) {
|
||||
$result=@(Invoke-OfficeScrubRecovery $media $workspace 'fixture corruption')
|
||||
Assert-OfficeTest ($result.Count -eq 1 -and $result[0] -eq 3010 -and $media.ScrubAttempted -and $media.ScrubReason -eq 'fixture corruption') 'Verified scrub must surface its use and restart requirement without leaking log messages.'
|
||||
} else {
|
||||
$pattern=switch ($script:scrubMode) { 'download' {'Scrub download failed'}; 'invalid-arguments' {'rejected its command line.*process 0'}; 'usage' {'rejected its command line.*process 0'}; { $_ -like 'background*' } {'background removal requires attended verification.*launcher exit 0'}; 'running' {'processes are still running'}; 'remaining' {'Office remains.*Leftover MSI Office'}; 'inventory-error' {'Registry unreadable'}; 'stale' {'Click-to-Run state remains'}; default {'failed or returned an unverified result'} }
|
||||
$pattern=switch ($script:scrubMode) { 'download' {'Scrub download failed'}; 'bad-help' {'Could not validate.*Scrub was not launched'}; 'preexisting-worker' {'Cannot launch scrub.*worker is running'}; 'invalid-arguments' {'rejected its command line.*process 0'}; 'usage' {'rejected its command line.*process 0'}; 'background-no-worker' {'background removal did not finish verification'}; 'background' {'processes are still running'}; 'running' {'processes are still running'}; 'remaining' {'Office remains.*Leftover MSI Office'}; 'inventory-error' {'Registry unreadable'}; 'stale' {'Click-to-Run state remains'}; default {'failed or returned an unverified result'} }
|
||||
Assert-OfficeThrows { Invoke-OfficeScrubRecovery $media $workspace 'fixture corruption' } $pattern
|
||||
}
|
||||
Assert-OfficeTest ($script:usageCalls -eq $(if ($script:scrubMode -eq 'download') {0} else {1})) 'Probe the fresh scrubber usage exactly once, only after recovery was selected.'
|
||||
Assert-OfficeTest ($script:backgroundWaits -eq $(if ($script:scrubMode -in @('background','background-no-worker','background-with-00')) {1} else {0})) 'Only an accepted background launch may enter the completion wait.'
|
||||
$before=$script:scrubLaunches
|
||||
Assert-OfficeThrows { Invoke-OfficeScrubRecovery $media $workspace 'second attempt' } 'already attempted'
|
||||
Assert-OfficeTest ($script:scrubLaunches -eq $before) 'Failure must not create a scrub loop.'
|
||||
$warnings=Complete-OfficeDeploymentFiles $workspace
|
||||
if ($script:scrubMode -in @('running','background')) { Assert-OfficeTest ($warnings.Count -gt 0 -and (Test-Path -LiteralPath (Join-Path $workspace 'GetHelp.zip'))) 'Surviving scrub workers must retain in-use media.' }
|
||||
if ($script:scrubMode -in @('running','background','preexisting-worker')) { Assert-OfficeTest ($warnings.Count -gt 0 -and (Test-Path -LiteralPath (Join-Path $workspace 'GetHelp.zip'))) 'Surviving scrub workers must retain in-use media.' }
|
||||
else {
|
||||
Assert-OfficeTest (!$warnings.Count -and !(Test-Path -LiteralPath (Join-Path $workspace 'GetHelp.zip'))) 'Scrub download must be removed on success and failure.'
|
||||
if ($script:scrubMode -ne 'download') { Assert-OfficeTest (Test-Path -LiteralPath (Join-Path $workspace 'office-scrub.stdout.log')) 'Scrub result logs must survive cleanup.' }
|
||||
if ($script:scrubMode -notin @('download','bad-help')) { Assert-OfficeTest (Test-Path -LiteralPath (Join-Path $workspace 'office-scrub.stdout.log')) 'Scrub result logs must survive cleanup.' }
|
||||
}
|
||||
}
|
||||
Assert-OfficeTest (Test-OfficeStagingInUse $testRoot @([pscustomobject]@{Name='cscript.exe';CommandLine='cscript.exe C:\Temp\OffScrubC2R.vbs ALL'})) 'Detached Office scrub scripts must block a new deployment and media cleanup.'
|
||||
@@ -998,7 +1095,9 @@ try {
|
||||
$quoted=[pscustomobject]@{Name='powershell.exe';CommandLine='powershell.exe -File "C:\Temp Folder\GETHELPELEVATED\abc\run.ps1" "C:\Temp Folder\GETHELPELEVATED\abc"'}
|
||||
Assert-OfficeTest (Test-OfficeStagingInUse $workspace @($quoted)) 'Quoted Get Help worker paths must be recognized case-insensitively.'
|
||||
$ordinary=@([pscustomobject]@{Name='powershell.exe';CommandLine='powershell.exe -File C:\Tools\Run.ps1'},[pscustomobject]@{Name='OfficeClickToRun.exe';CommandLine='OfficeClickToRun.exe /service'},[pscustomobject]@{Name='OfficeClickToRun.exe';CommandLine='OfficeClickToRun.exe /user'})
|
||||
Assert-OfficeTest (!(Test-OfficeStagingInUse $workspace $ordinary)) 'Unrelated PowerShell and idle Click-to-Run hosts must not be mistaken for scrub workers.'
|
||||
$ordinary += @([pscustomobject]@{Name='msiexec.exe';CommandLine='msiexec.exe /V'},[pscustomobject]@{Name='msiexec.exe';CommandLine='msiexec.exe /Embedding fixture'})
|
||||
Assert-OfficeTest (!(Test-OfficeStagingInUse $workspace $ordinary)) 'Unrelated PowerShell and idle Click-to-Run/MSI hosts must not block completed cleanup.'
|
||||
Assert-OfficeTest (Test-OfficeStagingInUse $workspace @([pscustomobject]@{Name='msiexec.exe';CommandLine='msiexec.exe /x {fixture} /quiet'})) 'Active MSI uninstall clients must still protect the workspace.'
|
||||
}
|
||||
# Deployment resolves release metadata and deletes it on success or failure.
|
||||
& {
|
||||
|
||||
Reference in New Issue
Block a user