Tests/Unit/MSFT_xScriptResource.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
# All tests with credentials will be skipped

$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName 'xPSDesiredStateConfiguration' `
    -DSCResourceName 'MSFT_xScriptResource' `
    -TestType Unit

InModuleScope 'MSFT_xScriptResource' {
    Describe 'xScript Unit Tests' {
        BeforeAll {
            Import-Module "$PSScriptRoot\..\CommonTestHelper.psm1"

            $script:skipAllCredentialTests = $true

            $script:originalErrorActionPreference = $ErrorActionPreference
            $ErrorActionPreference = 'Stop'
        }

        AfterAll {
            $ErrorActionPreference = $script:originalErrorActionPreference
        }
    
        It 'Get-TargetResource without credential' {
            $getScript = "@{ExecutionPolicy = Get-ExecutionPolicy; Date = Get-Date }"
            $setScript = "fakeSetScript"
            $testScript = "fakeTestScript"

            $getTargetResourceResultProperties = @('ExecutionPolicy', 'Date')
                 
            $getTargetResourceResult = Get-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript

            Test-GetTargetResourceResult -GetTargetResourceResult $getTargetResourceResult -GetTargetResourceResultProperties $getTargetResourceResultProperties
        }
    
        It 'Get-TargetResource with credential' -Skip:$script:skipAllCredentialTests {
            $credential = $null

            $getScript = "@{ExecutionPolicy = Get-ExecutionPolicy; Date = Get-Date }"
            $setScript = "fakeSetScript"
            $testScript = "fakeTestScript"

            $getTargetResourceResultProperties = @('ExecutionPolicy', 'Date')
                 
            $getTargetResourceResult = Get-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript -Credential $credential

            Test-GetTargetResourceResult -GetTargetResourceResult $getTargetResourceResult -GetTargetResourceResultProperties $getTargetResourceResultProperties
        }

        It 'Get-TargetResource with invalid result format from Get-Script' {
            $getScript = "'$true'"
            $setScript = "fakeSetScript"
            $testScript = "fakeTestScript"

            $getTargetResourceResultProperties = @('ExecutionPolicy', 'Date')
                 
            { Get-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript } | Should Throw
        }
    
        It 'Get-TargetResource with invalid command in Get-Script' {
            $getScript = "NonexistentCommand"
            $setScript = "fakeSetScript"
            $testScript = "fakeTestScript"

            $getTargetResourceResultProperties = @('ExecutionPolicy', 'Date')
                 
            { Get-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript } | Should Throw
        }

        It 'Set-TargetResource without credential' {
            $getScript = "fakeGetScript"
            $setScript = "'Executing Set-Script...'"
            $testScript = "fakeTestScript"

            $getTargetResourceResultProperties = @('ExecutionPolicy', 'Date')
                 
            { Set-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript } | Should Not Throw
        }

        It 'Set-TargetResource with credential' -Skip:$script:skipAllCredentialTests {
            $credential = $null

            $getScript = "fakeGetScript"
            $setScript = "'Executing Set-Script...'"
            $testScript = "fakeTestScript"

            $getTargetResourceResultProperties = @('ExecutionPolicy', 'Date')
                 
            { Set-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript -Credential $credential } | Should Not Throw
        }

        It 'Set-TargetResource with invalid command in Set-Script' {
            $getScript = "fakeGetScript"
            $setScript = "NonexistentCommand"
            $testScript = "fakeTestScript"

            $getTargetResourceResultProperties = @('ExecutionPolicy', 'Date')
                 
            { Set-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript } | Should Throw
        }

        It 'Test-TargetResource without credential' {
            $getScript = "fakeGetScript"
            $setScript = "fakeSetScript"
            $testScript = "'$true'"

            $testTargetResourceResult = Test-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript

            $testTargetResourceResult | Should Be $true
        }

        It 'Test-TargetResource with credential' -Skip:$script:skipAllCredentialTests {
            $credential = $null

            $getScript = "fakeGetScript"
            $setScript = "fakeSetScript"
            $testScript = "'$true'"

            $testTargetResourceResult = Test-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript -Credential $credential

            $testTargetResourceResult | Should Be $true
        }

        It 'Test-TargetResource with invalid command in Test-Script' {
            $getScript = "fakeGetScript"
            $setScript = "fakeSetScript"
            $testScript = "NonexistentCommand"

            { Test-TargetResource -GetScript $getScript -SetScript $setScript -TestScript $testScript } | Should Throw
        }
    }
}