summaryrefslogtreecommitdiffstats
path: root/tests/shunit2/tests.sh
blob: e2d8024f46f08ed85273b5f736e84fa72884255a (plain)
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/bin/bash

JSON_SCRIPT=tests.json
JSON_SCRIPT_BIN=${TEST_JSON_SCRIPT=:-./json_script-example}
FILE_STDOUT=tests.stdout
FILE_STDERR=tests.stderr
FILE_EXPECTED=tests.expected

call_json_script() {
	#export LD_PRELOAD=../libjson_script.so
	$JSON_SCRIPT_BIN "$@" "$JSON_SCRIPT" >"$FILE_STDOUT" 2>"$FILE_STDERR"
}

assertStdioEquals() {
	local expected="$1"
	local file_stdio="$2"

	echo "$expected" >"$FILE_EXPECTED"
	if [ -z "$expected" ]; then
		# we are expecting empty output, but we deliberately added a newline
		# with echo above, so adding another echo to compensate for that
		echo >>"$file_stdio"
	fi
	diff -up "$FILE_EXPECTED" "$file_stdio" >/dev/null 2>&1 || {
		cat >&2 <<EOF
|--- expecting
$expected<
|--- actual
$(cat $file_stdio)<
|--- END
EOF
		exit 1
	}
}

assertStdoutEquals() {
	assertStdioEquals "$1" "$FILE_STDOUT"
}

assertStderrEquals() {
	assertStdioEquals "$1" "$FILE_STDERR"
}

test_bad_json() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ ]
		[ ]
	]
	EOF
	call_json_script
	assertStderrEquals "load JSON data from $JSON_SCRIPT failed."
}

test_expr_eq() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "eq", "VAR", "foo" ],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "VAR=foo"
	assertStdoutEquals "echo bar"
	call_json_script "VAR=xxx"
	assertStdoutEquals "echo baz"
}

test_expr_has() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "has", "VAR" ],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "VAR=foo"
	assertStdoutEquals "echo bar"
	call_json_script
	assertStdoutEquals "echo baz"
}

test_expr_regex_single() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "regex", "VAR", ".ell." ],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "VAR=hello"
	assertStdoutEquals "echo bar"
	call_json_script "VAR=.ell."
	assertStdoutEquals "echo bar"
	call_json_script
	assertStdoutEquals "echo baz"
	call_json_script "VAR="
	assertStdoutEquals "echo baz"
	call_json_script "VAR=hell"
	assertStdoutEquals "echo baz"
}

test_expr_regex_multi() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "regex", "VAR", [ ".ell.", "w.rld" ] ],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "VAR=hello"
	assertStdoutEquals "echo bar"
	call_json_script "VAR=world"
	assertStdoutEquals "echo bar"
	call_json_script "VAR=.ell."
	assertStdoutEquals "echo bar"
	call_json_script "VAR=w.rld"
	assertStdoutEquals "echo bar"
	call_json_script
	assertStdoutEquals "echo baz"
	call_json_script "VAR="
	assertStdoutEquals "echo baz"
	call_json_script "VAR=hell"
	assertStdoutEquals "echo baz"
}

test_expr_not() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "not", [ "has", "VAR" ] ],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "VAR=foo"
	assertStdoutEquals "echo baz"
	call_json_script
	assertStdoutEquals "echo bar"
}

test_expr_and() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "and", [ "eq", "EQVAR", "eqval" ],
					 [ "regex", "REGEXVAR", "regex..." ]
			],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
	assertStdoutEquals "echo bar"
	call_json_script "EQVAR=foo"
	assertStdoutEquals "echo baz"
	call_json_script "REGEXVAR=regex***"
	assertStdoutEquals "echo baz"
	call_json_script
	assertStdoutEquals "echo baz"
}

test_expr_or() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "or", [ "not", [ "eq", "EQVAR", "eqval" ] ],
					[ "regex", "REGEXVAR", [ "regexva.[0-9]", "regexva.[a-z]" ] ]
			],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "EQVAR=eqval" "REGEXVAR=regexval1"
	assertStdoutEquals "echo bar"
	call_json_script "EQVAR=neq" "REGEXVAR=sxc"
	assertStdoutEquals "echo bar"
	call_json_script "REGEXVAR=sxc"
	assertStdoutEquals "echo bar"
	call_json_script "EQVAR=foo"
	assertStdoutEquals "echo bar"
	call_json_script
	assertStdoutEquals "echo bar"
	call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
	assertStdoutEquals "echo baz"
}

test_expr_isdir() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "isdir", "%VAR%" ],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "VAR=/"
	assertStdoutEquals "echo bar"
	call_json_script "VAR=$(mktemp -u)"
	assertStdoutEquals "echo baz"
	call_json_script
	assertStdoutEquals "echo baz"
}

test_cmd_case() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "case", "CASEVAR", {
			"0": [ "echo", "foo" ],
			"1": [
				[ "echo", "bar" ],
				[ "echo", "baz" ]
			],
			"%VAR%": [ "echo", "quz" ]
		} ]
	]
	EOF
	call_json_script "CASEVAR=0"
	assertStdoutEquals "echo foo"
	call_json_script "CASEVAR=1"
	assertStdoutEquals "echo bar
echo baz"
	call_json_script "CASEVAR=%VAR%"
	assertStdoutEquals "echo quz"
	call_json_script "CASEVAR="
	assertStdoutEquals ""
	call_json_script
	assertStdoutEquals ""
	call_json_script "CASEVAR=xxx" "VAR=xxx"
	assertStdoutEquals ""
}

