EvanZhu0721 / 公开仓库
SBMS
SBMS complete the calculation rules for the default logical desktop topology relationship and the physical dimensions of the displays in the Windows multi-screen collaboration system.
文件
../ build-installer-contract.Tests.ps13.0 KBinno-user-state.Tests.ps12.3 KBlaunch-entry-contract.Tests.ps18.1 KBmaintenance-compensation.Tests.ps15.3 KBprepare-upgrade.Tests.ps110.3 KBinstaller/tests/maintenance-compensation.Tests.ps15.3 KB
1
$ErrorActionPreference = 'Stop'2
Set-StrictMode -Version Latest3
4
$maintenanceScript = [IO.Path]::GetFullPath(5
(Join-Path $PSScriptRoot '..\maintenance.ps1')6
)7
8
Describe 'Install and uninstall compensation contracts' {9
BeforeAll {10
$loadRoot = Join-Path $TestDrive 'load-root'11
New-Item -ItemType Directory -Path $loadRoot -Force | Out-Null12
13
# Load the real orchestration functions without running the dispatcher.14
$maintenanceSource = [IO.File]::ReadAllText(15
$maintenanceScript,16
[Text.Encoding]::UTF817
)18
$dispatchMarker = 'switch ($Action) {'19
$dispatchIndex = $maintenanceSource.LastIndexOf(20
$dispatchMarker,21
[StringComparison]::Ordinal22
)23
if ($dispatchIndex -lt 0) {24
throw 'maintenance.ps1 action dispatcher was not found.'25
}26
$definitions = $maintenanceSource.Substring(0, $dispatchIndex)27
. ([scriptblock]::Create($definitions)) `28
-Action Stop `29
-InstallRoot $loadRoot30
}31
32
BeforeEach {33
$script:events = New-Object System.Collections.ArrayList34
$script:driverScan = 035
36
Mock Assert-InstallIdentity {37
[pscustomobject]@{ Sid = 'S-1-5-21-test' }38
}39
Mock Get-OwnedTaskSnapshots {40
@(41
[pscustomobject]@{42
Path = '\SBMS\'43
Name = 'old-task'44
Xml = '<Task />'45
WasRunning = $true46
}47
)48
}49
Mock Remove-SbmsTask {50
[void]$script:events.Add('remove-task')51
}52
Mock Find-SbmsDriverPackages {53
$script:driverScan++54
if ($script:driverScan -eq 1) {55
[IO.FileInfo]'C:\Windows\INF\oem-old.inf'56
}57
else {58
[IO.FileInfo]'C:\Windows\INF\oem-old.inf'59
[IO.FileInfo]'C:\Windows\INF\oem-new.inf'60
}61
}62
Mock Install-SbmsDriver {63
[void]$script:events.Add('install-driver')64
}65
Mock Install-SbmsTask {66
[void]$script:events.Add('install-task')67
throw 'startup registration failed'68
}69
Mock Remove-DriverPackages {70
param($Packages)71
[void]$script:events.Add(72
'remove-driver:' + (($Packages | ForEach-Object Name) -join ',')73
)74
}75
Mock Restore-SbmsTasks {76
[void]$script:events.Add('restore-task')77
}78
Mock Find-ObsoleteSbmsDriverPackages { @() }79
}80
81
It 'removes only newly added drivers and restores the previous task after install failure' {82
$caught = $null83
try {84
Install-Sbms85
}86
catch {87
$caught = $_88
}89
90
$caught | Should Not BeNullOrEmpty91
$caught.Exception.Message | Should Be 'startup registration failed'92
($script:events -join '|') | Should Be (93
'remove-task|install-driver|install-task|remove-task|' +94
'remove-driver:oem-new.inf|restore-task'95
)96
Assert-MockCalled Remove-DriverPackages -Times 1 -Exactly97
Assert-MockCalled Restore-SbmsTasks -Times 1 -Exactly98
}99
100
It 'reports both failures when install task restoration also fails' {101
Mock Restore-SbmsTasks {102
throw 'task restore failed'103
}104
105
$caught = $null106
try {107
Install-Sbms108
}109
catch {110
$caught = $_111
}112
113
$caught | Should Not BeNullOrEmpty114
$caught.Exception.Message |115
Should Match 'startup registration failed.*Startup-task compensation also failed.*task restore failed'116
}117
118
It 'reinstalls the driver before restoring tasks after uninstall failure' {119
Mock Test-UninstallPreflight {120
[void]$script:events.Add('preflight')121
}122
Mock Stop-Sbms {123
[void]$script:events.Add('stop')124
}125
Mock Remove-SbmsDriver {126
[void]$script:events.Add('remove-driver')127
throw 'driver removal failed'128
}129
Mock Install-SbmsDriver {130
[void]$script:events.Add('restore-driver')131
}132
Mock Restore-SbmsTasks {133
[void]$script:events.Add('restore-task')134
}135
136
$caught = $null137
try {138
Uninstall-Sbms139
}140
catch {141
$caught = $_142
}143
144
$caught | Should Not BeNullOrEmpty145
$caught.Exception.Message |146
Should Match 'driver removal failed.*External state was restored; application files were retained\.'147
($script:events -join '|') | Should Be (148
'preflight|stop|remove-task|remove-driver|' +149
'restore-driver|restore-task'150
)151
}152
153
It 'reports both failures when uninstall compensation fails' {154
Mock Test-UninstallPreflight {}155
Mock Stop-Sbms {}156
Mock Remove-SbmsDriver {157
throw 'driver removal failed'158
}159
Mock Install-SbmsDriver {160
throw 'driver restore failed'161
}162
163
$caught = $null164
try {165
Uninstall-Sbms166
}167
catch {168
$caught = $_169
}170
171
$caught | Should Not BeNullOrEmpty172
$caught.Exception.Message |173
Should Match 'driver removal failed.*Compensation also failed.*driver restore failed'174
}175
}176