Skip to content

Commit af5d489

Browse files
committed
run-tests.sh: Update to current CI procedures
The objective of the 'run-tests.sh' script is to provide for a developer a similar environment as the one used by the Azure CI to test the modules and roles. This patch updates the script to use the same tools as are being used after the latest change in the Azure CI.
1 parent 33c1c00 commit af5d489

File tree

8 files changed

+120
-349
lines changed

8 files changed

+120
-349
lines changed
Lines changed: 107 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,56 @@
11
#!/bin/bash -eu
22

33
SCRIPTDIR="$(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

1349
usage() {
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
1955
EOF
2056
}
@@ -33,10 +69,10 @@ 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
3875
-i IMAGE select image to run the tests (default: fedora-latest)
39-
-m MEMORY container memory, in GiB (default: 3)
4076
-p INTERPRETER Python interpreter to use on target container
4177
-s TEST_SUITE run all playbooks for test suite, which is a directory
4278
under ${WHITE}tests${RST}
@@ -47,10 +83,35 @@ EOF
4783
)"
4884
}
4985

86+
install_ansible() {
87+
ANSIBLE_VERSION="${1:-${ANSIBLE_VERSION:-"ansible-core"}}"
88+
[ $# -gt 0 ] && shift
89+
log info "Installing Ansible: ${ANSIBLE_VERSION}"
90+
pip install --quiet "${ANSIBLE_VERSION}"
91+
log debug "Ansible version: $(ansible --version | sed -n "1p")${RST}"
92+
93+
if [ -n "${ANSIBLE_COLLECTIONS}" ]
94+
then
95+
collections_path="$(mktemp -d)"
96+
export ANSIBLE_COLLECTIONS_PATH="${collections_path}:${ANSIBLE_COLLECTIONS_PATH:-}"
97+
for collection in ${ANSIBLE_COLLECTIONS}
98+
do
99+
echo "${collection}"
100+
quiet ansible-galaxy collection verify "${collection}" && continue
101+
log none "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+
log info "OH YEAH!"
107+
done
108+
fi
109+
export ANSIBLE_VERSION
110+
}
111+
50112

51113
# Defaults
52114
verbose=""
53-
engine="${engine:-"podman"}"
54115
CONTINUE_ON_ERROR=""
55116
STOP_CONTAINER="Y"
56117
STOP_VIRTUALENV="N"
@@ -59,17 +120,16 @@ declare -a ENABLED_TESTS
59120
read -r -a ENABLED_MODULES <<< "${IPA_ENABLED_MODULES:-""}"
60121
read -r -a ENABLED_TESTS <<< "${IPA_ENABLED_MODULES:-""}"
61122
IMAGE_TAG="fedora-latest"
62-
scenario="freeipa-tests"
63-
MEMORY=3
123+
scenario="ansible-freeipa-tests"
64124
IPA_HOSTNAME="ipaserver.test.local"
65125
SEED="$(date "+%Y%m%d")"
66126
GROUP=1
67127
SPLITS=0
68-
ANSIBLE_COLLECTIONS=${ANSIBLE_COLLECTIONS:-"${engine_collection}"}
128+
ANSIBLE_COLLECTIONS=${ANSIBLE_COLLECTIONS:-"containers.podman"}
69129
SKIP_ANSIBLE=""
70-
ansible_interpreter="/usr/bin/python3"
71130
EXTRA_OPTIONS=""
72131
unset ANSIBLE_VERSION
132+
unset ansible_interpreter
73133

74134
# Process command options
75135

@@ -89,8 +149,7 @@ do
89149
e) FORCE_ENV="Y" ;;
90150
i) IMAGE_TAG="${OPTARG}" ;;
91151
K) STOP_CONTAINER="N" ;;
92-
l) "${SCRIPTDIR}"/setup_test_container.sh -l && exit 0 || exit 1 ;;
93-
m) MEMORY="${OPTARG}" ;;
152+
l) list_images && exit 0 || exit 1 ;;
94153
p) ansible_interpreter="${OPTARG}" ;;
95154
s)
96155
[ ${SPLITS} -ne 0 ] && die -u "Can't use '-S' with '-s'"
@@ -131,10 +190,14 @@ done
131190

