Skip to content

Commit 0afc7b7

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 0afc7b7

File tree

8 files changed

+123
-364
lines changed

8 files changed

+123
-364
lines changed

utils/run-tests.sh infra/image/run-tests.sh

+91-46
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
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
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+
log info "Cleaning up environment"
30+
if [ "${STOP_VIRTUALENV:-"N"}" == "Y" ]
31+
then
32+
echo "Deactivating virtual environment"
33+
run_if_exists deactivate
34+
fi
35+
}
1236

1337
usage() {
1438
local prog="${0##*/}"
1539
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.
40+
usage: ${prog} [-h] [-L] [-e] [-A|-a ANSIBLE] [-s TESTS_SUITE] [-x] [-S SEED.GRP] [-l] [-i IMAGE] [-v...] [TEST...]
41+
${prog} runs test playbooks using an ansible-freeipa testing images.
1842
1943
EOF
2044
}
@@ -31,13 +55,11 @@ optional arguments:
3155
-a ANSIBLE Ansible version to use, e.g. "ansible-core==2.16.0"
3256
(default: latest ansible-core for the python version)
3357
-A Do not install Ansible, use host's provided one.
34-
-c CONTAINER use container CONTAINER to run tests
35-
-K keep container, even if tests succeed
36-
-l list available images
58+
-L list available images
59+
-l Try to use local image first, if not found download.
3760
-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)
40-
-p INTERPRETER Python interpreter to use on target container
61+
-i IMAGE select image to run the tests
62+
(default: fedora-latest-server)
4163
-s TEST_SUITE run all playbooks for test suite, which is a directory
4264
under ${WHITE}tests${RST}
4365
-S SEED.GROUP Replicate Azure's test group and seed (seed is YYYYMMDD)
@@ -47,33 +69,57 @@ EOF
4769
)"
4870
}
4971

72+
install_ansible() {
73+
ANSIBLE_VERSION="${1:-${ANSIBLE_VERSION:-"ansible-core"}}"
74+
[ $# -gt 0 ] && shift
75+
log info "Installing Ansible: ${ANSIBLE_VERSION}"
76+
pip install --quiet "${ANSIBLE_VERSION}"
77+
log debug "Ansible version: $(ansible --version | sed -n "1p")${RST}"
78+
79+
if [ -n "${ANSIBLE_COLLECTIONS}" ]
80+
then
81+
collections_path="$(mktemp -d)"
82+
for collection in ${ANSIBLE_COLLECTIONS}
83+
do
84+
if ! quiet ansible-galaxy collection verify --offline "${collection}"
85+
then
86+
log info "Installing: Ansible Collection ${collection}"
87+
# shellcheck disable=SC2086
88+
quiet ansible-galaxy collection install \
89+
-p "${collections_path}" \
90+
"${collection}" || die "Failed to install Ansible collection: ${collection}"
91+
fi
92+
done
93+
default_collections_path="$(ansible-config init | grep "collections_path=" | cut -d= -f2-)"
94+
export ANSIBLE_COLLECTIONS_PATH="${collections_path}:${ANSIBLE_COLLECTIONS_PATH:-${default_collections_path}}"
95+
fi
96+
export ANSIBLE_VERSION
97+
}
98+
5099

51100
# Defaults
52101
verbose=""
53-
engine="${engine:-"podman"}"
54102
CONTINUE_ON_ERROR=""
55-
STOP_CONTAINER="Y"
56103
STOP_VIRTUALENV="N"
104+
FORCE_ENV="N"
57105
declare -a ENABLED_MODULES
58106
declare -a ENABLED_TESTS
59107
read -r -a ENABLED_MODULES <<< "${IPA_ENABLED_MODULES:-""}"
60108
read -r -a ENABLED_TESTS <<< "${IPA_ENABLED_MODULES:-""}"
61-
IMAGE_TAG="fedora-latest"
62-
scenario="freeipa-tests"
63-
MEMORY=3
109+
IMAGE_TAG="fedora-latest-server"
64110
IPA_HOSTNAME="ipaserver.test.local"
65111
SEED="$(date "+%Y%m%d")"
66112
GROUP=1
67113
SPLITS=0
68-
ANSIBLE_COLLECTIONS=${ANSIBLE_COLLECTIONS:-"${engine_collection}"}
114+
ANSIBLE_COLLECTIONS=${ANSIBLE_COLLECTIONS:-"containers.podman"}
69115
SKIP_ANSIBLE=""
70-
ansible_interpreter="/usr/bin/python3"
71116
EXTRA_OPTIONS=""
117+
unset LOCAL_IMAGES
72118
unset ANSIBLE_VERSION
73119

74120
# Process command options
75121

76-
while getopts ":ha:Ac:ei:Klm:p:s:S:vx" option
122+
while getopts ":ha:Aei:lLs:S:vx" option
77123
do
78124
case "$option" in
79125
h) help && exit 0 ;;
@@ -85,13 +131,10 @@ do
85131
[ "${SKIP_ANSIBLE:-"no"}" == "YES" ] && die "Can't use -A with '-a'"
86132
ANSIBLE_VERSION="${OPTARG}"
87133
;;
88-
c) scenario="${OPTARG}" ;;
89134
e) FORCE_ENV="Y" ;;
90135
i) IMAGE_TAG="${OPTARG}" ;;
91-
K) STOP_CONTAINER="N" ;;
92-
l) "${SCRIPTDIR}"/setup_test_container.sh -l && exit 0 || exit 1 ;;
93-
m) MEMORY="${OPTARG}" ;;
94-
p) ansible_interpreter="${OPTARG}" ;;
136+
L) list_images && exit 0 || exit 1 ;;
137+
l) LOCAL_IMAGES="-l" ;;
95138
s)
96139
[ ${SPLITS} -ne 0 ] && die -u "Can't use '-S' with '-s'"
97140
if [ -d "${TOPDIR}/tests/${OPTARG}" ]
@@ -131,16 +174,18 @@ done
131174

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

