Tests/Integration/MSFT_xWindowsOptionalFeatureSet.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
$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName 'xPSDesiredStateConfiguration' `
    -DSCResourceName 'MSFT_xWindowsOptionalFeatureSet' `
    -TestType Integration

Describe "xWindowsOptionalFeatureSet Integration Tests" {
    It "Install two valid Windows optional features" {
        $configurationName = "InstallOptionalFeature"
        $configurationPath = Join-Path -Path (Get-Location) -ChildPath $configurationName
        $logPath = Join-Path -Path (Get-Location) -ChildPath 'InstallOptionalFeatureLog'

        $validFeatureName1 = 'MicrosoftWindowsPowerShellV2'
        $validFeatureName2 = 'Internet-Explorer-Optional-amd64'

        $originalFeature1 = Dism\Get-WindowsOptionalFeature -Online -FeatureName $validFeatureName1
        $originalFeature2 = Dism\Get-WindowsOptionalFeature -Online -FeatureName $validFeatureName2

        try
        {
            Configuration $configurationName
            {
                Import-DscResource -ModuleName xPSDesiredStateConfiguration

                xWindowsOptionalFeatureSet WindowsOptionalFeatureSet1
                {
                    Name = @($validFeatureName1, $validFeatureName2)
                    Ensure = "Present"
                    LogPath = $logPath
                    NoWindowsUpdateCheck = $true
                }
            }

            { & $configurationName -OutputPath $configurationPath } | Should Not Throw

            { Start-DscConfiguration -Path $configurationPath -Wait -Force -Verbose } | Should Not Throw

            $windowsOptionalFeature1 = Dism\Get-WindowsOptionalFeature -Online -FeatureName $validFeatureName1

            $windowsOptionalFeature1 | Should Not Be $null
            $windowsOptionalFeature1.State -eq 'Enabled' -or $windowsOptionalFeature1.State -eq 'EnablePending' | Should Be $true

            $windowsOptionalFeature2 = Dism\Get-WindowsOptionalFeature -Online -FeatureName $validFeatureName2

            $windowsOptionalFeature2 | Should Not Be $null
            $windowsOptionalFeature2.State -eq 'Enabled' -or $windowsOptionalFeature2.State -eq 'EnablePending' | Should Be $true
        }
        finally
        {
            if ($originalFeature1.State -eq 'Disabled' -or $originalFeature1.State -eq 'DisablePending')
            {
                Dism\Disable-WindowsOptionalFeature -Online -FeatureName $validFeatureName1 -NoRestart
            }

            if ($originalFeature2.State -eq 'Disabled' -or $originalFeature2.State -eq 'DisablePending')
            {
                Dism\Disable-WindowsOptionalFeature -Online -FeatureName $validFeatureName2 -NoRestart
            }

            if (Test-Path -Path $logPath) {
                Remove-Item -Path $logPath -Recurse -Force
            }

            if (Test-Path -Path $configurationPath)
            {
                Remove-Item -Path $configurationPath -Recurse -Force
            }
        }
    }

    It "Install an incorrect Windows optional feature" {
        $configurationName = "InstallIncorrectWindowsFeature"
        $configurationPath = Join-Path -Path (Get-Location) -ChildPath $configurationName
        $logPath = Join-Path -Path (Get-Location) -ChildPath 'InstallIncorrectWindowsFeatureLog'

        try
        {
            Configuration $configurationName
            {
                Import-DscResource -ModuleName xPSDesiredStateConfiguration

                xWindowsOptionalFeatureSet feature1
                {
                    Name = @("NonExistentWindowsOptionalFeature")
                    Ensure = "Present"
                    LogPath = $logPath
                }
            }

            { & $configurationName -OutputPath $configurationPath } | Should Not Throw

            # This should not work. LCM is expected to print errors, but the call to this function itself should not throw errors.
            { Start-DscConfiguration -Path $configurationPath -Wait -Force -ErrorAction SilentlyContinue } | Should Not Throw

            Test-Path -Path $logPath | Should Be $true
        }
        finally
        {
            if (Test-Path -Path $logPath)
            {
                Remove-Item -Path $logPath -Recurse -Force
            }

            if (Test-Path -Path $configurationPath)
            {
                Remove-Item -Path $configurationPath -Recurse -Force
            }
        }
    }
}