199 lines
13 KiB
PowerShell
199 lines
13 KiB
PowerShell
BeforeAll {
|
|
Import-Module "$PSScriptRoot/../src/State.psm1" -Force
|
|
Import-Module "$PSScriptRoot/../src/Repository.psm1" -Force
|
|
Import-Module "$PSScriptRoot/../src/Adapters.psm1" -Force
|
|
Import-Module "$PSScriptRoot/../src/Configs.psm1" -Force
|
|
}
|
|
|
|
Describe 'Configuration deployment' {
|
|
BeforeEach {
|
|
$caseRoot = Join-Path $TestDrive ([guid]::NewGuid().ToString('N'))
|
|
$script:repo = Join-Path $caseRoot 'repo'
|
|
$script:data = Join-Path $caseRoot 'data'
|
|
$script:destination = Join-Path $caseRoot 'user/config.txt'
|
|
New-Item -ItemType Directory "$repo/configs" -Force | Out-Null
|
|
'version one' | Set-Content "$repo/configs/config.txt" -NoNewline
|
|
$script:settings = @{ userPaths = @{ APPDATA = $caseRoot; LOCALAPPDATA = $caseRoot; USERPROFILE = $caseRoot; PROGRAMDATA = $caseRoot } }
|
|
$script:state = @{ schemaVersion = 1; configs = @{} }
|
|
$script:paths = Get-ConfigCentralPaths -DataRoot $data
|
|
Initialize-ConfigCentralData $paths
|
|
$script:configs = @([pscustomobject]@{ name = 'sample'; source = 'configs/config.txt'; target = $destination; mode = 'copy' })
|
|
}
|
|
|
|
It 'deploys and becomes idempotent' {
|
|
$plan = Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings
|
|
$plan[0].action | Should -Be 'Create'
|
|
(Invoke-ConfigOperation -Operation $plan[0] -Paths $paths -State $state).status | Should -Be 'Succeeded'
|
|
(Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0].action | Should -Be 'None'
|
|
}
|
|
|
|
It 'detects local edits and backs them up before force' {
|
|
$first = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
|
|
[void](Invoke-ConfigOperation -Operation $first -Paths $paths -State $state)
|
|
'local edit' | Set-Content $destination -NoNewline
|
|
$conflict = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
|
|
$conflict.action | Should -Be 'Conflict'
|
|
(Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state).status | Should -Be 'Conflict'
|
|
(Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state -Force).status | Should -Be 'Succeeded'
|
|
(Get-ChildItem "$data/backups/sample/*.bak").Count | Should -Be 1
|
|
}
|
|
|
|
It 'restores the newest backup and preserves the replaced file' {
|
|
$first = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
|
|
[void](Invoke-ConfigOperation -Operation $first -Paths $paths -State $state)
|
|
'local edit' | Set-Content $destination -NoNewline
|
|
$conflict = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
|
|
[void](Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state -Force)
|
|
(Restore-ConfigBackup -Name sample -Paths $paths -State $state).status | Should -Be 'Restored'
|
|
(Get-Content -LiteralPath $destination -Raw) | Should -Be 'local edit'
|
|
(Get-ChildItem "$data/backups/sample/*.bak").Count | Should -Be 2
|
|
}
|
|
|
|
It 'rejects sources outside configs' {
|
|
'secret' | Set-Content "$repo/outside.txt"
|
|
{ Resolve-ConfigSource -RepositoryRoot $repo -Source 'outside.txt' } | Should -Throw '*escapes configs*'
|
|
}
|
|
|
|
It 'handles spaces and non-ASCII target names' {
|
|
$configs[0].target = Join-Path (Split-Path -Parent $repo) 'Données utilisateur/a file.txt'
|
|
$plan = Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings
|
|
(Invoke-ConfigOperation -Operation $plan[0] -Paths $paths -State $state).status | Should -Be 'Succeeded'
|
|
Test-Path -LiteralPath $configs[0].target | Should -BeTrue
|
|
}
|
|
|
|
It 'reports repository and computer config locations without changing them' {
|
|
New-Item -ItemType Directory (Split-Path -Parent $destination) -Force | Out-Null
|
|
'computer value' | Set-Content $destination -NoNewline
|
|
$locations = Get-ConfigLocations -Configs $configs -RepositoryRoot $repo -Settings $settings -Name sample
|
|
$locations.Count | Should -Be 1
|
|
$locations[0].source | Should -Be (Join-Path $repo 'configs/config.txt')
|
|
$locations[0].target | Should -Be $destination
|
|
$locations[0].targetExists | Should -BeTrue
|
|
(Get-Content -LiteralPath $destination -Raw) | Should -Be 'computer value'
|
|
{ Get-ConfigLocations -Configs $configs -RepositoryRoot $repo -Settings $settings -Name missing } | Should -Throw "*Unknown config 'missing'*"
|
|
}
|
|
|
|
It 'captures safe files for upload and excludes the complete Nextcloud config' {
|
|
Mock Get-RepositoryRevision { 'abc123' } -ModuleName Configs
|
|
$xnviewTarget = Join-Path $caseRoot 'user/xnview.ini'
|
|
$nextcloudTarget = Join-Path $caseRoot 'user/nextcloud.cfg'
|
|
New-Item -ItemType Directory (Split-Path -Parent $xnviewTarget) -Force | Out-Null
|
|
'computer value' | Set-Content $destination -NoNewline
|
|
'portable=true' | Set-Content $xnviewTarget -NoNewline
|
|
"[General]`nlaunch=false`n[Accounts]`ntoken=never-export-this" | Set-Content $nextcloudTarget -NoNewline
|
|
'; overlay' | Set-Content "$repo/configs/xnview.ini"
|
|
"[General]`nlaunch=true" | Set-Content "$repo/configs/nextcloud.ini"
|
|
$captureConfigs = @(
|
|
$configs[0],
|
|
[pscustomobject]@{ name = 'xnviewmp-settings'; source = 'configs/xnview.ini'; target = $xnviewTarget; mode = 'ini-overlay' },
|
|
[pscustomobject]@{ name = 'nextcloud-settings'; source = 'configs/nextcloud.ini'; target = $nextcloudTarget; mode = 'ini-overlay' }
|
|
)
|
|
$output = Join-Path $caseRoot 'capture'
|
|
$capture = Export-ConfigCapture -Configs $captureConfigs -RepositoryRoot $repo -Settings $settings -Machine test -OutputPath $output
|
|
(Get-Content "$output/configs/config.txt" -Raw) | Should -Be 'computer value'
|
|
(Get-Content "$output/review/xnviewmp-settings/xnview.ini" -Raw) | Should -Be 'portable=true'
|
|
$nextcloudCapture = Get-Content "$output/configs/nextcloud.ini" -Raw
|
|
$nextcloudCapture | Should -Match 'launch=false'
|
|
$nextcloudCapture | Should -Not -Match 'token'
|
|
($capture.operations | Where-Object name -eq 'nextcloud-settings').status | Should -Be 'Captured'
|
|
(Get-Content "$output/capture.json" -Raw | ConvertFrom-Json).repositoryRevision | Should -Be 'abc123'
|
|
}
|
|
|
|
It 'adopts matching values without trying to clean up a nonexistent temporary file' {
|
|
$operation = [pscustomobject]@{
|
|
name = 'adopted'; action = 'Adopt'; mode = 'ini-overlay'
|
|
target = $destination; sourceHash = 'matching'
|
|
}
|
|
$result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state
|
|
$result.status | Should -Be 'Succeeded'
|
|
$state.configs.adopted.sourceHash | Should -Be 'matching'
|
|
}
|
|
|
|
It 'keeps the original registry failure when no temporary file was created' {
|
|
Mock Get-RegistryBackup { throw 'Registry access denied' } -ModuleName Configs
|
|
$operation = [pscustomobject]@{
|
|
name = 'registry-test'; action = 'Update'; mode = 'registry'
|
|
target = 'HKCU:\Software\Test'; sourceHash = 'new'; desired = @{ values = @() }
|
|
}
|
|
$result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state
|
|
$result.status | Should -Be 'Failed'
|
|
$result.message | Should -Be 'Registry access denied'
|
|
}
|
|
|
|
It 'explains how to initialize missing MPC-HC settings for capture' {
|
|
Mock Get-RepositoryRevision { 'abc123' } -ModuleName Configs
|
|
Mock Get-ConfigLocations {
|
|
[pscustomobject]@{ name = 'mpc-hc-settings'; targetExists = $false; target = 'HKCU:\Software\MPC-HC\MPC-HC' }
|
|
} -ModuleName Configs
|
|
$config = [pscustomobject]@{ name = 'mpc-hc-settings'; mode = 'registry' }
|
|
$result = Export-ConfigCapture -Configs @($config) -RepositoryRoot $repo -Settings $settings -Machine test -OutputPath (Join-Path $caseRoot 'missing-capture')
|
|
$result.operations[0].status | Should -Be 'Missing'
|
|
$result.operations[0].message | Should -Match 'Launch MPC-HC.*close both MPC-HC versions'
|
|
}
|
|
|
|
It 'reports partial registry writes as warnings and records actual deployed state' {
|
|
Mock Get-RegistryBackup { @{ values = @(); schemaVersion = 1 } } -ModuleName Configs
|
|
Mock Restore-RegistryDeclaration {} -ModuleName Configs
|
|
Mock Get-RegistrySnapshot { [pscustomobject]@{ values = @{ Volume = 80 }; hash = 'partial-state' } } -ModuleName Configs
|
|
Mock Set-RegistryDeclaration {
|
|
[pscustomobject]@{ written = 1; failures = @([pscustomobject]@{ path = ''; name = 'HWAcceleration'; error = 'Access denied' }) }
|
|
} -ModuleName Configs
|
|
$operation = [pscustomobject]@{ name = 'mpc-hc-settings'; action = 'Update'; mode = 'registry'; target = 'TestRoot'; sourceHash = 'desired-state'; desired = @{ values = @() } }
|
|
$result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state
|
|
$result.status | Should -Be 'Warning'
|
|
$result.message | Should -Be 'Could not write registry key(s): HWAcceleration'
|
|
Should -Invoke Restore-RegistryDeclaration -ModuleName Configs -Times 1 -Exactly -ParameterFilter { $Only.Count -eq 1 -and $Only[0].name -eq 'HWAcceleration' }
|
|
$state.configs['mpc-hc-settings'].deployedHash | Should -Be 'partial-state'
|
|
$state.configs['mpc-hc-settings'].sourceHash | Should -Be 'desired-state'
|
|
$details = Get-Content ($state.configs['mpc-hc-settings'].lastBackup + '.write-results.json') -Raw | ConvertFrom-Json
|
|
$details.failures[0].error | Should -Be 'Access denied'
|
|
}
|
|
|
|
It 'fails when no registry values could be written and leaves deployment state untouched' {
|
|
Mock Get-RegistryBackup { @{ values = @(); schemaVersion = 1 } } -ModuleName Configs
|
|
Mock Restore-RegistryDeclaration {} -ModuleName Configs
|
|
Mock Get-RegistrySnapshot { [pscustomobject]@{ values = @{}; hash = 'old-state' } } -ModuleName Configs
|
|
Mock Set-RegistryDeclaration {
|
|
[pscustomobject]@{ written = 0; failures = @([pscustomobject]@{ path = ''; name = 'HWAcceleration'; error = 'Access denied' }) }
|
|
} -ModuleName Configs
|
|
$operation = [pscustomobject]@{ name = 'mpc-hc-settings'; action = 'Update'; mode = 'registry'; target = 'TestRoot'; sourceHash = 'desired-state'; desired = @{ values = @() } }
|
|
$result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state
|
|
$result.status | Should -Be 'Failed'
|
|
$result.message | Should -Be 'Could not write registry key(s): HWAcceleration; previous managed registry values restored'
|
|
Should -Invoke Restore-RegistryDeclaration -ModuleName Configs -Times 1 -Exactly -ParameterFilter { $null -eq $Only }
|
|
$state.configs.ContainsKey('mpc-hc-settings') | Should -BeFalse
|
|
}
|
|
|
|
It 'rolls back all values and prior state when saving deployment state fails' {
|
|
Mock Get-RegistryBackup { @{ values = @(); schemaVersion = 1 } } -ModuleName Configs
|
|
Mock Set-RegistryDeclaration { @{ written = 2; failures = @() } } -ModuleName Configs
|
|
Mock Restore-RegistryDeclaration {} -ModuleName Configs
|
|
Mock Write-JsonAtomic { throw 'Disk full' } -ModuleName Configs -ParameterFilter { $Path -like '*state.json' }
|
|
$state.configs['mpc-hc-settings'] = @{ deployedHash = 'original' }
|
|
$operation = [pscustomobject]@{ name = 'mpc-hc-settings'; action = 'Update'; mode = 'registry'; target = 'TestRoot'; sourceHash = 'desired'; desired = @{ values = @() } }
|
|
$result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state
|
|
$result.status | Should -Be 'Failed'
|
|
$result.message | Should -Match 'Disk full'
|
|
Should -Invoke Restore-RegistryDeclaration -ModuleName Configs -Times 1 -Exactly -ParameterFilter { $null -eq $Only }
|
|
$state.configs['mpc-hc-settings'].deployedHash | Should -Be 'original'
|
|
}
|
|
|
|
It 'reports incomplete rollback with the retained backup path' {
|
|
Mock Get-RegistryBackup { @{ values = @(); schemaVersion = 1 } } -ModuleName Configs
|
|
Mock Set-RegistryDeclaration { throw 'Write failed' } -ModuleName Configs
|
|
Mock Restore-RegistryDeclaration { throw 'Access denied' } -ModuleName Configs
|
|
$operation = [pscustomobject]@{ name = 'mpc-hc-settings'; action = 'Update'; mode = 'registry'; target = 'TestRoot'; sourceHash = 'desired'; desired = @{ values = @() } }
|
|
$result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state
|
|
$result.status | Should -Be 'Failed'
|
|
$result.message | Should -Match 'rollback incomplete. Backup: .*json.*Access denied'
|
|
$result.message | Should -Not -Match 'values restored'
|
|
}
|
|
|
|
It 'refuses unsafe automatic restoration of registry and hosts backups' {
|
|
$registryState = @{ configs = @{ 'mpc-hc-settings' = @{ mode = 'registry'; target = 'HKCU:\Software\MPC-HC\MPC-HC' } } }
|
|
{ Restore-ConfigBackup -Name mpc-hc-settings -Paths $paths -State $registryState } | Should -Throw '*not supported for registry*'
|
|
$hostsState = @{ configs = @{ 'windows-hosts' = @{ mode = 'hosts-block'; target = 'C:\Windows\System32\drivers\etc\hosts' } } }
|
|
{ Restore-ConfigBackup -Name windows-hosts -Paths $paths -State $hostsState } | Should -Throw '*not supported for hosts*'
|
|
}
|
|
}
|