134-
export STOP_CONTAINER FORCE_ENV STOP_VIRTUALENV ansible_interpreter
177+
export IPA_SERVER_HOST="ansible-freeipa-tests"
178+
export STOP_VIRTUALENV
135179

136-
# Ensure $python is set
137-
[ -z "${python}" ] && python="python3"
180+
python="$(get_python_executable)"
138181

139-
log info "Controller Python executable: ${python}"
182+
log info "Python executable: ${python}"
140183
${python} --version
141184

142185
# Prepare virtual environment
143-
start_virtual_environment
186+
declare -a venv_opts=()
187+
[ "${FORCE_ENV}" == "Y" ] && venv_opts+=("-f")
188+
start_virtual_environment "${venv_opts[@]}"
144189
log info "Installing dependencies from 'requirements-tests.txt'"
145190
pip install --upgrade -r "${TOPDIR}/requirements-tests.txt"
146191

@@ -151,17 +196,13 @@ export ANSIBLE_ROLES_PATH="${TOPDIR}/roles"
151196
export ANSIBLE_LIBRARY="${TOPDIR}/plugins"
152197
export ANSIBLE_MODULE_UTILS="${TOPDIR}/plugins/module_utils"
153198

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-
199+
# Start test container
200+
"${TOPDIR}/infra/image/start.sh" ${LOCAL_IMAGES:-} "${IMAGE_TAG}" -n "${IPA_HOSTNAME}"
157201

158202
# run tests
159203
RESULT=0
160204

161-
export RUN_TESTS_IN_DOCKER=${engine}
162-
export IPA_SERVER_HOST="${scenario}"
163-
# Ensure proper ansible_python_interpreter is used by pytest.
164-
export IPA_PYTHON_PATH="${ansible_interpreter}"
205+
export RUN_TESTS_IN_DOCKER=podman
165206

166207
if [ ${SPLITS} -ne 0 ]
167208
then
@@ -184,13 +225,17 @@ IPA_VERBOSITY="${verbose}"
184225
[ -n "${IPA_VERBOSITY}" ] && export IPA_VERBOSITY
185226