test_cmd_if() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "if",
			[ "eq", "VAR", "foo" ],
			[ "echo", "bar" ],
			[ "echo", "baz" ]
		]
	]
	EOF
	call_json_script "VAR=foo"
	assertStdoutEquals "echo bar"
	call_json_script "VAR=xxx"
	assertStdoutEquals "echo baz"
}

test_cmd_cb() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "exec", "%VAR%", "/%VAS%%%/" ]
	]
	EOF
	call_json_script
	assertStdoutEquals "exec  /%/"
	call_json_script "VAR="
	assertStdoutEquals "exec  /%/"
	call_json_script "VAR=qux" "VAS=3"
	assertStdoutEquals "exec qux /3%/"
}

test_cmd_return() {
	cat >"$JSON_SCRIPT" <<-EOF
	[
		[ "heh", "%HEHVAR%" ],
		[ "%VAR%", "%VAR%" ],
		[ "return" ],
		[ "exec_non_reachable", "Arghhh" ]
	]
	EOF
	call_json_script "HEHVAR=dude" "VAR=ow"
	assertStdoutEquals "heh dude
%VAR% ow"
}

test_jshn_append_no_leading_space() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	# Test appending to empty variable - should not have leading space
	var=''
	_jshn_append var 'foo'
	assertEquals "foo" "$var"

	# Test appending to non-empty variable - should have space separator
	var='bar'
	_jshn_append var 'foo'
	assertEquals "bar foo" "$var"

	# Test multiple appends to empty variable
	var=''
	_jshn_append var 'first'
	_jshn_append var 'second'
	assertEquals "first second" "$var"
}

test_jshn_dump() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	assertEquals '{ }' "$(json_dump)"

	set -u
}

test_jshn_add_string() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_string "name" "joe"

	assertEquals '{ "name": "joe" }' "$(json_dump)"

	set -u
}

test_jshn_add_int() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_int "number" 1

	assertEquals '{ "number": 1 }' "$(json_dump)"

	set -u
}

test_jshn_add_boolean() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_boolean "done" false

	assertEquals '{ "done": false }' "$(json_dump)"

	set -u
}

test_jshn_add_double() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_double "power" 1.605

	assertEquals '{ "power": 1.605 }' "$(json_dump)"

	set -u
}

test_jshn_add_null() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_null "reference"

	assertEquals '{ "reference": null }' "$(json_dump)"

	set -u
}

test_jshn_add_object() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_object "inventory"

	json_add_int "apples" 61
	json_add_int "pears" 42
	json_add_int "melons" 5

	json_close_object # inventory

	assertEquals '{ "inventory": { "apples": 61, "pears": 42, "melons": 5 } }' "$(json_dump)"

	set -u
}

test_jshn_add_array() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_array "interfaces"

	json_add_string "" "eth0"
	json_add_string "" "eth1"
	json_add_string "" "eth2"

	json_close_array # interfaces

	assertEquals '{ "interfaces": [ "eth0", "eth1", "eth2" ] }' "$(json_dump)"

	set -u
}

test_jshn_add_multi() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	json_init

	json_add_fields "name:string=joe" "age:int=42" "veteran:boolean=false"

	assertEquals '{ "name": "joe", "age": 42, "veteran": false }' "$(json_dump)"

	set -u
}

test_jshn_push_stuff() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	# Test an array with anonymous strings, ints, booleans, doubles, and null in it
	json_init
	json_add_array "arr"

	# first element uses the legacy method with no name
	json_add_string "" "first"

	# the rest use the new functions
	json_push_string "second"
	json_push_int 42
	json_push_boolean true
	json_push_double 3.141592
	json_push_null

	assertEquals '{ "arr": [ "first", "second", 42, false, 3.1415920000000002, null ] }' "$(json_dump)"

	set -u
}

test_jshn_append_via_json_script() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	# __SHUNIT_SHELL_FLAGS='u' results in 'line 6: JSON_UNSET: unbound variable' in json_cleanup()
	set +u

	# Test appending first key to empty variable without leading space
	json_init
	json_add_string "first" "value1"
	json_get_keys keys
	assertEquals "first" "$keys"

	# Test appending second key should maintain no leading space on first key
	json_add_string "second" "value2"
	json_get_keys keys
	assertEquals "first second" "$keys"
	set -u
}

test_jshn_get_index() {
	JSON_PREFIX="${JSON_PREFIX:-}"
	. ../../sh/jshn.sh

	set +u

	local index

	json_init

	json_add_object "DHCP4"

	json_add_array "networks"

	json_add_object ""
	json_get_index index
	json_add_int "id" 1
	json_add_string "subnet" "192.168.1.0/24"
	json_close_object

	json_add_object ""
	json_add_int "id" 2
	json_add_string "subnet" "192.168.2.0/24"
	json_close_object

	json_select "$index" # revisit first anonymous object
	json_add_int "valid-lifetime" 3600
	json_select .. # pop back into array

	json_close_array # networks

	json_close_object # DHCP4

	assertEquals '{ "DHCP4": { "networks": [ { "id": 1, "subnet": "192.168.1.0\/24", "valid-lifetime": 3600 }, { "id": 2, "subnet": "192.168.2.0\/24" } ] } }' "$(json_dump)"

	set -u
}

. ./shunit2/shunit2