Tests/Integration/MSFT_xWindowsOptionalFeature.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
Import-Module -Name (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'CommonTestHelper.psm1')

$script:testEnvironment = Enter-DscResourceTestEnvironment `
    -DscResourceModuleName 'xPSDesiredStateConfiguration' `
    -DscResourceName 'MSFT_xWindowsOptionalFeature' `
    -TestType 'Integration'

try
{
    Describe 'xWindowsOptionalFeature Integration Tests' {
        BeforeAll {
            $script:enabledStates = @( 'Enabled', 'EnablePending' )
            $script:disabledStates = @( 'Disabled', 'DisablePending' )

            $script:confgurationFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_xWindowsOptionalFeature.config.ps1'
        }

        It 'Should enable a valid Windows optional feature' {
            $configurationName = 'EnableWindowsOptionalFeature'

            $resourceParameters = @{
                Name = 'TelnetClient'
                Ensure = 'Present'
                LogPath = Join-Path -Path $TestDrive -ChildPath 'EnableOptionalFeature.log'
                NoWindowsUpdateCheck = $true
            }

            $originalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online

            try
            {
                if ($originalFeature.State -in $script:enabledStates)
                {
                    Dism\Disable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart
                }

                {
                    . $script:confgurationFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw

                $windowsOptionalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online

                $windowsOptionalFeature | Should Not Be $null
                $windowsOptionalFeature.State -in $script:enabledStates | Should Be $true
            }
            finally
            {
                if ($originalFeature.State -in $script:disabledStates)
                {
                    Dism\Disable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart
                }
                elseif ($originalFeature.State -in $script:enabledStates)
                {
                    Dism\Enable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart
                }

                if (Test-Path -Path $resourceParameters.LogPath)
                {
                    Remove-Item -Path $resourceParameters.LogPath -Recurse -Force
                }
            }
        }

        It 'Should disable a valid Windows optional feature' {
            $configurationName = 'DisableWindowsOptionalFeature'

            $resourceParameters = @{
                Name = 'TelnetClient'
                Ensure = 'Absent'
                LogPath = Join-Path -Path $TestDrive -ChildPath 'DisableOptionalFeature.log'
                NoWindowsUpdateCheck = $true
                RemoveFilesOnDisable = $false
            }

            $originalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online

            try
            {
                if ($originalFeature.State -in $script:disabledStates)
                {
                    Dism\Enable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart
                }

                {
                    . $script:confgurationFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Not Throw

                $windowsOptionalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online

                $windowsOptionalFeature | Should Not Be $null
                $windowsOptionalFeature.State -in $script:disabledStates | Should Be $true
            }
            finally
            {
                if ($originalFeature.State -in $script:disabledStates)
                {
                    Dism\Disable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart
                }
                elseif ($originalFeature.State -in $script:enabledStates)
                {
                    Dism\Enable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart
                }

                if (Test-Path -Path $resourceParameters.LogPath)
                {
                    Remove-Item -Path $resourceParameters.LogPath -Recurse -Force
                }
            }
        }

        It 'Should not enable an incorrect Windows optional feature' {
            $configurationName = 'EnableIncorrectWindowsOptionalFeature'

            $resourceParameters = @{
                Name = 'NonExistentWindowsOptionalFeature'
                Ensure = 'Present'
                LogPath = Join-Path -Path $TestDrive -ChildPath 'EnableIncorrectWindowsFeature.log'
                NoWindowsUpdateCheck = $true
            }

            Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online | Should Be $null

            try
            {
                {
                    . $script:confgurationFilePath -ConfigurationName $configurationName
                    & $configurationName -OutputPath $TestDrive @resourceParameters
                    Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
                } | Should Throw "Feature name $($resourceParameters.Name) is unknown."

                Test-Path -Path $resourceParameters.LogPath | Should Be $true

                Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online | Should Be $null
            }
            finally
            {
                if (Test-Path -Path $resourceParameters.LogPath)
                {
                    Remove-Item -Path $resourceParameters.LogPath -Recurse -Force
                }
            }
        }
    }
}
finally
{
    Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}