-
Notifications
You must be signed in to change notification settings - Fork 27
/
functional
executable file
·691 lines (557 loc) · 21.9 KB
/
functional
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/env bats
#
# Functional tests written with bats - https://github.com/sstephenson/bats
#
# Copyright (c) 2017, Groupon, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# Neither the name of GROUPON nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
readonly BATS_ANSIBLE_SILO_INSTALL_PATH=/tmp/silo_installation_test_dir
readonly BATS_ANSIBLE_SILO_LOGFILE="/tmp/ansible.test.log"
readonly BATS_ANSIBLE_SILO_BUNDLE_NAME="test-silo-bundle"
readonly BATS_ANSIBLE_SILO_BUNDLE_URL="test.example/org/${BATS_ANSIBLE_SILO_BUNDLE_NAME}"
readonly VERSION="$(cat ${BATS_TEST_DIRNAME}/../VERSION)"
readonly BATS_ANSIBLE_SILO_BUNDLE_PATH="/tmp/${BATS_ANSIBLE_SILO_BUNDLE_NAME}"
readonly BATS_ANSIBLE_SILO_VAULT_PASSWORD_FILE="/tmp/silo-vault-password"
export PATH="$BATS_ANSIBLE_SILO_INSTALL_PATH:$PATH"
source "${BATS_TEST_DIRNAME}/../silo/exit_codes.sh"
# Test required bash version
test_array=(a b)
if [[ "${test_array[-2]}" != "a" ]]; then
echo "Require at least bash 4.2 to run these tests." >&2
exit 1
fi
debug() {
status="$1"
output="$2"
if [[ ! "${status}" -eq "0" ]]; then
echo "status: ${status}"
echo "output: ${output}"
fi
}
# Silo extension functions
# Should be loaded in standalone and bundle mode
silo_test_function_all() {
echo "--env SILO_TEST_FUNCTION_ALL='true'"
}
# Should be loaded in standalone mode
_silo_test_function_standalone() {
echo "--env SILO_TEST_FUNCTION_STANDALONE='true'"
}
# Should be loaded in bundle mode
test_silo_bundle_function() {
echo "--env SILO_TEST_FUNCTION_BUNDLE='true'"
}
@test "Reset volume to start with a clean state" {
ansible-silo --reset > /dev/null 2>&1 || :
}
@test "Build Silo image" {
cd "$(dirname ${BATS_TEST_DIRNAME})" || exit
run make ansible-silo
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Installation" {
rm -rf "$BATS_ANSIBLE_SILO_INSTALL_PATH"
mkdir -p "$BATS_ANSIBLE_SILO_INSTALL_PATH"
run docker run -it --rm \
-v "$BATS_ANSIBLE_SILO_INSTALL_PATH:/silo_install_path" \
-i grpn/ansible-silo:${VERSION} --install
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "ansible is pointing to ansible-silo" {
# Make sure the ansible* commands are symbolic links pointing to ansible-silo
for tool in "ansible" "ansible-config" "ansible-console" "ansible-connection" "ansible-doc" "ansible-galaxy" "ansible-inventory" "ansible-lint" "ansible-playbook" "ansible-pull" "ansible-vault"; do
[[ -L "$BATS_ANSIBLE_SILO_INSTALL_PATH/$tool" ]]
[[ "$(readlink -- "$BATS_ANSIBLE_SILO_INSTALL_PATH/$tool")" = "ansible-silo" ]]
done
# Make sure the setup module executed against localhost shows docker as
# virtualization type
result1=$("$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i localhost, -c local \
-m setup all | grep ansible_virtualization_type)
[[ "${result1}" =~ docker ]]
# Make sure the setup module executed against localhost shows the expected
# home directory
result2=$("$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i localhost, -c local \
-m setup all | grep ANSIBLE_HOME)
[[ "${result2}" =~ /silo/userspace/ansible ]]
}
@test "Run specific ansible-silo version" {
# Define which Silo version to use
export SILO_VERSION="1.3.3"
export SILO_VOLUME="silo_1.3.3"
# Get info from Silo and check it is the expected version
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --version
debug "${status}" "${output}" "${lines}"
[[ "${lines[-4]}" == 'ansible-silo 1.3.3'* ]]
# Cleanup
unset SILO_VERSION
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --reset
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
unset SILO_VOLUME
}
@test "Fail when no options were provided" {
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -ne 0 ]]
[[ "${lines[0]}" == "Usage: ansible-silo [option] [command]"* ]]
}
@test "Fail when unknown option was provided" {
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" slartibartfast
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq "${EX_USAGE}" ]]
[[ "${lines[0]}" == "Unknown option:"* ]]
[[ "${lines[1]}" == "Usage: ansible-silo [option] [command]"* ]]
}
@test "Show help message" {
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --help
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "Usage: ansible-silo [option] [command]"* ]]
}
@test "Switch to Ansible 2.0.0.2" {
# Switch to 2.0.0.2
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --switch v2.0.0.2-1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Confirm switch was successful
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" =~ ^ansible[[:space:]]2\.0\.0\.2.*detached[[:space:]]HEAD ]]
}
@test "Switch to Ansible 2.3.0.0" {
# Switch to 2.3.0.0
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --switch v2.3.0.0-1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Confirm switch was successful
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" =~ ^ansible[[:space:]]2\.3\.0\.0.*detached[[:space:]]HEAD ]]
}
@test "Default volume" {
# Reset volume
unset SILO_VOLUME
# Confirm Silo uses the defined volume
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == *$"ansible installed on volume silo.$(whoami)"* ]]
}
@test "Switch volume" {
# Set volume used by silo
export SILO_VOLUME="test"
# Confirm Silo uses the defined volume
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == *$'ansible installed on volume silo.test'* ]]
# Reset volume
unset SILO_VOLUME
}
@test "Reset volume" {
# Set volume used by silo
export SILO_VOLUME="test"
# Delete the test volume
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --reset
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == silo.test* ]]
# Reset volume, will use default volume
unset SILO_VOLUME
# Delete the default volume
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --reset
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == silo.$(whoami)* ]]
# Switch again to a specific version, so further tests do not run with the latest version
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --switch v2.3.2.0-1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Switch to Ansible 1.9.6 on a separate volume" {
# Set volume used by silo
export SILO_VOLUME="test.1.9.6"
# Switch to 1.9.6
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --switch v1.9.6-1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Confirm switch was successful
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" =~ ^ansible[[:space:]]1\.9\.6.*detached[[:space:]]HEAD ]]
# Delete the test volume
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --reset
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Reset volume, will use default volume
unset SILO_VOLUME
# Confirm silo on default volume is still 2.3.2.0
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" =~ ^ansible[[:space:]]2\.3\.2\.0.*detached[[:space:]]HEAD ]]
}
@test "Docker socket forwarding" {
# Disable
export SILO_NO_PRIVILEGED=true
# Make sure the socket does not exist
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell\
[[ -S /var/run/docker.sock ]]
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 1 ]]
# Enable
unset SILO_NO_PRIVILEGED
# Make sure the socket does exist
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell\
[[ -S /var/run/docker.sock ]]
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Docker implementation" {
cd "${BATS_TEST_DIRNAME}/playbooks"
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-playbook" -i inventory docker.yml --tags create
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Silo containers are not persistent" {
# Get current number of active silo containers
before=$(docker ps -a | grep ansible-silo | wc -l)
# Run a silo command
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Get current number of active silo containers
after=$(docker ps -a | grep ansible-silo | wc -l)
# Make sure there are still the same number of active silo containers running
[[ "${before}" -eq "${after}" ]]
}
@test "Ansible command through local connection" {
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i localhost, -c local \
-m setup all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Ansible command through ssh w/ key authentication" {
# Delete the test file, if it exists
rm -f /tmp/silo.test
# Disable hostkey checking
export ANSIBLE_HOST_KEY_CHECKING=False
# Run ansible command
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "`hostname -f`," -m shell \
-a "echo \$(whoami) > /tmp/silo.test" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Cleanup
unset ANSIBLE_HOST_KEY_CHECKING
rm -f /tmp/silo.test
}
@test "Ansible command through ssh w/ password authentication" {
cd "${BATS_TEST_DIRNAME}/playbooks"
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-playbook" -i inventory docker.yml --tags test_ssh
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Ansible command through docker socket connection" {
cd "${BATS_TEST_DIRNAME}/playbooks"
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-playbook" -i inventory docker.yml --tags test_socket
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Cleanup: we no longer need the test container
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-playbook" -i inventory docker.yml --tags create
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "User name/id forwarding" {
# Disable hostkey checking and colors
export ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_NOCOLOR=True
# Check user
MYNAME="$(whoami)"
MYID="$(id -u)"
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "`hostname -f`," -m shell \
-a "echo \$(whoami)" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == "$MYNAME"* ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "`hostname -f`," -m shell \
-a "id -u" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == "$MYID"* ]]
# Cleanup
unset ANSIBLE_HOST_KEY_CHECKING
unset ANSIBLE_NOCOLOR
}
@test "Bundle create" {
rm -rf "$BATS_ANSIBLE_SILO_BUNDLE_PATH"
# Create test bundle
cd /tmp
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" \
--bundle "$BATS_ANSIBLE_SILO_BUNDLE_URL"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ -d "$BATS_ANSIBLE_SILO_BUNDLE_PATH" ]]
[[ -d "$BATS_ANSIBLE_SILO_BUNDLE_PATH/playbooks" ]]
echo "silo_bundle_extension() {" > "$BATS_ANSIBLE_SILO_BUNDLE_PATH/bundle_extension.sh"
echo " echo \"--env SILO_TEST_EXTENSION_BUNDLE='true'\"" >> "$BATS_ANSIBLE_SILO_BUNDLE_PATH/bundle_extension.sh"
echo "}" >> "$BATS_ANSIBLE_SILO_BUNDLE_PATH/bundle_extension.sh"
}
@test "Bundle build" {
cd "$BATS_ANSIBLE_SILO_BUNDLE_PATH"
run "./build"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
run docker images -q "$BATS_ANSIBLE_SILO_BUNDLE_URL"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${#lines[@]}" -eq 2 ]]
}
@test "Bundle install" {
run docker run -it --rm \
-v "$BATS_ANSIBLE_SILO_INSTALL_PATH:/silo_install_path" \
-i "$BATS_ANSIBLE_SILO_BUNDLE_URL:latest" --install
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ -x "$BATS_ANSIBLE_SILO_INSTALL_PATH/$BATS_ANSIBLE_SILO_BUNDLE_NAME" ]]
}
@test "Bundle execute" {
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/$BATS_ANSIBLE_SILO_BUNDLE_NAME" \
playbook.yml
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "ansible-lint" {
cd "$BATS_ANSIBLE_SILO_BUNDLE_PATH/playbooks"
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-lint" playbook.yml
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Execute Playbook" {
cd "$BATS_ANSIBLE_SILO_BUNDLE_PATH/playbooks"
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-playbook" playbook.yml
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Ansible Galaxy" {
local role_url="https://github.com/geerlingguy/ansible-role-apache.git"
local role_hash="0b7cd353c0250e87a26e0499e59e7fd265cc2f25"
rm -rf /tmp/geerlingguy.apache /tmp/ansible-role-apache
cd /tmp
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-galaxy" install --roles-path . geerlingguy.apache
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ -d "/tmp/geerlingguy.apache" ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-galaxy" install --roles-path . git+$role_url,$role_hash
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ -d "ansible-role-apache" ]]
# Cleanup
rm -rf /tmp/geerlingguy.apache /tmp/ansible-role-apache
}
@test "Core modules available" {
# Switch to 2.0.0.2
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --switch v2.0.0.2-1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i localhost, -c local \
-m ping all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Extra modules available" {
# Switch to 2.0.0.2
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --switch v2.0.0.2-1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i localhost, -c local \
-m blockinfile -a "dest=/tmp/blockinfile.test block=content create=yes" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Shell execution" {
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell hostname -f
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "silo.$(hostname -f)"* ]]
}
@test "Standalone function loading" {
export -f silo_test_function_all
export -f _silo_test_function_standalone
export -f test_silo_bundle_function
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "localhost," -c local \
-m shell -a "printenv SILO_TEST_FUNCTION_ALL" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[1]}" == "true"* ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "localhost," -c local \
-m shell -a "printenv SILO_TEST_FUNCTION_STANDALONE" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[1]}" == "true"* ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "localhost," -c local \
-m shell -a "printenv SILO_TEST_FUNCTION_BUNDLE" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -ne 0 ]]
[[ "${lines[1]}" != *"true"* ]]
# Cleanup
unset silo_test_function_all
unset _silo_test_function_standalone
unset test_silo_bundle_function
}
@test "Bundle extension loading" {
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/$BATS_ANSIBLE_SILO_BUNDLE_NAME" \
--shell printenv SILO_TEST_EXTENSION_BUNDLE
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "true"* ]]
}
@test "Bundle function loading" {
export -f silo_test_function_all
export -f _silo_test_function_standalone
export -f test_silo_bundle_function
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/$BATS_ANSIBLE_SILO_BUNDLE_NAME" \
--shell printenv SILO_TEST_FUNCTION_ALL
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "true"* ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/$BATS_ANSIBLE_SILO_BUNDLE_NAME" \
--shell printenv SILO_TEST_FUNCTION_STANDALONE
debug "${status}" "${output}" "${lines}"
[[ "${status}" -ne 0 ]]
[[ "${lines[0]}" != *"true"* ]]
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/$BATS_ANSIBLE_SILO_BUNDLE_NAME" \
--shell printenv SILO_TEST_FUNCTION_BUNDLE
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "true"* ]]
# Cleanup
unset silo_test_function_all
unset _silo_test_function_standalone
unset test_silo_bundle_function
run docker rmi --force "$(docker images "$BATS_ANSIBLE_SILO_BUNDLE_URL" \
--format '{{.ID}}')"
debug "${status}" "${output}" "${lines}"
}
@test "Debug output" {
export SILO_DEBUG=true
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" --version
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == "SILO vars:" ]]
[[ "${lines[1]}" == " - SILO_DEBUG=true" ]]
[[ "${lines[-7]}" =~ ^Executing:[[:space:]]/tmp/ansible-silo-runner-.*[[:space:]]\"ansible\"[[:space:]]\"--version\" ]]
[[ "${lines[-6]}" =~ ^Executing:[[:space:]].*/docker[[:space:]]run ]]
unset SILO_DEBUG
}
@test "Mount logfile per environment variable" {
# Switch to an Ansible version that does not log color codes
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --switch v2.3.0.0-1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Create logfile
echo "" > "$BATS_ANSIBLE_SILO_LOGFILE"
export ANSIBLE_LOG_PATH="$BATS_ANSIBLE_SILO_LOGFILE"
# Run an ansible command
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "localhost," -c local \
-m shell -a "whoami" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
run cat "$BATS_ANSIBLE_SILO_LOGFILE"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == *'SUCCESS'* ]]
# Cleanup
unset ANSIBLE_LOG_PATH
rm -f "$BATS_ANSIBLE_SILO_LOGFILE"
}
@test "Mount logfile per ansible.cfg variable" {
# Create logfile
echo "" > "$BATS_ANSIBLE_SILO_LOGFILE"
# cd into the example bundle dir and add the logfile to the config
cd "$BATS_ANSIBLE_SILO_BUNDLE_PATH/playbooks"
echo "log_path = $BATS_ANSIBLE_SILO_LOGFILE" >> "ansible.cfg"
# Run an ansible command
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible" -i "localhost," -c local \
-m shell -a "whoami" all
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
run cat "$BATS_ANSIBLE_SILO_LOGFILE"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[0]}" == *'SUCCESS'* ]]
# Cleanup
rm -f "$BATS_ANSIBLE_SILO_LOGFILE"
}
@test "Install pip module" {
# Install a module through pip
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell pip install pbr==3.1.1
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Make sure we can import the just installed module
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell "python -c 'import pbr; print pbr'"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == *$"<module 'pbr' from '/silo/userspace/lib/python2.7/site-packages/pbr/__init__.py"* ]]
# pbr also installs an executable. Make sure we can call it
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell pbr -h
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Uninstall pbr
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell pip uninstall -y pbr
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
}
@test "Ansible module requirements exist" {
# Make sure we can import the ncclient module added to ansible-silo-base
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-silo" --shell "python -c 'import ncclient; print ncclient'"
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
[[ "${lines[-1]}" == *$"<module 'ncclient' from '/usr/lib/python2.7/site-packages/ncclient/__init__.py"* ]]
}
@test "Ansible vault w/ ANSIBLE_VAULT_PASSWORD_FILE" {
# Write password file
echo "slartibartfast" > "$BATS_ANSIBLE_SILO_VAULT_PASSWORD_FILE"
export ANSIBLE_VAULT_PASSWORD_FILE="$BATS_ANSIBLE_SILO_VAULT_PASSWORD_FILE"
cd "${BATS_TEST_DIRNAME}/playbooks"
run "$BATS_ANSIBLE_SILO_INSTALL_PATH/ansible-playbook" -i inventory vault.yml
debug "${status}" "${output}" "${lines}"
[[ "${status}" -eq 0 ]]
# Cleanup
rm -rf "$BATS_ANSIBLE_SILO_VAULT_PASSWORD_FILE"
unset ANSIBLE_VAULT_PASSWORD_FILE
}
@test "Run ansible without tty" {
run bash -c "true | ansible --version"
[[ "${status}" -eq 0 ]]
}