diff --git a/tests/install/e2e-assets/windows-stale-processes.ps1 b/tests/install/e2e-assets/windows-stale-processes.ps1 index 50cb5287ba..94a0b84d90 100644 --- a/tests/install/e2e-assets/windows-stale-processes.ps1 +++ b/tests/install/e2e-assets/windows-stale-processes.ps1 @@ -2,6 +2,7 @@ # When any are seen initially, poll up to -SettleSeconds for post-swap teardown while # recording full process identity (ProcessId, ParentProcessId, Name, CreationDate, # ExecutablePath, CommandLine) to both stdout and proof/stale-processes.json. +# A query that fails while waiting still saves what the first one saw, then fails the check. param( [Parameter(Mandatory = $true)][string]$OldInstallLocation, [Parameter(Mandatory = $true)][string]$ProofDir, @@ -30,11 +31,17 @@ Write-Host " [stale] $($initial.Count) old payload process(es) alive under $($O foreach ($record in $initial) { Write-Host " $(Format-ProcessLine $record)" } $remaining = $initial +$queryError = $null if ($SettleSeconds -gt 0) { $deadline = (Get-Date).AddSeconds($SettleSeconds) - while ($remaining.Count -gt 0 -and (Get-Date) -lt $deadline) { - if ($PollMilliseconds -gt 0) { Start-Sleep -Milliseconds $PollMilliseconds } - $remaining = @(Find-StaleProcesses) + try { + while ($remaining.Count -gt 0 -and (Get-Date) -lt $deadline) { + if ($PollMilliseconds -gt 0) { Start-Sleep -Milliseconds $PollMilliseconds } + $remaining = @(Find-StaleProcesses) + } + } catch { + # Keep the last records read: a process that has exited meanwhile cannot be read again. + $queryError = $_.Exception.Message } } @@ -47,6 +54,7 @@ $report = [ordered]@{ checkedAt = $checkedAt.ToString('o') settleSeconds = $SettleSeconds settled = $settled + queryError = $queryError initial = [object[]]@($initial) remaining = [object[]]@($remaining) } @@ -57,6 +65,7 @@ if ($settled) { Write-Host " [stale] old payload processes exited within ${elapsed}s (saved stale-processes.json)" return } +if ($queryError) { throw "Could not re-read old payload processes while waiting for teardown (saved stale-processes.json): $queryError" } Write-Host " [stale] $($remaining.Count) old payload process(es) still alive after ${SettleSeconds}s (saved stale-processes.json):" foreach ($record in $remaining) { Write-Host " $(Format-ProcessLine $record)" } diff --git a/tests/scripts/install/test_windows_stale_processes.py b/tests/scripts/install/test_windows_stale_processes.py index 376ff4d26a..ba270a7273 100644 --- a/tests/scripts/install/test_windows_stale_processes.py +++ b/tests/scripts/install/test_windows_stale_processes.py @@ -103,3 +103,26 @@ function Get-CimInstance {{ [CmdletBinding()] param([string]$ClassName) assert [p["ProcessId"] for p in report["initial"]] == [9001] assert [p["ProcessId"] for p in report["remaining"]] == [9001] assert report["remaining"][0]["ExecutablePath"].endswith(r"\python.exe") + + +def test_query_error_while_settling_still_saves_the_sighting_and_fails(tmp_path): + fake_cim = rf""" +$global:stale_calls = 0 +function Get-CimInstance {{ [CmdletBinding()] param([string]$ClassName) + $global:stale_calls++ + if ($global:stale_calls -gt 1) {{ throw 'WMI query failed' }} + @([pscustomobject]@{{ ProcessId = 5150; ParentProcessId = 1044; Name = 'python.exe'; CreationDate = $at; ExecutablePath = '{OLD_LOC}\app\resources\agent-payload\tools\python\python.exe'; CommandLine = 'python.exe -m hermes_cli.main gateway run' }}) +}} +""" + result, proof = _run_checker(tmp_path, fake_cim, settle_seconds=3, poll_ms=25) + + combined = result.stdout + result.stderr + assert result.returncode != 0 + assert "WMI query failed" in combined + assert (proof / "stale-processes.json").exists(), combined + report = json.loads((proof / "stale-processes.json").read_text(encoding="utf-8-sig")) + assert report["settled"] is False + assert report["queryError"] == "WMI query failed" + assert [p["ProcessId"] for p in report["initial"]] == [5150] + assert report["initial"][0]["CommandLine"] == "python.exe -m hermes_cli.main gateway run" + assert [p["ProcessId"] for p in report["remaining"]] == [5150]