| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -eux |
| 4 |
set -o pipefail |
| 5 |
|
| 6 |
# http://unix.stackexchange.com/questions/30091/fix-or-alternative-for-mktemp-in-os-x |
| 7 |
temp_dir=$(shell mktemp -d 2>/dev/null || mktemp -d -t 'ansible-testing-XXXXXXXXXX') |
| 8 |
trap 'rm -rf "${temp_dir}"' EXIT |
| 9 |
|
| 10 |
repo_dir="${temp_dir}/repo" |
| 11 |
pull_dir="${temp_dir}/pull" |
| 12 |
temp_log="${temp_dir}/pull.log" |
| 13 |
|
| 14 |
ansible-playbook setup.yml -i ../../inventory |
| 15 |
|
| 16 |
cleanup="$(pwd)/cleanup.yml" |
| 17 |
|
| 18 |
trap 'ansible-playbook "${cleanup}" -i ../../inventory' EXIT |
| 19 |
|
| 20 |
cp -av "pull-integration-test" "${repo_dir}" |
| 21 |
cd "${repo_dir}" |
| 22 |
( |
| 23 |
git init |
| 24 |
git config user.email "[email protected]" |
| 25 |
git config user.name "Ansible Test Runner" |
| 26 |
git add . |
| 27 |
git commit -m "Initial commit." |
| 28 |
) |
| 29 |
|
| 30 |
function change_repo { |
| 31 |
cd "${repo_dir}" |
| 32 |
date > forced_change |
| 33 |
git add forced_change |
| 34 |
git commit -m "forced changed" |
| 35 |
cd - |
| 36 |
} |
| 37 |
|
| 38 |
function no_change_tests { |
| 39 |
# test for https://github.com/ansible/ansible/issues/13688 |
| 40 |
if grep MAGICKEYWORD "${temp_log}"; then |
| 41 |
cat "${temp_log}" |
| 42 |
echo "Ran the playbook, found MAGICKEYWORD in output." |
| 43 |
exit 1 |
| 44 |
fi |
| 45 |
} |
| 46 |
|
| 47 |
function pass_tests { |
| 48 |
# test for https://github.com/ansible/ansible/issues/13688 |
| 49 |
if ! grep MAGICKEYWORD "${temp_log}"; then |
| 50 |
cat "${temp_log}" |
| 51 |
echo "Missing MAGICKEYWORD in output." |
| 52 |
exit 1 |
| 53 |
fi |
| 54 |
|
| 55 |
# test for https://github.com/ansible/ansible/issues/13681 |
| 56 |
# match play default output stats, was matching limit + docker |
| 57 |
if grep -E '127\.0\.0\.1\s*: ok=' "${temp_log}"; then |
| 58 |
cat "${temp_log}" |
| 59 |
echo "Found host 127.0.0.1 in output. Only localhost should be present." |
| 60 |
exit 1 |
| 61 |
fi |
| 62 |
# make sure one host was run |
| 63 |
if ! grep -E 'localhost.*ok' "${temp_log}"; then |
| 64 |
cat "${temp_log}" |
| 65 |
echo "Did not find host localhost in output." |
| 66 |
exit 1 |
| 67 |
fi |
| 68 |
} |
| 69 |
|
| 70 |
function pass_tests_multi { |
| 71 |
# test for https://github.com/ansible/ansible/issues/72708 |
| 72 |
if ! grep 'test multi_play_1' "${temp_log}"; then |
| 73 |
cat "${temp_log}" |
| 74 |
echo "Did not run multiple playbooks" |
| 75 |
exit 1 |
| 76 |
fi |
| 77 |
if ! grep 'test multi_play_2' "${temp_log}"; then |
| 78 |
cat "${temp_log}" |
| 79 |
echo "Did not run multiple playbooks" |
| 80 |
exit 1 |
| 81 |
fi |
| 82 |
} |
| 83 |
|
| 84 |
export ANSIBLE_INVENTORY |
| 85 |
export ANSIBLE_HOST_PATTERN_MISMATCH |
| 86 |
|
| 87 |
unset ANSIBLE_INVENTORY |
| 88 |
unset ANSIBLE_HOST_PATTERN_MISMATCH |
| 89 |
|
| 90 |
ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" "$@" | tee "${temp_log}" |
| 91 |
|
| 92 |
pass_tests |
| 93 |
|
| 94 |
# ensure complex extra vars work |
| 95 |
PASSWORD='test' |
| 96 |
USER=${USER:-'broken_docker'} |
| 97 |
JSON_EXTRA_ARGS='{"docker_registries_login": [{ "docker_password": "'"${PASSWORD}"'", "docker_username": "'"${USER}"'", "docker_registry_url":"repository-manager.company.com:5001"}], "docker_registries_logout": [{ "docker_password": "'"${PASSWORD}"'", "docker_username": "'"${USER}"'", "docker_registry_url":"repository-manager.company.com:5001"}] }' |
| 98 |
|
| 99 |
ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" -e "${JSON_EXTRA_ARGS}" "$@" --tags untagged,test_ev | tee "${temp_log}" |
| 100 |
|
| 101 |
pass_tests |
| 102 |
|
| 103 |
ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" "$@" multi_play_1.yml multi_play_2.yml | tee "${temp_log}" |
| 104 |
|
| 105 |
pass_tests_multi |
| 106 |
|
| 107 |
ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" conn_secret.yml --connection-password-file "${repo_dir}/secret_connection_password" "$@" |
| 108 |
|
| 109 |
# fail if we try do delete /var/tmp |
| 110 |
ANSIBLE_CONFIG='' ansible-pull -d var/tmp -U "${repo_dir}" --purge "$@" |
| 111 |
|
| 112 |
# test flushing the fact cache |
| 113 |
export ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION=./ |
| 114 |
ansible-pull -d "${pull_dir}" -U "${repo_dir}" "$@" gather_facts.yml |
| 115 |
ansible-pull -d "${pull_dir}" -U "${repo_dir}" --flush-cache "$@" test_empty_facts.yml |
| 116 |
unset ANSIBLE_CACHE_PLUGIN ANSIBLE_CACHE_PLUGIN_CONNECTION |
| 117 |
|
| 118 |
#### CHACHCHCHANGES! |
| 119 |
echo 'setup for change detection' |
| 120 |
ORIG_CONFIG="${ANSIBLE_CONFIG}" |
| 121 |
unset ANSIBLE_CONFIG |
| 122 |
|
| 123 |
echo 'test no run on no changes' |
| 124 |
ansible-pull -d "${pull_dir}" -U "${repo_dir}" --only-if-changed "$@" | tee "${temp_log}" |
| 125 |
no_change_tests |
| 126 |
|
| 127 |
echo 'test run on changes' |
| 128 |
change_repo |
| 129 |
ansible-pull -d "${pull_dir}" -U "${repo_dir}" --only-if-changed "$@" | tee "${temp_log}" |
| 130 |
pass_tests |
| 131 |
|
| 132 |
# test changed with non yaml result format, ensures we ignore callback or format changes for adhoc/change detection |
| 133 |
echo 'test no run on no changes, yaml result format' |
| 134 |
ANSIBLE_CALLBACK_RESULT_FORMAT='yaml' ansible-pull -d "${pull_dir}" -U "${repo_dir}" --only-if-changed "$@" | tee "${temp_log}" |
| 135 |
no_change_tests |
| 136 |
|
| 137 |
echo 'test run on changes, yaml result format' |
| 138 |
change_repo |
| 139 |
ANSIBLE_CALLBACK_RESULT_FORMAT='yaml' ansible-pull -d "${pull_dir}" -U "${repo_dir}" --only-if-changed "$@" | tee "${temp_log}" |
| 140 |
pass_tests |
| 141 |
|
| 142 |
if [ "${ORIG_CONFIG}" != "" ]; then |
| 143 |
export ANSIBLE_CONFIG="${ORIG_CONFIG}" |
| 144 |
fi |