feat(stagging): prepare Windows staging server in version 0.2.0
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#requires -Version 5.1
|
||||
$ErrorActionPreference='Stop'
|
||||
Import-Module (Join-Path $PSScriptRoot '../Modules/WindowsPreparation.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 Settings { return @{ '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' } }
|
||||
function State { return [pscustomobject]@{ Caption='Microsoft Windows Server 2025 Standard'; ProductType=3; PartOfDomain=$false; Adapters=@([pscustomobject]@{ Name='Ethernet'; ifIndex=4; HardwareInterface=$true; Status='Up' }); Addresses=@(); OtherAddresses=@(); DefaultRoutes=@(); Scopes=@(); Scopes6=@(); Options=@(); Bindings=@(); DhcpInstalled=$false } }
|
||||
$plan=Get-WindowsPreparationPlan (Settings)
|
||||
Assert ($plan.ScopeId -eq '192.168.50.0') 'Scope network'
|
||||
Assert ($plan.SubnetMask -eq '255.255.255.0') 'Subnet mask'
|
||||
Assert ($plan.WindowsYear -eq '2025') 'Version from Excel'
|
||||
Assert ($plan.MediaDirectory -eq 'C:\Staging\Medien') 'Media path from Excel'
|
||||
Assert ($plan.BootDirectory -eq 'C:\Staging-PXE\Boot') 'Separate boot directory'
|
||||
Assert-PreparationState $plan (State); Assert $true 'Fresh dedicated server accepted'
|
||||
foreach ($key in @('Windows-Server-Version','Netzwerkkarte','GETAC-IP','Netzpräfix','DHCP von','DHCP bis','Medienordner')) { Reject { $s=Settings; $s[$key]=''; Get-WindowsPreparationPlan $s } "Missing $key" }
|
||||
Reject { $s=Settings; $s['Windows-Server-Version']='2030'; Get-WindowsPreparationPlan $s } 'Unknown Windows version'
|
||||
Reject { $s=Settings; $s.Netzwerkkarte='Ether*'; Get-WindowsPreparationPlan $s } 'Adapter wildcard'
|
||||
Reject { $s=Settings; $s['GETAC-IP']='192.168.50.100'; Get-WindowsPreparationPlan $s } 'IP in pool'
|
||||
Reject { $s=Settings; $s['DHCP von']='192.168.50.220'; Get-WindowsPreparationPlan $s } 'Reversed pool'
|
||||
Reject { $s=Settings; $s['DHCP bis']='192.168.51.10'; Get-WindowsPreparationPlan $s } 'Pool outside subnet'
|
||||
Reject { $s=Settings; $s['GETAC-IP']='192.168.50.255'; Get-WindowsPreparationPlan $s } 'Broadcast station IP'
|
||||
Reject { $s=Settings; $s['Netzpräfix']='33'; Get-WindowsPreparationPlan $s } 'Invalid prefix'
|
||||
Reject { $s=Settings; $s.Medienordner='\\nas\media'; Get-WindowsPreparationPlan $s } 'Remote media path'
|
||||
Reject { $s=Settings; $s.Medienordner='C:\Staging\..\Windows'; Get-WindowsPreparationPlan $s } 'Path traversal'
|
||||
Reject { $s=State; $s.ProductType=1; Assert-PreparationState $plan $s } 'Windows client'
|
||||
Reject { $s=State; $s.ProductType=2; Assert-PreparationState $plan $s } 'Domain controller'
|
||||
Reject { $s=State; $s.PartOfDomain=$true; Assert-PreparationState $plan $s } 'Domain member'
|
||||
Reject { $s=State; $s.Caption='Microsoft Windows Server 2022 Standard'; Assert-PreparationState $plan $s } 'Excel/OS mismatch'
|
||||
Reject { $s=State; $s.Adapters=@(); Assert-PreparationState $plan $s } 'Missing adapter'
|
||||
Reject { $s=State; $s.Adapters[0].Status='Disconnected'; Assert-PreparationState $plan $s } 'Unplugged adapter'
|
||||
Reject { $s=State; $s.Adapters[0].HardwareInterface=$false; Assert-PreparationState $plan $s } 'Virtual adapter'
|
||||
Reject { $s=State; $s.DefaultRoutes=@('0.0.0.0/0'); Assert-PreparationState $plan $s } 'Management default route'
|
||||
Reject { $s=State; $s.Addresses=@([pscustomobject]@{ IPAddress='192.168.1.1'; PrefixLength=24; PrefixOrigin='Manual' }); Assert-PreparationState $plan $s } 'Existing static configuration'
|
||||
Reject { $s=State; $s.OtherAddresses=@([pscustomobject]@{ IPAddress='192.168.50.7'; PrefixLength=24 }); Assert-PreparationState $plan $s } 'Other adapter overlap'
|
||||
Reject { $s=State; $s.Scopes=@([pscustomobject]@{ ScopeId='10.0.0.0'; Name='Office'; Description='Other'; StartRange='10.0.0.10'; EndRange='10.0.0.99'; SubnetMask='255.255.255.0' }); Assert-PreparationState $plan $s } 'Existing production scope'
|
||||
Reject { $s=State; $s.Scopes6=@('anything'); Assert-PreparationState $plan $s } 'Existing IPv6 scope'
|
||||
Reject { $s=State; $s.Options=@('router'); Assert-PreparationState $plan $s } 'Existing global options'
|
||||
|
||||
# Windows cmdlets are mocked inside module scope. No local network/role changes.
|
||||
$module=Get-Module WindowsPreparation
|
||||
$simulation=& $module {
|
||||
param($p,$s)
|
||||
$script:events=[Collections.Generic.List[string]]::new(); $script:scopeState='InActive'; $script:binding=$false; $script:addressExists=$false
|
||||
function Import-Module { param($Name) }
|
||||
function Install-WindowsFeature { param($Name,[switch]$IncludeManagementTools) $script:events.Add('install-role'); [pscustomobject]@{ Success=$true; RestartNeeded='No' } }
|
||||
function Start-Service { param($Name) $script:events.Add('start-service') }
|
||||
function Stop-Service { param($Name,$ErrorAction) $script:events.Add('stop-service') }
|
||||
function Get-DhcpServerv4Binding { @([pscustomobject]@{ InterfaceAlias='Ethernet'; BindingState=$script:binding },[pscustomobject]@{ InterfaceAlias='Wi-Fi'; BindingState=$false }) }
|
||||
function Get-DhcpServerv6Binding { @([pscustomobject]@{ InterfaceAlias='Ethernet' }) }
|
||||
function Set-DhcpServerv4Binding { param($InterfaceAlias,$BindingState) $script:events.Add("binding:$InterfaceAlias=$BindingState"); if ($InterfaceAlias -eq 'Ethernet') { $script:binding=$BindingState } }
|
||||
function Set-DhcpServerv6Binding { param($InterfaceAlias,$BindingState) $script:events.Add("v6:$BindingState") }
|
||||
function Set-NetIPInterface { param($InterfaceIndex,$AddressFamily,$Dhcp) $script:events.Add('static-ip') }
|
||||
function Get-NetIPAddress { param($InterfaceIndex,$AddressFamily) if ($script:addressExists) { [pscustomobject]@{ IPAddress=$p.IP; AddressState='Preferred'; PrefixOrigin='Manual' } } }
|
||||
function Remove-NetIPAddress { param($InterfaceIndex,$IPAddress,$Confirm) throw 'Unexpected IP removal' }
|
||||
function New-NetIPAddress { param($InterfaceIndex,$IPAddress,$PrefixLength,$AddressFamily) $script:addressExists=$true; $script:events.Add('new-ip') }
|
||||
function Set-DnsClient { param($InterfaceIndex,$RegisterThisConnectionsAddress) }
|
||||
function New-Item { param($ItemType,$Path,[switch]$Force) $script:events.Add('directory') }
|
||||
function Add-DhcpServerv4Scope { param($Name,$Description,$StartRange,$EndRange,$SubnetMask,$State,$LeaseDuration) $script:scopeState=$State; $script:events.Add('create-scope') }
|
||||
function Get-NetFirewallRule { param($Name,$ErrorAction) }
|
||||
function New-NetFirewallRule { param($Name,$DisplayName,$Direction,$Action,$Protocol,$LocalPort,$RemotePort,$InterfaceAlias,$Profile,$Service) $script:events.Add("firewall:$InterfaceAlias/$LocalPort") }
|
||||
function Set-Service { param($Name,$StartupType) }
|
||||
function Set-DhcpServerv4Scope { param($ScopeId,$State,$ErrorAction) $script:scopeState=$State; $script:events.Add("scope:$State") }
|
||||
function Get-DhcpServerv4Scope { param($ScopeId) [pscustomobject]@{ State=$script:scopeState } }
|
||||
$first=Invoke-WindowsPreparation $p $s
|
||||
$firstEvents=@($script:events); $script:events.Clear()
|
||||
$s.DhcpInstalled=$true
|
||||
$s.Addresses=@([pscustomobject]@{ IPAddress=$p.IP; PrefixLength=$p.Prefix; PrefixOrigin='Manual' })
|
||||
$s.Scopes=@([pscustomobject]@{ ScopeId=$p.ScopeId; Name=$p.ScopeName; Description=$p.ScopeDescription; StartRange=$p.DHCPStart; EndRange=$p.DHCPEnd; SubnetMask=$p.SubnetMask })
|
||||
$second=Invoke-WindowsPreparation $p $s -ActivateDhcp
|
||||
$secondEvents=@($script:events); $script:events.Clear()
|
||||
function New-NetFirewallRule { throw 'Simulated firewall failure' }
|
||||
$failed=$false; try { Invoke-WindowsPreparation $p $s -ActivateDhcp | Out-Null } catch { $failed=$true }
|
||||
[pscustomobject]@{ First=$first; FirstEvents=$firstEvents; Second=$second; SecondEvents=$secondEvents; Failed=$failed; FailureEvents=@($script:events) }
|
||||
} $plan (State)
|
||||
Assert ($simulation.First.DHCP -eq 'InActive') 'Default leaves scope inactive'
|
||||
Assert ($simulation.FirstEvents -contains 'v6:False') 'DHCPv6 unbound'
|
||||
Assert ($simulation.FirstEvents.IndexOf('binding:Wi-Fi=False') -lt $simulation.FirstEvents.IndexOf('new-ip')) 'Unbind before network changes'
|
||||
Assert ($simulation.FirstEvents -contains 'firewall:Ethernet/67') 'Firewall bound to selected adapter'
|
||||
Assert ($simulation.Second.DHCP -eq 'Active') 'Explicit activation'
|
||||
Assert ($simulation.SecondEvents -notcontains 'install-role') 'Rerun preserves installed role'
|
||||
Assert ($simulation.SecondEvents -notcontains 'new-ip') 'Rerun preserves static IP'
|
||||
Assert ($simulation.SecondEvents -notcontains 'create-scope') 'Rerun preserves scope'
|
||||
Assert ($simulation.Failed) 'Mutation failure surfaced'
|
||||
Assert ($simulation.FailureEvents -contains 'scope:InActive') 'Failure deactivates owned scope'
|
||||
Assert ($simulation.FailureEvents -contains 'stop-service') 'Failure stops DHCP'
|
||||
Write-Host "$script:count Windows preparation checks passed (Windows cmdlets simulated)."
|
||||
Reference in New Issue
Block a user