-
Notifications
You must be signed in to change notification settings - Fork 3
/
PrtgXml.Tests.ps1
263 lines (219 loc) · 8.93 KB
/
PrtgXml.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
Import-Module $PSScriptRoot\src\PrtgXml.psd1
Describe "Result Tags" {
Context "Valid XML" {
It "Should have one channel" {
$xml = Prtg {
Result {
Channel "My channel"
Value 3
}
}
$xml | Should Be "<Prtg>`r`n <Result>`r`n <Channel>My channel</Channel>`r`n <Value>3</Value>`r`n </Result>`r`n</Prtg>"
}
It "Should have multiple channels" {
$xml = Prtg {
Result {
Channel "First channel"
Value 3.5
}
Result {
Channel "Second channel"
Value "4000"
}
}
$expectedXml =
"<Prtg>`r`n" +
" <Result>`r`n <Channel>First channel</Channel>`r`n <Value>3.5</Value>`r`n </Result>`r`n" +
" <Result>`r`n <Channel>Second channel</Channel>`r`n <Value>4000</Value>`r`n </Result>`r`n" +
"</Prtg>"
$xml | Should Be $expectedXml
}
It "Should have a channel and a message" {
$xml = Prtg {
Text "Hello!"
Result {
Channel "My channel"
Value 4
}
}
$xml | Should Be "<Prtg>`r`n <Text>Hello!</Text>`r`n <Result>`r`n <Channel>My channel</Channel>`r`n <Value>4</Value>`r`n </Result>`r`n</Prtg>"
}
It "Should have multiple channel modifiers" {
$xml = Prtg {
Result {
Channel "My channel"
Value 50
ValueLookup prtg.standardlookups.yesno.stateyesok
}
}
$xml | Should Be "<Prtg>`r`n <Result>`r`n <Channel>My channel</Channel>`r`n <Value>50</Value>`r`n <ValueLookup>prtg.standardlookups.yesno.stateyesok</ValueLookup>`r`n </Result>`r`n</Prtg>"
}
It "Should process all tags" {
$xml = Prtg {
Error 1
Text "Woops!"
Result {
Channel "My channel"
Value 3
Unit Custom
CustomUnit Bloops
SpeedSize KiloByte
VolumeSize TeraBit
SpeedTime Day
Mode Difference
Float 1
DecimalMode Auto
Warning 1
ShowChart 1
ShowTable 1
LimitMaxError 30
LimitMaxWarning 20
LimitMinWarning 10
LimitMinError 5
LimitErrorMsg "Things are not looking good"
LimitWarningMsg "Things could be better"
LimitMode 1
ValueLookup "prtg.standardlookups.yesno.stateyesok"
NotifyChanged
}
}
$expectedXml =
"<Prtg>`r`n" +
" <Error>1</Error>`r`n" +
" <Text>Woops!</Text>`r`n" +
" <Result>`r`n" +
" <Channel>My channel</Channel>`r`n" +
" <Value>3</Value>`r`n" +
" <Unit>Custom</Unit>`r`n" +
" <CustomUnit>Bloops</CustomUnit>`r`n" +
" <SpeedSize>KiloByte</SpeedSize>`r`n" +
" <VolumeSize>TeraBit</VolumeSize>`r`n" +
" <SpeedTime>Day</SpeedTime>`r`n" +
" <Mode>Difference</Mode>`r`n" +
" <Float>1</Float>`r`n" +
" <DecimalMode>Auto</DecimalMode>`r`n" +
" <Warning>1</Warning>`r`n" +
" <ShowChart>1</ShowChart>`r`n" +
" <ShowTable>1</ShowTable>`r`n" +
" <LimitMaxError>30</LimitMaxError>`r`n" +
" <LimitMaxWarning>20</LimitMaxWarning>`r`n" +
" <LimitMinWarning>10</LimitMinWarning>`r`n" +
" <LimitMinError>5</LimitMinError>`r`n" +
" <LimitErrorMsg>Things are not looking good</LimitErrorMsg>`r`n" +
" <LimitWarningMsg>Things could be better</LimitWarningMsg>`r`n" +
" <LimitMode>1</LimitMode>`r`n" +
" <ValueLookup>prtg.standardlookups.yesno.stateyesok</ValueLookup>`r`n" +
" <NotifyChanged />`r`n" +
" </Result>`r`n" +
"</Prtg>"
$xml | Should Be $expectedXml
}
}
Context "Invalid Prtg block" {
$errorMessage = "Prtg block requires an inner element"
It "Should throw without argument" {
{ Prtg } | Should Throw $errorMessage
}
It "Should throw with empty script block" {
{ Prtg {} } | Should Throw $errorMessage
}
It "Should throw with empty script block ignoring space" {
{ Prtg { } } | Should Throw $errorMessage
}
It "Should throw with empty script block ignoring tab/newline" {
{ Prtg {
} } | Should Throw $errorMessage
}
It "Should throw with invalid argument type" {
{ Prtg "a" } | Should Throw "Cannot convert the `"a`" value of type `"System.String`" to type `"System.Management.Automation.ScriptBlock`""
}
It "escapes XML characters inside an outer container" {
$xml = Prtg {
Text "A & B"
}
$xml | Should Be "<Prtg>`r`n <Text>A & B</Text>`r`n</Prtg>"
}
}
Context "Invalid Result block" {
$errorMessage = "Result block requires an inner element"
It "Should throw without argument" {
{ Prtg { Result } } | Should Throw $errorMessage
}
It "Should throw with empty script block" {
{ Prtg { Result {} } } | Should Throw $errorMessage
}
It "Should throw with empty script block ignoring space" {
{ Prtg { Result { }} } | Should Throw $errorMessage
}
It "Should throw with empty script block ignoring tab/newline" {
{ Prtg {
Result {
}
} } | Should Throw $errorMessage
}
It "Should throw with invalid argument type" {
{ Prtg {
Result "a"
} } | Should Throw "Cannot convert the `"a`" value of type `"System.String`" to type `"System.Management.Automation.ScriptBlock`""
}
It "escapes invalid XML characters inside an inner container" {
$xml = Prtg {
Result {
Channel "A & B"
}
}
$xml | Should Be "<Prtg>`r`n <Result>`r`n <Channel>A & B</Channel>`r`n </Result>`r`n</Prtg>"
}
}
Context "Value Parsing" {
It "Should treat 0 as a value" {
$xml = Prtg {
Result {
Channel "My channel"
Value 0
}
}
$xml | Should Be "<Prtg>`r`n <Result>`r`n <Channel>My channel</Channel>`r`n <Value>0</Value>`r`n </Result>`r`n</Prtg>"
}
It "Should treat a missing value as null" {
$xml = Prtg {
Result {
Channel "My channel"
Value
}
}
$xml | Should Be "<Prtg>`r`n <Result>`r`n <Channel>My channel</Channel>`r`n <Value />`r`n </Result>`r`n</Prtg>"
}
It "Should treat `$null as null" {
$xml = Prtg {
Result {
Channel "My channel"
Value $null
}
}
$xml | Should Be "<Prtg>`r`n <Result>`r`n <Channel>My channel</Channel>`r`n <Value />`r`n </Result>`r`n</Prtg>"
}
It "Should treat an empty string as null" {
$xml = Prtg {
Result {
Channel "My channel"
Value ""
}
}
$xml | Should Be "<Prtg>`r`n <Result>`r`n <Channel>My channel</Channel>`r`n <Value />`r`n </Result>`r`n</Prtg>"
}
It "escapes invalid XML characters inside a value block" {
$xml = Text "A & B"
$xml | Should Be "<Text>A & B</Text>"
}
}
}
Describe "Error Tags" {
It "Should contain an error" {
$xml = Prtg {
Error 1
Text "Woops!"
}
$xml | Should Be "<Prtg>`r`n <Error>1</Error>`r`n <Text>Woops!</Text>`r`n</Prtg>"
}
}