| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -eux |
| 4 |
|
| 5 |
export ANSIBLE_COLLECTIONS_PATH=$PWD/collection_root_user:$PWD/collection_root_sys |
| 6 |
export ANSIBLE_GATHERING=explicit |
| 7 |
export ANSIBLE_HOST_PATTERN_MISMATCH=error |
| 8 |
export NO_COLOR=1 |
| 9 |
unset ANSIBLE_COLLECTIONS_ON_ANSIBLE_VERSION_MISMATCH |
| 10 |
|
| 11 |
# ensure we can call collection module |
| 12 |
ansible localhost -m testns.testcoll.testmodule |
| 13 |
|
| 14 |
# ensure we can call collection module with ansible_collections in path |
| 15 |
ANSIBLE_COLLECTIONS_PATH=$PWD/collection_root_sys/ansible_collections ansible localhost -m testns.testcoll.testmodule |
| 16 |
|
| 17 |
|
| 18 |
echo "--- validating callbacks" |
| 19 |
# validate FQ callbacks in ansible-playbook |
| 20 |
ANSIBLE_CALLBACKS_ENABLED=testns.testcoll.usercallback ansible-playbook noop.yml | grep "usercallback says ok" |
| 21 |
# use adhoc for the rest of these tests, must force it to load other callbacks |
| 22 |
export ANSIBLE_LOAD_CALLBACK_PLUGINS=1 |
| 23 |
# validate redirected callback |
| 24 |
ANSIBLE_CALLBACKS_ENABLED=formerly_core_callback ansible localhost -m debug 2>&1 | grep -- "usercallback says ok" |
| 25 |
## validate missing redirected callback |
| 26 |
ANSIBLE_CALLBACKS_ENABLED=formerly_core_missing_callback ansible localhost -m debug 2>&1 | grep -- "Skipping callback plugin 'formerly_core_missing_callback'" |
| 27 |
## validate redirected + removed callback (fatal) |
| 28 |
ANSIBLE_CALLBACKS_ENABLED=formerly_core_removed_callback ansible localhost -m debug 2>&1 | grep -- "The 'testns.testcoll.removedcallback' callback plugin has been removed" |
| 29 |
# validate avoiding duplicate loading of callback, even if using diff names |
| 30 |
[ "$(ANSIBLE_CALLBACKS_ENABLED=testns.testcoll.usercallback,formerly_core_callback ansible localhost -m debug 2>&1 | grep -c 'usercallback says ok')" = "1" ] |
| 31 |
# ensure non existing callback does not crash ansible |
| 32 |
ANSIBLE_CALLBACKS_ENABLED=charlie.gomez.notme ansible localhost -m debug 2>&1 | grep -- "Skipping callback plugin 'charlie.gomez.notme'" |
| 33 |
|
| 34 |
# test configuring collection callback plugins with extra variables |
| 35 |
ANSIBLE_CALLBACKS_ENABLED=testns.testcoll.usercallback ansible-playbook noop.yml --extra-vars 'on_ok="configured msg"' | grep 'configured msg' |
| 36 |
|
| 37 |
unset ANSIBLE_LOAD_CALLBACK_PLUGINS |
| 38 |
# adhoc normally shouldn't load non-default plugins- let's be sure |
| 39 |
output=$(ANSIBLE_CALLBACKS_ENABLED=testns.testcoll.usercallback ansible localhost -m debug) |
| 40 |
if [[ "${output}" =~ "usercallback says ok" ]]; then echo fail; exit 1; fi |
| 41 |
|
| 42 |
echo "--- validating docs" |
| 43 |
# test documentation |
| 44 |
ansible-doc testns.testcoll.testmodule -vvv | grep -- "- normal_doc_frag" |
| 45 |
# same with symlink |
| 46 |
ln -s "${PWD}/testcoll2" ./collection_root_sys/ansible_collections/testns/testcoll2 |
| 47 |
ansible-doc testns.testcoll2.testmodule2 -vvv | grep "Test module" |
| 48 |
# now test we can list with symlink |
| 49 |
ansible-doc -l -vvv| grep "testns.testcoll2.testmodule2" |
| 50 |
|
| 51 |
echo "testing bad doc_fragments (expected ERROR message follows)" |
| 52 |
# test documentation failure |
| 53 |
ansible-doc testns.testcoll.testmodule_bad_docfrags -vvv 2>&1 | grep -- "unknown doc_fragment" |
| 54 |
|
| 55 |
echo "--- validating default collection" |
| 56 |
# test adhoc default collection resolution (use unqualified collection module with playbook dir under its collection) |
| 57 |
|
| 58 |
echo "testing adhoc default collection support with explicit playbook dir" |
| 59 |
ANSIBLE_PLAYBOOK_DIR=./collection_root_user/ansible_collections/testns/testcoll ansible localhost -m testmodule |
| 60 |
|
| 61 |
# we need multiple plays, and conditional import_playbook is noisy and causes problems, so choose here which one to use... |
| 62 |
if [[ ${INVENTORY_PATH} == *.winrm ]]; then |
| 63 |
export TEST_PLAYBOOK=windows.yml |
| 64 |
else |
| 65 |
export TEST_PLAYBOOK=posix.yml |
| 66 |
|
| 67 |
echo "testing default collection support" |
| 68 |
ansible-playbook -i "${INVENTORY_PATH}" collection_root_user/ansible_collections/testns/testcoll/playbooks/default_collection_playbook.yml "$@" |
| 69 |
fi |
| 70 |
|
| 71 |
# test redirects and warnings for filter redirects |
| 72 |
echo "testing redirect and deprecation display" |
| 73 |
ANSIBLE_DEPRECATION_WARNINGS=yes ansible localhost -m debug -a msg='{{ "data" | testns.testredirect.multi_redirect_filter }}' -vvvvv 2>&1 | tee out.txt |
| 74 |
cat out.txt |
| 75 |
|
| 76 |
test "$(grep out.txt -ce 'deprecation1' -ce 'deprecation2' -ce 'deprecation3')" == 3 |
| 77 |
grep out.txt -e 'redirecting (type: filter) testns.testredirect.multi_redirect_filter to testns.testredirect.redirect_filter1' |
| 78 |
grep out.txt -e 'Filter "testns.testredirect.multi_redirect_filter"' |
| 79 |
grep out.txt -e 'redirecting (type: filter) testns.testredirect.redirect_filter1 to testns.testredirect.redirect_filter2' |
| 80 |
grep out.txt -e 'Filter "testns.testredirect.redirect_filter1"' |
| 81 |
grep out.txt -e 'redirecting (type: filter) testns.testredirect.redirect_filter2 to testns.testcoll.testfilter' |
| 82 |
grep out.txt -e 'Filter "testns.testredirect.redirect_filter2"' |
| 83 |
|
| 84 |
echo "--- validating collections support in playbooks/roles" |
| 85 |
# run test playbooks |
| 86 |
ansible-playbook -i "${INVENTORY_PATH}" -v "${TEST_PLAYBOOK}" "$@" |
| 87 |
|
| 88 |
if [[ ${INVENTORY_PATH} != *.winrm ]]; then |
| 89 |
ansible-playbook -i "${INVENTORY_PATH}" -v invocation_tests.yml "$@" |
| 90 |
fi |
| 91 |
|
| 92 |
echo "--- validating bypass_host_loop with collection search" |
| 93 |
ansible-playbook -i host1,host2, -v test_bypass_host_loop.yml "$@" |
| 94 |
|
| 95 |
echo "--- validating inventory" |
| 96 |
# test collection inventories |
| 97 |
ansible-playbook inventory_test.yml -i a.statichost.yml -i redirected.statichost.yml "$@" |
| 98 |
|
| 99 |
if [[ ${INVENTORY_PATH} != *.winrm ]]; then |
| 100 |
# base invocation tests |
| 101 |
ansible-playbook -i "${INVENTORY_PATH}" -v invocation_tests.yml "$@" |
| 102 |
|
| 103 |
# run playbook from collection, test default again, but with FQCN |
| 104 |
ansible-playbook -i "${INVENTORY_PATH}" testns.testcoll.default_collection_playbook.yml "$@" |
| 105 |
|
| 106 |
# run playbook from collection, test default again, but with FQCN and no extension |
| 107 |
ansible-playbook -i "${INVENTORY_PATH}" testns.testcoll.default_collection_playbook "$@" |
| 108 |
|
| 109 |
# run playbook that imports from collection |
| 110 |
ansible-playbook -i "${INVENTORY_PATH}" import_collection_pb.yml "$@" |
| 111 |
fi |
| 112 |
|
| 113 |
# test collection inventories |
| 114 |
ansible-playbook inventory_test.yml -i a.statichost.yml -i redirected.statichost.yml "$@" |
| 115 |
|
| 116 |
# test plugin loader redirect_list |
| 117 |
ansible-playbook test_redirect_list.yml -v "$@" |
| 118 |
|
| 119 |
# test ansiballz cache dupe |
| 120 |
ansible-playbook ansiballz_dupe/test_ansiballz_cache_dupe_shortname.yml -v "$@" |
| 121 |
|
| 122 |
# test adjacent with --playbook-dir |
| 123 |
export ANSIBLE_COLLECTIONS_PATH='' |
| 124 |
ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=1 ansible-inventory --list --export --playbook-dir=. -v "$@" |
| 125 |
|
| 126 |
# use an inventory source with caching enabled |
| 127 |
ansible-playbook -i a.statichost.yml -i ./cache.statichost.yml -v check_populated_inventory.yml |
| 128 |
|
| 129 |
# Check that the inventory source with caching enabled was stored |
| 130 |
if [[ "$(find ./inventory_cache -type f ! -path "./inventory_cache/.keep" | wc -l)" -ne "1" ]]; then |
| 131 |
echo "Failed to find the expected single cache" |
| 132 |
exit 1 |
| 133 |
fi |
| 134 |
|
| 135 |
CACHEFILE="$(find ./inventory_cache -type f ! -path './inventory_cache/.keep')" |
| 136 |
|
| 137 |
if [[ $CACHEFILE != ./inventory_cache/prefix_* ]]; then |
| 138 |
echo "Unexpected cache file" |
| 139 |
exit 1 |
| 140 |
fi |
| 141 |
|
| 142 |
# Check the cache for the expected hosts |
| 143 |
|
| 144 |
if [[ "$(grep -wc "cache_host_a" "$CACHEFILE")" -ne "1" ]]; then |
| 145 |
echo "Failed to cache host as expected" |
| 146 |
exit 1 |
| 147 |
fi |
| 148 |
|
| 149 |
if [[ "$(grep -wc "dynamic_host_a" "$CACHEFILE")" -ne "0" ]]; then |
| 150 |
echo "Cached an incorrect source" |
| 151 |
exit 1 |
| 152 |
fi |
| 153 |
|
| 154 |
./vars_plugin_tests.sh |
| 155 |
|
| 156 |
./test_task_resolved_plugin.sh |
| 157 |
|
| 158 |
# Test tombstoned module/action plugins include the location of the fatal task |
| 159 |
cat <<EOF > test_dead_ping_error.yml |
| 160 |
- hosts: localhost |
| 161 |
gather_facts: no |
| 162 |
tasks: |
| 163 |
- dead_ping: |
| 164 |
EOF |
| 165 |
ansible-playbook test_dead_ping_error.yml 2>&1 >/dev/null | grep -e ':4:5' |
| 166 |
echo PASS |