|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe 'Parameters tests' { |
| 5 | + It 'Input can be provided as <inputType>' -TestCases @( |
| 6 | + @{ inputType = 'string' } |
| 7 | + @{ inputType = 'file' } |
| 8 | + ) { |
| 9 | + param($inputType) |
| 10 | + |
| 11 | + $config_yaml = @" |
| 12 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 13 | + parameters: |
| 14 | + param1: |
| 15 | + type: string |
| 16 | + resources: |
| 17 | + - name: Echo |
| 18 | + type: Test/Echo |
| 19 | + properties: |
| 20 | + text: '[parameters(''param1'')]' |
| 21 | +"@ |
| 22 | + $params_json = @{ parameters = @{ param1 = 'hello' }} | ConvertTo-Json |
| 23 | + |
| 24 | + if ($inputType -eq 'file') { |
| 25 | + $file_path = "$TestDrive/test.parameters.json" |
| 26 | + Set-Content -Path $file_path -Value $params_json |
| 27 | + $out = $config_yaml | dsc config -f $file_path get | ConvertFrom-Json |
| 28 | + } |
| 29 | + else { |
| 30 | + $out = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json |
| 31 | + } |
| 32 | + |
| 33 | + $LASTEXITCODE | Should -Be 0 |
| 34 | + $out.results[0].result.actualState.text | Should -BeExactly '"hello"' |
| 35 | + } |
| 36 | + |
| 37 | + It 'Input is <type>' -TestCases @( |
| 38 | + @{ type = 'string'; value = 'hello'; expected = '"hello"' } |
| 39 | + @{ type = 'int'; value = 42; expected = 42 } |
| 40 | + @{ type = 'bool'; value = $true; expected = $true } |
| 41 | + @{ type = 'array'; value = @('hello', 'world'); expected = '["hello","world"]' } |
| 42 | + ) { |
| 43 | + param($type, $value, $expected) |
| 44 | + |
| 45 | + $config_yaml = @" |
| 46 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 47 | + parameters: |
| 48 | + param1: |
| 49 | + type: $type |
| 50 | + resources: |
| 51 | + - name: Echo |
| 52 | + type: Test/Echo |
| 53 | + properties: |
| 54 | + text: '[parameters(''param1'')]' |
| 55 | +"@ |
| 56 | + $params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json |
| 57 | + |
| 58 | + $out = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json |
| 59 | + $LASTEXITCODE | Should -Be 0 |
| 60 | + $out.results[0].result.actualState.text | Should -BeExactly $expected |
| 61 | + } |
| 62 | + |
| 63 | + It 'Input is incorrect type <type>' -TestCases @( |
| 64 | + @{ type = 'string'; value = 42 } |
| 65 | + @{ type = 'int'; value = 'hello' } |
| 66 | + @{ type = 'bool'; value = 'hello' } |
| 67 | + @{ type = 'array'; value = 'hello' } |
| 68 | + ) { |
| 69 | + param($type, $value) |
| 70 | + |
| 71 | + $config_yaml = @" |
| 72 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 73 | + parameters: |
| 74 | + param1: |
| 75 | + type: $type |
| 76 | + resources: |
| 77 | + - name: Echo |
| 78 | + type: Test/Echo |
| 79 | + properties: |
| 80 | + text: '[parameters(''param1'')]' |
| 81 | +"@ |
| 82 | + $params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json |
| 83 | + |
| 84 | + $null = $config_yaml | dsc config -p $params_json get |
| 85 | + $LASTEXITCODE | Should -Be 4 |
| 86 | + } |
| 87 | + |
| 88 | + It 'Input length is wrong for <type>' -TestCases @( |
| 89 | + @{ type = 'string'; value = 'hi' } |
| 90 | + @{ type = 'string'; value = 'hello' } |
| 91 | + @{ type = 'array'; value = @('hello', 'there') } |
| 92 | + @{ type = 'array'; value = @('hello', 'there', 'bye', 'now') } |
| 93 | + ) { |
| 94 | + param($type, $value) |
| 95 | + |
| 96 | + $config_yaml = @" |
| 97 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 98 | + parameters: |
| 99 | + param1: |
| 100 | + type: $type |
| 101 | + minLength: 3 |
| 102 | + maxLength: 3 |
| 103 | + resources: |
| 104 | + - name: Echo |
| 105 | + type: Test/Echo |
| 106 | + properties: |
| 107 | + text: '[parameters(''param1'')]' |
| 108 | +"@ |
| 109 | + $params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json |
| 110 | + |
| 111 | + $null = $config_yaml | dsc config -p $params_json get |
| 112 | + $LASTEXITCODE | Should -Be 4 |
| 113 | + } |
| 114 | + |
| 115 | + It 'Input number value is out of range for <min> and <max>' -TestCases @( |
| 116 | + @{ value = 42; min = 43; max = 44 } |
| 117 | + @{ value = 42; min = 41; max = 41 } |
| 118 | + @{ value = 42; min = 43; max = 41 } |
| 119 | + ) { |
| 120 | + param($type, $value, $min, $max) |
| 121 | + |
| 122 | + $config_yaml = @" |
| 123 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 124 | + parameters: |
| 125 | + param1: |
| 126 | + type: int |
| 127 | + minValue: $min |
| 128 | + maxValue: $max |
| 129 | + resources: |
| 130 | + - name: Echo |
| 131 | + type: Test/Echo |
| 132 | + properties: |
| 133 | + text: '[parameters(''param1'')]' |
| 134 | +"@ |
| 135 | + $params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json |
| 136 | + |
| 137 | + $null = $config_yaml | dsc config -p $params_json get |
| 138 | + $LASTEXITCODE | Should -Be 4 |
| 139 | + } |
| 140 | + |
| 141 | + It 'Input is not in the allowed value list for <type>' -TestCases @( |
| 142 | + @{ type = 'string'; value = 'hello'; allowed = @('world', 'planet') } |
| 143 | + @{ type = 'int'; value = 42; allowed = @(43, 44) } |
| 144 | + ) { |
| 145 | + param($type, $value, $allowed) |
| 146 | + |
| 147 | + $config_yaml = @" |
| 148 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 149 | + parameters: |
| 150 | + param1: |
| 151 | + type: $type |
| 152 | + allowedValues: $($allowed | ConvertTo-Json -Compress) |
| 153 | + resources: |
| 154 | + - name: Echo |
| 155 | + type: Test/Echo |
| 156 | + properties: |
| 157 | + text: '[parameters(''param1'')]' |
| 158 | +"@ |
| 159 | + $params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json |
| 160 | + |
| 161 | + $null = $config_yaml | dsc config -p $params_json get |
| 162 | + $LASTEXITCODE | Should -Be 4 |
| 163 | + } |
| 164 | + |
| 165 | + It 'Length constraint is incorrectly applied to <type> with <constraint>' -TestCases @( |
| 166 | + @{ type = 'int'; value = 42; constraint = 'minLength' } |
| 167 | + @{ type = 'int'; value = 42; constraint = 'maxLength' } |
| 168 | + @{ type = 'bool'; value = $true; constraint = 'minLength' } |
| 169 | + @{ type = 'bool'; value = $true; constraint = 'maxLength' } |
| 170 | + ) { |
| 171 | + param($type, $value, $constraint) |
| 172 | + |
| 173 | + $config_yaml = @" |
| 174 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 175 | + parameters: |
| 176 | + param1: |
| 177 | + type: $type |
| 178 | + ${constraint}: 3 |
| 179 | + resources: |
| 180 | + - name: Echo |
| 181 | + type: Test/Echo |
| 182 | + properties: |
| 183 | + text: '[parameters(''param1'')]' |
| 184 | +"@ |
| 185 | + $params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json |
| 186 | + |
| 187 | + $null = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json |
| 188 | + $LASTEXITCODE | Should -Be 4 |
| 189 | + } |
| 190 | + |
| 191 | + It 'Default value is used when not provided' { |
| 192 | + $config_yaml = @" |
| 193 | + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json |
| 194 | + parameters: |
| 195 | + param1: |
| 196 | + type: string |
| 197 | + defaultValue: 'hello' |
| 198 | + param2: |
| 199 | + type: int |
| 200 | + defaultValue: 7 |
| 201 | + param3: |
| 202 | + type: bool |
| 203 | + defaultValue: false |
| 204 | + param4: |
| 205 | + type: array |
| 206 | + defaultValue: ['hello', 'world'] |
| 207 | + resources: |
| 208 | + - name: Echo |
| 209 | + type: Test/Echo |
| 210 | + properties: |
| 211 | + text: '[concat(parameters(''param1''),'','',parameters(''param2''),'','',parameters(''param3''),'','',parameters(''param4''))]' |
| 212 | +"@ |
| 213 | + |
| 214 | + $out = $config_yaml | dsc config get | ConvertFrom-Json |
| 215 | + $LASTEXITCODE | Should -Be 0 |
| 216 | + $out.results[0].result.actualState.text | Should -BeExactly '"hello",7,false,["hello","world"]' |
| 217 | + } |
| 218 | +} |
0 commit comments