feat(sprint7): §5.1 — Pester smoke tests (no real VM required)
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>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#Requires -Version 5.1
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Pester v5 tests for scripts/New-BuildVM.ps1
|
||||
#>
|
||||
|
||||
BeforeAll {
|
||||
$script:ScriptPath = Join-Path $PSScriptRoot '..\scripts\New-BuildVM.ps1'
|
||||
$script:CloneBaseDir = Join-Path $env:TEMP "CI-Tests-CloneBase-$PID"
|
||||
New-Item -ItemType Directory -Path $script:CloneBaseDir -Force | Out-Null
|
||||
|
||||
# Fake vmrun.cmd — exits with %FAKE_VMRUN_EXIT% (default 0)
|
||||
# On success it also creates the destination VMX so the post-clone Test-Path passes.
|
||||
$script:FakeVmrun = Join-Path $env:TEMP "fake-vmrun-$PID.cmd"
|
||||
Set-Content $script:FakeVmrun -Value @'
|
||||
@echo off
|
||||
if "%FAKE_VMRUN_EXIT%"=="" ( set _exit=0 ) else ( set _exit=%FAKE_VMRUN_EXIT% )
|
||||
if "%_exit%"=="0" (
|
||||
REM Create the destination VMX file so the caller's Test-Path check passes.
|
||||
REM The destination path is the 4th positional arg (after -T ws clone <template>).
|
||||
echo. > %5
|
||||
)
|
||||
exit /b %_exit%
|
||||
'@
|
||||
|
||||
# Fake template VMX — just needs to exist
|
||||
$script:FakeTemplate = Join-Path $env:TEMP "fake-template-$PID.vmx"
|
||||
Set-Content $script:FakeTemplate -Value 'config.version = "8"'
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
Remove-Item $script:CloneBaseDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item $script:FakeVmrun -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item $script:FakeTemplate -Force -ErrorAction SilentlyContinue
|
||||
$env:FAKE_VMRUN_EXIT = $null
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
Describe 'New-BuildVM — parameter validation' {
|
||||
It 'throws when TemplatePath does not exist' {
|
||||
{
|
||||
& $script:ScriptPath `
|
||||
-TemplatePath 'C:\DoesNotExist\template.vmx' `
|
||||
-CloneBaseDir $script:CloneBaseDir `
|
||||
-JobId 'test-job-1' `
|
||||
-VmrunPath $script:FakeVmrun
|
||||
} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
Describe 'New-BuildVM — vmrun failure' {
|
||||
It 'throws and cleans up clone dir when vmrun clone exits non-zero' {
|
||||
$env:FAKE_VMRUN_EXIT = '1'
|
||||
{
|
||||
& $script:ScriptPath `
|
||||
-TemplatePath $script:FakeTemplate `
|
||||
-CloneBaseDir $script:CloneBaseDir `
|
||||
-JobId 'test-job-fail' `
|
||||
-VmrunPath $script:FakeVmrun
|
||||
} | Should -Throw -ExpectedMessage '*clone failed*'
|
||||
|
||||
# No leftover clone dir
|
||||
$leftover = Get-ChildItem $script:CloneBaseDir -Directory |
|
||||
Where-Object { $_.Name -like '*test-job-fail*' }
|
||||
$leftover | Should -BeNullOrEmpty
|
||||
}
|
||||
|
||||
AfterEach { $env:FAKE_VMRUN_EXIT = $null }
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
Describe 'New-BuildVM — clone name format' {
|
||||
It 'clone folder is named Clone_{JobId}_{yyyyMMdd_HHmmss}' {
|
||||
$env:FAKE_VMRUN_EXIT = '0'
|
||||
$vmx = & $script:ScriptPath `
|
||||
-TemplatePath $script:FakeTemplate `
|
||||
-CloneBaseDir $script:CloneBaseDir `
|
||||
-JobId 'run-42' `
|
||||
-VmrunPath $script:FakeVmrun
|
||||
|
||||
$vmx | Should -Match 'Clone_run-42_\d{8}_\d{6}'
|
||||
}
|
||||
|
||||
It 'returns a string ending in .vmx' {
|
||||
$env:FAKE_VMRUN_EXIT = '0'
|
||||
$vmx = & $script:ScriptPath `
|
||||
-TemplatePath $script:FakeTemplate `
|
||||
-CloneBaseDir $script:CloneBaseDir `
|
||||
-JobId 'run-99' `
|
||||
-VmrunPath $script:FakeVmrun
|
||||
|
||||
$vmx | Should -Match '\.vmx$'
|
||||
}
|
||||
|
||||
AfterEach {
|
||||
$env:FAKE_VMRUN_EXIT = $null
|
||||
# Clean up clones created by successful tests
|
||||
Get-ChildItem $script:CloneBaseDir -Directory -ErrorAction SilentlyContinue |
|
||||
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user