Tests/Unit/MSFT_xWindowsFeature.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# Needed to create a fake credential
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
param ()

Import-Module -Name (Join-Path -Path (Split-Path $PSScriptRoot -Parent) `
                               -ChildPath 'CommonTestHelper.psm1') `
                               -Force

$script:testEnvironment = Enter-DscResourceTestEnvironment `
    -DSCResourceModuleName 'xPSDesiredStateConfiguration' `
    -DSCResourceName 'MSFT_xWindowsFeature' `
    -TestType Unit

try {
    InModuleScope 'MSFT_xWindowsFeature' {

        $testUserName = 'TestUserName12345'
        $testUserPassword = 'StrongOne7.'
        $testSecurePassword = ConvertTo-SecureString -String $testUserPassword -AsPlainText -Force
        $testCredential = New-Object -TypeName PSCredential -ArgumentList ($testUserName, $testSecurePassword)

        $testWindowsFeatureName1 = 'Test1'
        $testWindowsFeatureName2 = 'Test2'
        $testSubFeatureName1 = 'SubTest1'
        $testSubFeatureName2 = 'SubTest2'
        $testSubFeatureName3 = 'SubTest3'


        $mockWindowsFeatures = @{
            Test1 = @{ 
                Name                      = 'Test1'
                DisplayName               = 'Test Feature 1'
                Description               = 'Test Feature with 3 subfeatures'
                Installed                 = $false 
                InstallState              = 'Available' 
                FeatureType               = 'Role Service'
                Path                      = 'Test1'
                Depth                     = 1
                DependsOn                 = @()
                Parent                    = ''
                ServerComponentDescriptor = 'ServerComponent_Test_Cert_Authority'
                Subfeatures               = @('SubTest1','SubTest2','SubTest3')
                SystemService             = @()
                Notification              = @()
                BestPracticesModelId      = $null
                EventQuery                = $null
                PostConfigurationNeeded   = $false
                AdditionalInfo            = @('MajorVersion', 'MinorVersion', 'NumericId', 'InstallName')
            }

            SubTest1 = @{ 
                Name                      = 'SubTest1'
                DisplayName               = 'Sub Test Feature 1'
                Description               = 'Sub Test Feature with parent as test1'
                Installed                 = $true
                InstallState              = 'Available'
                FeatureType               = 'Role Service'
                Path                      = 'Test1\SubTest1'
                Depth                     = 2
                DependsOn                 = @()
                Parent                    = 'Test1'
                ServerComponentDescriptor = $null
                Subfeatures               = @()
                SystemService             = @()
                Notification              = @()
                BestPracticesModelId      = $null
                EventQuery                = $null
                PostConfigurationNeeded   = $false
                AdditionalInfo            = @('MajorVersion', 'MinorVersion', 'NumericId', 'InstallName')
            }

            SubTest2 = @{ 
                Name                      = 'SubTest2'
                DisplayName               = 'Sub Test Feature 2'
                Description               = 'Sub Test Feature with parent as test1'
                Installed                 = $true
                InstallState              = 'Available'
                FeatureType               = 'Role Service'
                Path                      = 'Test1\SubTest2'
                Depth                     = 2
                DependsOn                 = @()
                Parent                    = 'Test1'
                ServerComponentDescriptor = $null
                Subfeatures               = @()
                SystemService             = @()
                Notification              = @()
                BestPracticesModelId      = $null
                EventQuery                = $null
                PostConfigurationNeeded   = $false
                AdditionalInfo            = @('MajorVersion', 'MinorVersion', 'NumericId', 'InstallName')
            }

            SubTest3 = @{
                Name                      = 'SubTest3'
                DisplayName               = 'Sub Test Feature 3'
                Description               = 'Sub Test Feature with parent as test1'
                Installed                 = $true
                InstallState              = 'Available'
                FeatureType               = 'Role Service'
                Path                      = 'Test\SubTest3'
                Depth                     = 2
                DependsOn                 = @()
                Parent                    = 'Test1'
                ServerComponentDescriptor = $null
                Subfeatures               = @()
                SystemService             = @()
                Notification              = @()
                BestPracticesModelId      = $null
                EventQuery                = $null
                PostConfigurationNeeded   = $false
                AdditionalInfo            = @('MajorVersion', 'MinorVersion', 'NumericId', 'InstallName')
            }

            Test2 = @{ 
                Name                      = 'Test2'
                DisplayName               = 'Test Feature 2'
                Description               = 'Test Feature with 0 subfeatures'
                Installed                 = $true 
                InstallState              = 'Available' 
                FeatureType               = 'Role Service'
                Path                      = 'Test2'
                Depth                     = 1
                DependsOn                 = @()
                Parent                    = ''
                ServerComponentDescriptor = 'ServerComponent_Test_Cert_Authority'
                Subfeatures               = @()
                SystemService             = @()
                Notification              = @()
                BestPracticesModelId      = $null
                EventQuery                = $null
                PostConfigurationNeeded   = $false
                AdditionalInfo            = @('MajorVersion', 'MinorVersion', 'NumericId', 'InstallName')
            }
        }


        Describe 'xWindowsFeature/Get-TargetResource' {
            Mock -CommandName Import-ServerManager -MockWith {}

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testWindowsFeatureName2 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testWindowsFeatureName2]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testWindowsFeatureName1 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testWindowsFeatureName1]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testSubFeatureName1 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testSubFeatureName1]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testSubFeatureName2 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testSubFeatureName2]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testSubFeatureName3 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testSubFeatureName3]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }
            

            Context 'Windows Feature exists with no sub features' {
              
                It 'Should return the correct hashtable when not on a 2008 Server' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                    $getTargetResourceResult = Get-TargetResource -Name $testWindowsFeatureName2
                    $getTargetResourceResult.Name | Should Be $testWindowsFeatureName2
                    $getTargetResourceResult.DisplayName | Should Be $mockWindowsFeatures[$testWindowsFeatureName2].DisplayName
                    $getTargetResourceResult.Ensure | Should Be 'Present'
                    $getTargetResourceResult.IncludeAllSubFeature | Should Be $false
                }

                It 'Should return the correct hashtable when on a 2008 Server' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $true }

                    $getTargetResourceResult = Get-TargetResource -Name $testWindowsFeatureName2
                    $getTargetResourceResult.Name | Should Be $testWindowsFeatureName2
                    $getTargetResourceResult.DisplayName | Should Be $mockWindowsFeatures[$testWindowsFeatureName2].DisplayName
                    $getTargetResourceResult.Ensure | Should Be 'Present'
                    $getTargetResourceResult.IncludeAllSubFeature | Should Be $false
                }

                It 'Should return the correct hashtable when on a 2008 Server and Credential is passed' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $true }
                    Mock -CommandName Invoke-Command -MockWith { 
                        $windowsFeature = $mockWindowsFeatures[$testWindowsFeatureName2]
                        $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                        $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                        return $windowsFeatureObject
                    }

                    $getTargetResourceResult = Get-TargetResource -Name $testWindowsFeatureName2 -Credential $testCredential
                    $getTargetResourceResult.Name | Should Be $testWindowsFeatureName2
                    $getTargetResourceResult.DisplayName | Should Be $mockWindowsFeatures[$testWindowsFeatureName2].DisplayName
                    $getTargetResourceResult.Ensure | Should Be 'Present'
                    $getTargetResourceResult.IncludeAllSubFeature | Should Be $false

                    Assert-MockCalled -CommandName Invoke-Command -Times 1 -Exactly -Scope It

                }
            }
            Context 'Windows Feature exists with sub features' {

                It 'Should return the correct hashtable when all subfeatures are installed' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                    $getTargetResourceResult = Get-TargetResource -Name $testWindowsFeatureName1
                    $getTargetResourceResult.Name | Should Be $testWindowsFeatureName1
                    $getTargetResourceResult.DisplayName | Should Be $mockWindowsFeatures[$testWindowsFeatureName1].DisplayName
                    $getTargetResourceResult.Ensure | Should Be 'Absent'
                    $getTargetResourceResult.IncludeAllSubFeature | Should Be $true

                    Assert-MockCalled -CommandName Test-IsWinServer2008R2SP1 -Times 1 -Exactly -Scope It
                }

                It 'Should return the correct hashtable when not all subfeatures are installed' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }
                    $mockWindowsFeatures[$testSubFeatureName3].Installed = $false

                    $getTargetResourceResult = Get-TargetResource -Name $testWindowsFeatureName1
                    $getTargetResourceResult.Name | Should Be $testWindowsFeatureName1
                    $getTargetResourceResult.DisplayName | Should Be $mockWindowsFeatures[$testWindowsFeatureName1].DisplayName
                    $getTargetResourceResult.Ensure | Should Be 'Absent'
                    $getTargetResourceResult.IncludeAllSubFeature | Should Be $false

                    Assert-MockCalled -CommandName Test-IsWinServer2008R2SP1 -Times 1 -Exactly -Scope It

                    $mockWindowsFeatures[$testSubFeatureName3].Installed = $true
                }
            }

            Context 'Windows Feature does not exist' {

                It 'Should throw invalid operation exception' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }
                    $invalidName = 'InvalidFeature'
                    { Get-TargetResource -Name $invalidName } | Should Throw ($script:localizedData.FeatureNotFoundError -f $invalidName)
                }
            }
        }
        
        Describe 'xWindowsFeature/Set-TargetResource' {
            Mock -CommandName Import-ServerManager -MockWith {}

            Context 'Install/Uninstall successful' {
                Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                Mock -CommandName Add-WindowsFeature -MockWith {
                    $windowsFeature = @{
                        Success = $true
                        RestartNeeded = 'No'
                        FeatureResult = @($testWindowsFeatureName2)
                        ExitCode = 'Success'
                    }
                    $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                    $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                    return $windowsFeatureObject
                }

                Mock -CommandName Remove-WindowsFeature -MockWith {
                    $windowsFeature = @{
                        Success = $true
                        RestartNeeded = 'No'
                        FeatureResult = @($testWindowsFeatureName2)
                        ExitCode = 'Success'
                    }
                    $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                    $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                    return $windowsFeatureObject
                }

                It 'Should call Add-WindowsFeature when Ensure set to Present' {
                    { Set-TargetResource -Name $testWindowsFeatureName2 -Ensure 'Present' } | Should Not Throw
                    Assert-MockCalled -CommandName Add-WindowsFeature -Times 1 -Exactly -Scope It
                }

                It 'Should call Remove-WindowsFeature when Ensure set to Absent' {
                    { Set-TargetResource -Name $testWindowsFeatureName2 -Ensure 'Absent' } | Should Not Throw
                    Assert-MockCalled -CommandName Remove-WindowsFeature -Times 1 -Exactly -Scope It
                }

            }

            Context 'Install/Uninstall unsuccessful' {
                Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                Mock -CommandName Add-WindowsFeature -MockWith {
                    $windowsFeature = @{
                        Success = $false
                        RestartNeeded = 'No'
                        FeatureResult = @($testWindowsFeatureName2)
                        ExitCode = 'Nothing succeeded'
                    }
                    $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                    $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                    return $windowsFeatureObject
                }

                Mock -CommandName Remove-WindowsFeature -MockWith {
                    $windowsFeature = @{
                        Success = $false
                        RestartNeeded = 'No'
                        FeatureResult = @($testWindowsFeatureName2)
                        ExitCode = 'Nothing succeeded'
                    }
                    $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                    $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                    return $windowsFeatureObject
                }

                It 'Should throw invalid operation exception when Ensure set to Present' {
                    { Set-TargetResource -Name $testWindowsFeatureName2 -Ensure 'Present' } | 
                        Should Throw ($script:localizedData.FeatureInstallationFailureError -f $testWindowsFeatureName2)
                    Assert-MockCalled -CommandName Add-WindowsFeature -Times 1 -Exactly -Scope It
                }

                It 'Should throw invalid operation exception when Ensure set to Absent' {
                    { Set-TargetResource -Name $testWindowsFeatureName2 -Ensure 'Absent' } | 
                        Should Throw ($script:localizedData.FeatureUninstallationFailureError -f $testWindowsFeatureName2)
                    Assert-MockCalled -CommandName Remove-WindowsFeature -Times 1 -Exactly -Scope It
                }

            }

            Context 'Uninstall/Install with R2/SP1' {
                Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $true }

                Mock -CommandName Invoke-Command -MockWith {
                    $windowsFeature = @{
                        Success = $true
                        RestartNeeded = 'No'
                        FeatureResult = @($testWindowsFeatureName2)
                        ExitCode = 'Success'
                    }
                    $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                    $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                    return $windowsFeatureObject
                }

                Mock -CommandName 'Add-WindowsFeature' -MockWith { }
                Mock -CommandName 'Remove-WindowsFeature' -MockWith { }

                It 'Should install the feature when Ensure set to Present and Credential passed in' {

                    { 
                        Set-TargetResource -Name $testWindowsFeatureName2 `
                                           -Ensure 'Present' `
                                           -Credential $testCredential
                    } | Should Not Throw
                    Assert-MockCalled -CommandName Invoke-Command -Times 1 -Exactly -Scope It
                    Assert-MockCalled -CommandName Add-WindowsFeature -Times 0 -Scope It
                }

                It 'Should uninstall the feature when Ensure set to Absent and Credential passed in' {

                    { 
                        Set-TargetResource -Name $testWindowsFeatureName2 `
                                           -Ensure 'Absent' `
                                           -Credential $testCredential
                    } | Should Not Throw
                    Assert-MockCalled -CommandName Invoke-Command -Times 1 -Exactly -Scope It
                    Assert-MockCalled -CommandName Remove-WindowsFeature -Times 0 -Scope It
                }
            }
        }

        Describe 'xWindowsFeature/Test-TargetResource' {
            Mock -CommandName Import-ServerManager -MockWith {}

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testWindowsFeatureName1 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testWindowsFeatureName1]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testSubFeatureName1 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testSubFeatureName1]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testSubFeatureName2 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testSubFeatureName2]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Mock -CommandName Get-WindowsFeature -ParameterFilter { $Name -eq $testSubFeatureName3 } -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testSubFeatureName3]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            # Used as Get-WindowsFeature when on R2/SP1 2008
            Mock -CommandName Invoke-Command -MockWith {
                $windowsFeature = $mockWindowsFeatures[$testWindowsFeatureName1]
                $windowsFeatureObject = New-Object -TypeName PSObject -Property $windowsFeature
                $windowsFeatureObject.PSTypeNames[0] = 'Microsoft.Windows.ServerManager.Commands.Feature'
            
                return $windowsFeatureObject
            }

            Context 'Feature is in the desired state' {

                It 'Should return true when Ensure set to Absent and Feature not installed' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Absent' `
                                                  -IncludeAllSubFeature $false
                    $testTargetResourceResult | Should Be $true
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 1 -Exactly -Scope It
                }

                It 'Should return true when Ensure set to Present and Feature installed not checking subFeatures' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $true
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Present' `
                                                  -IncludeAllSubFeature $false
                    $testTargetResourceResult | Should Be $true
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 1 -Exactly -Scope It
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $false
                }

                It 'Should return true when Ensure set to Present and Feature and subFeatures installed' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $true
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Present' `
                                                  -IncludeAllSubFeature $true
                    $testTargetResourceResult | Should Be $true
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 4 -Exactly -Scope It
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $false
                }

                It 'Should return true when Ensure set to Absent and Feature not installed and on R2/SP1 2008' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $true }

                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Absent' `
                                                  -IncludeAllSubFeature $false `
                                                  -Credential $testCredential
                    $testTargetResourceResult | Should Be $true
                    Assert-MockCalled -CommandName Invoke-Command -Times 1 -Exactly -Scope It
                }
            }

            Context 'Feature is not in the desired state' {
                Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $false }

                It 'Should return false when Ensure set to Present and Feature not installed' {
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Present' `
                                                  -IncludeAllSubFeature $false
                    $testTargetResourceResult | Should Be $false
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 1 -Exactly -Scope It
                }

                It 'Should return false when Ensure set to Absent and Feature installed not checking subFeatures' {
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $true
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Absent' `
                                                  -IncludeAllSubFeature $false
                    $testTargetResourceResult | Should Be $false
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 1 -Exactly -Scope It
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $false
                }

                It 'Should return false when Ensure set to Present, Feature not installed and subFeatures installed' {
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Present' `
                                                  -IncludeAllSubFeature $true
                    $testTargetResourceResult | Should Be $false
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 1 -Exactly -Scope It
                }

                It 'Should return false when Ensure set to Absent and Feature not installed but subFeatures installed' {
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Absent' `
                                                  -IncludeAllSubFeature $true
                    $testTargetResourceResult | Should Be $false
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 2 -Exactly -Scope It
                }

                It 'Should return false when Ensure set to Absent and Feature installed and subFeatures installed' {
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $true
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Absent' `
                                                  -IncludeAllSubFeature $true
                    $testTargetResourceResult | Should Be $false
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 1 -Exactly -Scope It
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $false
                }

                It 'Should return false when Ensure set to Present and Feature installed but not all subFeatures installed' {
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $true
                    $mockWindowsFeatures[$testSubFeatureName2].Installed = $false
                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Present' `
                                                  -IncludeAllSubFeature $true
                    $testTargetResourceResult | Should Be $false
                    Assert-MockCalled -CommandName Get-WindowsFeature -Times 3 -Exactly -Scope It
                    $mockWindowsFeatures[$testWindowsFeatureName1].Installed = $false
                    $mockWindowsFeatures[$testSubFeatureName2].Installed = $true
                }

                It 'Should return false when Ensure set to Present and Feature not installed and on R2/SP1 2008' {
                    Mock -CommandName Test-IsWinServer2008R2SP1 -MockWith { return $true }

                    $testTargetResourceResult = Test-TargetResource -Name $testWindowsFeatureName1 `
                                                  -Ensure 'Present' `
                                                  -IncludeAllSubFeature $false `
                                                  -Credential $testCredential
                    $testTargetResourceResult | Should Be $false
                    Assert-MockCalled -CommandName Invoke-Command -Times 1 -Exactly -Scope It
                }
            }
        }

        Describe 'xWindowsFeature/Assert-SingleFeatureExists' {
            $multipleFeature = @{
                Name = 'MultiFeatureName'
                Count = 2
            }

            It 'Should throw invalid operation when feature equals null' {
                $nonexistentName = 'NonexistentFeatureName'
                { Assert-SingleFeatureExists -Feature $null -Name $nonexistentName } | 
                    Should Throw ($script:localizedData.FeatureNotFoundError -f $nonexistentName)
            }

            It 'Should throw invalid operation when there are multiple features with the given name' {
                { Assert-SingleFeatureExists -Feature $multipleFeature -Name $multipleFeature.Name } | 
                    Should Throw ($script:localizedData.MultipleFeatureInstancesError -f $multipleFeature.Name)
            }
        }

        Describe 'xWindowsFeature/Import-ServerManager' {
            $mockOperatingSystem = @{
                Name = 'mockOS'
                Version = '6.1.'
                OperatingSystemSKU = 10
            }
            Mock -CommandName Get-WmiObject -MockWith { return $mockOperatingSystem }

            It 'Should Not Throw' {
                Mock -CommandName Import-Module -MockWith {}
                { Import-ServerManager } | Should Not Throw
            }
            
            It 'Should Not Throw when exception is Identity Reference Runtime Exception' {
                $mockIdentityReferenceRuntimeException = New-Object -TypeName System.Management.Automation.RuntimeException -ArgumentList 'Some or all identity references could not be translated'
                Mock -CommandName Import-Module -MockWith { Throw $mockIdentityReferenceRuntimeException }

                { Import-ServerManager } | Should Not Throw
            }
            
            It 'Should throw invalid operation exception when exception is not Identity Reference Runtime Exception' {
                $mockOtherRuntimeException = New-Object -TypeName System.Management.Automation.RuntimeException -ArgumentList 'Other error'
                Mock -CommandName Import-Module -MockWith { Throw $mockOtherRuntimeException }

                { Import-ServerManager } | Should Throw ($script:localizedData.SkuNotSupported)
            }

            It 'Should throw invalid operation exception' {
                Mock -CommandName Import-Module -MockWith { Throw }

                { Import-ServerManager } | Should Throw ($script:localizedData.SkuNotSupported)
            }
        }
    }
}
finally
{
    Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}