forked from PowerShell/DSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsc_expressions.tests.ps1
110 lines (104 loc) · 3.58 KB
/
dsc_expressions.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Expressions tests' {
It 'Accessors work: <text>' -TestCases @(
@{ text = "[parameters('test').hello]"; expected = '@{world=there}' }
@{ text = "[parameters('test').hello.world]"; expected = 'there' }
@{ text = "[parameters('test').array[0]]"; expected = 'one' }
@{ text = "[parameters('test').array[1][1]]"; expected = 'three' }
@{ text = "[parameters('test').objectArray[0].name]"; expected = 'one' }
@{ text = "[parameters('test').objectArray[1].value[0]]"; expected = '2' }
@{ text = "[parameters('test').objectArray[1].value[1].name]"; expected = 'three' }
@{ text = "[parameters('test').index]"; expected = '1' }
@{ text = "[parameters('test').objectArray[parameters('test').index].name]"; expected = 'two' }
) {
param($text, $expected)
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
test:
type: object
defaultValue:
index: 1
hello:
world: there
array:
- one
- [ 'two', 'three' ]
objectArray:
- name: one
value: 1
- name: two
value:
- 2
- nestedObject:
name: three
value: 3
resources:
- name: echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "$text"
"@
$debug = $yaml | dsc -l trace config get -o yaml -f - 2>&1 | Out-String
$out = $yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because $debug
$out.results[0].result.actualState.output | Should -Be $expected -Because $debug
}
It 'Invalid expressions: <expression>' -TestCases @(
@{ expression = "[concat('A','B')].hello" }
@{ expression = "[concat('A','B')](0)" }
@{ expression = "[concat('a','b').hello]" }
@{ expression = "[concat('a','b')[0]]" }
) {
param($expression)
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "$expression"
"@
$out = dsc config get -i $yaml 2>&1
$LASTEXITCODE | Should -Be 2
$out | Should -BeLike "*ERROR*"
}
It 'Multi-line string literals work' {
$yamlPath = "$PSScriptRoot/../examples/multiline.dsc.yaml"
$out = dsc config get -f $yamlPath | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -BeExactly @"
This is a
'multi-line'
string.
"@.Replace("`r", "")
$out.results[1].result.actualState.output | Should -BeExactly "This is a single-quote: '"
}
It 'Nested Group resource does not invoke expressions' {
$yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Nested Group
type: Microsoft.DSC/Group
properties:
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Deeply nested OSInfo
type: Microsoft/OSInfo
properties: {}
- name: Deeply nested echo
type: Microsoft.DSC.Debug/Echo
properties:
output: >-
[reference(
resourceId('Microsoft/OSInfo', 'Deeply nested OSInfo')
)]
dependsOn:
- "[resourceId('Microsoft/OSInfo', 'Deeply nested OSInfo')]"
'@
$out = dsc config get -i $yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result[1].result.actualState.output.family | Should -BeExactly $out.results[0].result[0].result.actualState.family
}
}