Tests/Integration/MSFT_xPackageResource.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
Import-Module "$PSScriptRoot\..\CommonTestHelper.psm1"

$script:testEnvironment = Enter-DscResourceTestEnvironment `
    -DscResourceModuleName 'xPSDesiredStateConfiguration' `
    -DscResourceName 'MSFT_xPackageResource' `
    -TestType 'Integration'
try
{
    Describe 'xPackage Integration Tests' {
        BeforeAll {
            Import-Module "$PSScriptRoot\..\MSFT_xPackageResource.TestHelper.psm1" -Force

            $script:testDirectoryPath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_xPackageResourceTests'

            if (Test-Path -Path $script:testDirectoryPath)
            {
                Remove-Item -Path $script:testDirectoryPath -Recurse -Force | Out-Null
            }

            New-Item -Path $script:testDirectoryPath -ItemType 'Directory' | Out-Null

            $script:msiName = 'DSCSetupProject.msi'
            $script:msiLocation = Join-Path -Path $script:testDirectoryPath -ChildPath $script:msiName

            $script:packageName = 'DSCUnitTestPackage'
            $script:packageId = '{deadbeef-80c6-41e6-a1b9-8bdb8a05027f}'

            New-TestMsi -DestinationPath $script:msiLocation | Out-Null

            Clear-PackageCache | Out-Null
        }

        BeforeEach {
            Clear-PackageCache | Out-Null

            if (Test-PackageInstalledByName -Name $script:packageName)
            {
                Start-Process -FilePath 'msiexec.exe' -ArgumentList @("/x$script:packageId", '/passive') -Wait | Out-Null
                Start-Sleep -Seconds 1 | Out-Null
            }

            if (Test-PackageInstalledByName -Name $script:packageName)
            {
                throw 'Package could not be removed.'
            }
        }

        AfterAll {
            if (Test-Path -Path $script:testDirectoryPath)
            {
                Remove-Item -Path $script:testDirectoryPath -Recurse -Force | Out-Null
            }

            Clear-PackageCache | Out-Null

            if (Test-PackageInstalledByName -Name $script:packageName)
            {
                Start-Process -FilePath 'msiexec.exe' -ArgumentList @("/x$script:packageId", '/passive') -Wait | Out-Null
                Start-Sleep -Seconds 1 | Out-Null
            }

            if (Test-PackageInstalledByName -Name $script:packageName)
            {
                throw 'Test output will not be valid - package could not be removed.'
            }
        }

        It 'Install a .msi package' {
            $configurationName = 'EnsurePackageIsPresent'
            $configurationPath = Join-Path -Path $TestDrive -ChildPath $configurationName
            $errorPath = Join-Path -Path $TestDrive -ChildPath 'StdErrorPath.txt'
            $outputPath = Join-Path -Path $TestDrive -ChildPath 'StdOutputPath.txt'

            try
            {
                $configurationScriptText = @"
                Configuration $configurationName
                {
                    Import-DscResource -ModuleName xPSDesiredStateConfiguration
 
                    xPackage Package1
                    {
                        Path = '$script:msiLocation'
                        Ensure = "Present"
                        Name = '$script:packageName'
                        ProductId = '$script:packageId'
                    }
                }
"@

                .([scriptblock]::Create($configurationScriptText))

                & $configurationName -OutputPath $configurationPath

                Start-DscConfiguration -Path $configurationPath -Wait -Force -Verbose

                Test-PackageInstalledByName -Name $script:packageName | Should Be $true
            }
            finally
            {
                if (Test-Path -Path $configurationPath)
                {
                    Remove-Item -Path $configurationPath -Recurse -Force
                }
            }
        }
    }
}
finally
{
    Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}