1#!/usr/bin/env bash
2
3set -eux
4
5export ANSIBLE_COLLECTIONS_PATH=$PWD/collection_root_user:$PWD/collection_root_sys
6export ANSIBLE_GATHERING=explicit
7export ANSIBLE_GATHER_SUBSET=minimal
8export ANSIBLE_HOST_PATTERN_MISMATCH=error
9
10# FUTURE: just use INVENTORY_PATH as-is once ansible-test sets the right dir
11ipath=../../$(basename "${INVENTORY_PATH:-../../inventory}")
12export INVENTORY_PATH="$ipath"
13
14echo "--- validating callbacks"
15# validate FQ callbacks in ansible-playbook
16ANSIBLE_CALLBACK_WHITELIST=testns.testcoll.usercallback ansible-playbook noop.yml | grep "usercallback says ok"
17# use adhoc for the rest of these tests, must force it to load other callbacks
18export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
19# validate redirected callback
20ANSIBLE_CALLBACK_WHITELIST=formerly_core_callback ansible localhost -m debug 2>&1 | grep -- "usercallback says ok"
21## validate missing redirected callback
22ANSIBLE_CALLBACK_WHITELIST=formerly_core_missing_callback ansible localhost -m debug 2>&1 | grep -- "Skipping callback plugin 'formerly_core_missing_callback'"
23## validate redirected + removed callback (fatal)
24ANSIBLE_CALLBACK_WHITELIST=formerly_core_removed_callback ansible localhost -m debug 2>&1 | grep -- "testns.testcoll.removedcallback has been removed"
25# validate avoiding duplicate loading of callback, even if using diff names
26[ "$(ANSIBLE_CALLBACK_WHITELIST=testns.testcoll.usercallback,formerly_core_callback ansible localhost -m debug 2>&1 | grep -c 'usercallback says ok')" = "1" ]
27# ensure non existing callback does not crash ansible
28ANSIBLE_CALLBACK_WHITELIST=charlie.gomez.notme ansible localhost -m debug 2>&1 | grep -- "Skipping callback plugin 'charlie.gomez.notme'"
29unset ANSIBLE_LOAD_CALLBACK_PLUGINS
30# adhoc normally shouldn't load non-default plugins- let's be sure
31output=$(ANSIBLE_CALLBACK_WHITELIST=testns.testcoll.usercallback ansible localhost -m debug)
32if [[ "${output}" =~ "usercallback says ok" ]]; then echo fail; exit 1; fi
33
34echo "--- validating docs"
35# test documentation
36ansible-doc testns.testcoll.testmodule -vvv | grep -- "- normal_doc_frag"
37# same with symlink
38ln -s "${PWD}/testcoll2" ./collection_root_sys/ansible_collections/testns/testcoll2
39ansible-doc testns.testcoll2.testmodule2 -vvv | grep "Test module"
40# now test we can list with symlink
41ansible-doc -l -vvv| grep "testns.testcoll2.testmodule2"
42
43echo "testing bad doc_fragments (expected ERROR message follows)"
44# test documentation failure
45ansible-doc testns.testcoll.testmodule_bad_docfrags -vvv 2>&1 | grep -- "unknown doc_fragment"
46
47echo "--- validating default collection"
48# test adhoc default collection resolution (use unqualified collection module with playbook dir under its collection)
49
50echo "testing adhoc default collection support with explicit playbook dir"
51ANSIBLE_PLAYBOOK_DIR=./collection_root_user/ansible_collections/testns/testcoll ansible localhost -m testmodule
52
53# we need multiple plays, and conditional import_playbook is noisy and causes problems, so choose here which one to use...
54if [[ ${INVENTORY_PATH} == *.winrm ]]; then
55  export TEST_PLAYBOOK=windows.yml
56else
57  export TEST_PLAYBOOK=posix.yml
58
59  echo "testing default collection support"
60  ansible-playbook -i "${INVENTORY_PATH}" collection_root_user/ansible_collections/testns/testcoll/playbooks/default_collection_playbook.yml "$@"
61fi
62
63echo "--- validating collections support in playbooks/roles"
64# run test playbooks
65ansible-playbook -i "${INVENTORY_PATH}" -v "${TEST_PLAYBOOK}" "$@"
66
67if [[ ${INVENTORY_PATH} != *.winrm ]]; then
68	ansible-playbook -i "${INVENTORY_PATH}" -v invocation_tests.yml "$@"
69fi
70
71echo "--- validating bypass_host_loop with collection search"
72ansible-playbook -i host1,host2, -v test_bypass_host_loop.yml "$@"
73
74echo "--- validating inventory"
75# test collection inventories
76ansible-playbook inventory_test.yml -i a.statichost.yml -i redirected.statichost.yml "$@"
77
78# test plugin loader redirect_list
79ansible-playbook test_redirect_list.yml -v "$@"
80
81# test adjacent with --playbook-dir
82export ANSIBLE_COLLECTIONS_PATH=''
83ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=1 ansible-inventory --list --export --playbook-dir=. -v "$@"
84
85# use an inventory source with caching enabled
86ansible-playbook -i a.statichost.yml -i ./cache.statichost.yml -v check_populated_inventory.yml
87
88# Check that the inventory source with caching enabled was stored
89if [[ "$(find ./inventory_cache -type f ! -path "./inventory_cache/.keep" | wc -l)" -ne "1" ]]; then
90    echo "Failed to find the expected single cache"
91    exit 1
92fi
93
94CACHEFILE="$(find ./inventory_cache -type f ! -path './inventory_cache/.keep')"
95
96if [[ $CACHEFILE != ./inventory_cache/prefix_* ]]; then
97    echo "Unexpected cache file"
98    exit 1
99fi
100
101# Check the cache for the expected hosts
102
103if [[ "$(grep -wc "cache_host_a" "$CACHEFILE")" -ne "1" ]]; then
104    echo "Failed to cache host as expected"
105    exit 1
106fi
107
108if [[ "$(grep -wc "dynamic_host_a" "$CACHEFILE")" -ne "0" ]]; then
109    echo "Cached an incorrect source"
110    exit 1
111fi
112
113./vars_plugin_tests.sh
114
115