Files
SGU-CredentialProvider/tests/CedNetworkConsolidation.Tests.ps1

37 lines
2.4 KiB
PowerShell

$ErrorActionPreference='Stop'
$script=Join-Path $PSScriptRoot '..\scripts\Set-SguCedClientNetwork.ps1'
$root=Join-Path ([IO.Path]::GetTempPath()) ('ced-network-test-'+[guid]::NewGuid().ToString('N'))
$priorWindows=$env:WINDIR;$priorData=$env:ProgramData;$priorName=$env:COMPUTERNAME
function Assert($Condition,$Message){if(-not $Condition){throw $Message}}
try{
$env:WINDIR=$root;$env:ProgramData=$root;$env:COMPUTERNAME='AF-99'
New-Item (Join-Path $root 'System32\drivers\etc'),(Join-Path $root 'LCI-Admin\CED-WiFi') -ItemType Directory -Force|Out-Null
$hosts=Join-Path $root 'System32\drivers\etc\hosts'
function Get-CimInstance { [pscustomobject]@{PartOfDomain=$true;DomainRole=1;Domain='lci.lasalle.mx'} }
function Clear-DnsClientCache {}
[IO.File]::WriteAllText($hosts,"127.0.0.1 localhost`r`n20.9.81.130 sgu-dc01.lci.lasalle.mx lci.lasalle.mx sgu-auth.lci.lasalle.mx rustdesk.lci.lasalle.mx # SGU managed direct enrollment`r`n")
$first=& $script
$text=[IO.File]::ReadAllText($hosts)
Assert $first.HostsChanged 'Expected legacy entry migration.'
Assert ($text.Contains('sgu-auth.lci.lasalle.mx rustdesk.lci.lasalle.mx # SGU managed direct enrollment')) 'Broker and remote-access aliases were lost.'
Assert (([regex]::Matches($text,'(?i)sgu-dc01\.lci\.lasalle\.mx')).Count -eq 1) 'Duplicate DC host aliases.'
$second=& $script
Assert (-not $second.HostsChanged) 'Repeated application modified hosts.'
[IO.File]::WriteAllText($hosts,"192.168.1.1 SGU-DC01.lci.lasalle.mx`r`n")
$conflict=$false
try{& $script|Out-Null}catch{$conflict=$true}
Assert $conflict 'Unmanaged host conflict was silently overwritten.'
Assert ([IO.File]::ReadAllText($hosts) -eq "192.168.1.1 SGU-DC01.lci.lasalle.mx`r`n") 'Conflict changed hosts.'
$env:COMPUTERNAME='APPS-01'
& $script|Out-Null
Assert ([IO.File]::ReadAllText($hosts) -eq "192.168.1.1 SGU-DC01.lci.lasalle.mx`r`n") 'Ethernet workstation was changed.'
'CED network tests passed: migration, preservation, idempotence, conflict and scope.'
}finally{
$env:WINDIR=$priorWindows;$env:ProgramData=$priorData;$env:COMPUTERNAME=$priorName
# Remove only the freshly created, validated temporary test directory.
$resolved=[IO.Path]::GetFullPath($root)
if($resolved.StartsWith([IO.Path]::GetTempPath(),[StringComparison]::OrdinalIgnoreCase) -and (Split-Path $resolved -Leaf) -like 'ced-network-test-*'){
Remove-Item -LiteralPath $resolved -Recurse -Force
}
}