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

Describe "xProcessSet Integration Tests" {
    BeforeAll {
        Import-Module "$PSScriptRoot\..\Unit\MSFT_xProcessResource.TestHelper.psm1" -Force

        $script:cmdProcess1ShortName = 'ProcessTest1'
        $script:cmdProcess1FullName = 'ProcessTest1.exe'
        $script:cmdProcess1FullPath = "$env:WinDir\system32\ProcessTest1.exe"
        Copy-Item "$env:WinDir\system32\cmd.exe" $script:cmdProcess1FullPath -Force -ErrorAction SilentlyContinue

        $script:cmdProcess2ShortName = 'ProcessTest2'
        $script:cmdProcess2FullName = 'ProcessTest2.exe'
        $script:cmdProcess2FullPath = "$env:WinDir\system32\ProcessTest2.exe"
        Copy-Item "$env:WinDir\system32\cmd.exe" $script:cmdProcess2FullPath -Force -ErrorAction SilentlyContinue
    }

    AfterEach {
        Stop-ProcessByName -ProcessName $script:cmdProcess1ShortName
        Stop-ProcessByName -ProcessName $script:cmdProcess2ShortName
    }

    AfterAll {
        Remove-Item $script:cmdProcess1FullPath -ErrorAction SilentlyContinue
        Remove-Item $script:cmdProcess2FullPath -ErrorAction SilentlyContinue
    }

    It "Ensure a set of processes is present" {
        $configurationName = "EnsureProcessIsPresent"
        $configurationPath = Join-Path -Path (Get-Location) -ChildPath $configurationName
        $errorPath = Join-Path -Path (Get-Location) -ChildPath "StdErrorPath.txt"
        $outputPath = Join-Path -Path (Get-Location) -ChildPath "StdOutputPath.txt"

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

                xProcessSet ProcessSet1
                {
                    Path = @($script:cmdProcess1FullPath, $script:cmdProcess2FullPath)
                    Ensure = "Present"
                    StandardErrorPath = $errorPath
                    StandardOutputPath = $outputPath
                }
            }

            & $configurationName -OutputPath $configurationPath

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

            $process1 = Get-Process $script:cmdProcess1ShortName
            $process1 | Should Not Be $null

            $process2 = Get-Process $script:cmdProcess2ShortName
            $process2 | Should Not Be $null
        }
        finally
        {
            if (Test-Path -Path $configurationPath)
            {
                Remove-Item -Path $configurationPath -Recurse -Force
            }
        }
    }

    It "Ensure a set of processes is absent" {
        $configurationName = "EnsureProcessIsAbsent"
        $configurationPath = Join-Path -Path (Get-Location) -ChildPath $configurationName

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

                xProcessSet ProcessSet1
                {
                    Path = @($script:cmdProcess1FullPath, $script:cmdProcess2FullPath)
                    Ensure = "Absent"
                }
            }

            & $configurationName -OutputPath $configurationPath

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

            $process1 = Get-Process $script:cmdProcess1ShortName -ErrorAction SilentlyContinue
            $process1 | Should Be $null

            $process2 = Get-Process $script:cmdProcess2ShortName -ErrorAction SilentlyContinue
            $process2 | Should Be $null

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