Tests/Unit/MSFT_xWindowsOptionalFeature.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
$Global:DSCModuleName      = 'xPSDesiredStateConfiguration'
$Global:DSCResourceName    = 'MSFT_xWindowsOptionalFeature'

#region HEADER
if ( (-not (Test-Path -Path '.\DSCResource.Tests\')) -or `
     (-not (Test-Path -Path '.\DSCResource.Tests\TestHelper.psm1')) )
{
    & git @('clone','https://github.com/PowerShell/DscResource.Tests.git')
}
else
{
    & git @('-C',(Join-Path -Path (Get-Location) -ChildPath '\DSCResource.Tests\'),'pull')
}
Import-Module .\DSCResource.Tests\TestHelper.psm1 -Force
$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $Global:DSCModuleName `
    -DSCResourceName $Global:DSCResourceName `
    -TestType Unit 
#endregion

# Begin Testing
try
{

    #region Pester Tests

    InModuleScope $Global:DSCResourceName {

        $testFeatureName = 'TestFeature';
        $fakeEnabledFeature = [PSCustomObject] @{ Name = $testFeatureName; State = 'Enabled'; }
        $fakeDisabledFeature = [PSCustomObject] @{ Name = $testFeatureName; State = 'Disabled'; }
        
        Describe "$($Global:DSCResourceName)\ConvertStateToEnsure" {    

            It 'Returns "Present" when state is "Enabled"' {
                ConvertStateToEnsure -State 'Enabled' | Should Be 'Present';
            }

            It 'Returns "Absent" when state is "Disabled"' {
                ConvertStateToEnsure -State 'Disabled' | Should Be 'Absent';
            }

        } #end Describe ConvertStateToEnsure

        Describe "$($Global:DSCResourceName)\ValidatePrerequisites" {    

            $fakeWindows7 = [PSCustomObject] @{ ProductType = 1; BuildNumber = 7601; }
            $fakeServer2008R2 = [PSCustomObject] @{ ProductType = 2; BuildNumber = 7601; }
            $fakeServer2012 = [PSCustomObject] @{ ProductType = 2; BuildNumber = 9200; }
            $fakeWindows81 = [PSCustomObject] @{ ProductType = 1; BuildNumber = 9600; }
            $fakeServer2012R2 = [PSCustomObject] @{ ProductType = 2; BuildNumber = 9600; }

            It 'Throws when server operating system is Server 2008 R2' {
                Mock Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } -MockWith { return $fakeServer2008R2; }
                Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { }

                { ValidatePrerequisites } | Should Throw;
            }

            It 'Throws when server operating system is Server 2012' {
                Mock Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } -MockWith { return $fakeServer2012; }
                Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { }

                { ValidatePrerequisites } | Should Throw;
            }

            It 'Throws when DISM module is not available' {
                Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { Write-Error 'Cannot find module'; }

                { ValidatePrerequisites } | Should Throw;
            }

            It 'Does not throw when desktop operating system is Windows 7' {
                Mock Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } -MockWith { return $fakeWindows7; }
                Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { }

                { ValidatePrerequisites } | Should Not Throw;
            }

            It 'Does not throw when desktop operating system is Windows 8.1' {
                Mock Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } -MockWith { return $fakeWindows81; }
                Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { }

                { ValidatePrerequisites } | Should Not Throw;
            }

            It 'Does not throw when server operating system is Server 2012 R2' {
                Mock Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } -MockWith { return $fakeServer2012R2; }
                Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { }

                { ValidatePrerequisites } | Should Not Throw;
            }

        } #end Describe ValidatePrerequisites

        Describe "$($Global:DSCResourceName)\Get-TargetResource" {
            
            It 'Returns [System.Collections.Hashtable] object type' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeEnabledFeature; }

                $targetResource = Get-TargetResource -Name $testFeatureName;

                $targetResource -is [System.Collections.Hashtable] | Should Be $true;
            }

            It 'Calls "ValidateProperties" method' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeEnabledFeature; }

                $targetResource = Get-TargetResource -Name $testFeatureName;

                Assert-MockCalled Dism\Get-WindowsOptionalFeature -ParameterFilter { $FeatureName -eq $testFeatureName } -Scope It;
            }

            It 'Returns "Present" when optional feature is enabled' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeEnabledFeature; }

                $targetResource = Get-TargetResource -Name $testFeatureName;

                $targetResource.Ensure | Should Be 'Present';
            }

            It 'Returns "Absent" when optional feature is enabled' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeDisabledFeature; }

                $targetResource = Get-TargetResource -Name $testFeatureName;

                $targetResource.Ensure | Should Be 'Absent';
            }
        
        } #end Describe Get-TargetResource

        Describe "$($Global:DSCResourceName)\Test-TargetResource" {

            It 'Returns a "[System.Boolean]" object type' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeEnabledFeature; }

                $targetResource = Test-TargetResource -Name $testFeatureName;

                $targetResource -is [System.Boolean] | Should Be $true;
            }

            It 'Returns false when optional feature is not available and "Ensure" = "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { }

                $targetResource = Test-TargetResource -Name $testFeatureName -Ensure Present;

                $targetResource | Should Be $false;
            }
            
            It 'Returns false when optional feature is disabled and "Ensure" = "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeDisabledFeature; }

                $targetResource = Test-TargetResource -Name $testFeatureName -Ensure Present;

                $targetResource | Should Be $false;
            }

            It 'Returns true when optional feature is enabled and "Ensure" = "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeEnabledFeature; }

                $targetResource = Test-TargetResource -Name $testFeatureName -Ensure Present;

                $targetResource | Should Be $true;
            }

            It 'Returns true when optional feature is not available and "Ensure" = "Absent"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { }

                $targetResource = Test-TargetResource -Name $testFeatureName -Ensure Absent;

                $targetResource | Should Be $true;
            }

            It 'Returns true when optional feature is disabled and "Ensure" = "Absent"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeDisabledFeature; }

                $targetResource = Test-TargetResource -Name $testFeatureName -Ensure Absent;

                $targetResource | Should Be $true;
            }

            It 'Returns false when optional feature is enabled and "Ensure" = "Absent"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $testFeatureName } -MockWith { return $fakeEnabledFeature; }

                $targetResource = Test-TargetResource -Name $testFeatureName -Ensure Absent;

                $targetResource | Should Be $false;
            }

        } #end Describe Test-TargetResource

        Describe "$($Global:DSCResourceName)\Set-TargetResource" {
        
            It 'Calls "Enable-WindowsOptionalFeature" with default "NoRestart" when "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Enable-WindowsOptionalFeature -ParameterFilter { $NoRestart -eq $true } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName;

                Assert-MockCalled Dism\Enable-WindowsOptionalFeature -ParameterFilter { $NoRestart -eq $true } -Scope It
            }

            It 'Calls "Enable-WindowsOptionalFeature" with "Online" when "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Enable-WindowsOptionalFeature -ParameterFilter { $Online -eq $true } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName;

                Assert-MockCalled Dism\Enable-WindowsOptionalFeature -ParameterFilter { $Online -eq $true } -Scope It
            }

            It 'Calls "Enable-WindowsOptionalFeature" with default "WarningsInfo" logging level when "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'WarningsInfo' } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName

                Assert-MockCalled Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'WarningsInfo' } -Scope It
            }

            It 'Calls "Enable-WindowsOptionalFeature" with "Errors" logging level when "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'Errors' } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName -LogLevel ErrorsOnly

                Assert-MockCalled Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'Errors' } -Scope It
            }

            It 'Calls "Enable-WindowsOptionalFeature" with "Warnings" logging level when "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'Warnings' } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName -LogLevel ErrorsAndWarning

                Assert-MockCalled Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'Warnings' } -Scope It
            }
            
            It 'Calls "Enable-WindowsOptionalFeature" without "LimitAccess" by default when "Present"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LimitAccess -eq $null } -MockWith { }

                Set-TargetResource -Name $testFeatureName

                Assert-MockCalled Dism\Enable-WindowsOptionalFeature -ParameterFilter  { $LimitAccess -eq $null } -Scope It
            }
            
            It 'Calls "Enable-WindowsOptionalFeature" with "LimitAccess" when NoWindowsUpdateCheck is specified' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Enable-WindowsOptionalFeature -ParameterFilter { $LimitAccess -eq $true } -MockWith { }

                Set-TargetResource -Name $testFeatureName -NoWindowsUpdateCheck $true

                Assert-MockCalled Dism\Enable-WindowsOptionalFeature -ParameterFilter  { $LimitAccess -eq $true } -Scope It
            }

            It 'Calls "Disable-WindowsOptionalFeature" with default "WarningsInfo" logging level when "Absent"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Disable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'WarningsInfo' } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName -Ensure Absent;

                Assert-MockCalled Dism\Disable-WindowsOptionalFeature -ParameterFilter { $LogLevel -eq 'WarningsInfo' } -Scope It
            }

            It 'Calls "Disable-WindowsOptionalFeature" with "NoRestart" when "Absent"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Disable-WindowsOptionalFeature -ParameterFilter { $NoRestart -eq $true } -MockWith { }

                Set-TargetResource -Name $testFeatureName -Ensure Absent;

                Assert-MockCalled Dism\Disable-WindowsOptionalFeature -ParameterFilter  { $NoRestart -eq $true } -Scope It
            }

            It 'Calls "Disable-WindowsOptionalFeature" with "Online" when "Absent"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Disable-WindowsOptionalFeature -ParameterFilter { $Online -eq $true } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName -Ensure Absent;

                Assert-MockCalled Dism\Disable-WindowsOptionalFeature -ParameterFilter { $Online -eq $true } -Scope It
            }

            It 'Calls "Disable-WindowsOptionalFeature" with "Remove" when "Absent" and "RemoveFilesOnDisable"' {
                Mock ValidatePrerequisites -MockWith { }
                Mock Dism\Disable-WindowsOptionalFeature -ParameterFilter { $Remove -eq $true } -MockWith { }
                
                Set-TargetResource -Name $testFeatureName -Ensure Absent -RemoveFilesOnDisable $true;

                Assert-MockCalled Dism\Disable-WindowsOptionalFeature -ParameterFilter { $Remove -eq $true } -Scope It
            }

        } #end Describe Set-TargetResource

    } #end InModuleScope $DSCResourceName
    #endregion
}
finally
{
    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}