| 1 | #requires -Version 5.1 |
| 2 | $ErrorActionPreference = 'Stop' |
| 3 | $parseErrors = $null |
| 4 | $ast = [Management.Automation.Language.Parser]::ParseFile((Join-Path $PSScriptRoot 'windows-host-enrollment.ps1'), [ref]$null, [ref]$parseErrors) |
| 5 | if ($parseErrors.Count) { throw $parseErrors[0] } |
| 6 | $wait = $ast.Find({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Wait-CageHostEnrollment' }, $true) |
| 7 | . ([scriptblock]::Create($wait.Extent.Text)) |
| 8 | $script:requests = 0 |
| 9 | $worker = [pscustomobject]@{ HasExited = $false; ExitCode = 1 } |
| 10 | function Invoke-CageHostEnrollment { |
| 11 | param($Context, $Request, $Secret) |
| 12 | $script:requests++ |
| 13 | if ($script:failRequest) { $worker.HasExited = $true; throw 'Injected pipe failure' } |
| 14 | if ($Request.action -cne 'ready') { throw 'Unexpected enrollment request' } |
| 15 | } |
| 16 | Wait-CageHostEnrollment @{} $worker |
| 17 | if ($script:requests -ne 1) { throw 'The ready handshake was not performed.' } |
| 18 | $script:failRequest = $true |
| 19 | $watch = [Diagnostics.Stopwatch]::StartNew() |
| 20 | try { Wait-CageHostEnrollment @{} $worker; throw 'Worker failure was ignored.' } |
| 21 | catch { |
| 22 | if ($_.Exception.Message -notlike '*enrollment_worker_exited*') { throw } |
| 23 | } |
| 24 | if ($script:requests -ne 2 -or $watch.Elapsed.TotalSeconds -gt 2) { throw 'An exited worker was retried until the ready timeout.' } |
| 25 | Write-Output 'Passed ready handshake and early worker-exit reporting.' |