config-central/tests/Installer.Tests.ps1

122 lines
5.7 KiB
PowerShell

BeforeAll {
$messageTokens = $null; $messageErrors = $null
$messageAst = [System.Management.Automation.Language.Parser]::ParseFile("$PSScriptRoot/../install.ps1", [ref]$messageTokens, [ref]$messageErrors)
$messageFn = $messageAst.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Write-ConfigCentralMessage' }, $true)
. ([scriptblock]::Create($messageFn.Extent.Text))
$script:installerPath = "$PSScriptRoot/../install.ps1"
$tokens = $null
$errors = $null
$script:installerAst = [System.Management.Automation.Language.Parser]::ParseFile($installerPath, [ref]$tokens, [ref]$errors)
if ($errors.Count -gt 0) { throw "install.ps1 does not parse: $($errors[0].Message)" }
$functionAst = $installerAst.Find({
param($node)
$node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Select-ConfigCentralMachine'
}, $true)
. ([scriptblock]::Create($functionAst.Extent.Text))
$outcomeAst = $installerAst.Find({
param($node)
$node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Write-SetupFailure'
}, $true)
. ([scriptblock]::Create($outcomeAst.Extent.Text))
}
Describe 'One-stop installer machine selection' {
BeforeEach {
$script:Branch = 'main'
$script:previousComputerName = $env:COMPUTERNAME
$env:COMPUTERNAME = 'OFFICE-PC'
}
AfterEach { $env:COMPUTERNAME = $script:previousComputerName }
It 'uses an explicit machine selection' {
Select-ConfigCentralMachine -Requested living-room -Saved office-pc -Available @('living-room', 'office-pc') |
Should -Be 'living-room'
}
It 'reuses the saved machine on subsequent runs' {
Select-ConfigCentralMachine -Requested '' -Saved living-room -Available @('living-room', 'office-pc') |
Should -Be 'living-room'
}
It 'matches the Windows computer name before prompting' {
Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('living-room', 'office-pc') |
Should -Be 'office-pc'
}
It 'automatically selects the only available manifest' {
Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('beffroi-laptop') |
Should -Be 'beffroi-laptop'
}
It 'allows unset environment defaults through parameter binding' {
$previousMachine = $env:CONFIG_CENTRAL_MACHINE
$previousBranch = $env:CONFIG_CENTRAL_BRANCH
try {
$env:CONFIG_CENTRAL_MACHINE = $null
$env:CONFIG_CENTRAL_BRANCH = $null
$parameterProbe = [scriptblock]::Create(
$installerAst.ParamBlock.Extent.Text + "`n@{ Machine = `$Machine; Branch = `$Branch }"
)
$bound = & $parameterProbe
$bound.Machine | Should -BeNullOrEmpty
$bound.Branch | Should -BeNullOrEmpty
}
finally {
$env:CONFIG_CENTRAL_MACHINE = $previousMachine
$env:CONFIG_CENTRAL_BRANCH = $previousBranch
}
}
}
Describe 'Setup config replacement policy' {
It 'forwards the preserve option through setup and disables force only when requested' {
$installerText = Get-Content "$PSScriptRoot/../install.ps1" -Raw
$bootstrapText = Get-Content "$PSScriptRoot/../bootstrap.ps1" -Raw
$installerArguments = [regex]::Match($installerText, '(?s) \$arguments = @\(.*?(?= & \$windowsPowerShell)').Value
$bootstrapArguments = [regex]::Match($bootstrapText, '(?s) \$applyArguments = @\(.*?(?= \$applyExitCode = Invoke-BootstrapApply)').Value
$installerArguments | Should -Not -BeNullOrEmpty
$bootstrapArguments | Should -Not -BeNullOrEmpty
$bootstrapPath = 'bootstrap.ps1'; $repository = 'https://example.test/repo.git'
$Branch = 'main'; $Machine = 'test'; $SkipApply = $false
$Checkout = $TestDrive; $dataRoot = $TestDrive
foreach ($preserve in @($false, $true)) {
$PreserveLocalConfigs = $preserve
. ([scriptblock]::Create($installerArguments))
($arguments -contains '-PreserveLocalConfigs') | Should -Be $preserve
. ([scriptblock]::Create($bootstrapArguments))
($applyArguments -contains '-ForceConfigs') | Should -Be (-not $preserve)
}
}
}
Describe 'Friendly setup outcomes' {
BeforeEach {
$script:previousLocalAppData = $env:LOCALAPPDATA
$env:LOCALAPPDATA = $TestDrive
Mock Write-Host {}
}
AfterEach { $env:LOCALAPPDATA = $script:previousLocalAppData }
It 'explains partial completion without throwing' {
{ Write-SetupFailure -ExitCode 5 } | Should -Not -Throw
Should -Invoke Write-Host -ParameterFilter { $Object -like '*Successful changes were kept*' } -Times 1
Should -Invoke Write-Host -ParameterFilter { $Object -like '*Deployment logs:*' } -Times 1
}
It 'presents reboot required as completion rather than a failure' {
Write-SetupFailure -ExitCode 6
Should -Invoke Write-Host -ParameterFilter { $Object -like '*Setup completed*restart*' } -Times 1
Should -Invoke Write-Host -ParameterFilter { $Object -like '*could not complete*' } -Times 0
}
It 'gives a retry instruction for an active update' {
Write-SetupFailure -ExitCode 10
Should -Invoke Write-Host -ParameterFilter { $Object -like '*Wait for it to finish*' } -Times 1
}
It 'retains unknown error codes in the summary' {
Mock Write-ConfigCentralMessage {}
Write-SetupFailure -ExitCode 123
Should -Invoke Write-ConfigCentralMessage -ParameterFilter { $Level -eq 'Error' -and $Message -like '*exit code 123*' } -Times 1
}
}