forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlService.Tests.ps1
More file actions
95 lines (89 loc) · 4.01 KB
/
Copy pathControlService.Tests.ps1
File metadata and controls
95 lines (89 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Describe "Control Service cmdlet tests" -Tags "Feature","RequireAdminOnWindows" {
BeforeAll {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
if ( -not $IsWindows ) {
$PSDefaultParameterValues["it:skip"] = $true
}
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
It "StopServiceCommand can be used as API for '<parameter>' with '<value>'" -TestCases @(
@{parameter="Force";value=$true},
@{parameter="Force";value=$false},
@{parameter="NoWait";value=$true},
@{parameter="NoWait";value=$false}
) {
param($parameter, $value)
$stopservicecmd = [Microsoft.PowerShell.Commands.StopServiceCommand]::new()
$stopservicecmd.$parameter = $value
$stopservicecmd.$parameter | Should Be $value
}
It "RestartServiceCommand can be used as API for '<parameter>' with '<value>'" -TestCases @(
@{parameter="Force";value=$true},
@{parameter="Force";value=$false}
) {
param($parameter, $value)
$restartservicecmd = [Microsoft.PowerShell.Commands.RestartServiceCommand]::new()
$restartservicecmd.$parameter = $value
$restartservicecmd.$parameter | Should Be $value
}
It "Stop/Start/Restart-Service works" {
$wasStopped = $false
try {
$spooler = Get-Service Spooler
$spooler | Should Not BeNullOrEmpty
if ($spooler.Status -ne "Running") {
$wasStopped = $true
$spooler = Start-Service Spooler -PassThru
}
$spooler.Status | Should Be "Running"
$spooler = Stop-Service Spooler -PassThru
$spooler.Status | Should Be "Stopped"
(Get-Service Spooler).Status | Should Be "Stopped"
$spooler = Start-Service Spooler -PassThru
$spooler.Status | Should Be "Running"
(Get-Service Spooler).Status | Should Be "Running"
Stop-Service Spooler
(Get-Service Spooler).Status | Should Be "Stopped"
$spooler = Restart-Service Spooler -PassThru
$spooler.Status | Should Be "Running"
(Get-Service Spooler).Status | Should Be "Running"
} finally {
if ($wasStopped) {
Stop-Service Spooler
}
}
}
It "Suspend/Resume-Service works" {
try {
$originalState = "Running"
$serviceName = "WerSvc"
$service = Get-Service $serviceName
if ($service.Status -ne $originalState) {
$originalState = $service.Status
Start-Service $serviceName
}
$service | Should Not BeNullOrEmpty
Suspend-Service $serviceName
(Get-Service $serviceName).Status | Should Be "Paused"
Resume-Service $serviceName
(Get-Service $serviceName).Status | Should Be "Running"
} finally {
Set-Service $serviceName -Status $originalState
}
}
It "Failure to control service with '<script>'" -TestCases @(
@{script={Stop-Service dcomlaunch -ErrorAction Stop};errorid="ServiceHasDependentServices,Microsoft.PowerShell.Commands.StopServiceCommand"},
@{script={Suspend-Service winrm -ErrorAction Stop};errorid="CouldNotSuspendServiceNotSupported,Microsoft.PowerShell.Commands.SuspendServiceCommand"},
@{script={Resume-Service winrm -ErrorAction Stop};errorid="CouldNotResumeServiceNotSupported,Microsoft.PowerShell.Commands.ResumeServiceCommand"},
@{script={Stop-Service $(new-guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StopServiceCommand"},
@{script={Start-Service $(new-guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StartServiceCommand"},
@{script={Resume-Service $(new-guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.ResumeServiceCommand"},
@{script={Suspend-Service $(new-guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.SuspendServiceCommand"},
@{script={Restart-Service $(new-guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand"}
) {
param($script,$errorid)
{ & $script } | ShouldBeErrorId $errorid
}
}