forked from rcbops/rpc-openstack-system-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_tests.sh
executable file
·208 lines (175 loc) · 6.46 KB
/
execute_tests.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
## Deploy virtualenv for testing environment molecule/ansible-playbook/infratest
## Shell Opts ----------------------------------------------------------------
set -x
set -o pipefail
export ANSIBLE_HOST_KEY_CHECKING=False
## Variables -----------------------------------------------------------------
SYS_VENV_NAME="${SYS_VENV_NAME:-venv-molecule}"
SYS_CONSTRAINTS="constraints.txt"
SYS_REQUIREMENTS="requirements.txt"
SYS_INVENTORY="${SYS_INVENTORY:-/etc/openstack_deploy/openstack_inventory.json}"
SKIP_CONVERGE=false
SKIP_VERIFY=false
MOLECULES=()
SCENARIOS=()
## Remove Ansible Plug-ins Prior to System Tests Execution
rm -rf /root/.ansible/plugins
## Functions -----------------------------------------------------------------
usage() {
echo -n "execute_tests [-p] [-m MOLECULE_PATH(S)] [--sc|skip-converge] [--sv|skip-verify]
Execute Molecule tests.
Options:
-p Set 'MNAIO_SSH' env var for testing MNAIO topology in Phobos
-m Path of single Molecule to execute
-s Scenario to execute
--sc Skip the Molecule converge stage [--skip-converge]
--sv Skip the Molecule verify stage [--skip-verify]
-h Display this help and exit
"
}
## Parse Args ----------------------------------------------------------------
while getopts ":pms:-:h" opt;
do
case ${opt} in
p)
export MNAIO_SSH="ssh -ttt -oStrictHostKeyChecking=no root@infra1"
;;
m)
MOLECULES+=("$OPTARG")
;;
s)
SCENARIOS+=("$OPTARG")
;;
-)
case ${OPTARG} in
skip-converge)
SKIP_CONVERGE=true
;;
sc)
SKIP_CONVERGE=true
;;
skip-verify)
SKIP_VERIFY=true
;;
sv)
SKIP_VERIFY=true
;;
*)
echo error "Invalid option: --${OPTARG}" >&2
usage
exit 1
;;
esac
;;
h)
usage
exit 1
;;
\?)
echo error "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
## Main ----------------------------------------------------------------------
# Determine if the user specified a specific Molecule to execute or not
if [ "${#MOLECULES[@]}" -eq 0 ]; then
MOLECULES=(molecules/*)
fi
# Determine if the user specified a specific scenario to execute or not
if [ "${#SCENARIOS[@]}" -eq 0 ]; then
SCENARIOS=(default)
fi
# fail hard during setup
set -e
# Create virtualenv for molecule
virtualenv --no-pip --no-setuptools --no-wheel "${SYS_VENV_NAME}"
# Activate virtualenv
source "${SYS_VENV_NAME}/bin/activate"
# Ensure that correct pip version is installed
PIP_TARGET="$(awk -F= '/^pip==/ {print $3}' ${SYS_CONSTRAINTS})"
VENV_PYTHON="${SYS_VENV_NAME}/bin/python"
VENV_PIP="${SYS_VENV_NAME}/bin/pip"
if [[ "$(${VENV_PIP} --version 2>/dev/null)" != "pip ${PIP_TARGET}"* ]]; then
CURL_CMD="curl --silent --show-error --retry 5"
OUTPUT_FILE="get-pip.py"
${CURL_CMD} https://bootstrap.pypa.io/get-pip.py > ${OUTPUT_FILE} \
|| ${CURL_CMD} https://raw.githubusercontent.com/pypa/get-pip/master/get-pip.py > ${OUTPUT_FILE}
GETPIP_OPTIONS="pip setuptools wheel --constraint ${SYS_CONSTRAINTS}"
${VENV_PYTHON} ${OUTPUT_FILE} ${GETPIP_OPTIONS} \
|| ${VENV_PYTHON} ${OUTPUT_FILE} --isolated ${GETPIP_OPTIONS}
fi
# Install test suite requirements
PIP_OPTIONS="-r ${SYS_REQUIREMENTS}"
${VENV_PIP} install ${PIP_OPTIONS} || ${VENV_PIP} install --isolated ${PIP_OPTIONS}
# Generate moleculerized inventory from openstack-ansible dynamic inventory
echo "+-------------------- ANSIBLE INVENTORY --------------------+"
if [[ -e ${SYS_INVENTORY} ]]; then
echo "Local inventory source found."
cp ${SYS_INVENTORY} dynamic_inventory.json
else
echo "No local inventory source found, copying from MNAIO infra1 node instead."
rsync infra1:${SYS_INVENTORY} dynamic_inventory.json
fi
cat dynamic_inventory.json
echo "+-------------------- ANSIBLE INVENTORY --------------------+"
# Run molecule converge and verify
set +e # allow test stages to return errors
for TEST in "${MOLECULES[@]}" ; do
# Introspect all scenarios from given molecule
ALL_SCENARIOS=()
SCENARIO_PATHS=($TEST/molecule/*)
for path in "${SCENARIO_PATHS[@]}"; do
ALL_SCENARIOS+=("${path##*/}")
done
# The molecule.yml file for all scenarios needs to be valid for any of
# them to work. So, we first generate them all before focusing on the
# ones specified by the user.
for SCENARIO in "${ALL_SCENARIOS[@]}" ; do
moleculerize --scenario "${SCENARIO}" --output "$TEST/molecule/$SCENARIO/molecule.yml" dynamic_inventory.json
done
# Resume normal operation.
for SCENARIO in "${SCENARIOS[@]}" ; do
moleculerize --scenario "${SCENARIO}" --output "$TEST/molecule/$SCENARIO/molecule.yml" dynamic_inventory.json
pushd "$TEST"
repo_uri=$(git remote -v | awk '/fetch/{print $2}')
echo "TESTING: $repo_uri at SHA $(git rev-parse HEAD)"
# Capture the molecule test repo in the environment so "pytest-rpc" can record it.
export MOLECULE_TEST_REPO=$(echo ${repo_uri} | rev | cut -d'/' -f1 - | rev | cut -d. -f1)
# Capture the SHA of the tests we are executing
export MOLECULE_GIT_COMMIT=$(git rev-parse HEAD)
# Execute the converge step
if [[ "${SKIP_CONVERGE}" = false ]]; then
molecule --debug converge --scenario-name "${SCENARIO}"
else
echo "Skipping converge step!"
fi
if [[ $? -ne 0 ]] && RC=$?; then # do not run tests if converge fails
echo "CONVERGE: Failure in $(basename ${TEST}) for scenario ${SCENARIO}, verify step being skipped"
continue
fi
# Execute the verify step
if [[ "${SKIP_VERIFY}" = false ]]; then
molecule --debug verify --scenario-name "${SCENARIO}"
else
echo "Skipping verify step!"
fi
[[ $? -ne 0 ]] && RC=$? # record non-zero exit code
popd
done
done
# Gather junit.xml results if verify stage was executed
if [[ "${SKIP_VERIFY}" = false ]]; then
rm -f test_results.tar # ensure any previous results are deleted
ls molecules/*/molecule/*/*.xml | tar -cvf test_results.tar --files-from=-
# Gather pytest debug files
ls molecules/*/molecule/*/pytestdebug.log | tar -rvf test_results.tar --files-from=-
fi
# if exit code is recorded, use it, otherwise let it exit naturally
[[ -z ${RC+x} ]] && exit ${RC}