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>
91 lines
3.3 KiB
PowerShell
91 lines
3.3 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Pester v5 tests for scripts/_Common.psm1
|
|
#>
|
|
|
|
BeforeAll {
|
|
Import-Module (Join-Path $PSScriptRoot '..\scripts\_Common.psm1') -Force
|
|
|
|
# Fake vmrun: accepts any args, exits with %FAKE_VMRUN_EXIT% (default 0)
|
|
$script:FakeVmrun = Join-Path $env:TEMP "fake-vmrun-$PID.cmd"
|
|
Set-Content $script:FakeVmrun -Value @'
|
|
@echo off
|
|
echo fake-vmrun-output
|
|
if "%FAKE_VMRUN_EXIT%"=="" ( exit /b 0 ) else ( exit /b %FAKE_VMRUN_EXIT% )
|
|
'@
|
|
}
|
|
|
|
AfterAll {
|
|
Remove-Item $script:FakeVmrun -Force -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
Describe 'New-CISessionOption' {
|
|
It 'returns a PSSessionOption' {
|
|
$opt = New-CISessionOption
|
|
$opt | Should -BeOfType [System.Management.Automation.Remoting.PSSessionOption]
|
|
}
|
|
|
|
It 'has SkipCACheck = true' {
|
|
(New-CISessionOption).SkipCACheck | Should -Be $true
|
|
}
|
|
|
|
It 'has SkipCNCheck = true' {
|
|
(New-CISessionOption).SkipCNCheck | Should -Be $true
|
|
}
|
|
|
|
It 'has SkipRevocationCheck = true' {
|
|
(New-CISessionOption).SkipRevocationCheck | Should -Be $true
|
|
}
|
|
}
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
Describe 'Resolve-VmrunPath' {
|
|
It 'returns the path when the file exists' {
|
|
$tmp = [System.IO.Path]::GetTempFileName()
|
|
try {
|
|
Resolve-VmrunPath -VmrunPath $tmp | Should -Be $tmp
|
|
} finally {
|
|
Remove-Item $tmp -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
It 'throws with a descriptive message when file is missing' {
|
|
{ Resolve-VmrunPath -VmrunPath 'C:\DoesNotExist\vmrun.exe' } |
|
|
Should -Throw -ExpectedMessage '*vmrun.exe not found*'
|
|
}
|
|
}
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
Describe 'Invoke-Vmrun' {
|
|
It 'returns ExitCode 0 and Output when command succeeds' {
|
|
$env:FAKE_VMRUN_EXIT = '0'
|
|
$r = Invoke-Vmrun -VmrunPath $script:FakeVmrun -Operation 'list'
|
|
$r.ExitCode | Should -Be 0
|
|
"$($r.Output)" | Should -Match 'fake-vmrun-output'
|
|
}
|
|
|
|
It 'returns non-zero ExitCode without throwing by default' {
|
|
$env:FAKE_VMRUN_EXIT = '1'
|
|
$r = Invoke-Vmrun -VmrunPath $script:FakeVmrun -Operation 'clone'
|
|
$r.ExitCode | Should -Be 1
|
|
}
|
|
|
|
It 'throws when -ThrowOnError and ExitCode is non-zero' {
|
|
$env:FAKE_VMRUN_EXIT = '1'
|
|
{ Invoke-Vmrun -VmrunPath $script:FakeVmrun -Operation 'clone' -ThrowOnError } |
|
|
Should -Throw -ExpectedMessage '*clone failed*'
|
|
}
|
|
|
|
It 'does not throw when -ThrowOnError and ExitCode is 0' {
|
|
$env:FAKE_VMRUN_EXIT = '0'
|
|
{ Invoke-Vmrun -VmrunPath $script:FakeVmrun -Operation 'start' -ThrowOnError } |
|
|
Should -Not -Throw
|
|
}
|
|
|
|
AfterEach {
|
|
$env:FAKE_VMRUN_EXIT = $null
|
|
}
|
|
}
|