11#! /bin/bash -eu
22
33SCRIPTDIR=" $( readlink -f " $( dirname " $0 " ) " ) "
4- TOPDIR=" $( readlink -f " ${SCRIPTDIR} /.." ) "
4+ TOPDIR=" $( readlink -f " ${SCRIPTDIR} /../.." ) "
5+ UTILSDIR=" ${SCRIPTDIR} "
56
6- # shellcheck source=utils/shfun
7- . " ${SCRIPTDIR} /shfun"
8- # shellcheck source=utils/shcontainer
9- . " ${SCRIPTDIR} /shcontainer"
10- # shellcheck source=utils/shansible
11- . " ${SCRIPTDIR} /shansible"
7+ # shellcheck source=infra/image/shfun
8+ . " ${UTILSDIR} /shfun"
9+ # shellcheck source=infra/image/shcontainer
10+ . " ${UTILSDIR} /shcontainer"
11+
12+ set -o errexit -o errtrace
13+
14+ trap interrupt_exception SIGINT
15+
16+ interrupt_exception () {
17+ trap - ERR SIGINT
18+ log warn " User interrupted test execution."
19+ # shellcheck disable=SC2119
20+ cleanup " ${scenario: +${scenario} } "
21+ exit 1
22+ }
23+
24+ trap cleanup ERR EXIT SIGABRT SIGTERM SIGQUIT
25+
26+ # shellcheck disable=SC2120
27+ cleanup () {
28+ trap - ERR EXIT SIGABRT SIGTERM SIGQUIT
29+ local container
30+ log info " Cleaning up environment"
31+ container=" ${1:- ${scenario: +${scenario} } } "
32+ if [ " ${STOP_CONTAINER:- " Y" } " == " Y" ] && [ -n " ${container} " ]
33+ then
34+ run_if_exists container_stop " ${container} "
35+ [ -f " ${inventory:- } " ] && rm " ${inventory} "
36+ else
37+ if [ -n " ${container} " ]
38+ then
39+ log info " Keeping container: $( podman ps --format " {{.Names}} - {{.ID}}" --filter " name=${container} " ) "
40+ fi
41+ fi
42+ if [ " ${STOP_VIRTUALENV:- " N" } " == " Y" ]
43+ then
44+ echo " Deactivating virtual environment"
45+ run_if_exists deactivate
46+ fi
47+ }
1248
1349usage () {
1450 local prog=" ${0##*/ } "
1551 cat << EOF
16- usage: ${prog} [-h] [-l] [-e] [-K] [-A|-a ANSIBLE] [-p INTERPRETER] [-c CONTAINER] [-s TESTS_SUITE] [-x] [-S SEED.GRP] [-i IMAGE] [-m MEMORY] [- v...] [TEST...]
17- ${prog} runs playbook(s) TEST using an ansible-freeipa testing image .
52+ usage: ${prog} [-h] [-l] [-e] [-K] [-A|-a ANSIBLE] [-p INTERPRETER] [-c CONTAINER] [-s TESTS_SUITE] [-x] [-S SEED.GRP] [-i IMAGE] [-v...] [TEST...]
53+ ${prog} runs test playbooks using an ansible-freeipa testing images .
1854
1955EOF
2056}
@@ -33,10 +69,11 @@ optional arguments:
3369 -A Do not install Ansible, use host's provided one.
3470 -c CONTAINER use container CONTAINER to run tests
3571 -K keep container, even if tests succeed
36- -l list available images
72+ -L list available images
73+ -l Try to use local image first, if not found download.
3774 -e force recreation of the virtual environment
38- -i IMAGE select image to run the tests (default: fedora-latest)
39- -m MEMORY container memory, in GiB (default: 3 )
75+ -i IMAGE select image to run the tests
76+ (default: fedora-latest-server )
4077 -p INTERPRETER Python interpreter to use on target container
4178 -s TEST_SUITE run all playbooks for test suite, which is a directory
4279 under ${WHITE} tests${RST}
4784) "
4885}
4986
87+ install_ansible () {
88+ ANSIBLE_VERSION=" ${1:- ${ANSIBLE_VERSION:- " ansible-core" } } "
89+ [ $# -gt 0 ] && shift
90+ log info " Installing Ansible: ${ANSIBLE_VERSION} "
91+ pip install --quiet " ${ANSIBLE_VERSION} "
92+ log debug " Ansible version: $( ansible --version | sed -n " 1p" ) ${RST} "
93+
94+ if [ -n " ${ANSIBLE_COLLECTIONS} " ]
95+ then
96+ collections_path=" $( mktemp -d) "
97+ for collection in ${ANSIBLE_COLLECTIONS}
98+ do
99+ if ! quiet ansible-galaxy collection verify --offline " ${collection} "
100+ then
101+ log info " Installing: Ansible Collection ${collection} "
102+ # shellcheck disable=SC2086
103+ quiet ansible-galaxy collection install \
104+ -p " ${collections_path} " \
105+ " ${collection} " || die " Failed to install Ansible collection: ${collection} "
106+ fi
107+ done
108+ export ANSIBLE_COLLECTIONS_PATH=" ${collections_path} :${ANSIBLE_COLLECTIONS_PATH:- } "
109+ fi
110+ export ANSIBLE_VERSION
111+ }
112+
50113
51114# Defaults
52115verbose=" "
53- engine=" ${engine:- " podman" } "
54116CONTINUE_ON_ERROR=" "
55117STOP_CONTAINER=" Y"
56118STOP_VIRTUALENV=" N"
119+ FORCE_ENV=" N"
57120declare -a ENABLED_MODULES
58121declare -a ENABLED_TESTS
59122read -r -a ENABLED_MODULES <<< " ${IPA_ENABLED_MODULES:-" " }"
60123read -r -a ENABLED_TESTS <<< " ${IPA_ENABLED_MODULES:-" " }"
61- IMAGE_TAG=" fedora-latest"
62- scenario=" freeipa-tests"
63- MEMORY=3
124+ IMAGE_TAG=" fedora-latest-server"
125+ scenario=" ansible-freeipa-tests"
64126IPA_HOSTNAME=" ipaserver.test.local"
65127SEED=" $( date " +%Y%m%d" ) "
66128GROUP=1
67129SPLITS=0
68- ANSIBLE_COLLECTIONS=${ANSIBLE_COLLECTIONS:- " ${engine_collection} " }
130+ ANSIBLE_COLLECTIONS=${ANSIBLE_COLLECTIONS:- " containers.podman " }
69131SKIP_ANSIBLE=" "
70- ansible_interpreter=" /usr/bin/python3"
71132EXTRA_OPTIONS=" "
72133unset ANSIBLE_VERSION
134+ unset ansible_interpreter
73135
74136# Process command options
75137
89151 e) FORCE_ENV=" Y" ;;
90152 i) IMAGE_TAG=" ${OPTARG} " ;;
91153 K) STOP_CONTAINER=" N" ;;
92- l) " ${SCRIPTDIR} " /setup_test_container.sh -l && exit 0 || exit 1 ;;
93- m) MEMORY=" ${OPTARG} " ;;
154+ l) list_images && exit 0 || exit 1 ;;
94155 p) ansible_interpreter=" ${OPTARG} " ;;
95156 s)
96157 [ ${SPLITS} -ne 0 ] && die -u " Can't use '-S' with '-s'"
@@ -131,16 +192,22 @@ done
131192
132193[ ${SPLITS} -eq 0 ] && [ ${# ENABLED_MODULES[@]} -eq 0 ] && [ ${# ENABLED_TESTS[@]} -eq 0 ] && die -u " No test defined."
133194
134- export STOP_CONTAINER FORCE_ENV STOP_VIRTUALENV ansible_interpreter
195+ export STOP_CONTAINER STOP_VIRTUALENV
135196
136197# Ensure $python is set
137- [ -z " ${python} " ] && python=" python3"
198+ for py in " python3" " python"
199+ do
200+ python=" $( command -v " ${py} " ) "
201+ [ -n " ${python} " ] && break
202+ done
138203
139204log info " Controller Python executable: ${python} "
140205${python} --version
141206
142207# Prepare virtual environment
143- start_virtual_environment
208+ declare -a venv_opts=()
209+ [ " ${FORCE_ENV} " == " Y" ] && venv_opts+=(" -f" )
210+ start_virtual_environment " ${venv_opts[@]} "
144211log info " Installing dependencies from 'requirements-tests.txt'"
145212pip install --upgrade -r " ${TOPDIR} /requirements-tests.txt"
146213
@@ -151,17 +218,32 @@ export ANSIBLE_ROLES_PATH="${TOPDIR}/roles"
151218export ANSIBLE_LIBRARY=" ${TOPDIR} /plugins"
152219export ANSIBLE_MODULE_UTILS=" ${TOPDIR} /plugins/module_utils"
153220
154- # Start container
155- " ${SCRIPTDIR} /setup_test_container.sh" -e " ${engine} " -m " ${MEMORY} " -p " ${ansible_interpreter} " -i " ${IMAGE_TAG} " -n " ${IPA_HOSTNAME} " -a " ${scenario} " || die " Failed to setup test container"
156-
221+ # Ensure container is up and running
222+ if [ " ${FORCE_ENV:- " N" } " != " Y" ]
223+ then
224+ state=" $( container_get_state " ${scenario} " ) "
225+ case " ${state} " in
226+ exited)
227+ podman start " ${scenario} "
228+ container_wait_for_journald " ${scenario} "
229+ container_wait_up " ${scenario} "
230+ ;;
231+ running) ;; # container is running, nothing to do
232+ * )
233+ # assume container needs to be recreated
234+ " ${TOPDIR} /infra/image/start.sh" -l " ${IMAGE_TAG} " -n " ${IPA_HOSTNAME} "
235+ ;;
236+ esac
237+ else
238+ " ${TOPDIR} /infra/image/start.sh" -l " ${IMAGE_TAG} " -n " ${IPA_HOSTNAME} "
239+ fi
157240
158241# run tests
159242RESULT=0
160243
161- export RUN_TESTS_IN_DOCKER=${engine}
244+ export RUN_TESTS_IN_DOCKER=podman
162245export IPA_SERVER_HOST=" ${scenario} "
163- # Ensure proper ansible_python_interpreter is used by pytest.
164- export IPA_PYTHON_PATH=" ${ansible_interpreter} "
246+ [ -z " ${ansible_interpreter:- } " ] || export IPA_PYTHON_PATH=" ${ansible_interpreter} "
165247
166248if [ ${SPLITS} -ne 0 ]
167249then
@@ -184,13 +266,20 @@ IPA_VERBOSITY="${verbose}"
184266[ -n " ${IPA_VERBOSITY} " ] && export IPA_VERBOSITY
185267
186268# shellcheck disable=SC2086
187- if ! pytest -m " playbook" --verbose --color=yes --suppress-no-test-exit-code --junit-xml=TEST-results-group-${GROUP:- 1} .xml ${EXTRA_OPTIONS}
269+ if ! pytest -m " playbook" \
270+ --verbose \
271+ --color=yes \
272+ --suppress-no-test-exit-code \
273+ --junit-xml=TEST-results-group-${GROUP:- 1} .xml \
274+ ${EXTRA_OPTIONS}
188275then
189276 RESULT=2
277+ export STOP_CONTAINER=" N"
190278 log error " Container not stopped for verification: ${scenario} "
191- log info " Container: $( ${engine} ps -f " name=${scenario} " --format " {{.Names}} - {{.ID}}" ) "
279+ log info " Container: $( podman ps -f " name=${scenario} " --format " {{.Names}} - {{.ID}}" ) "
192280fi
193- [ -z " ${CONTINUE_ON_ERROR} " ] && [ $RESULT -ne 0 ] && die " Stopping on test failure."
194281
195- # cleanup environment
196- cleanup " ${scenario} " " ${engine} "
282+ if [ -z " ${CONTINUE_ON_ERROR} " ] && [ $RESULT -ne 0 ]
283+ then
284+ die " Stopping on test failure."
285+ fi
0 commit comments