247 lines
16 KiB
PowerShell
247 lines
16 KiB
PowerShell
BeforeAll {
|
|
foreach ($module in @('State', 'Manifest', 'Repository', 'Packages', 'Configs', 'Elevation', 'Scheduling', 'Core')) {
|
|
Import-Module "$PSScriptRoot/../src/$module.psm1" -Force
|
|
}
|
|
}
|
|
|
|
Describe 'Core workflow policy' {
|
|
It 'does not inventory or change packages in config-only mode' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Read-JsonState { @{ configs = @{} } }
|
|
Mock Resolve-MachineManifest { [pscustomobject]@{ packages = @([pscustomobject]@{ id = 'Never.Run' }); configs = @() } }
|
|
Mock Get-WingetInventory { throw 'WinGet must not run' }
|
|
Mock Get-ConfigPlan { @() }
|
|
$paths = [pscustomobject]@{ Settings = "$Working/settings.json"; State = "$Working/state.json" }
|
|
$plan = Get-OperationPlan -Machine test -RepositoryRoot $Working -Paths $paths -ConfigsOnly
|
|
$plan.packages.Count | Should -Be 0
|
|
Should -Invoke Get-WingetInventory -Times 0
|
|
}
|
|
}
|
|
|
|
It 'does not fail the run for a partial registry warning' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Invoke-ConfigOperation { [pscustomobject]@{ name = 'mpc-hc-settings'; status = 'Warning'; message = 'Could not write registry key(s): HWAcceleration' } }
|
|
$paths = Get-ConfigCentralPaths -DataRoot (Join-Path $Working 'warning-data')
|
|
$operation = [pscustomobject]@{ name = 'mpc-hc-settings'; requiresPackage = $null; requiresAdmin = $false; action = 'Update' }
|
|
$plan = [pscustomobject]@{ packages = @(); configs = @($operation); state = @{ schemaVersion = 1; configs = @{} } }
|
|
$result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision abc
|
|
$result.succeeded | Should -BeTrue
|
|
$result.exitCode | Should -Be 0
|
|
$result.operations[0].status | Should -Be 'Warning'
|
|
}
|
|
}
|
|
|
|
It 'returns the stable reboot-required exit code after successful work' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Invoke-PackageOperation { [pscustomobject]@{ name = 'Reboot.App'; status = 'Succeeded'; rebootRequired = $true; message = 'Installed; reboot required' } }
|
|
$paths = Get-ConfigCentralPaths -DataRoot (Join-Path $Working 'reboot-data')
|
|
$operation = [pscustomobject]@{
|
|
kind = 'package'; name = 'Reboot.App'; action = 'Install'; reason = 'Missing'
|
|
package = [pscustomobject]@{ id = 'Reboot.App'; source = 'winget'; update = 'latest' }
|
|
}
|
|
$plan = [pscustomobject]@{ packages = @($operation); configs = @(); state = @{ schemaVersion = 1; configs = @{} } }
|
|
$result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision abc
|
|
$result.succeeded | Should -BeTrue
|
|
$result.exitCode | Should -Be 6
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe 'Child process progress feedback' {
|
|
It 'captures child output and reports a plain start status' {
|
|
InModuleScope Core {
|
|
Mock Write-Host {}
|
|
$powerShell = (Get-Process -Id $PID).Path
|
|
$arguments = @('-NoProfile', '-Command', '[Console]::Out.Write("child-output"); [Console]::Error.Write("child-error"); exit 7')
|
|
$result = Invoke-ConfigCentralChildProcess -FilePath $powerShell -Arguments $arguments -Status 'Updating test software...' -ShowProgress
|
|
$result.exitCode | Should -Be 7
|
|
$result.output | Should -Be 'child-output'
|
|
$result.error | Should -Be 'child-error'
|
|
Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -eq 'Updating test software...' }
|
|
}
|
|
}
|
|
It 'keeps machine-readable child runs free of progress output' {
|
|
InModuleScope Core {
|
|
Mock Write-Host {}
|
|
$powerShell = (Get-Process -Id $PID).Path
|
|
$result = Invoke-ConfigCentralChildProcess -FilePath $powerShell -Arguments @('-NoProfile', '-Command', '[Console]::Out.Write("{}")') -Status 'Hidden status'
|
|
$result.output | Should -Be '{}'
|
|
Should -Invoke Write-Host -Times 0
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe 'Result status colors' {
|
|
It 'colors statuses without coloring item names and messages' {
|
|
InModuleScope Core {
|
|
Mock Test-ResultColor { $true }
|
|
Mock Write-Host {}
|
|
$result = [pscustomobject]@{ command = 'apply'; revision = ''; warnings = @(); operations = @(
|
|
[pscustomobject]@{ name = 'one'; status = 'Failed'; message = 'Problem' },
|
|
[pscustomobject]@{ name = 'two'; status = 'Succeeded'; message = 'Done' },
|
|
[pscustomobject]@{ name = 'three'; status = 'Conflict'; message = 'Local edits' },
|
|
[pscustomobject]@{ name = 'four'; status = 'Unchanged'; message = 'Current' }
|
|
) }
|
|
Write-ConfigCentralResult $result
|
|
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Red' -and $NoNewline }
|
|
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Green' -and $NoNewline }
|
|
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Yellow' -and $NoNewline }
|
|
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'DarkGray' -and $NoNewline }
|
|
}
|
|
}
|
|
It 'writes complete plain rows when color is disabled' {
|
|
InModuleScope Core {
|
|
Mock Test-ResultColor { $false }
|
|
Mock Write-Host {}
|
|
$result = [pscustomobject]@{ command = 'apply'; revision = ''; warnings = @(); operations = @(
|
|
[pscustomobject]@{ name = 'one'; status = 'Failed'; message = 'Problem' }
|
|
) }
|
|
Write-ConfigCentralResult $result
|
|
Should -Invoke Write-Host -Times 1 -ParameterFilter { $Object -match '^Failed\s+one\s+Problem$' -and -not $NoNewline }
|
|
Should -Invoke Write-Host -Times 0 -ParameterFilter { $PSBoundParameters.ContainsKey('ForegroundColor') }
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe 'Portable workflow integration' {
|
|
It 'plans portable-only machines without WinGet and performs no writes' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Read-JsonState { @{ configs = @{} } }
|
|
Mock Resolve-MachineManifest { [pscustomobject]@{ packages = @(
|
|
[pscustomobject]@{ id = 'presenter'; source = 'gitea'; provider = 'portable'; version = '1.0'; update = 'pinned'; sha256 = ('a' * 64); executable = 'app.exe' }
|
|
); configs = @() } }
|
|
Mock Get-WingetInventory { throw 'WinGet must not run' }
|
|
Mock Get-ConfigPlan { @() }
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/portable-plan"
|
|
$plan = Get-OperationPlan -Machine test -RepositoryRoot $Working -Paths $paths
|
|
$plan.packages[0].action | Should -Be 'Install'
|
|
Test-Path $paths.Root | Should -BeFalse
|
|
Should -Invoke Get-WingetInventory -Times 0
|
|
}
|
|
}
|
|
It 'continues independent packages and skips configs after a portable failure' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Invoke-PackageOperation {
|
|
if ($Operation.name -eq 'presenter') { return [pscustomobject]@{ name = 'presenter'; status = 'Failed'; message = 'Checksum mismatch'; rebootRequired = $false } }
|
|
[pscustomobject]@{ name = 'other'; status = 'Succeeded'; message = 'Installed'; rebootRequired = $false }
|
|
}
|
|
Mock Invoke-ConfigOperation { throw 'Dependent config must not run' }
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/portable-failure"
|
|
$plan = [pscustomobject]@{ state = @{ configs = @{} }; packages = @(
|
|
[pscustomobject]@{ name = 'presenter'; package = [pscustomobject]@{ provider = 'portable'; scope = 'user' } },
|
|
[pscustomobject]@{ name = 'other'; package = [pscustomobject]@{ provider = 'winget'; scope = 'user' } }
|
|
); configs = @([pscustomobject]@{ name = 'dependent'; requiresPackage = 'presenter' }) }
|
|
$result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision abc
|
|
$result.exitCode | Should -Be 5
|
|
$result.operations.status | Should -Be @('Failed', 'Succeeded', 'Skipped')
|
|
$json = $result | ConvertTo-Json -Depth 16 | ConvertFrom-Json
|
|
$json.operations[0].message | Should -Be 'Checksum mismatch'
|
|
(Read-JsonState $paths.State @{}).lastRun.revision | Should -Be 'abc'
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe 'Software update command' {
|
|
It 'resolves portable releases, plans WinGet once, and skips configs' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Read-JsonState { @{ schemaVersion = 1; configs = @{} } }
|
|
Mock Resolve-MachineManifest { [pscustomobject]@{ packages = @(
|
|
[pscustomobject]@{ id = 'Winget.App'; source = 'winget'; update = 'latest' },
|
|
[pscustomobject]@{ id = 'Portable.App'; provider = 'portable'; source = 'owner'; version = '1.0'; update = 'pinned'; releaseApi = 'https://example.org/api/v1/repos/a/b/releases' }
|
|
); configs = @([pscustomobject]@{ name = 'never-plan-config' }) } }
|
|
Mock Resolve-PackageRelease { [pscustomobject]@{ id = 'Portable.App'; provider = 'portable'; source = 'owner'; version = '2.0'; update = 'pinned' } }
|
|
Mock Get-WingetInventory { @{} }
|
|
Mock Get-PackagePlan {
|
|
[pscustomobject]@{ kind = 'package'; name = $Packages[0].id; action = 'Install'; reason = 'Missing'; package = $Packages[0] }
|
|
}
|
|
Mock Get-ConfigPlan { throw 'Configs must not be planned' }
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/software-plan"
|
|
$plan = Get-SoftwareUpdatePlan -Machine test -RepositoryRoot $Working -Paths $paths
|
|
# Portable packages are deliberately reordered ahead of WinGet operations.
|
|
$plan.packages.name | Should -Be @('Portable.App', 'Winget.App')
|
|
$plan.configs.Count | Should -Be 0
|
|
Should -Invoke Resolve-PackageRelease -Times 1 -Exactly
|
|
Should -Invoke Get-WingetInventory -Times 1 -Exactly
|
|
Should -Invoke Get-PackagePlan -Times 2 -Exactly
|
|
Should -Invoke Get-ConfigPlan -Times 0
|
|
}
|
|
}
|
|
|
|
It 'continues planning after an independent release lookup failure' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Read-JsonState { @{ schemaVersion = 1; configs = @{} } }
|
|
Mock Resolve-MachineManifest { [pscustomobject]@{ packages = @(
|
|
[pscustomobject]@{ id = 'Bad.Portable'; provider = 'portable'; source = 'owner'; version = '1.0'; update = 'pinned' },
|
|
[pscustomobject]@{ id = 'Good.Portable'; provider = 'portable'; source = 'owner'; version = '1.0'; update = 'pinned' }
|
|
); configs = @() } }
|
|
Mock Resolve-PackageRelease { if ($Package.id -eq 'Bad.Portable') { throw 'API unavailable' }; $Package }
|
|
Mock Get-PackagePlan { [pscustomobject]@{ kind = 'package'; name = $Packages[0].id; action = 'None'; reason = 'Current'; package = $Packages[0] } }
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/release-failure"
|
|
$plan = Get-SoftwareUpdatePlan -Machine test -RepositoryRoot $Working -Paths $paths
|
|
$plan.packages.action | Should -Be @('Failed', 'None')
|
|
$plan.packages[0].reason | Should -Match 'API unavailable'
|
|
}
|
|
}
|
|
|
|
It 'refreshes Config Central and runs update-software from the remote snapshot' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Read-JsonState { @{ branch = 'main' } }
|
|
Mock Resolve-RemoteRevision { 'remote-revision' }
|
|
Mock New-RepositorySnapshot { "$Working/snapshot" }
|
|
Mock Invoke-SnapshotCommand { [pscustomobject]@{ command = $Command; revision = $Revision; operations = @(); succeeded = $true; exitCode = 0 } }
|
|
Mock Update-RepositoryCheckout { [pscustomobject]@{ name = 'Config Central'; status = 'Succeeded'; message = 'Updated' } }
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/software-refresh"
|
|
$result = Invoke-ConfigCentral -Command update-software -Machine test -Paths $paths -RepositoryRoot $Working -ShowProgress
|
|
$result.command | Should -Be 'update-software'
|
|
$result.revision | Should -Be 'remote-revision'
|
|
$result.operations[0].name | Should -Be 'Config Central'
|
|
Should -Invoke Resolve-RemoteRevision -Times 1 -Exactly
|
|
Should -Invoke New-RepositorySnapshot -Times 1 -Exactly -ParameterFilter { $Revision -eq 'remote-revision' }
|
|
Should -Invoke Invoke-SnapshotCommand -Times 1 -Exactly -ParameterFilter { $Command -eq 'update-software' -and $Revision -eq 'remote-revision' -and $ShowProgress }
|
|
Should -Invoke Update-RepositoryCheckout -Times 1 -Exactly -ParameterFilter { $Revision -eq 'remote-revision' }
|
|
}
|
|
}
|
|
|
|
It 'reports a self-update failure without hiding completed software results' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Read-JsonState { @{ branch = 'main' } }
|
|
Mock Resolve-RemoteRevision { 'remote-revision' }
|
|
Mock New-RepositorySnapshot { "$Working/snapshot" }
|
|
Mock Invoke-SnapshotCommand { [pscustomobject]@{ command = $Command; revision = $Revision; operations = @([pscustomobject]@{ name = 'App'; status = 'Succeeded' }); succeeded = $true; exitCode = 0 } }
|
|
Mock Update-RepositoryCheckout { throw 'Branch diverged' }
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/software-refresh-failure"
|
|
$result = Invoke-ConfigCentral -Command update-software -Machine test -Paths $paths -RepositoryRoot $Working
|
|
$result.succeeded | Should -BeFalse
|
|
$result.exitCode | Should -Be 5
|
|
$result.operations.name | Should -Be @('Config Central', 'App')
|
|
$result.operations[0].message | Should -Match 'Branch diverged'
|
|
}
|
|
}
|
|
|
|
It 'runs package-only work inside the refreshed snapshot' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
Mock Resolve-RemoteRevision { throw 'Snapshot child must not fetch' }
|
|
Mock Get-SoftwareUpdatePlan { [pscustomobject]@{ packages = @(); configs = @(); state = @{ schemaVersion = 1; configs = @{} } } }
|
|
Mock Invoke-ApplyPlan { [pscustomobject]@{ command = $Command; succeeded = $true; exitCode = 0 } }
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/software-command"
|
|
$result = Invoke-ConfigCentral -Command update-software -Machine test -Paths $paths -RepositoryRoot $Working -FromSnapshot -Revision abc
|
|
$result.command | Should -Be 'update-software'
|
|
Should -Invoke Invoke-ApplyPlan -Times 1 -Exactly -ParameterFilter { $Command -eq 'update-software' }
|
|
Should -Invoke Resolve-RemoteRevision -Times 0
|
|
}
|
|
}
|
|
|
|
It 'records the software run without claiming configs used that revision' {
|
|
InModuleScope Core -Parameters @{ Working = $TestDrive } {
|
|
$paths = Get-ConfigCentralPaths -DataRoot "$Working/software-state"
|
|
$plan = [pscustomobject]@{ packages = @(); configs = @(); state = @{ schemaVersion = 1; configs = @{}; lastAppliedRevision = 'previous' } }
|
|
$result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision newer -Command update-software
|
|
$result.command | Should -Be 'update-software'
|
|
$state = Read-JsonState $paths.State @{}
|
|
$state.lastAppliedRevision | Should -Be 'previous'
|
|
$state.lastRun.command | Should -Be 'update-software'
|
|
$state.lastRun.revision | Should -Be 'newer'
|
|
}
|
|
}
|
|
}
|