f46e4a8ca3
Four test files under tests/ using fake vmrun.cmd (exit code via
%FAKE_VMRUN_EXIT% env var) — no VMware or network dependency:
_Common.Tests.ps1 — New-CISessionOption TLS flags, Resolve-VmrunPath
throw/return, Invoke-Vmrun ExitCode/Output/ThrowOnError
New-BuildVM.Tests.ps1 — throw on missing template, throw+cleanup on clone
failure, Clone_{JobId}_{timestamp} name format
Remove-BuildVM.Tests.ps1 — no-op on missing VMX, partial dir cleanup,
-WhatIf preservation, full destroy removes dir
Wait-VMReady.Tests.ps1 — throw on missing vmrun, timeout throw,
IP ValidatePattern accept/reject
Run: Invoke-Pester tests\ -Output Detailed
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
69 lines
3.0 KiB
PowerShell
69 lines
3.0 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Pester v5 tests for scripts/Remove-BuildVM.ps1
|
|
#>
|
|
|
|
BeforeAll {
|
|
$script:ScriptPath = Join-Path $PSScriptRoot '..\scripts\Remove-BuildVM.ps1'
|
|
|
|
# Fake vmrun: accepts any args, always exits 0 (best-effort ops don't need to fail here)
|
|
$script:FakeVmrun = Join-Path $env:TEMP "fake-vmrun-remove-$PID.cmd"
|
|
Set-Content $script:FakeVmrun -Value '@echo off & exit /b 0'
|
|
}
|
|
|
|
AfterAll {
|
|
Remove-Item $script:FakeVmrun -Force -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
Describe 'Remove-BuildVM — missing VMX' {
|
|
It 'returns without error when VMX does not exist' {
|
|
{ & $script:ScriptPath -VMPath 'C:\DoesNotExist\clone.vmx' -VmrunPath $script:FakeVmrun } |
|
|
Should -Not -Throw
|
|
}
|
|
|
|
It 'removes partial clone directory even when VMX is missing' {
|
|
$partialDir = Join-Path $env:TEMP "partial-clone-$PID"
|
|
New-Item -ItemType Directory -Path $partialDir -Force | Out-Null
|
|
$fakeMissingVmx = Join-Path $partialDir 'missing.vmx'
|
|
|
|
& $script:ScriptPath -VMPath $fakeMissingVmx -VmrunPath $script:FakeVmrun
|
|
|
|
Test-Path $partialDir | Should -Be $false
|
|
}
|
|
}
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
Describe 'Remove-BuildVM — WhatIf' {
|
|
It 'does not remove clone directory when -WhatIf is passed' {
|
|
$cloneDir = Join-Path $env:TEMP "whatif-clone-$PID"
|
|
$cloneVmx = Join-Path $cloneDir 'whatif-clone.vmx'
|
|
New-Item -ItemType Directory -Path $cloneDir -Force | Out-Null
|
|
Set-Content $cloneVmx -Value 'config.version = "8"'
|
|
|
|
& $script:ScriptPath -VMPath $cloneVmx -VmrunPath $script:FakeVmrun -WhatIf
|
|
|
|
Test-Path $cloneDir | Should -Be $true
|
|
|
|
Remove-Item $cloneDir -Recurse -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
Describe 'Remove-BuildVM — successful destroy' {
|
|
It 'removes the clone directory when VMX exists and vmrun succeeds' {
|
|
$cloneDir = Join-Path $env:TEMP "live-clone-$PID"
|
|
$cloneVmx = Join-Path $cloneDir 'live-clone.vmx'
|
|
New-Item -ItemType Directory -Path $cloneDir -Force | Out-Null
|
|
Set-Content $cloneVmx -Value 'config.version = "8"'
|
|
|
|
& $script:ScriptPath `
|
|
-VMPath $cloneVmx `
|
|
-VmrunPath $script:FakeVmrun `
|
|
-GracefulTimeoutSeconds 1 # short timeout for test speed
|
|
|
|
Test-Path $cloneDir | Should -Be $false
|
|
}
|
|
}
|