107 lines
4.5 KiB
PowerShell
107 lines
4.5 KiB
PowerShell
#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.ps1 — exits with $env:FAKE_VMRUN_EXIT (default 0)
|
|
# On success it also creates the destination VMX so the post-clone Test-Path passes.
|
|
# Using .ps1 instead of .cmd avoids Windows cmd redirect limitations inside if blocks.
|
|
$script:FakeVmrun = Join-Path $env:TEMP "fake-vmrun-$PID.ps1"
|
|
Set-Content $script:FakeVmrun -Value @'
|
|
# Args: -T ws clone <template> <dst_vmx> linked -snapshot <snapshot>
|
|
$exitCode = if ($env:FAKE_VMRUN_EXIT) { [int]$env:FAKE_VMRUN_EXIT } else { 0 }
|
|
if ($exitCode -eq 0) {
|
|
# Create the destination VMX (5th positional arg) so Test-Path in caller passes.
|
|
# Real vmrun creates the clone dir+file; we must do the same here.
|
|
$dstVmx = $args[4] # -T ws clone <tmpl> <dst> ...
|
|
$dstDir = Split-Path $dstVmx -Parent
|
|
if (-not (Test-Path $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
Set-Content $dstVmx ''
|
|
}
|
|
exit $exitCode
|
|
'@
|
|
|
|
# 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
|
|
}
|
|
}
|