2019-03-28 18:41:39 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eux
|
|
|
|
|
2020-06-11 20:40:13 +02:00
|
|
|
export ANSIBLE_COLLECTIONS_PATH=$PWD/collection_root_user:$PWD/collection_root_sys
|
2019-03-28 18:41:39 +01:00
|
|
|
export ANSIBLE_GATHERING=explicit
|
|
|
|
export ANSIBLE_GATHER_SUBSET=minimal
|
2019-04-11 07:59:53 +02:00
|
|
|
export ANSIBLE_HOST_PATTERN_MISMATCH=error
|
2021-01-14 21:11:30 +01:00
|
|
|
unset ANSIBLE_COLLECTIONS_ON_ANSIBLE_VERSION_MISMATCH
|
2019-04-11 07:59:53 +02:00
|
|
|
|
2019-08-29 01:31:40 +02:00
|
|
|
# FUTURE: just use INVENTORY_PATH as-is once ansible-test sets the right dir
|
2020-05-14 17:45:02 +02:00
|
|
|
ipath=../../$(basename "${INVENTORY_PATH:-../../inventory}")
|
2019-04-11 07:59:53 +02:00
|
|
|
export INVENTORY_PATH="$ipath"
|
2019-03-28 18:41:39 +01:00
|
|
|
|
2020-12-15 01:30:13 +01:00
|
|
|
# ensure we can call collection module
|
|
|
|
ansible localhost -m testns.testcoll.testmodule
|
|
|
|
|
|
|
|
# ensure we can call collection module with ansible_collections in path
|
|
|
|
ANSIBLE_COLLECTIONS_PATH=$PWD/collection_root_sys/ansible_collections ansible localhost -m testns.testcoll.testmodule
|
|
|
|
|
|
|
|
|
2020-06-17 02:17:38 +02:00
|
|
|
echo "--- validating callbacks"
|
|
|
|
# validate FQ callbacks in ansible-playbook
|
2020-10-30 22:13:12 +01:00
|
|
|
ANSIBLE_CALLBACKS_ENABLED=testns.testcoll.usercallback ansible-playbook noop.yml | grep "usercallback says ok"
|
2020-06-17 02:17:38 +02:00
|
|
|
# use adhoc for the rest of these tests, must force it to load other callbacks
|
|
|
|
export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
|
|
|
|
# validate redirected callback
|
2020-10-30 22:13:12 +01:00
|
|
|
ANSIBLE_CALLBACKS_ENABLED=formerly_core_callback ansible localhost -m debug 2>&1 | grep -- "usercallback says ok"
|
2020-10-15 21:31:18 +02:00
|
|
|
## validate missing redirected callback
|
2020-10-30 22:13:12 +01:00
|
|
|
ANSIBLE_CALLBACKS_ENABLED=formerly_core_missing_callback ansible localhost -m debug 2>&1 | grep -- "Skipping callback plugin 'formerly_core_missing_callback'"
|
2020-10-15 21:31:18 +02:00
|
|
|
## validate redirected + removed callback (fatal)
|
2020-10-30 22:13:12 +01:00
|
|
|
ANSIBLE_CALLBACKS_ENABLED=formerly_core_removed_callback ansible localhost -m debug 2>&1 | grep -- "testns.testcoll.removedcallback has been removed"
|
2020-10-15 21:31:18 +02:00
|
|
|
# validate avoiding duplicate loading of callback, even if using diff names
|
2020-10-30 22:13:12 +01:00
|
|
|
[ "$(ANSIBLE_CALLBACKS_ENABLED=testns.testcoll.usercallback,formerly_core_callback ansible localhost -m debug 2>&1 | grep -c 'usercallback says ok')" = "1" ]
|
2020-06-17 02:17:38 +02:00
|
|
|
# ensure non existing callback does not crash ansible
|
2020-10-30 22:13:12 +01:00
|
|
|
ANSIBLE_CALLBACKS_ENABLED=charlie.gomez.notme ansible localhost -m debug 2>&1 | grep -- "Skipping callback plugin 'charlie.gomez.notme'"
|
|
|
|
|
2020-06-17 02:17:38 +02:00
|
|
|
unset ANSIBLE_LOAD_CALLBACK_PLUGINS
|
|
|
|
# adhoc normally shouldn't load non-default plugins- let's be sure
|
2020-11-02 17:26:25 +01:00
|
|
|
output=$(ANSIBLE_CALLBACKS_ENABLED=testns.testcoll.usercallback ansible localhost -m debug)
|
2020-06-17 02:17:38 +02:00
|
|
|
if [[ "${output}" =~ "usercallback says ok" ]]; then echo fail; exit 1; fi
|
2019-03-28 18:41:39 +01:00
|
|
|
|
2020-06-17 02:17:38 +02:00
|
|
|
echo "--- validating docs"
|
2019-05-03 03:15:58 +02:00
|
|
|
# test documentation
|
|
|
|
ansible-doc testns.testcoll.testmodule -vvv | grep -- "- normal_doc_frag"
|
2020-05-14 17:45:02 +02:00
|
|
|
# same with symlink
|
|
|
|
ln -s "${PWD}/testcoll2" ./collection_root_sys/ansible_collections/testns/testcoll2
|
|
|
|
ansible-doc testns.testcoll2.testmodule2 -vvv | grep "Test module"
|
|
|
|
# now test we can list with symlink
|
|
|
|
ansible-doc -l -vvv| grep "testns.testcoll2.testmodule2"
|
2019-05-03 03:15:58 +02:00
|
|
|
|
2019-08-21 11:45:04 +02:00
|
|
|
echo "testing bad doc_fragments (expected ERROR message follows)"
|
|
|
|
# test documentation failure
|
|
|
|
ansible-doc testns.testcoll.testmodule_bad_docfrags -vvv 2>&1 | grep -- "unknown doc_fragment"
|
|
|
|
|
2020-06-17 02:17:38 +02:00
|
|
|
echo "--- validating default collection"
|
|
|
|
# test adhoc default collection resolution (use unqualified collection module with playbook dir under its collection)
|
|
|
|
|
|
|
|
echo "testing adhoc default collection support with explicit playbook dir"
|
|
|
|
ANSIBLE_PLAYBOOK_DIR=./collection_root_user/ansible_collections/testns/testcoll ansible localhost -m testmodule
|
|
|
|
|
2019-04-11 07:59:53 +02:00
|
|
|
# we need multiple plays, and conditional import_playbook is noisy and causes problems, so choose here which one to use...
|
|
|
|
if [[ ${INVENTORY_PATH} == *.winrm ]]; then
|
|
|
|
export TEST_PLAYBOOK=windows.yml
|
|
|
|
else
|
|
|
|
export TEST_PLAYBOOK=posix.yml
|
2019-08-29 01:31:40 +02:00
|
|
|
|
|
|
|
echo "testing default collection support"
|
2020-05-26 18:42:06 +02:00
|
|
|
ansible-playbook -i "${INVENTORY_PATH}" collection_root_user/ansible_collections/testns/testcoll/playbooks/default_collection_playbook.yml "$@"
|
2019-04-11 07:59:53 +02:00
|
|
|
fi
|
|
|
|
|
2020-06-17 02:17:38 +02:00
|
|
|
echo "--- validating collections support in playbooks/roles"
|
2020-03-25 16:57:53 +01:00
|
|
|
# run test playbooks
|
2020-05-26 18:42:06 +02:00
|
|
|
ansible-playbook -i "${INVENTORY_PATH}" -v "${TEST_PLAYBOOK}" "$@"
|
2019-05-22 17:06:54 +02:00
|
|
|
|
2020-03-25 16:57:53 +01:00
|
|
|
if [[ ${INVENTORY_PATH} != *.winrm ]]; then
|
2020-05-26 18:42:06 +02:00
|
|
|
ansible-playbook -i "${INVENTORY_PATH}" -v invocation_tests.yml "$@"
|
2020-03-25 16:57:53 +01:00
|
|
|
fi
|
|
|
|
|
2020-10-19 16:54:24 +02:00
|
|
|
echo "--- validating bypass_host_loop with collection search"
|
|
|
|
ansible-playbook -i host1,host2, -v test_bypass_host_loop.yml "$@"
|
|
|
|
|
2020-06-17 02:17:38 +02:00
|
|
|
echo "--- validating inventory"
|
2020-05-26 18:42:06 +02:00
|
|
|
# test collection inventories
|
|
|
|
ansible-playbook inventory_test.yml -i a.statichost.yml -i redirected.statichost.yml "$@"
|
|
|
|
|
2020-10-28 16:46:39 +01:00
|
|
|
if [[ ${INVENTORY_PATH} != *.winrm ]]; then
|
|
|
|
# base invocation tests
|
|
|
|
ansible-playbook -i "${INVENTORY_PATH}" -v invocation_tests.yml "$@"
|
|
|
|
|
|
|
|
# run playbook from collection, test default again, but with FQCN
|
|
|
|
ansible-playbook -i "${INVENTORY_PATH}" testns.testcoll.default_collection_playbook.yml "$@"
|
|
|
|
|
|
|
|
# run playbook from collection, test default again, but with FQCN and no extension
|
|
|
|
ansible-playbook -i "${INVENTORY_PATH}" testns.testcoll.default_collection_playbook "$@"
|
|
|
|
|
|
|
|
# run playbook that imports from collection
|
|
|
|
ansible-playbook -i "${INVENTORY_PATH}" import_collection_pb.yml "$@"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# test collection inventories
|
|
|
|
ansible-playbook inventory_test.yml -i a.statichost.yml -i redirected.statichost.yml "$@"
|
|
|
|
|
2019-05-22 17:06:54 +02:00
|
|
|
# test adjacent with --playbook-dir
|
2020-06-11 20:40:13 +02:00
|
|
|
export ANSIBLE_COLLECTIONS_PATH=''
|
2020-05-26 18:42:06 +02:00
|
|
|
ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=1 ansible-inventory --list --export --playbook-dir=. -v "$@"
|
2019-11-04 17:41:14 +01:00
|
|
|
|
2020-03-26 17:05:46 +01:00
|
|
|
# use an inventory source with caching enabled
|
|
|
|
ansible-playbook -i a.statichost.yml -i ./cache.statichost.yml -v check_populated_inventory.yml
|
|
|
|
|
|
|
|
# Check that the inventory source with caching enabled was stored
|
|
|
|
if [[ "$(find ./inventory_cache -type f ! -path "./inventory_cache/.keep" | wc -l)" -ne "1" ]]; then
|
|
|
|
echo "Failed to find the expected single cache"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
CACHEFILE="$(find ./inventory_cache -type f ! -path './inventory_cache/.keep')"
|
|
|
|
|
2020-06-11 16:40:25 +02:00
|
|
|
if [[ $CACHEFILE != ./inventory_cache/prefix_* ]]; then
|
|
|
|
echo "Unexpected cache file"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-03-26 17:05:46 +01:00
|
|
|
# Check the cache for the expected hosts
|
|
|
|
|
|
|
|
if [[ "$(grep -wc "cache_host_a" "$CACHEFILE")" -ne "1" ]]; then
|
|
|
|
echo "Failed to cache host as expected"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$(grep -wc "dynamic_host_a" "$CACHEFILE")" -ne "0" ]]; then
|
|
|
|
echo "Cached an incorrect source"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-11-04 17:41:14 +01:00
|
|
|
./vars_plugin_tests.sh
|