132191
[ ${SPLITS} -eq 0 ] && [ ${#ENABLED_MODULES[@]} -eq 0 ] && [ ${#ENABLED_TESTS[@]} -eq 0 ] && die -u "No test defined."
133192

134-
export STOP_CONTAINER FORCE_ENV STOP_VIRTUALENV ansible_interpreter
193+
export STOP_CONTAINER FORCE_ENV STOP_VIRTUALENV
135194

136195
# Ensure $python is set
137-
[ -z "${python}" ] && python="python3"
196+
for py in "python3" "python"
197+
do
198+
python="$(command -v "${py}")"
199+
[ -n "${python}" ] && break
200+
done
138201

139202
log info "Controller Python executable: ${python}"
140203
${python} --version
@@ -151,17 +214,23 @@ export ANSIBLE_ROLES_PATH="${TOPDIR}/roles"
151214
export ANSIBLE_LIBRARY="${TOPDIR}/plugins"
152215
export ANSIBLE_MODULE_UTILS="${TOPDIR}/plugins/module_utils"
153216

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-
217+
# Check if container is running
218+
state="$(container_get_state "${scenario}")"
219+
case "${state}" in
220+
exited) podman start "${scenario}" ;;
221+
running) ;; # container is running, nothing to do
222+
*)
223+
# assume container needs to be recreated
224+
"${TOPDIR}/infra/image/start.sh" -l "${IMAGE_TAG}" -n "${IPA_HOSTNAME}"
225+
;;
226+
esac
157227

158228
# run tests
159229
RESULT=0
160230

161-
export RUN_TESTS_IN_DOCKER=${engine}
231+
export RUN_TESTS_IN_DOCKER=podman
162232
export IPA_SERVER_HOST="${scenario}"
163-
# Ensure proper ansible_python_interpreter is used by pytest.
164-
export IPA_PYTHON_PATH="${ansible_interpreter}"
233+
[ -z "${ansible_interpreter:-}" ] || export IPA_PYTHON_PATH="${ansible_interpreter}"
165234

166235
if [ ${SPLITS} -ne 0 ]
167236
then
@@ -184,13 +253,20 @@ IPA_VERBOSITY="${verbose}"
184253
[ -n "${IPA_VERBOSITY}" ] && export IPA_VERBOSITY
185254

186255
# 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}
256+
if ! pytest -m "playbook" \
257+
--verbose \
258+
--color=yes \
259+
--suppress-no-test-exit-code \
260+
--junit-xml=TEST-results-group-${GROUP:-1}.xml \
261+
${EXTRA_OPTIONS}
188262
then
189263
RESULT=2
264+
export STOP_CONTAINER="N"
190265
log error "Container not stopped for verification: ${scenario}"
191-
log info "Container: $(${engine} ps -f "name=${scenario}" --format "{{.Names}} - {{.ID}}")"
266+
log info "Container: $(podman ps -f "name=${scenario}" --format "{{.Names}} - {{.ID}}")"
192267
fi
193-
[ -z "${CONTINUE_ON_ERROR}" ] && [ $RESULT -ne 0 ] && die "Stopping on test failure."
194268

195-
# cleanup environment
196-
cleanup "${scenario}" "${engine}"
269+
if [ -z "${CONTINUE_ON_ERROR}" ] && [ $RESULT -ne 0 ]
270+
then
271+
die "Stopping on test failure."
272+
fi

infra/image/shcontainer

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
SCRIPTDIR="$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")"
55
TOPDIR="$(readlink -f "${SCRIPTDIR}/../..")"
66

