|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe 'Tests for copy loops' { |
| 5 | + It 'Works for resources' { |
| 6 | + $configYaml = @' |
| 7 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 8 | +resources: |
| 9 | +- name: "[format('Test-{0}', copyIndex())]" |
| 10 | + copy: |
| 11 | + name: testLoop |
| 12 | + count: 3 |
| 13 | + type: Microsoft.DSC.Debug/Echo |
| 14 | + properties: |
| 15 | + output: Hello |
| 16 | +'@ |
| 17 | + $out = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 18 | + $LASTEXITCODE | Should -Be 0 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 19 | + $out.results.Count | Should -Be 3 |
| 20 | + $out.results[0].name | Should -Be 'Test-0' |
| 21 | + $out.results[0].result.actualState.output | Should -Be 'Hello' |
| 22 | + $out.results[1].name | Should -Be 'Test-1' |
| 23 | + $out.results[1].result.actualState.output | Should -Be 'Hello' |
| 24 | + $out.results[2].name | Should -Be 'Test-2' |
| 25 | + $out.results[2].result.actualState.output | Should -Be 'Hello' |
| 26 | + } |
| 27 | + |
| 28 | + It 'copyIndex() works with offset' { |
| 29 | + $configYaml = @' |
| 30 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 31 | +resources: |
| 32 | +- name: "[format('Test-{0}', copyIndex(10))]" |
| 33 | + copy: |
| 34 | + name: testLoop |
| 35 | + count: 3 |
| 36 | + type: Microsoft.DSC.Debug/Echo |
| 37 | + properties: |
| 38 | + output: Hello |
| 39 | +'@ |
| 40 | + |
| 41 | + $out = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 42 | + $LASTEXITCODE | Should -Be 0 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 43 | + $out.results.Count | Should -Be 3 |
| 44 | + $out.results[0].name | Should -Be 'Test-10' |
| 45 | + $out.results[0].result.actualState.output | Should -Be 'Hello' |
| 46 | + $out.results[1].name | Should -Be 'Test-11' |
| 47 | + $out.results[1].result.actualState.output | Should -Be 'Hello' |
| 48 | + $out.results[2].name | Should -Be 'Test-12' |
| 49 | + $out.results[2].result.actualState.output | Should -Be 'Hello' |
| 50 | + } |
| 51 | + |
| 52 | + It 'copyIndex() with negative index returns error' { |
| 53 | + $configYaml = @' |
| 54 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 55 | +resources: |
| 56 | +- name: "[format('Test-{0}', copyIndex(-1))]" |
| 57 | + copy: |
| 58 | + name: testLoop |
| 59 | + count: 3 |
| 60 | + type: Microsoft.DSC.Debug/Echo |
| 61 | + properties: |
| 62 | + output: Hello |
| 63 | +'@ |
| 64 | + |
| 65 | + $null = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 66 | + $LASTEXITCODE | Should -Be 2 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 67 | + (Get-Content $testdrive/error.log -Raw) | Should -Match 'The offset cannot be negative' |
| 68 | + } |
| 69 | + |
| 70 | + It 'Copy works with count 0' { |
| 71 | + $configYaml = @' |
| 72 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 73 | +resources: |
| 74 | +- name: "[format('Test-{0}', copyIndex())]" |
| 75 | + copy: |
| 76 | + name: testLoop |
| 77 | + count: 0 |
| 78 | + type: Microsoft.DSC.Debug/Echo |
| 79 | + properties: |
| 80 | + output: Hello |
| 81 | +'@ |
| 82 | + |
| 83 | + $out = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 84 | + $LASTEXITCODE | Should -Be 0 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 85 | + $out.results.Count | Should -Be 0 |
| 86 | + } |
| 87 | + |
| 88 | + It 'copyIndex() with loop name works' { |
| 89 | + $configYaml = @' |
| 90 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 91 | +resources: |
| 92 | +- name: "[format('Test-{0}', copyIndex('testLoop'))]" |
| 93 | + copy: |
| 94 | + name: testLoop |
| 95 | + count: 3 |
| 96 | + type: Microsoft.DSC.Debug/Echo |
| 97 | + properties: |
| 98 | + output: Hello |
| 99 | +'@ |
| 100 | + $out = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 101 | + $LASTEXITCODE | Should -Be 0 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 102 | + $out.results.Count | Should -Be 3 |
| 103 | + $out.results[0].name | Should -Be 'Test-0' |
| 104 | + $out.results[0].result.actualState.output | Should -Be 'Hello' |
| 105 | + $out.results[1].name | Should -Be 'Test-1' |
| 106 | + $out.results[1].result.actualState.output | Should -Be 'Hello' |
| 107 | + $out.results[2].name | Should -Be 'Test-2' |
| 108 | + $out.results[2].result.actualState.output | Should -Be 'Hello' |
| 109 | + } |
| 110 | + |
| 111 | + It 'copyIndex() with invalid loop name "<name>" returns error' -TestCases @( |
| 112 | + @{ name = "'noSuchLoop'" } |
| 113 | + @{ name = "'noSuchLoop', 1" } |
| 114 | + ){ |
| 115 | + param($name) |
| 116 | + $configYaml = @" |
| 117 | +`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 118 | +resources: |
| 119 | +- name: "[format('Test-{0}', copyIndex($name))]" |
| 120 | + copy: |
| 121 | + name: testLoop |
| 122 | + count: 3 |
| 123 | + type: Microsoft.DSC.Debug/Echo |
| 124 | + properties: |
| 125 | + output: Hello |
| 126 | +"@ |
| 127 | + |
| 128 | + $null = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 129 | + $LASTEXITCODE | Should -Be 2 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 130 | + (Get-Content $testdrive/error.log -Raw) | Should -Match "The specified loop name 'noSuchLoop' was not found" |
| 131 | + } |
| 132 | + |
| 133 | + It 'Copy mode is not supported' { |
| 134 | + $configYaml = @' |
| 135 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 136 | +resources: |
| 137 | +- name: "[format('Test-{0}', copyIndex())]" |
| 138 | + copy: |
| 139 | + name: testLoop |
| 140 | + count: 3 |
| 141 | + mode: serial |
| 142 | + type: Microsoft.DSC.Debug/Echo |
| 143 | + properties: |
| 144 | + output: Hello |
| 145 | +'@ |
| 146 | + $null = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 147 | + $LASTEXITCODE | Should -Be 2 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 148 | + (Get-Content $testdrive/error.log -Raw) | Should -Match "Copy mode is not supported" |
| 149 | + } |
| 150 | + |
| 151 | + It 'Copy batch size is not supported' { |
| 152 | + $configYaml = @' |
| 153 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 154 | +resources: |
| 155 | +- name: "[format('Test-{0}', copyIndex())]" |
| 156 | + copy: |
| 157 | + name: testLoop |
| 158 | + count: 3 |
| 159 | + batchSize: 2 |
| 160 | + type: Microsoft.DSC.Debug/Echo |
| 161 | + properties: |
| 162 | + output: Hello |
| 163 | +'@ |
| 164 | + $null = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 165 | + $LASTEXITCODE | Should -Be 2 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 166 | + (Get-Content $testdrive/error.log -Raw) | Should -Match "Copy batch size is not supported" |
| 167 | + } |
| 168 | + |
| 169 | + It 'Name expression during copy must be a string' { |
| 170 | + $configYaml = @' |
| 171 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 172 | +resources: |
| 173 | +- name: "[copyIndex()]" |
| 174 | + copy: |
| 175 | + name: testLoop |
| 176 | + count: 3 |
| 177 | + type: Microsoft.DSC.Debug/Echo |
| 178 | + properties: |
| 179 | + output: Hello |
| 180 | +'@ |
| 181 | + $null = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json |
| 182 | + $LASTEXITCODE | Should -Be 2 -Because ((Get-Content $testdrive/error.log) | Out-String) |
| 183 | + (Get-Content $testdrive/error.log -Raw) | Should -Match "Copy name result is not a string" |
| 184 | + } |
| 185 | +} |
0 commit comments