-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMSFT_UpdateServicesServer.tests.ps1
More file actions
302 lines (231 loc) · 11.7 KB
/
MSFT_UpdateServicesServer.tests.ps1
File metadata and controls
302 lines (231 loc) · 11.7 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
$script:DSCModuleName = 'UpdateServicesDsc' # Example xNetworking
$script:DSCResourceName = 'MSFT_UpdateServicesServer' # Example MSFT_xFirewall
#region HEADER
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
Import-Module -Name DscResource.Test -Force -ErrorAction Stop
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:dscModuleName `
-DSCResourceName $script:dscResourceName `
-ResourceType 'Mof' `
-TestType Unit
#endregion HEADER
# Begin Testing
try
{
#region Pester Tests
# The InModuleScope command allows you to perform white-box unit testing on the internal
# (non-exported) code of a Script Module.
InModuleScope $script:DSCResourceName {
#region Pester Test Initialization
Import-Module $PSScriptRoot\..\Helpers\ImitateUpdateServicesModule.psm1 -force
$DSCGetValues = @{
SQLServer = 'SQLServer'
ContentDir = 'C:\WSUSContent\'
UpdateImprovementProgram = $true
UpstreamServerName = ''
UpstreamServerPort = $null
UpstreamServerSSL = $null
UpstreamServerReplica = $null
ProxyServerName = ''
ProxyServerPort = $null
ProxyServerCredentialUsername = $null
ProxyServerBasicAuthentication = $null
Languages = '*'
Products = '*'
Classifications = '*'
SynchronizeAutomatically = $true
SynchronizeAutomaticallyTimeOfDay = '04:00:00'
SynchronizationsPerDay = 24
ClientTargetingMode = "Client"
}
$DSCTestValues = @{
SetupCredential = New-Object -typename System.Management.Automation.PSCredential -argumentlist 'foo', $('bar' | ConvertTo-SecureString -AsPlainText -Force)
SQLServer = 'SQLServer'
ContentDir = 'C:\WSUSContent\'
UpdateImprovementProgram = $true
UpstreamServerName = 'UpstreamServer'
UpstreamServerPort = $false
UpstreamServerSSL = $false
UpstreamServerReplica = $false
ProxyServerName = 'ProxyServer'
ProxyServerPort = 8080
Languages = "*"
Products = @("Windows", "Office")
Classifications = @('E6CF1350-C01B-414D-A61F-263D14D133B4', 'E0789628-CE08-4437-BE74-2495B842F43B', '0FA1201D-4330-4FA8-8AE9-B877473B6441')
SynchronizeAutomatically = $true
SynchronizeAutomaticallyTimeOfDay = '04:00:00'
SynchronizationsPerDay = 24
ClientTargetingMode = "Client"
}
#endregion
#region Function Get-TargetResource expecting Ensure Present
Describe "MSFT_UpdateServicesServer\Get-TargetResource" {
Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq 'HKLM:\SOFTWARE\Microsoft\Update Services\Server\Setup' -and $Name -eq 'SQLServerName' } -MockWith { @{SQLServerName = 'SQLServer' } } -Verifiable
Mock -CommandName Get-ItemProperty -ParameterFilter { $Path -eq 'HKLM:\SOFTWARE\Microsoft\Update Services\Server\Setup' -and $Name -eq 'ContentDir' } -MockWith { @{ContentDir = 'C:\WSUSContent\' } } -Verifiable
Context 'server should be configured.' {
It 'calling Get should not throw' {
{ $Script:resource = Get-TargetResource -Ensure 'Present' -verbose } | Should not throw
}
It 'sets the value for Ensure' {
$Script:resource.Ensure | Should be 'Present'
}
foreach ($setting in $DSCSetValues.Keys)
{
It "returns $setting in Get results" {
$Script:resource.$setting | Should be $DSCGetReturnValues.$setting
}
}
It 'mocks were called' {
Assert-VerifiableMock
}
}
Context 'server should not be configured.' {
It 'calling Get should not throw' {
Mock -CommandName Get-WSUSServer -MockWith { }
{ $Script:resource = Get-TargetResource -Ensure 'Absent' -verbose } | Should not throw
}
It 'sets the value for Ensure' {
$Script:resource.Ensure | Should be 'Absent'
}
It 'mocks were called' {
Assert-VerifiableMock
}
}
}
#endregion
#region Function Test-TargetResource
Describe "MSFT_UpdateServicesServer\Test-TargetResource" {
Context 'server is in correct state (Ensure=Present)' {
$DSCTestValues.Remove('Ensure')
$DSCTestValues.Add('Ensure', 'Present')
Mock -CommandName Get-TargetResource -MockWith { $DSCTestValues } -Verifiable
$script:result = $null
It 'calling test should not throw' {
{ $script:result = Test-TargetResource @DSCTestValues -verbose } | Should not throw
}
It "result should be true" {
$script:result | Should be $true
}
It 'mocks were called' {
Assert-VerifiableMock
}
}
Context 'server should not be configured (Ensure=Absent)' {
$DSCTestValues.Remove('Ensure')
$DSCTestValues.Add('Ensure', 'Absent')
Mock -CommandName Get-TargetResource -MockWith { $DSCTestValues } -Verifiable
$script:result = $null
It 'calling test should not throw' {
{ $script:result = Test-TargetResource @DSCTestValues -verbose } | Should not throw
}
It "result should be true" {
$script:result | Should be $true
}
It 'mocks were called' {
Assert-VerifiableMock
}
}
Context 'server should be configured correctly but is not' {
$DSCTestValues.Remove('Ensure')
Mock -CommandName Get-TargetResource -MockWith { $DSCTestValues } -Verifiable
$script:result = $null
It 'calling test should not throw' {
{ $script:result = Test-TargetResource @DSCTestValues -Ensure 'Present' -verbose } | Should not throw
}
It "result should be false" {
$script:result | Should be $false
}
It 'mocks were called' {
Assert-VerifiableMock
}
}
Context "setting has drifted" {
$DSCTestValues.Remove('Ensure')
$DSCTestValues.Add('Ensure', 'Present')
# Settings not currently tested: ProxyServerUserName, ProxyServerCredential, ProxyServerBasicAuthentication, 'Languages', 'Products', 'Classifications', 'SynchronizeAutomatically'
$settingsList = 'UpdateImprovementProgram', 'UpstreamServerName', 'UpstreamServerPort', 'UpstreamServerSSL', 'UpstreamServerReplica', 'ProxyServerName', 'ProxyServerPort', 'SynchronizeAutomaticallyTimeOfDay', 'SynchronizationsPerDay'
foreach ($setting in $settingsList)
{
Mock -CommandName Get-TargetResource -MockWith {
$DSCTestValues.Remove("$setting")
$DSCTestValues
} -Verifiable
$script:result = $null
It "calling test with change to $setting should not throw" {
{ $script:result = Test-TargetResource @DSCTestValues -verbose } | Should not throw
}
It "result should be false when $setting has changed" {
$script:result | Should be $false
}
It 'mocks were called' {
Assert-VerifiableMock
}
$DSCTestValues.Add("$setting", $true)
}
}
Context "upstream server difference is ignored" {
$DSCTestValues.Remove('UpstreamServerName')
$DSCTestValues.Add('UpstreamServerName', 'IGNORE')
Mock -CommandName Get-TargetResource -MockWith {
$GetValues = $DSCTestValues
$GetValues.Remove('UpstreamServerName')
$GetValues.Add('UpstreamServerName', 'SomeOtherServer')
$GetValues
} -Verifiable
$script:result = $null
It "calling test with change to UpstreamServerName should not throw" {
{ $script:result = Test-TargetResource @DSCTestValues -verbose } | Should not throw
}
It "result should be true, even when UpstreamServerName is different" {
$script:result | Should be $true
}
It 'mocks were called' {
Assert-VerifiableMock
}
$DSCTestValues.Remove('UpstreamServerName')
$DSCTestValues.Add('UpstreamServerName', 'UpstreamServer')
}
}
#endregion
#region Function Set-TargetResource
Describe "MSFT_UpdateServicesServer\Set-TargetResource" {
$DSCTestValues.Remove('Ensure')
Mock -CommandName Test-TargetResource -MockWith { $true }
Mock -CommandName New-InvalidOperationException -MockWith { }
Mock -CommandName New-InvalidResultException -MockWith { }
Mock SaveWsusConfiguration -MockWith { }
Context 'resource is idempotent (Ensure=Present)' {
It 'should not throw when running on a properly configured server' {
{ Set-targetResource @DSCTestValues -Ensure Present -verbose } | Should not throw
}
It "mocks were called" {
Assert-MockCalled -CommandName Test-TargetResource -Times 1
Assert-MockCalled -CommandName SaveWsusConfiguration -Times 1
}
It "mocks were not called that log errors" {
Assert-MockCalled -CommandName New-InvalidResultException -Times 0
Assert-MockCalled -CommandName New-InvalidOperationException -Times 0
}
}
Context 'resource supports Ensure=Absent' {
It 'should not throw when running on a properly configured server' {
{ Set-targetResource @DSCTestValues -Ensure Absent -verbose } | Should not throw
}
It "mocks were called" {
Assert-MockCalled -CommandName Test-TargetResource -Times 1
Assert-MockCalled -CommandName SaveWsusConfiguration -Times 1
}
It "mocks were not called that log errors" {
Assert-MockCalled -CommandName New-InvalidResultException -Times 0
}
}
}
#endregion
}
}
finally
{
#region FOOTER
Restore-TestEnvironment -TestEnvironment $TestEnvironment
#endregion
}