7-
. "${TOPDIR}/utils/shfun"
7+
. "${SCRIPTDIR}/shfun"
8+
9+
list_images() {
10+
local quay_api="https://quay.io/api/v1/repository/ansible-freeipa/upstream-tests/tag"
11+
log info "Available images on quay:"
12+
curl --silent -L "${quay_api}" | jq '.tags[]|.name' | tr -d '"'| sort | uniq | sed "s/.*/ &/"
13+
echo
14+
log info "Local images (use -l):"
15+
local_image=$(container_image_list "${repo:-"quay.io/ansible-freeipa/upstream-tests"}:")
16+
echo "${local_image}" | sed -e "s/.*://" | sed "s/.*/ &/"
17+
echo
18+
}
819

920
container_create() {
1021
local name=${1}

utils/shfun renamed to infra/image/shfun

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,12 @@ SCRIPTDIR="$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")"
55

66
. "${SCRIPTDIR}/shlog"
77

8-
[ -n "$(command -v python3)" ] && python="$(command -v python3)" || python="$(command -v python2)"
9-
export python
10-
11-
trap interrupt_exception SIGINT
12-
13-
interrupt_exception() {
14-
trap - SIGINT
15-
log warn "User interrupted test execution."
16-
# shellcheck disable=SC2119
17-
cleanup "${scenario:+${scenario}}"
18-
exit 1
19-
}
20-
218
run_if_exists() {
229
cmd="${1}"
2310
shift
2411
[ -n "$(command -v "${cmd}")" ] && "${cmd}" "${@}"
2512
}
2613

27-
# shellcheck disable=SC2120
28-
cleanup() {
29-
local container container_engine
30-
container="${1:-${scenario:+${scenario}}}"
31-
container_engine="${2:-${engine:-"podman"}}"
32-
if [ "${STOP_CONTAINER:-"Y"}" == "Y" ] && [ -n "${container}" ]
33-
then
34-
run_if_exists stop_container "${container}" "${container_engine}"
35-
[ -f "${inventory:-}" ] && rm "${inventory}"
36-
else
37-
if [ -n "${container}" ]
38-
then
39-
log info "Keeping container: $(${container_engine} 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-
}
48-
4914
start_virtual_environment() {
5015
# options -f
5116
local FORCE_ENV VENV envdirectory
@@ -101,7 +66,6 @@ die() {
10166
fi
10267
log error "${*}"
10368
STOP_CONTAINER="N"
104-
cleanup "${scenario:+${scenario}}"
10569
[ "${usg}" == "Y" ] && run_if_exists usage
10670
exit 1
10771
}
File renamed without changes.

infra/image/start.sh

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/bin/bash -eu
22

33
BASEDIR="$(readlink -f "$(dirname "$0")")"
4-
TOPDIR="$(readlink -f "${BASEDIR}/../..")"
54

65
# shellcheck disable=SC1091
76
. "${BASEDIR}/shcontainer"
87
# shellcheck disable=SC1091
9-
. "${TOPDIR}/utils/shfun"
8+
. "${BASEDIR}/shfun"
109

1110
usage() {
1211
local prog="${0##*/}"
@@ -36,17 +35,6 @@ NOTE:
3635
EOF
3736
}
3837

39-
list_images() {
40-
local quay_api="https://quay.io/api/v1/repository/ansible-freeipa/upstream-tests/tag"
41-
log info "Available images on quay:"
42-
curl --silent -L "${quay_api}" | jq '.tags[]|.name' | tr -d '"'| sort | uniq | sed "s/.*/ &/"
43-
echo
44-
log info "Local images (use -l):"
45-
local_image=$(container_image_list "${repo}:")
46-
echo "${local_image}" | sed -e "s/.*://" | sed "s/.*/ &/"
47-
echo
48-
}
49-
5038
repo="quay.io/ansible-freeipa/upstream-tests"
5139
name="ansible-freeipa-tests"
5240
hostname="ipaserver.test.local"

0 commit comments

Comments
 (0)