Tests/Integration/MSFT_WindowsPackageCab.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
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'

$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_WindowsPackageCab' `
    -TestType 'Integration'

try
{
    Describe 'WindowsPackageCab Integration Tests' {
        BeforeAll {
            Import-Module -Name 'Dism'
            
            $script:installedStates = @( 'Installed', 'InstallPending' )
            $script:confgurationFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_WindowsPackageCab.config.ps1'

            $script:testPackageName = '' 
            $script:testSourcePath = Join-Path -Path $PSScriptRoot -ChildPath ''

            $script:cabPackageNotProvided = $script:testPackageName -eq [String]::Empty

            try
            {
                $originalPackage = Dism\Get-WindowsPackage -PackageName $script:testPackageName -Online
                if ($null -ne $originalPackage -and $originalPackage.PackageState -in $script:installedStates)
                {
                    $script:packageOriginallyInstalled = $true
                }
                else
                {
                    $script:packageOriginallyInstalled = $false
                }
            }
            catch
            {
                $script:packageOriginallyInstalled = $false
            }

            if ($script:packageOriginallyInstalled)
            {
                throw "Package $script:testPackageName is currently installed on this machine. These tests may destroy this package. Aborting."
            }
        }

        AfterEach {
            if (-not $script:packageOriginallyInstalled)
            {
                try
                {
                    $windowsPackage = Dism\Get-WindowsPackage -PackageName $script:testPackageName -Online 
                    if ($null -ne $windowsPackage -and $windowsPackage.PackageState -in $script:installedStates)
                    {
                        Dism\Remove-WindowsPackage -PackageName $script:testPackageName.Name -Online -NoRestart
                    }
                }
                catch
                {
                    Write-Verbose -Message "No test cleanup needed. Package $script:testPackageName not found."
                }
            }
        }
    
        It 'Should install a Windows package through a cab file' -Skip:$script:cabPackageNotProvided {
            $configurationName = 'InstallWindowsPackageCab'

            $resourceParameters = @{
                Name = $script:testPackageName
                SourcePath = $script:testSourcePath
                Ensure = 'Present'
            }

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

            { $windowsPackage = Dism\Get-WindowsPackage -PackageName $resourceParameters.Name -Online } | Should Not Throw

            $windowsPackage = Dism\Get-WindowsPackage -PackageName $resourceParameters.Name -Online
            $windowsPackage | Should Not Be $null
            $windowsPackage.PackageState -in $script:installedStates | Should Be $true
        }

        It 'Should uninstall a Windows package through a cab file' -Skip:$script:cabPackageNotProvided {
            $configurationName = 'UninstallWindowsPackageCab'

            $resourceParameters = @{
                Name = $script:testPackageName
                SourcePath = $script:testSourcePath
                Ensure = 'Absent'
            }

            Dism\Add-WindowsPackage -PackagePath $resourceParameters.SourcePath -Online -NoRestart

            { $windowsPackage = Dism\Get-WindowsPackage -PackageName $resourceParameters.Name -Online } | Should Not Throw

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

            { $windowsPackage = Dism\Get-WindowsPackage -PackageName $resourceParameters.Name -Online } | Should Throw
        }

        It 'Should not install an invalid Windows package through a cab file' {
            $configurationName = 'InstallInvalidWindowsPackageCab'

            $resourceParameters = @{
                Name = 'NonExistentWindowsPackageCab'
                SourcePath = (Join-Path -Path $TestDrive -ChildPath 'FakePath.cab')
                Ensure = 'Present'
                LogPath = (Join-Path -Path $TestDrive -ChildPath 'InvalidWindowsPackageCab.log')
            }

            { Dism\Get-WindowsPackage -PackageName $resourceParameters.Name -Online } | Should Throw

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

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

            { Dism\Get-WindowsPackage -PackageName $resourceParameters.Name -Online } | Should Throw
        }
    }
}
finally
{
    Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}