|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +BeforeDiscovery { |
| 5 | + $testCases = if ($IsWindows) { |
| 6 | + @( |
| 7 | + @{ |
| 8 | + resourceType = 'Microsoft.DSC.Transitional/PowerShellScript' |
| 9 | + } |
| 10 | + @{ |
| 11 | + resourceType = 'Microsoft.DSC.Transitional/WindowsPowerShellScript' |
| 12 | + } |
| 13 | + ) |
| 14 | + } else { |
| 15 | + @( |
| 16 | + @{ |
| 17 | + resourceType = 'Microsoft.DSC.Transitional/PowerShellScript' |
| 18 | + } |
| 19 | + ) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +Describe 'Tests for PSScript resource' { |
| 24 | + It 'Get operation returns the script content for <resourceType>' -TestCases $testCases { |
| 25 | + param($resourceType) |
| 26 | + |
| 27 | + $yaml = @" |
| 28 | + GetScript: | |
| 29 | + "Hello, World!" |
| 30 | + 1+1 |
| 31 | + SetScript: | |
| 32 | + throw 'This should not be executed' |
| 33 | +"@ |
| 34 | + $result = dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 35 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 36 | + $result.actualState.output.Count | Should -Be 2 -Because ($result | ConvertTo-Json | Out-String) |
| 37 | + $result.actualState.output[0] | Should -BeExactly "Hello, World!" |
| 38 | + $result.actualState.output[1] | Should -BeExactly 2 |
| 39 | + } |
| 40 | + |
| 41 | + It 'Set operation executes the script for <resourceType>' -TestCases $testCases { |
| 42 | + param($resourceType) |
| 43 | + |
| 44 | + $yaml = @" |
| 45 | + SetScript: | |
| 46 | + "Hello, World!" |
| 47 | + 1+1 |
| 48 | +"@ |
| 49 | + $result = dsc resource set -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 50 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 51 | + $result.afterState.output.Count | Should -Be 2 -Because ($result | ConvertTo-Json | Out-String) |
| 52 | + $result.afterState.output[0] | Should -BeExactly "Hello, World!" |
| 53 | + $result.afterState.output[1] | Should -BeExactly 2 |
| 54 | + } |
| 55 | + |
| 56 | + It 'Get w/ Set operation succeeds for <resourceType>' -TestCases $testCases { |
| 57 | + param($resourceType) |
| 58 | + |
| 59 | + $yaml = @" |
| 60 | + GetScript: | |
| 61 | + "Hello, World!" |
| 62 | + 1+1 |
| 63 | + SetScript: | |
| 64 | + "Hello, World!" |
| 65 | + 2+2 |
| 66 | +"@ |
| 67 | + $result = dsc resource set -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 68 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 69 | + $result.beforeState.output.Count | Should -Be 2 -Because ($result | ConvertTo-Json | Out-String) |
| 70 | + $result.beforeState.output[0] | Should -BeExactly "Hello, World!" |
| 71 | + $result.beforeState.output[1] | Should -BeExactly 2 |
| 72 | + $result.afterState.output.Count | Should -Be 2 |
| 73 | + $result.afterState.output[0] | Should -BeExactly "Hello, World!" |
| 74 | + $result.afterState.output[1] | Should -BeExactly 4 |
| 75 | + } |
| 76 | + |
| 77 | + It 'Test operation returns in desired state for <resourceType>' -TestCases $testCases { |
| 78 | + param($resourceType) |
| 79 | + |
| 80 | + $yaml = @' |
| 81 | + TestScript: | |
| 82 | + $true |
| 83 | +'@ |
| 84 | + $result = dsc resource test -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 85 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 86 | + $result.InDesiredState | Should -BeTrue |
| 87 | + } |
| 88 | + |
| 89 | + It 'Test operation returns not in desired state for <resourceType>' -TestCases $testCases { |
| 90 | + param($resourceType) |
| 91 | + |
| 92 | + $yaml = @' |
| 93 | + TestScript: | |
| 94 | + $false |
| 95 | +'@ |
| 96 | + $result = dsc resource test -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 97 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 98 | + $result.InDesiredState | Should -BeFalse |
| 99 | + } |
| 100 | + |
| 101 | + It 'Test operation returns error for non-boolean result for <resourceType>' -TestCases $testCases { |
| 102 | + param($resourceType) |
| 103 | + |
| 104 | + $yaml = @' |
| 105 | + TestScript: | |
| 106 | + "This is not a boolean" |
| 107 | +'@ |
| 108 | + $result = dsc resource test -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 109 | + $LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 110 | + $result | Should -BeNullOrEmpty -Because "Test operation should return an error" |
| 111 | + (Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*Test operation did not return a single boolean value.*' |
| 112 | + } |
| 113 | + |
| 114 | + It 'Test operation returns error for multiple boolean results for <resourceType>' -TestCases $testCases { |
| 115 | + param($resourceType) |
| 116 | + |
| 117 | + $yaml = @' |
| 118 | + TestScript: | |
| 119 | + $true |
| 120 | + $false |
| 121 | +'@ |
| 122 | + $result = dsc resource test -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 123 | + $LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 124 | + $result | Should -BeNullOrEmpty -Because "Test operation should return an error" |
| 125 | + (Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*Test operation did not return a single boolean value.*' |
| 126 | + } |
| 127 | + |
| 128 | + It 'Empty SetScript is ignored for <resourceType>' -TestCases $testCases { |
| 129 | + param($resourceType) |
| 130 | + |
| 131 | + $yaml = @' |
| 132 | + GetScript: | |
| 133 | + "Hello, World!" |
| 134 | + 1+1 |
| 135 | + TestScript: | |
| 136 | + $true |
| 137 | +'@ |
| 138 | + |
| 139 | + $result = dsc resource set -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 140 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 141 | + $result.beforeState.output.Count | Should -Be 2 -Because ($result | ConvertTo-Json | Out-String) |
| 142 | + $result.beforeState.output[0] | Should -BeExactly "Hello, World!" |
| 143 | + $result.beforeState.output[1] | Should -BeExactly 2 |
| 144 | + $result.afterState.output.Count | Should -Be 0 |
| 145 | + } |
| 146 | + |
| 147 | + It 'Empty GetScript is ignored for <resourceType>' -TestCases $testCases { |
| 148 | + param($resourceType) |
| 149 | + |
| 150 | + $yaml = @' |
| 151 | + SetScript: | |
| 152 | + "Hello, World!" |
| 153 | + 1+1 |
| 154 | + TestScript: | |
| 155 | + $true |
| 156 | +'@ |
| 157 | + $result = dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 158 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 159 | + $result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String) |
| 160 | + } |
| 161 | + |
| 162 | + It 'Empty TestScript is ignored for <resourceType>' -TestCases $testCases { |
| 163 | + param($resourceType) |
| 164 | + |
| 165 | + $yaml = @' |
| 166 | + GetScript: | |
| 167 | + "Hello, World!" |
| 168 | + 1+1 |
| 169 | + SetScript: | |
| 170 | + "Hello, World!" |
| 171 | + 2+2 |
| 172 | +'@ |
| 173 | + $result = dsc resource test -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json |
| 174 | + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String) |
| 175 | + $result.InDesiredState | Should -BeTrue |
| 176 | + } |
| 177 | +} |
0 commit comments