-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_commands_yaml_get.py
442 lines (394 loc) · 14.6 KB
/
test_commands_yaml_get.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import pytest
from tests.conftest import create_temp_yaml_file
class Test_yaml_get():
"""Tests for the yaml-get command-line interface."""
command = "yaml-get"
def test_no_options(self, script_runner):
result = script_runner.run([self.command, "--nostdin"])
assert not result.success, result.stderr
assert "the following arguments are required: -p/--query" in result.stderr
def test_no_input_file(self, script_runner):
result = script_runner.run([self.command, "--nostdin", "--query='/test'"])
assert not result.success, result.stderr
assert "YAML_FILE must be set or be read from STDIN" in result.stderr
def test_bad_input_file(self, script_runner):
result = script_runner.run([self.command, "--query='/test'", "no-such-file"])
assert not result.success, result.stderr
assert "File not found:" in result.stderr
def test_no_query(self, script_runner, tmp_path_factory):
content = """---
no: ''
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, yaml_file])
assert not result.success, result.stderr
assert "the following arguments are required: -p/--query" in result.stderr
def test_bad_privatekey(self, script_runner, tmp_path_factory):
content = """---
no: ''
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=aliases", "--privatekey=no-such-file", yaml_file])
assert not result.success, result.stderr
assert "EYAML private key is not a readable file" in result.stderr
def test_bad_publickey(self, script_runner, tmp_path_factory):
content = """---
no: ''
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=aliases", "--publickey=no-such-file", yaml_file])
assert not result.success, result.stderr
assert "EYAML public key is not a readable file" in result.stderr
def test_yaml_parsing_error(self, script_runner, imparsible_yaml_file):
result = script_runner.run([self.command, "--query=/", imparsible_yaml_file])
assert not result.success, result.stderr
assert "YAML parsing error" in result.stderr
def test_yaml_syntax_error(self, script_runner, badsyntax_yaml_file):
result = script_runner.run([self.command, "--query=/", badsyntax_yaml_file])
assert not result.success, result.stderr
assert "YAML syntax error" in result.stderr
def test_yaml_composition_error(self, script_runner, badcmp_yaml_file):
result = script_runner.run([self.command, "--query=/", badcmp_yaml_file])
assert not result.success, result.stderr
assert "YAML composition error" in result.stderr
def test_bad_yaml_path(self, script_runner, tmp_path_factory):
content = """---
aliases:
- &plainScalar Plain scalar string
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=aliases[1]", yaml_file])
assert not result.success, result.stderr
assert "Required YAML Path does not match any nodes" in result.stderr
def test_bad_eyaml_value(self, script_runner, tmp_path_factory):
content = """---
aliases:
- &encryptedScalar >
ENC[PKCS7,MIIx...broken-on-purpose...==]
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--query=aliases[&encryptedScalar]",
"--eyaml=/does/not/exist-on-most/systems",
yaml_file
])
assert not result.success, result.stderr
assert "No accessible eyaml command" in result.stderr
def test_recursive_yaml_anchor(self, script_runner, tmp_path_factory):
content = """--- &recursive_this
hash:
recursive_key: *recursive_this
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--query=/hash",
yaml_file
])
assert not result.success, result.stderr
assert "contains an infinitely recursing" in result.stderr
def test_query_anchor(self, script_runner, tmp_path_factory):
content = """---
aliases:
- &plainScalar Plain scalar string
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=aliases[&plainScalar]", yaml_file])
assert result.success, result.stderr
assert "Plain scalar string" in result.stdout
def test_query_list(self, script_runner, tmp_path_factory):
content = """---
aliases:
- &plainScalar Plain scalar string
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=aliases", yaml_file])
assert result.success, result.stderr
assert '["Plain scalar string"]' in result.stdout
def test_query_doc_from_stdin(
self, script_runner, tmp_path_factory
):
import subprocess
yaml_file = """---
hash:
lhs_exclusive: LHS exclusive
merge_target: LHS original value
"""
result = subprocess.run(
[self.command
, "--query=/hash/lhs_exclusive"
, "-"]
, stdout=subprocess.PIPE
, input=yaml_file
, universal_newlines=True
)
assert 0 == result.returncode, result.stderr
assert "LHS exclusive\n" == result.stdout
def test_get_every_data_type(self, script_runner, tmp_path_factory):
# Contributed by https://github.com/AndydeCleyre
content = """---
intthing: 6
floatthing: 6.8
yesthing: yes
nothing: no
truething: true
falsething: false
nullthing: null
nothingthing:
emptystring: ""
nullstring: "null"
datething: 2022-09-23
timestampthing: 2022-09-24T14:13:12-7:30
"""
# Note that true nulls are translated as "\x00" (hexadecimal NULL
# control-characters).
results = ["6", "6.8", "yes", "no", "True", "False", "\x00", "\x00", "", "null", "2022-09-23", "2022-09-24T14:13:12-07:30"]
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=*", yaml_file])
assert result.success, result.stderr
match_index = 0
for line in result.stdout.splitlines():
assert line == results[match_index]
match_index += 1
def test_get_only_aoh_nodes_without_named_child(self, script_runner, tmp_path_factory):
content = """---
items:
- - alpha
- bravo
- charlie
- - alpha
- charlie
- delta
- - alpha
- bravo
- delta
- - bravo
- charlie
- delta
"""
results = [
"alpha",
"charlie",
"delta"
]
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=/items/*[!has_child(bravo)]*", yaml_file])
assert result.success, result.stderr
match_index = 0
for line in result.stdout.splitlines():
assert line == results[match_index]
match_index += 1
def test_get_only_aoh_nodes_with_named_child(self, script_runner, tmp_path_factory):
content = """---
products:
- name: something
price: 0.99
weight: 0.75
recalled: false
- name: other
price: 9.99
weight: 2.25
dimensions:
width: 1
height: 1
depth: 1
- name: moar
weight: 100
dimensions:
width: 100
height: 100
depth: 100
- name: less
price: 5
dimensions:
width: 5
height: 5
- name: bad
price: 0
weight: 4
dimensions:
width: 13
height: 4
depth: 7
recalled: true
"""
results = [
"something",
"bad",
]
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query=/products[has_child(recalled)]/name", yaml_file])
assert result.success, result.stderr
match_index = 0
for line in result.stdout.splitlines():
assert line == results[match_index]
match_index += 1
@pytest.mark.parametrize("query,output", [
("/items/*[!has_child(bravo)][2][parent(0)]", ['delta']),
("/items/*[!has_child(bravo)][2][parent()]", ['["alpha", "charlie", "delta"]']),
("/items/*[!has_child(bravo)][2][parent(2)]", ['[["alpha", "bravo", "charlie"], ["alpha", "charlie", "delta"], ["alpha", "bravo", "delta"], ["bravo", "charlie", "delta"]]']),
("/prices_hash/*[has_child(price)][name()]", ['doohickey', 'whatchamacallit', 'widget']),
("/prices_hash/*[!has_child(price)][name()]", ['unknown']),
])
def test_get_parent_nodes(self, script_runner, tmp_path_factory, query, output):
content = """---
items:
- - alpha
- bravo
- charlie
- - alpha
- charlie
- delta
- - alpha
- bravo
- delta
- - bravo
- charlie
- delta
prices_hash:
doohickey:
price: 4.99
whatchamacallit:
price: 9.95
widget:
price: 0.98
unknown:
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query={}".format(query), yaml_file])
assert result.success, result.stderr
match_index = 0
for line in result.stdout.splitlines():
assert line == output[match_index]
match_index += 1
@pytest.mark.parametrize("query,output", [
("svcs.*[name()]", ['coolserver', 'logsender']),
("svcs.*[enabled=false][parent()][name()]", ['logsender']),
("svcs[name()]", ['svcs']),
("indexes[.^Item][name()]", ['0', '1', '3']),
])
def test_get_node_names(self, script_runner, tmp_path_factory, query, output):
# Contributed by https://github.com/AndydeCleyre
content = """---
svcs:
coolserver:
enabled: true
exec: ./coolserver.py
logsender:
enabled: false
exec: remote_syslog -D
indexes:
- Item 1
- Item 2
- Disabled 3
- Item 4
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query={}".format(query), yaml_file])
assert result.success, result.stderr
match_index = 0
for line in result.stdout.splitlines():
assert line == output[match_index]
match_index += 1
@pytest.mark.parametrize("query,output", [
("prices_aoh[max(price)].product", ["whatchamacallit"]),
("prices_aoh[!max(price)].price", ["4.99", "4.99", "0.98"]),
("/prices_hash[max(price)][name()]", ["whatchamacallit"]),
("/prices_hash[!max(price)][name()]", ["doohickey", "fob", "widget", "unknown"]),
("(prices_hash.*.price)[max()]", ["9.95"]),
("(prices_hash.*.price)[!max()]", ["4.99", "4.99", "0.98"]),
("/prices_array[max()]", ["9.95"]),
("/prices_array[!max()]", ["4.99", "4.99", "0.98", "\x00"]),
("bare[max()]", ["value"]),
("/bad_prices_aoh[max(price)]/product", ["fob"]),
("/bad_prices_aoh[!max(price)]/price", ["4.99", "9.95", "True"]),
("bad_prices_hash[max(price)][name()]", ["fob"]),
("bad_prices_hash[!max(price)][name()]", ["doohickey", "whatchamacallit", "widget", "unknown"]),
("(/bad_prices_hash/*/price)[max()]", ["not set"]),
("(/bad_prices_hash/*/price)[!max()]", ["4.99", "9.95", "True"]),
("bad_prices_array[max()]", ["not set"]),
("bad_prices_array[!max()]", ["4.99", "9.95", "0.98", "\x00"]),
("prices_aoh[min(price)].product", ["widget"]),
("prices_aoh[!min(price)].price", ["9.95", "4.99", "4.99"]),
("/prices_hash[min(price)][name()]", ["widget"]),
("/prices_hash[!min(price)][name()]", ["whatchamacallit", "doohickey", "fob", "unknown"]),
("(prices_hash.*.price)[min()]", ["0.98"]),
("(prices_hash.*.price)[!min()]", ["9.95", "4.99", "4.99"]),
("/prices_array[min()]", ["0.98"]),
("/prices_array[!min()]", ["9.95", "4.99", "4.99", "\x00"]),
("bare[min()]", ["value"]),
("/bad_prices_aoh[min(price)]/product", ["widget"]),
("/bad_prices_aoh[!min(price)]/price", ["not set", "9.95", "4.99"]),
("bad_prices_hash[min(price)][name()]", ["widget"]),
("bad_prices_hash[!min(price)][name()]", ["fob", "whatchamacallit", "doohickey", "unknown"]),
("(/bad_prices_hash/*/price)[min()]", ["True"]),
("(/bad_prices_hash/*/price)[!min()]", ["not set", "9.95", "4.99"]),
("bad_prices_array[min()]", ["0.98"]),
("bad_prices_array[!min()]", ["not set", "9.95", "4.99", "\x00"]),
])
def test_get_min_max_nodes(self, script_runner, tmp_path_factory, query, output):
content = """---
# Consistent Data Types
prices_aoh:
- product: doohickey
price: 4.99
- product: fob
price: 4.99
- product: whatchamacallit
price: 9.95
- product: widget
price: 0.98
- product: unknown
prices_hash:
doohickey:
price: 4.99
fob:
price: 4.99
whatchamacallit:
price: 9.95
widget:
price: 0.98
unknown:
prices_array:
- 4.99
- 4.99
- 9.95
- 0.98
- null
# Inconsistent Data Types
bare: value
bad_prices_aoh:
- product: doohickey
price: 4.99
- product: fob
price: not set
- product: whatchamacallit
price: 9.95
- product: widget
price: true
- product: unknown
bad_prices_hash:
doohickey:
price: 4.99
fob:
price: not set
whatchamacallit:
price: 9.95
widget:
price: true
unknown:
bad_prices_array:
- 4.99
- not set
- 9.95
- 0.98
- null
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([self.command, "--query={}".format(query), yaml_file])
assert result.success, result.stderr
match_index = 0
for line in result.stdout.splitlines():
assert line == output[match_index]
match_index += 1