Files
syno-scripts/Stagging/Tests/03-Unattended.Tests.ps1
T

129 lines
11 KiB
PowerShell

#requires -Version 5.1
$ErrorActionPreference='Stop'
Import-Module (Join-Path $PSScriptRoot '../Modules/Unattended.psm1') -Force -DisableNameChecking
Add-Type -Path (Join-Path $PSScriptRoot '../Modules/Sha512Crypt.cs')
$script:count=0
function Assert($Condition,[string]$Label) { if (-not $Condition) { throw "FAIL: $Label" }; $script:count++ }
function Reject([scriptblock]$Action,[string]$Label) { $caught=$false; try { & $Action | Out-Null } catch { $caught=$true }; Assert $caught $Label }
function Fixture {
return @{
Settings=@{ 'Windows-Server-Version'='2025'; 'Netzwerkkarte'='Ethernet'; 'GETAC-IP'='192.168.50.1'; 'Netzpräfix'='24'; 'DHCP von'='192.168.50.100'; 'DHCP bis'='192.168.50.199'; 'Medienordner'=[IO.Path]::GetTempPath(); 'Proxmox-Medium'='proxmox-ve_9.0-1.iso'; 'ESXi-Medium'='VMware-VMvisor-Installer-8.0U3.iso' }
Servers=@(
@{Row=7; Installieren='JA'; Servername='pve01.lab.example'; 'PXE-MAC-Adresse'='02:00:00:00:00:11'; System='Proxmox VE'; Version='9.0-1'; Installationsdatenträger='ID_SERIAL=TEST_DISK_01'; 'Ziel-IP'='192.168.50.11'; 'Präfix'='24'; Gateway='192.168.50.254'; 'DNS-Server'='192.168.50.53'; Benutzername='root'},
@{Row=8; Installieren='JA'; Servername='esxi01.lab.example'; 'PXE-MAC-Adresse'='02:00:00:00:00:12'; System='VMware ESXi'; Version='8.0U3'; Installationsdatenträger='naa.6000000000000001'; 'Ziel-IP'='192.168.50.12'; 'Präfix'='24'; Gateway='192.168.50.254'; 'DNS-Server'='192.168.50.53'; Benutzername='root'}
)
}
}
$expected='$6$rounds=5000$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1'
$hash=[Staging.Sha512Crypt]::Compute('Hello world!','saltstring',5000)
Assert ($hash -ceq $expected) 'SHA512-crypt published vector'
$hash2=[Staging.Sha512Crypt]::Compute('Hello world!','saltstringsaltst',10000)
Assert ($hash2 -ceq '$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.') 'SHA512-crypt 10000 rounds vector'
Assert ([Staging.Sha512Crypt]::NewSalt() -ne [Staging.Sha512Crypt]::NewSalt()) 'Random salt'
$source=Join-Path $PSScriptRoot '../outputs/01a07d3f/ConfigWorkbook_KIS.xlsx'
$config=Read-KisWorkbook $source
Assert (@($config.Servers).Count -eq 0) 'Unselected workbook stays inactive'
Assert ($config.Settings['GETAC-IP'] -eq '192.168.50.1') 'Native XLSX import'
Assert (@(Get-InstallPlan $config).Count -eq 0) 'No selected rows, no preparation'
Assert ($config.Settings.ContainsKey('ISO-Ablage') -and $config.Settings['ISO-Ablage'] -eq '') 'Unknown ISO location remains blank in delivered workbook'
Assert ($config.Settings.ContainsKey('Proxmox-Medium') -and $config.Settings.ContainsKey('ESXi-Medium')) 'New ISO name labels map to installer media'
$plan=@(Get-InstallPlan (Fixture))
Assert ($plan.Count -eq 2) 'Both installer profiles'
Assert ($plan[0].Version -eq '9.0-1' -and $plan[1].Version -eq '8.0U3') 'Versions from workbook'
Assert ((Get-Netmask 24) -eq '255.255.255.0') 'Netmask /24'
Assert ((Get-Netmask 23) -eq '255.255.254.0') 'Netmask /23'
$pve=ConvertTo-AnswerText $plan[0] $hash 'admin@example.test'
$esxi=ConvertTo-AnswerText $plan[1] $hash ''
Assert ($pve.Contains('root-password-hashed = "'+$hash+'"')) 'PVE hashed root password'
Assert ($pve.Contains('filter.ID_SERIAL = "TEST_DISK_01"')) 'PVE exact serial filter'
Assert ($pve.Contains('filter.ID_NET_NAME_MAC = "*020000000011"')) 'PVE NIC selection'
Assert ($esxi.Contains('install --disk=naa.6000000000000001 --overwritevmfs')) 'ESXi explicit disk'
Assert ($esxi.Contains('--device=02:00:00:00:00:12')) 'ESXi NIC selection'
Assert (-not $esxi.Contains('--firstdisk')) 'No first-disk fallback'
Assert (-not ($pve+$esxi).Contains('Hello world!')) 'No plaintext password in answers'
Reject { $c=Fixture; $c.Servers[0].Version=''; Get-InstallPlan $c } 'Missing version'
Reject { $c=Fixture; $c.Servers[0].Version='99.0'; Get-InstallPlan $c } 'Unknown schema version'
Reject { $c=Fixture; $c.Servers[0]['PXE-MAC-Adresse']='02:00:00:00:00:12'; Get-InstallPlan $c } 'Duplicate MAC'
Reject { $c=Fixture; $c.Servers[0]['PXE-MAC-Adresse']='ff:ff:ff:ff:ff:ff'; Get-InstallPlan $c } 'Broadcast MAC'
Reject { $c=Fixture; $c.Servers[0].Installationsdatenträger='*'; Get-InstallPlan $c } 'Wildcard disk'
Reject { $c=Fixture; $c.Servers[0].Installationsdatenträger="sda`nreboot"; Get-InstallPlan $c } 'Directive injection'
Reject { $c=Fixture; $c.Servers[0]['Ziel-IP']='192.168.50.101'; Get-InstallPlan $c } 'DHCP collision'
Reject { $c=Fixture; $c.Servers[0]['Ziel-IP']='192.168.50.1'; Get-InstallPlan $c } 'GETAC collision'
Reject { $c=Fixture; $c.Servers[0]['Ziel-IP']='192.168.50.12'; Get-InstallPlan $c } 'Duplicate target IP'
Reject { $c=Fixture; $c.Servers[0]['Ziel-IP']='192.168.51.11'; Get-InstallPlan $c } 'Different staging subnet'
Reject { $c=Fixture; $c.Servers[0]['DNS-Server']=''; Get-InstallPlan $c } 'Missing DNS'
Reject { $c=Fixture; $c.Servers[0].Gateway=''; Get-InstallPlan $c } 'Missing gateway'
Reject { $c=Fixture; $c.Servers[0].Benutzername='administrator'; Get-InstallPlan $c } 'Unsupported account'
Reject { $c=Fixture; $c.Settings['Proxmox-Medium']='../wrong.iso'; Get-InstallPlan $c } 'Media path traversal'
Reject { $c=Fixture; $c.Settings['GETAC-IP']='192.168.50.150'; Get-InstallPlan $c } 'DHCP includes station'
$c=Fixture; $c.Settings['ISO-Ablage']='C:\ISOs'; $localPlan=@(Get-InstallPlan $c)
Assert ($localPlan[0].Media -eq 'C:\ISOs\proxmox-ve_9.0-1.iso') 'Explicit local ISO source overrides work directory'
$c=Fixture; $c.Settings['ISO-Ablage']='\\fileserver\Installation Media'; $sharePlan=@(Get-InstallPlan $c)
Assert ($sharePlan[1].Media -eq '\\fileserver\Installation Media\VMware-VMvisor-Installer-8.0U3.iso') 'UNC ISO source with spaces, no network access in preview'
Reject { $c=Fixture; $c.Settings['ISO-Ablage']=''; Get-InstallPlan $c } 'Explicit blank source never falls back to work directory'
Reject { $c=Fixture; $c.Settings['ISO-Ablage']='\\fileserver'; Get-InstallPlan $c } 'UNC share name required'
Reject { $c=Fixture; $c.Settings['ISO-Ablage']='C:\ISOs\..\Windows'; Get-InstallPlan $c } 'Source path traversal'
Reject { $c=Fixture; $c.Settings['ISO-Ablage']='https://example.test/isos'; Get-InstallPlan $c } 'Unsupported URL source'
Reject { $c=Fixture; $c.Settings['ISO-Ablage']='relative\ISOs'; Get-InstallPlan $c } 'Relative source rejected'
Reject { $c=Fixture; $c.Settings['Proxmox-Medium']="bad`nname.iso"; Get-InstallPlan $c } 'Control characters in ISO filename'
Reject { ConvertTo-AnswerText $plan[0] $hash "mail@example.test`nroot-password=evil" } 'Email injection'
$pw=ConvertTo-SecureString 'Only-Test-Password-123!' -AsPlainText -Force
try { $secureHash=New-RootHash $pw; Assert ($secureHash.StartsWith('$6$rounds=100000$')) 'Interactive hash profile' } finally { $pw.Dispose() }
# End-to-end import and file generation using a disposable XLSX fixture and fake
# media. These files are test data, never installation artifacts.
$temp=Join-Path ([IO.Path]::GetTempPath()) ('staging-test-'+[Guid]::NewGuid().ToString('N'))
$null=New-Item -ItemType Directory $temp
try {
$fixturePath=Join-Path $temp 'fixture.xlsx'; Copy-Item -LiteralPath $source -Destination $fixturePath
$zip=[IO.Compression.ZipFile]::Open($fixturePath,[IO.Compression.ZipArchiveMode]::Update)
try {
function Patch-Cells($Archive,[string]$EntryName,$Changes) {
$entry=$Archive.GetEntry($EntryName); $reader=New-Object IO.StreamReader($entry.Open())
try { [xml]$xml=$reader.ReadToEnd() } finally { $reader.Dispose() }
foreach ($address in $Changes.Keys) {
$node=$xml.SelectSingleNode("//*[local-name()='c' and @r='$address']")
if (-not $node) { throw "Test cell missing: $address" }
$node.RemoveAll(); $node.SetAttribute('r',$address); $node.SetAttribute('t','inlineStr')
$inline=$xml.CreateElement('is',$xml.DocumentElement.NamespaceURI); $t=$xml.CreateElement('t',$xml.DocumentElement.NamespaceURI); $t.InnerText=[string]$Changes[$address]
$null=$inline.AppendChild($t); $null=$node.AppendChild($inline)
}
$entry.Delete(); $entry=$Archive.CreateEntry($EntryName); $writer=New-Object IO.StreamWriter($entry.Open())
try { $writer.Write($xml.OuterXml) } finally { $writer.Dispose() }
}
Patch-Cells $zip 'xl/worksheets/sheet1.xml' @{ B8='2025'; B14=$temp; B15='test-pve-9.0-1.iso'; B16='test-esxi-8.0U3.iso'; B32=$temp }
Patch-Cells $zip 'xl/worksheets/sheet2.xml' @{ A7='JA'; E7='9.0-1'; F7='ID_SERIAL=TEST_DISK'; I7='192.168.50.254'; J7='192.168.50.53'; A8='JA'; E8='8.0U3'; F8='naa.6000000000000001'; I8='192.168.50.254'; J8='192.168.50.53' }
} finally { $zip.Dispose() }
[IO.File]::WriteAllText((Join-Path $temp 'test-pve-9.0-1.iso'),'TEST ONLY - NOT AN ISO')
[IO.File]::WriteAllText((Join-Path $temp 'test-esxi-8.0U3.iso'),'TEST ONLY - NOT AN ISO')
$imported=Read-KisWorkbook $fixturePath
Assert (@($imported.Servers).Count -eq 2) 'Selected XLSX rows imported'
$parsed=@(Get-InstallPlan $imported -CheckMedia)
Assert ($parsed.Count -eq 2 -and $parsed[1].Version -eq '8.0U3') 'Complete XLSX validation'
$global:StagingTestResponses=[Collections.Generic.Queue[string]]::new()
$global:StagingTestResponses.Enqueue('admin@example.test'); $global:StagingTestResponses.Enqueue('VORBEREITEN')
function Read-Host { param($Prompt,[switch]$AsSecureString)
if ($AsSecureString) { return (ConvertTo-SecureString 'Only-Test-Password-123!' -AsPlainText -Force) }
return $global:StagingTestResponses.Dequeue()
}
& (Join-Path $PSScriptRoot '../03-Prepare-Unattended.ps1') -Workbook $fixturePath -Generate -OutputDirectory (Join-Path $temp 'runs')
$run=@(Get-ChildItem -LiteralPath (Join-Path $temp 'runs') -Directory)[0]
$manifestText=[IO.File]::ReadAllText((Join-Path $run.FullName 'manifest.json'))
$manifest=@(ConvertFrom-Json $manifestText)
Assert ($manifest.Count -eq 2) 'Manifest includes both hosts'
Assert (-not $manifestText.Contains('$6$')) 'Manifest excludes hashes'
foreach ($item in $manifest) {
$answer=[IO.File]::ReadAllText((Join-Path $run.FullName $item.AnswerFile))
Assert ($answer.Contains('$6$rounds=100000$')) 'Saved answer has salted hash'
Assert (-not $answer.Contains('Only-Test-Password-123!')) 'Saved answer excludes plaintext'
Assert ($item.MediaSHA256 -eq (Get-FileHash -LiteralPath $item.Media).Hash) 'Manifest media hash'
}
Assert (-not (Test-Path (Join-Path $temp 'runs/ks.cfg'))) 'No generic catch-all kickstart'
} finally {
Remove-Item Function:Read-Host -ErrorAction SilentlyContinue
Remove-Variable StagingTestResponses -Scope Global -ErrorAction SilentlyContinue
# Only the unique disposable fixture directory created by this test.
Remove-Item -LiteralPath $temp -Recurse -Force
}
Write-Host "$script:count checks passed. No network services or installations started."