#Requires -Version 5.1 <# .SYNOPSIS Pester v5 tests for scripts/Wait-VMReady.ps1 #> BeforeAll { $script:ScriptPath = Join-Path $PSScriptRoot '..\scripts\Wait-VMReady.ps1' # Fake vmrun: getGuestIPAddress exits with %FAKE_VMRUN_EXIT% (default 1 = not ready) $script:FakeVmrun = Join-Path $env:TEMP "fake-vmrun-wait-$PID.cmd" Set-Content $script:FakeVmrun -Value @' @echo off if "%FAKE_VMRUN_EXIT%"=="" ( exit /b 1 ) else ( exit /b %FAKE_VMRUN_EXIT% ) '@ # Fake VMX file — just needs to exist $script:FakeVmx = Join-Path $env:TEMP "fake-wait-$PID.vmx" Set-Content $script:FakeVmx -Value 'config.version = "8"' } AfterAll { Remove-Item $script:FakeVmrun -Force -ErrorAction SilentlyContinue Remove-Item $script:FakeVmx -Force -ErrorAction SilentlyContinue $env:FAKE_VMRUN_EXIT = $null } # ───────────────────────────────────────────────────────────────────────────── Describe 'Wait-VMReady — missing vmrun' { It 'throws when VmrunPath does not exist' { { & $script:ScriptPath ` -VMPath $script:FakeVmx ` -IPAddress '192.168.79.101' ` -VmrunPath 'C:\DoesNotExist\vmrun.exe' } | Should -Throw } } # ───────────────────────────────────────────────────────────────────────────── Describe 'Wait-VMReady — timeout' { It 'throws with timeout message when VM never becomes ready' { $env:FAKE_VMRUN_EXIT = '1' # vmrun getGuestIPAddress always fails → VM not running { & $script:ScriptPath ` -VMPath $script:FakeVmx ` -IPAddress '192.168.79.101' ` -VmrunPath $script:FakeVmrun ` -TimeoutSeconds 5 ` -PollIntervalSeconds 2 ` -SkipPing } | Should -Throw -ExpectedMessage '*Timed out*' } AfterEach { $env:FAKE_VMRUN_EXIT = $null } } # ───────────────────────────────────────────────────────────────────────────── Describe 'Wait-VMReady — IP validation' { It 'rejects an invalid IP address' { { & $script:ScriptPath ` -VMPath $script:FakeVmx ` -IPAddress '999.999.999.999' ` -VmrunPath $script:FakeVmrun } | Should -Throw } It 'accepts a valid IP address without immediate error from parameter binding' { # This will still time out (vmrun fake exits 1), but the IP itself must not # cause a parameter validation error — that would throw before the timeout. $env:FAKE_VMRUN_EXIT = '1' $err = $null try { & $script:ScriptPath ` -VMPath $script:FakeVmx ` -IPAddress '192.168.79.101' ` -VmrunPath $script:FakeVmrun ` -TimeoutSeconds 3 ` -PollIntervalSeconds 2 ` -SkipPing ` -ErrorAction Stop } catch { $err = $_ } # Error must be the timeout, not a parameter validation error "$err" | Should -Match 'Timed out' "$err" | Should -Not -Match 'Cannot validate' $env:FAKE_VMRUN_EXIT = $null } }