186227
# 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}
228+
if ! pytest -m "playbook" \
229+
--verbose \
230+
--color=yes \
231+
--suppress-no-test-exit-code \
232+
--junit-xml=TEST-results-group-${GROUP:-1}.xml \
233+
${EXTRA_OPTIONS}
188234
then
189235
RESULT=2
190-
log error "Container not stopped for verification: ${scenario}"
191-
log info "Container: $(${engine} ps -f "name=${scenario}" --format "{{.Names}} - {{.ID}}")"
192236
fi
193-
[ -z "${CONTINUE_ON_ERROR}" ] && [ $RESULT -ne 0 ] && die "Stopping on test failure."
194237

195-
# cleanup environment
196-
cleanup "${scenario}" "${engine}"
238+
if [ -z "${CONTINUE_ON_ERROR}" ] && [ $RESULT -ne 0 ]
239+
then
240+
die "Stopping on test failure."
241+
fi

infra/image/shcontainer

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
# This file is meant to be source'd by other scripts
33

44
SCRIPTDIR="$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")"
5-
TOPDIR="$(readlink -f "${SCRIPTDIR}/../..")"
65

7-
. "${TOPDIR}/utils/shfun"
6+
# shellcheck source=infra/image/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 infra/image/shfun

+17-34
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,32 @@
33

44
SCRIPTDIR="$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")"
55

6+
# shellcheck source=infra/image/shlog
67
. "${SCRIPTDIR}/shlog"
78

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-
219
run_if_exists() {
10+
trap - ERR
2211
cmd="${1}"
2312
shift
2413
[ -n "$(command -v "${cmd}")" ] && "${cmd}" "${@}"
2514
}
2615

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}" ]
16+
get_python_executable() {
17+
trap - ERR
18+
for py in "/usr/libexec/platform-python" "python3" "python2" "python"
19+
do
20+
python_cmd="$(command -v "${py}")"
21+
if [ -n "${python_cmd}" ]
3822
then
39-
log info "Keeping container: $(${container_engine} ps --format "{{.Names}} - {{.ID}}" --filter "name=${container}")"
23+
echo "${python_cmd}"
24+
return 0
4025
fi
41-
fi
42-
if [ "${STOP_VIRTUALENV:-"N"}" == "Y" ]
43-
then
44-
echo "Deactivating virtual environment"
45-
run_if_exists deactivate
46-
fi
26+
done
27+
die "Could not find python executable."
4728
}
4829

4930
start_virtual_environment() {
31+
trap - ERR
5032
# options -f
5133
local FORCE_ENV VENV envdirectory
5234
FORCE_ENV="N"
@@ -59,6 +41,8 @@ start_virtual_environment() {
5941
done
6042
envdirectory="${test_env:-/tmp/ansible-freeipa-tests}"
6143

44+
python="$(get_python_executable)"
45+
6246
# Prepare virtual environment
6347
VENV=$(in_python_virtualenv && echo Y || echo N)
6448

@@ -77,7 +61,7 @@ start_virtual_environment() {
7761
then
7862
log info "Creating virtual environment: ${envdirectory}..."
7963
log warn "RUN: ${python} -m venv ${envdirectory}"
80-
${python} -m venv "${envdirectory}" || die "Cannot create virtual environment."
64+
"${python}" -m venv "${envdirectory}" || die "Cannot create virtual environment."
8165
fi
8266
log info "Starting virtual environment: ${envdirectory}"
8367
[ -f "${envdirectory}/bin/activate" ] || die "Failed to create virtual environment."
@@ -100,8 +84,7 @@ die() {
10084
shift 1
10185
fi
10286
log error "${*}"
103-
STOP_CONTAINER="N"
104-
cleanup "${scenario:+${scenario}}"
87+
export STOP_CONTAINER="N"
10588
[ "${usg}" == "Y" ] && run_if_exists usage
10689
exit 1
10790
}

utils/shlog infra/image/shlog

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ log() {
5252
}
5353

5454
quiet() {
55-
"$@" >/dev/null 2>&1
55+
"$@" >/dev/null 2>/dev/null
5656
}
5757

infra/image/start.sh

+1-13
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)