Tests/Integration/MSFT_ServiceResource.Integration.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<#
    These tests should only be run in AppVeyor since they currently require the AppVeyor
    administrator account credential to run.
 
    Also please note that these tests are currently dependent on each other.
    They must be run in the order given and if one test fails, subsequent tests will
    also fail.
#>


if ($PSVersionTable.PSVersion -lt [Version] '5.1')
{
    Write-Warning -Message 'Cannot run PSDscResources integration tests on PowerShell versions lower than 5.1'
    return
}

$errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'

# Import CommonTestHelper for Enter-DscResourceTestEnvironment, Exit-DscResourceTestEnvironment
$script:moduleRootPath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$script:testFolderPath = Split-Path -Path $PSScriptRoot -Parent
$script:testHelpersPath = Join-Path -Path $script:testFolderPath -ChildPath 'TestHelpers'
Import-Module -Name (Join-Path -Path $script:testHelpersPath -ChildPath 'CommonTestHelper.psm1')

$script:testEnvironment = Enter-DscResourceTestEnvironment `
    -DscResourceModuleName 'PSDscResources' `
    -DscResourceName 'MSFT_ServiceResource' `
    -TestType 'Integration'

try
{
    Describe 'Service Integration Tests' {
        BeforeAll {
            # Import CommonResourceHelper for Test-IsNanoServer, Get-AppveyorAdministratorCredential
            $dscResourcesFolderFilePath = Join-Path -Path $script:moduleRootPath -ChildPath 'DscResources'
            $commonResourceHelperFilePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath 'CommonResourceHelper.psm1'
            Import-Module -Name $commonResourceHelperFilePath

            # Import DscResource.Tests TestHelper for Reset-Dsc
            $dscResourceTestsFilePath = Join-Path -Path $script:moduleRootPath -ChildPath 'DscResource.Tests'
            $dscResourceTestHelperFilePath = Join-Path -Path $dscResourceTestsFilePath -ChildPath 'TestHelper.psm1'
            Import-Module -Name $dscResourceTestHelperFilePath

            # Import Service test helper for New-ServiceBinary, Test-ServiceExists, Remove-ServiceWithTimeout
            $serviceTestHelperFilePath = Join-Path -Path $script:testHelpersPath -ChildPath 'MSFT_ServiceResource.TestHelper.psm1'
            Import-Module -Name $serviceTestHelperFilePath

            # Import Service resource module for Test-TargetResource
            $serviceResourceFolderFilePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath 'MSFT_ServiceResource'
            $serviceResourceModuleFilePath = Join-Path -Path $serviceResourceFolderFilePath -ChildPath 'MSFT_ServiceResource.psm1'
            Import-Module -Name $serviceResourceModuleFilePath

            # Configuration file paths
            $script:configurationAllExceptCredentialFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_ServiceResource_AllExceptCredential.config.ps1'
            $script:configurationCredentialOnlyFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_ServiceResource_CredentialOnly.config.ps1'

            # Create test service binary to be the existing service
            $script:existingServiceProperties = @{
                Name = 'TestService'
                DisplayName = 'TestDisplayName'
                Description = 'Test service description'
                Dependencies = @( 'winrm' )
                Path = Join-Path -Path $TestDrive -ChildPath 'DscTestService.exe'
            }

            $existingServiceNewExecutableParameters = @{
                ServiceName = $script:existingServiceProperties.Name
                ServiceCodePath = Join-Path -Path $script:testHelpersPath -ChildPath 'DscTestService.cs'
                ServiceDisplayName = $script:existingServiceProperties.DisplayName
                ServiceDescription = $script:existingServiceProperties.Description
                ServiceDependsOn = $script:existingServiceProperties.Dependencies -join ', '
                OutputPath = $script:existingServiceProperties.Path
            }

            New-ServiceExecutable @existingServiceNewExecutableParameters

            # Create test service binary to be the new service with the same name as the existing service
            $script:newServiceProperties = @{
                Name = $script:existingServiceProperties.Name
                DisplayName = 'NewTestDisplayName'
                Description = 'New test service description'
                Dependencies = @( 'spooler' )
                Path = Join-Path -Path $TestDrive -ChildPath 'NewDscTestService.exe'
            }

            if (Test-IsNanoServer)
            {
                # Nano Server does not recognize 'spooler', so keep the dependencies value as 'winrm'
                $newServiceProperties['Dependencies'] = @( 'winrm' )
            }

            $newServiceNewExecutableParameters = @{
                ServiceName = $script:newServiceProperties.Name
                ServiceCodePath = Join-Path -Path $script:testHelpersPath -ChildPath 'DscTestServiceNew.cs'
                ServiceDisplayName = $script:newServiceProperties.DisplayName
                ServiceDescription = $script:newServiceProperties.Description
                ServiceDependsOn = $script:newServiceProperties.Dependencies -join ', '
                OutputPath = $script:newServiceProperties.Path
            }

            New-ServiceExecutable @newServiceNewExecutableParameters

            $script:testServiceNames = @( $script:existingServiceProperties.Name )
            $script:testServiceExecutables = @( $script:existingServiceProperties.Path, $script:newServiceProperties.Path )
        }

        AfterAll {
            # Remove any created services
            foreach ($testServiceName in $script:testServiceNames)
            {
                if (Test-ServiceExists -Name $testServiceName)
                {
                    Remove-ServiceWithTimeout -Name $testServiceName
                }
            }
        }

        Context 'Create a service' {
            Reset-DSC

            $configurationName = 'TestCreateService'
            $resourceParameters = $script:existingServiceProperties

            It 'Should compile and apply the MOF without throwing' {
                { 
                    . $script:configurationAllExceptCredentialFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw
            }

            $service = Get-Service -Name $resourceParameters.Name -ErrorAction 'SilentlyContinue'
            $serviceCimInstance = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name='$($resourceParameters.Name)'" -ErrorAction 'SilentlyContinue'

            It 'Should have created a new service with the specified name' {
                 $service | Should Not Be $null
                 $serviceCimInstance | Should Not Be $null

                 $service.Name | Should Be $resourceParameters.Name
                 $serviceCimInstance.Name | Should Be $resourceParameters.Name
            }

            It 'Should have created a new service with the specified path' {
                $serviceCimInstance.PathName | Should Be $resourceParameters.Path
            }

            It 'Should have created a new service with the specified display name' {
                $service.DisplayName | Should Be $resourceParameters.DisplayName
            }

            It 'Should have created a new service with the specified description' {
                $serviceCimInstance.Description | Should Be $resourceParameters.Description
            }

            It 'Should have created a new service with the specified dependencies' {
                $differentDependencies = Compare-Object -ReferenceObject $resourceParameters.Dependencies -DifferenceObject $service.ServicesDependedOn.Name
                $differentDependencies | Should Be $null
            }

            It 'Should have created a new service with the default state as Running' {
                $service.Status | Should Be 'Running'
            }

            It 'Should have created a new service with the default startup type as Auto' {
                $serviceCimInstance.StartMode | Should Be 'Auto'
            }

            It 'Should have created a new service with the default startup account name as LocalSystem' {
                $serviceCimInstance.StartName | Should Be 'LocalSystem'
            }

            It 'Should have created a new service with the default desktop interaction setting as False' {
                $serviceCimInstance.DesktopInteract | Should Be $false
            }

            It 'Should return true from Test-TargetResource with the same parameters' {
                MSFT_ServiceResource\Test-TargetResource @resourceParameters | Should Be $true
            }
        }

        Context 'Edit the service path, display name, description, and dependencies' {
            Reset-DSC

            $configurationName = 'TestCreateService'
            $resourceParameters = $script:newServiceProperties

            It 'Should compile and apply the MOF without throwing' {
                { 
                    . $script:configurationAllExceptCredentialFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw
            }

            $service = Get-Service -Name $resourceParameters.Name -ErrorAction 'SilentlyContinue'
            $serviceCimInstance = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name='$($resourceParameters.Name)'" -ErrorAction 'SilentlyContinue'

            It 'Should not have removed service with specified name' {
                 $service | Should Not Be $null
                 $serviceCimInstance | Should Not Be $null

                 $service.Name | Should Be $resourceParameters.Name
                 $serviceCimInstance.Name | Should Be $resourceParameters.Name
            }

            It 'Should have edited service to have the specified path' {
                $serviceCimInstance.PathName | Should Be $resourceParameters.Path
            }

            It 'Should have edited service to have the specified display name' {
                $service.DisplayName | Should Be $resourceParameters.DisplayName
            }

            It 'Should have edited service to have the specified description' {
                $serviceCimInstance.Description | Should Be $resourceParameters.Description
            }

            It 'Should have edited service to have the specified dependencies' {
                $differentDependencies = Compare-Object -ReferenceObject $resourceParameters.Dependencies -DifferenceObject $service.ServicesDependedOn.Name
                $differentDependencies | Should Be $null
            }

            It 'Should not have changed the service state from Running' {
                $service.Status | Should Be 'Running'
            }

            It 'Should not have changed the service startup type from Auto' {
                $serviceCimInstance.StartMode | Should Be 'Auto'
            }

            It 'Should not have changed the service startup account name from LocalSystem' {
                $serviceCimInstance.StartName | Should Be 'LocalSystem'
            }

            It 'Should not have changed the service desktop interaction setting from False' {
                $serviceCimInstance.DesktopInteract | Should Be $false
            }

            It 'Should return true from Test-TargetResource with the same parameters' {
                MSFT_ServiceResource\Test-TargetResource @resourceParameters | Should Be $true
            }
        }

        Context 'Edit the service startup type and state' {
            Reset-DSC

            $configurationName = 'TestCreateService'
            $resourceParameters = @{
                Name = $script:existingServiceProperties.Name
                Path = $script:newServiceProperties.Path
                StartupType = 'Manual'
                State = 'Stopped'
            }

            It 'Should compile and apply the MOF without throwing' {
                { 
                    . $script:configurationAllExceptCredentialFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw
            }

            $service = Get-Service -Name $resourceParameters.Name -ErrorAction 'SilentlyContinue'
            $serviceCimInstance = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name='$($resourceParameters.Name)'" -ErrorAction 'SilentlyContinue'

            It 'Should not have removed service with specified name' {
                 $service | Should Not Be $null
                 $serviceCimInstance | Should Not Be $null

                 $service.Name | Should Be $resourceParameters.Name
                 $serviceCimInstance.Name | Should Be $resourceParameters.Name
            }

            It 'Should have edited the service to have the specified state' {
                $service.Status | Should Be $resourceParameters.State
            }

            It 'Should have edited the service to have the specified startup type' {
                $serviceCimInstance.StartMode | Should Be $resourceParameters.StartupType
            }

            It 'Should return true from Test-TargetResource with the same parameters' {
                MSFT_ServiceResource\Test-TargetResource @resourceParameters | Should Be $true
            }
        }

        Context 'Edit the service start name and start password with Credential' {
            Reset-DSC

            $configData = @{
                AllNodes = @(
                    @{
                        NodeName = 'localhost'
                        PSDscAllowPlainTextPassword = $true
                    }
                )
            }

            $configurationName = 'TestCreateService'
            $resourceParameters = @{
                Name = 'TestService'
                Credential = Get-AppVeyorAdministratorCredential
            }

            It 'Should compile and apply the MOF without throwing' {
                { 
                    . $script:configurationCredentialOnlyFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive -ConfigurationData $configData @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw
            }

            $service = Get-Service -Name $resourceParameters.Name -ErrorAction 'SilentlyContinue'
            $serviceCimInstance = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name='$($resourceParameters.Name)'" -ErrorAction 'SilentlyContinue'

            It 'Should not have removed service with specified name' {
                    $service | Should Not Be $null
                    $serviceCimInstance | Should Not Be $null

                    $service.Name | Should Be $resourceParameters.Name
                    $serviceCimInstance.Name | Should Be $resourceParameters.Name
            }

            It 'Should have edited the service to have the specified startup account name' {
                $expectedStartName = $resourceParameters.Credential.UserName

                if ($expectedStartName.StartsWith("$env:COMPUTERNAME\"))
                {
                    $expectedStartName = $expectedStartName.TrimStart("$env:COMPUTERNAME\")
                }

                $serviceCimInstance.StartName | Should Be ".\$expectedStartName"
            }

            It 'Should return true from Test-TargetResource with the same parameters' {
                MSFT_ServiceResource\Test-TargetResource @resourceParameters | Should Be $true
            }
        }

        Context 'Edit the service start name and start password with BuiltInAccount' {
            Reset-DSC

            $configurationName = 'TestCreateService'
            $resourceParameters = @{
                Name = 'TestService'
                Path = $script:newServiceProperties.Path
                BuiltInAccount = 'LocalService'
            }

            It 'Should compile and apply the MOF without throwing' {
                { 
                    . $script:configurationAllExceptCredentialFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw
            }

            $service = Get-Service -Name $resourceParameters.Name -ErrorAction 'SilentlyContinue'
            $serviceCimInstance = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name='$($resourceParameters.Name)'" -ErrorAction 'SilentlyContinue'

            It 'Should not have removed service with specified name' {
                 $service | Should Not Be $null
                 $serviceCimInstance | Should Not Be $null

                 $service.Name | Should Be $resourceParameters.Name
                 $serviceCimInstance.Name | Should Be $resourceParameters.Name
            }

            It 'Should have edited the service to have the specified startup account name' {
                $serviceCimInstance.StartName | Should Be 'NT Authority\LocalService'
            }

            It 'Should return true from Test-TargetResource with the same parameters' {
                MSFT_ServiceResource\Test-TargetResource @resourceParameters | Should Be $true
            }
        }

        Context 'Remove the service' {
            Reset-DSC

            $configurationName = 'TestCreateService'
            $resourceParameters = @{
                Name = $script:existingServiceProperties.Name
                Path = $script:existingServiceProperties.Path
                Ensure = 'Absent'
            }

            It 'Should compile and apply the MOF without throwing' {
                { 
                    . $script:configurationAllExceptCredentialFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw
            }

            $service = Get-Service -Name $resourceParameters.Name -ErrorAction 'SilentlyContinue'
            $serviceCimInstance = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name='$($resourceParameters.Name)'" -ErrorAction 'SilentlyContinue'

            It 'Should have removed the service with specified name' {
                 $service | Should Be $null
                 $serviceCimInstance | Should Be $null
            }

            It 'Should return true from Test-TargetResource with the same parameters' {
                MSFT_ServiceResource\Test-TargetResource @resourceParameters | Should Be $true
            }
        }
    }
}
finally
{
    Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}