-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest-Service.Tests.ps1
99 lines (75 loc) · 2.76 KB
/
Test-Service.Tests.ps1
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
96
97
98
99
Import-Module ".\Test-Service.ps1" -Force
Describe 'Test-Service' {
Mock -CommandName Write-Verbose -MockWith {
} -ParameterFilter {
$Message -eq "[$Server] Testing $Service service"
}
Context 'When service is running' {
Mock -CommandName Get-Service -MockWith {
return @{Status='Running'}
}
Mock -CommandName Write-Verbose -MockWith {
Write-Output "[$Server] PASSED"
} -ParameterFilter {
$Message -eq "[$Server] PASSED"
}
$result = Test-Service -ComputerName TEST01 -Service TEST
it 'should return PASSED' {
$result | should be "[TEST01] PASSED"
}
}
Context 'When service is stopped and successfully started' {
$Script:MockCounter = 0
Mock -CommandName Get-Service -MockWith {
$Script:MockCounter++
if ($Script:MockCounter -eq 0) {
return @{Status='Stopped'}
}
if ($Script:MockCounter -eq 1) {
return @{Status='Running'}
}
}
Mock -CommandName Write-Warning -MockWith {
Write-Output "[$Server] FAILED"
} -ParameterFilter {
$Message -eq "[$Server] FAILED"
}
Mock -CommandName Write-Verbose -MockWith {
Write-Output "[$Server] PASSED"
} -ParameterFilter {
$Message -eq "[$Server] PASSED"
}
Mock -CommandName Write-Warning -MockWith {
} -ParameterFilter {
$Message -eq "[$Server] Starting $Service service"
}
Mock -CommandName Invoke-Command -MockWith {}
$result = Test-Service -ComputerName TEST01 -Service TEST
It 'should return PASSED' {
$result | should be "[TEST01] PASSED"
}
}
Context 'When service is stopped and cannot be started' {
Mock -CommandName Get-Service -MockWith {
return @{Status='Stopped'}
}
Mock -CommandName Write-Warning -MockWith {
} -ParameterFilter {
$Message -eq "[$Server] FAILED"
}
Mock -CommandName Write-Warning -MockWith {
} -ParameterFilter {
$Message -eq "[$Server] Starting $Service service"
}
Mock -CommandName Write-Warning -MockWith {
Write-Output "[$Server] Cannot start $Service service"
} -ParameterFilter {
$Message -eq "[$Server] Cannot start $Service service"
}
Mock -CommandName Invoke-Command -MockWith {}
$result = Test-Service -ComputerName TEST01 -Service TEST
It 'should return cannot start service' {
$result | should be "[TEST01] Cannot start TEST service"
}
}
}