feat(stagging): number workflow and add DNS reservations in 0.3.0
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
#requires -Version 5.1
|
||||
$ErrorActionPreference='Stop'
|
||||
Import-Module (Join-Path $PSScriptRoot '../Modules/Unattended.psm1') -Force -DisableNameChecking
|
||||
Import-Module (Join-Path $PSScriptRoot '../Modules/NetworkServices.psm1') -Force -DisableNameChecking
|
||||
$script:count=0
|
||||
function Assert($Condition,$Label) { if (-not $Condition) { throw "FAIL: $Label" }; $script:count++ }
|
||||
function Reject([scriptblock]$Action,$Label) { $failed=$false; try { & $Action | Out-Null } catch { $failed=$true }; Assert $failed $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='C:\Staging\Medien'; 'DNS-Zone'='lab.example'; 'GETAC-Name'='getac' }; Servers=@(@{ Servername='pve01.lab.example'; 'PXE-MAC-Adresse'='02:00:00:00:00:11'; 'PXE-IP'='192.168.50.110'; 'Ziel-IP'='192.168.50.11'; 'Präfix'='24' }) }
|
||||
}
|
||||
function State($Plan) {
|
||||
$b=$Plan.Base
|
||||
return [pscustomobject]@{
|
||||
Base=[pscustomobject]@{ Caption='Microsoft Windows Server 2025 Standard'; ProductType=3; PartOfDomain=$false; Adapters=@([pscustomobject]@{ Name='Ethernet'; ifIndex=4; HardwareInterface=$true; Status='Up' }); Addresses=@([pscustomobject]@{ IPAddress=$b.IP; PrefixLength=24; PrefixOrigin='Manual'; AddressState='Preferred' }); OtherAddresses=@(); DefaultRoutes=@(); Scopes=@([pscustomobject]@{ ScopeId=$b.ScopeId; Name=$b.ScopeName; Description=$b.ScopeDescription; StartRange=$b.DHCPStart; EndRange=$b.DHCPEnd; SubnetMask=$b.SubnetMask }); Scopes6=@(); Options=@(); Bindings=@([pscustomobject]@{ InterfaceAlias='Ethernet'; BindingState=$true }); DhcpInstalled=$true }
|
||||
Reservations=@(); Leases=@(); Exclusions=@(); Policies=@(); DnsInstalled=$false; Zones=@(); Records=@(); DnsSettings=[pscustomobject]@{ ListeningIPAddress=@() }
|
||||
}
|
||||
}
|
||||
$plan=Get-NetworkServicesPlan (Fixture)
|
||||
$source=Join-Path $PSScriptRoot '../outputs/01a07d3f/ConfigWorkbook_KIS.xlsx'
|
||||
$workbook=Read-KisWorkbook $source
|
||||
Assert ($workbook.Settings['DNS-Zone'] -eq 'lab.example' -and $workbook.Settings['GETAC-Name'] -eq 'getac') 'DNS fields imported from delivered XLSX'
|
||||
Assert (@($workbook.Servers).Count -eq 0) 'Examples stay unselected'
|
||||
$temp=Join-Path ([IO.Path]::GetTempPath()) ('network-test-'+[Guid]::NewGuid().ToString('N')+'.xlsx')
|
||||
try {
|
||||
Copy-Item $source $temp
|
||||
$zip=[IO.Compression.ZipFile]::Open($temp,[IO.Compression.ZipArchiveMode]::Update)
|
||||
try {
|
||||
foreach ($patch in @(@{ Entry='xl/worksheets/sheet1.xml'; Cell='B8'; Value='2025' },@{ Entry='xl/worksheets/sheet2.xml'; Cell='A7'; Value='JA' })) {
|
||||
$entry=$zip.GetEntry($patch.Entry); $reader=New-Object IO.StreamReader($entry.Open())
|
||||
try { [xml]$xml=$reader.ReadToEnd() } finally { $reader.Dispose() }
|
||||
$node=$xml.SelectSingleNode("//*[local-name()='c' and @r='$($patch.Cell)']")
|
||||
$node.RemoveAll(); $node.SetAttribute('r',$patch.Cell); $node.SetAttribute('t','inlineStr')
|
||||
$inline=$xml.CreateElement('is',$xml.DocumentElement.NamespaceURI); $t=$xml.CreateElement('t',$xml.DocumentElement.NamespaceURI); $t.InnerText=$patch.Value
|
||||
$null=$inline.AppendChild($t); $null=$node.AppendChild($inline)
|
||||
$entry.Delete(); $entry=$zip.CreateEntry($patch.Entry); $writer=New-Object IO.StreamWriter($entry.Open())
|
||||
try { $writer.Write($xml.OuterXml) } finally { $writer.Dispose() }
|
||||
}
|
||||
} finally { $zip.Dispose() }
|
||||
$imported=Get-NetworkServicesPlan (Read-KisWorkbook $temp)
|
||||
Assert ($imported.Reservations.Count -eq 1 -and $imported.Reservations[0].IP -eq '192.168.50.110') 'Selected XLSX row yields correct reservation'
|
||||
Assert ($imported.Records[1].IP -eq '192.168.50.11') 'Selected XLSX row yields management DNS'
|
||||
} finally { Remove-Item $temp -ErrorAction SilentlyContinue }
|
||||
Assert ($plan.Records.Count -eq 2) 'GETAC and target DNS'
|
||||
Assert ($plan.Records[1].IP -eq '192.168.50.11') 'DNS uses management IP'
|
||||
Assert ($plan.Reservations[0].IP -eq '192.168.50.110') 'Reservation uses PXE IP'
|
||||
Assert ($plan.Reservations[0].ClientId -eq '02-00-00-00-00-11') 'Windows client identifier'
|
||||
Assert-NetworkServicesState $plan (State $plan); Assert $true 'Prepared base accepted'
|
||||
Reject { $c=Fixture; $c.Settings['DNS-Zone']=''; Get-NetworkServicesPlan $c } 'Missing zone'
|
||||
Reject { $c=Fixture; $c.Settings['DNS-Zone']='lab;example'; Get-NetworkServicesPlan $c } 'Zone injection'
|
||||
Reject { $c=Fixture; $c.Settings['GETAC-Name']='getac.lab.example'; Get-NetworkServicesPlan $c } 'GETAC short name'
|
||||
Reject { $c=Fixture; $c.Servers[0].Servername='pve01.foreign.example'; Get-NetworkServicesPlan $c } 'Foreign domain'
|
||||
Reject { $c=Fixture; $c.Servers[0].Servername='getac.lab.example'; Get-NetworkServicesPlan $c } 'GETAC name conflict'
|
||||
Reject { $c=Fixture; $c.Servers[0]['PXE-IP']=''; Get-NetworkServicesPlan $c } 'Missing PXE IP'
|
||||
Reject { $c=Fixture; $c.Servers[0]['PXE-IP']='192.168.50.11'; Get-NetworkServicesPlan $c } 'PXE outside pool'
|
||||
Reject { $c=Fixture; $c.Servers[0]['Ziel-IP']='192.168.50.110'; Get-NetworkServicesPlan $c } 'Target inside pool'
|
||||
Reject { $c=Fixture; $c.Servers[0]['Ziel-IP']='192.168.50.1'; Get-NetworkServicesPlan $c } 'GETAC IP conflict'
|
||||
Reject { $c=Fixture; $c.Servers[0]['PXE-MAC-Adresse']='ff:ff:ff:ff:ff:ff'; Get-NetworkServicesPlan $c } 'Multicast MAC'
|
||||
Reject { $c=Fixture; $c.Servers[0]['PXE-MAC-Adresse']='xx'; Get-NetworkServicesPlan $c } 'Malformed MAC'
|
||||
Reject { $c=Fixture; $c.Servers+=$c.Servers[0].Clone(); Get-NetworkServicesPlan $c } 'Duplicate hosts'
|
||||
Reject { $c=Fixture; $c.Servers+=$c.Servers[0].Clone(); $c.Servers[1].Servername='pve02.lab.example'; Get-NetworkServicesPlan $c } 'Duplicate MAC with different names'
|
||||
Reject { $c=Fixture; $c.Servers+=$c.Servers[0].Clone(); $c.Servers[1].Servername='pve02.lab.example'; $c.Servers[1]['PXE-MAC-Adresse']='02:00:00:00:00:12'; Get-NetworkServicesPlan $c } 'Duplicate PXE IP with different names/MACs'
|
||||
Reject { $s=State $plan; $s.Base.Scopes=@(); Assert-NetworkServicesState $plan $s } 'Missing base scope'
|
||||
Reject { $s=State $plan; $s.Base.Bindings=@(); Assert-NetworkServicesState $plan $s } 'Missing binding'
|
||||
Reject { $s=State $plan; $s.Base.Addresses[0].AddressState='Duplicate'; Assert-NetworkServicesState $plan $s } 'Unusable GETAC IP'
|
||||
Reject { $s=State $plan; $s.Exclusions=@('existing'); Assert-NetworkServicesState $plan $s } 'Exclusions'
|
||||
Reject { $s=State $plan; $s.Policies=@('existing'); Assert-NetworkServicesState $plan $s } 'Policies'
|
||||
Reject { $s=State $plan; $s.Leases=@([pscustomobject]@{ IPAddress='192.168.50.110'; ClientId='02-11-22-33-44-55' }); Assert-NetworkServicesState $plan $s } 'Conflicting lease'
|
||||
Reject { $s=State $plan; $s.Reservations=@([pscustomobject]@{ IPAddress='192.168.50.110'; ClientId='02-00-00-00-00-11'; Name='pve01.lab.example'; Description='Foreign'; Type='Both' }); Assert-NetworkServicesState $plan $s } 'Foreign reservation'
|
||||
Reject { $s=State $plan; $s.Zones=@([pscustomobject]@{ ZoneName='corp.example'; ZoneFile='corp.dns'; ZoneType='Primary'; IsDsIntegrated=$false; DynamicUpdate='None' }); Assert-NetworkServicesState $plan $s } 'Foreign DNS zone'
|
||||
Reject { $s=State $plan; $s.Records=@([pscustomobject]@{ HostName='pve01'; RecordType='A'; RecordData=[pscustomobject]@{ IPv4Address='192.168.50.88' } }); Assert-NetworkServicesState $plan $s } 'DNS IP conflict'
|
||||
|
||||
$module=Get-Module NetworkServices
|
||||
$readback=& $module {
|
||||
param($p,$s)
|
||||
function Get-PreparationState { param($Plan) $s.Base }
|
||||
function Import-Module { param($Name) }
|
||||
function Get-DhcpServerv4Reservation { param($ScopeId) }
|
||||
function Get-DhcpServerv4Lease { param($ScopeId,[switch]$AllLeases) }
|
||||
function Get-DhcpServerv4ExclusionRange { param($ScopeId) }
|
||||
function Get-DhcpServerv4Policy { param($ScopeId) }
|
||||
function Get-WindowsFeature { param($Name) [pscustomobject]@{ Installed=$true } }
|
||||
function Get-DnsServerSetting { param([switch]$All) [pscustomobject]@{ ListeningIPAddress=@($p.Base.IP) } }
|
||||
function Get-DnsServerZone {
|
||||
@([pscustomobject]@{ ZoneName='0.in-addr.arpa'; IsAutoCreated=$true; ZoneType='Primary'; IsDsIntegrated=$false },
|
||||
[pscustomobject]@{ ZoneName=$p.Zone; IsAutoCreated=$false; ZoneType='Primary'; IsDsIntegrated=$false })
|
||||
}
|
||||
function Get-CimInstance { param($Namespace,$ClassName) [pscustomobject]@{ ZoneName=$p.Zone; ZoneFile=$p.ZoneFile; DynamicUpdate='None' } }
|
||||
function Get-DnsServerResourceRecord { param($ZoneName) [pscustomobject]@{ HostName='@'; RecordType='SOA' } }
|
||||
Get-NetworkServicesState $p
|
||||
} $plan (State $plan)
|
||||
Assert ($readback.Zones.Count -eq 1 -and $readback.Zones[0].ZoneFile -eq $plan.ZoneFile) 'Readback joins generic and primary DNS properties, ignores automatic zones'
|
||||
Assert ($readback.Records.Count -eq 1 -and $readback.DnsSettings.ListeningIPAddress[0] -eq $plan.Base.IP) 'Readback captures records and listener'
|
||||
$simulation=& $module {
|
||||
param($p,$initial)
|
||||
$script:current=$initial; $script:events=[Collections.Generic.List[string]]::new(); $script:dhcp='InActive'
|
||||
function Get-NetworkServicesState { param($Plan) return $script:current }
|
||||
function Import-Module { param($Name) }
|
||||
function Install-WindowsFeature { param($Name,[switch]$IncludeManagementTools) $script:current.DnsInstalled=$true; $script:events.Add('install'); [pscustomobject]@{ Success=$true; RestartNeeded='No' } }
|
||||
function Start-Service { param($Name) }
|
||||
function Restart-Service { param($Name) }
|
||||
function Stop-Service { param($Name,$ErrorAction) $script:events.Add("stop:$Name") }
|
||||
function Set-Service { param($Name,$StartupType) }
|
||||
function Get-DnsServerSetting { param([switch]$All) $script:current.DnsSettings }
|
||||
function Set-DnsServerSetting { param($InputObject) $script:current.DnsSettings=$InputObject }
|
||||
function Set-DnsServerRecursion { param($Enable) $script:events.Add("recursion:$Enable") }
|
||||
function Add-DnsServerPrimaryZone { param($Name,$ZoneFile,$DynamicUpdate) $script:events.Add('zone'); $script:current.Zones+= [pscustomobject]@{ ZoneName=$Name; ZoneFile=$ZoneFile; ZoneType='Primary'; IsDsIntegrated=$false; DynamicUpdate=$DynamicUpdate } }
|
||||
function Add-DnsServerResourceRecordA { param($ZoneName,$Name,$IPv4Address) $script:events.Add('record'); $script:current.Records += [pscustomobject]@{ HostName=$Name; RecordType='A'; RecordData=[pscustomobject]@{ IPv4Address=$IPv4Address } } }
|
||||
function Add-DhcpServerv4Reservation { param($ScopeId,$IPAddress,$ClientId,$Name,$Description,$Type) $script:events.Add('reservation'); $script:current.Reservations += [pscustomobject]@{ IPAddress=$IPAddress; ClientId=$ClientId; Name=$Name; Description=$Description; Type=$Type } }
|
||||
function Set-DhcpServerv4OptionValue { param($ScopeId,$DnsServer,$DnsDomain) $script:current.Base.Options=@([pscustomobject]@{ ScopeId=$ScopeId; OptionId=6; Value=@($DnsServer) },[pscustomobject]@{ ScopeId=$ScopeId; OptionId=15; Value=@($DnsDomain) }) }
|
||||
function Set-DhcpServerv4Scope { param($ScopeId,$State,$ErrorAction) $script:dhcp=$State; $script:events.Add("dhcp:$State") }
|
||||
function Get-DhcpServerv4Scope { param($ScopeId) [pscustomobject]@{ State=$script:dhcp } }
|
||||
function Get-NetFirewallRule { param($Name,$ErrorAction) }
|
||||
function New-NetFirewallRule { param($Name,$DisplayName,$Direction,$Action,$Protocol,$LocalPort,$LocalAddress,$RemoteAddress,$InterfaceAlias,$Profile,$Service) $script:events.Add("firewall:$Protocol/$LocalPort/$InterfaceAlias") }
|
||||
function Resolve-DnsName { param($Name,$Server,$Type,[switch]$DnsOnly) $script:events.Add('resolve'); $r=@($p.Records | Where-Object { "$($_.Name).$($p.Zone)" -eq $Name })[0]; [pscustomobject]@{ Type='A'; IPAddress=$r.IP } }
|
||||
$first=Invoke-NetworkServices $p $script:current
|
||||
$firstEvents=@($script:events); $script:events.Clear()
|
||||
$second=Invoke-NetworkServices $p $script:current -ActivateDhcp
|
||||
$secondEvents=@($script:events); $script:events.Clear()
|
||||
function Resolve-DnsName { param($Name,$Server,$Type,[switch]$DnsOnly) throw 'Simulated DNS lookup failure' }
|
||||
$failed=$false; try { Invoke-NetworkServices $p $script:current -ActivateDhcp | Out-Null } catch { $failed=$true }
|
||||
[pscustomobject]@{ First=$first; FirstEvents=$firstEvents; Second=$second; SecondEvents=$secondEvents; Failed=$failed; FailureEvents=@($script:events); FinalDhcp=$script:dhcp }
|
||||
} $plan (State $plan)
|
||||
Assert ($simulation.First.DHCP -eq 'InActive') 'Default inactive'
|
||||
Assert ($simulation.First.Reservations -eq 1 -and $simulation.First.ARecords -eq 2) 'Created desired resources'
|
||||
Assert ($simulation.FirstEvents[0] -eq 'dhcp:InActive') 'Disable DHCP before changes'
|
||||
Assert ($simulation.FirstEvents -contains 'recursion:False') 'Local DNS only'
|
||||
Assert ($simulation.FirstEvents -contains 'firewall:TCP/53/Ethernet' -and $simulation.FirstEvents -contains 'firewall:UDP/53/Ethernet') 'DNS firewall protocols and adapter'
|
||||
Assert ($simulation.Second.DHCP -eq 'Active') 'Explicit activation'
|
||||
Assert (@($simulation.SecondEvents | Where-Object { $_ -in @('install','zone','record','reservation') }).Count -eq 0) 'Idempotent repeated setup'
|
||||
Assert ($simulation.SecondEvents[-1] -eq 'dhcp:Active') 'Activation after DNS readback'
|
||||
Assert ($simulation.Failed -and $simulation.FinalDhcp -eq 'InActive' -and $simulation.FailureEvents -contains 'stop:DNS') 'Lookup failure leaves DHCP inactive and stops DNS'
|
||||
Write-Host "$script:count network service checks passed (Windows cmdlets simulated)."
|
||||
@@ -94,7 +94,7 @@ try {
|
||||
if ($AsSecureString) { return (ConvertTo-SecureString 'Only-Test-Password-123!' -AsPlainText -Force) }
|
||||
return $global:StagingTestResponses.Dequeue()
|
||||
}
|
||||
& (Join-Path $PSScriptRoot '../Prepare-Unattended.ps1') -Workbook $fixturePath -Generate -OutputDirectory (Join-Path $temp 'runs')
|
||||
& (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)
|
||||
Reference in New Issue
Block a user