Debian Codegraph @

ansible_collections/community/libvirt/tests/utils/shippable/shippable.sh

raw
1 #!/usr/bin/env bash
2
3 set -o pipefail -eux
4
5 declare -a args
6 IFS='/:' read -ra args <<< "$1"
7
8 ansible_version="${args[0]}"
9 script="${args[1]}"
10
11 function join {
12 local IFS="$1";
13 shift;
14 echo "$*";
15 }
16
17 # Ensure we can write other collections to this dir
18 sudo chown "$(whoami)" "${PWD}/../../"
19
20 test="$(join / "${args[@]:1}")"
21
22 docker images ansible/ansible
23 docker images quay.io/ansible/*
24 docker ps
25
26 for container in $(docker ps --format '{{.Image}} {{.ID}}' | grep -v -e '^drydock/' -e '^quay.io/ansible/azure-pipelines-test-container:' | sed 's/^.* //'); do
27 docker rm -f "${container}" || true # ignore errors
28 done
29
30 docker ps
31
32 if [ -d /home/shippable/cache/ ]; then
33 ls -la /home/shippable/cache/
34 fi
35
36 command -v python
37 python -V
38
39 function retry
40 {
41 # shellcheck disable=SC2034
42 for repetition in 1 2 3; do
43 set +e
44 "$@"
45 result=$?
46 set -e
47 if [ ${result} == 0 ]; then
48 return ${result}
49 fi
50 echo "@* -> ${result}"
51 done
52 echo "Command '@*' failed 3 times!"
53 exit 1
54 }
55
56 command -v pip
57 pip --version
58 pip list --disable-pip-version-check
59 if [ "${ansible_version}" == "devel" ]; then
60 retry pip install https://github.com/ansible/ansible/archive/devel.tar.gz --disable-pip-version-check
61 else
62 retry pip install "https://github.com/ansible/ansible/archive/stable-${ansible_version}.tar.gz" --disable-pip-version-check
63 fi
64
65 export PATH="${PWD}/bin:${PATH}"
66 if [ "${SHIPPABLE_BUILD_ID:-}" ]; then
67 export ANSIBLE_COLLECTIONS_PATHS="${HOME}/.ansible"
68 SHIPPABLE_RESULT_DIR="$(pwd)/shippable"
69 TEST_DIR="${ANSIBLE_COLLECTIONS_PATHS}/ansible_collections/community/libvirt"
70 mkdir -p "${TEST_DIR}"
71 # shellcheck disable=SC2153
72 cp -aT "${SHIPPABLE_BUILD_DIR}" "${TEST_DIR}"
73 cd "${TEST_DIR}"
74 else
75 # AZP
76 export ANSIBLE_COLLECTIONS_PATHS="$PWD/../../../"
77 fi
78
79 export PYTHONIOENCODING='utf-8'
80
81 if [ "${JOB_TRIGGERED_BY_NAME:-}" == "nightly-trigger" ]; then
82 COVERAGE=yes
83 COMPLETE=yes
84 fi
85
86 if [ -n "${COVERAGE:-}" ]; then
87 # on-demand coverage reporting triggered by setting the COVERAGE environment variable to a non-empty value
88 export COVERAGE="--coverage"
89 elif [[ "${COMMIT_MESSAGE}" =~ ci_coverage ]]; then
90 # on-demand coverage reporting triggered by having 'ci_coverage' in the latest commit message
91 export COVERAGE="--coverage"
92 else
93 # on-demand coverage reporting disabled (default behavior, always-on coverage reporting remains enabled)
94 export COVERAGE="--coverage-check"
95 fi
96
97 if [ -n "${COMPLETE:-}" ]; then
98 # disable change detection triggered by setting the COMPLETE environment variable to a non-empty value
99 export CHANGED=""
100 elif [[ "${COMMIT_MESSAGE}" =~ ci_complete ]]; then
101 # disable change detection triggered by having 'ci_complete' in the latest commit message
102 export CHANGED=""
103 else
104 # enable change detection (default behavior)
105 export CHANGED="--changed"
106 fi
107
108 if [ "${IS_PULL_REQUEST:-}" == "true" ]; then
109 # run unstable tests which are targeted by focused changes on PRs
110 export UNSTABLE="--allow-unstable-changed"
111 else
112 # do not run unstable tests outside PRs
113 export UNSTABLE=""
114 fi
115
116 # remove empty core/extras module directories from PRs created prior to the repo-merge
117 find plugins -type d -empty -print -delete
118
119 function cleanup
120 {
121 # for complete on-demand coverage generate a report for all files with no coverage on the "sanity/5" job so we only have one copy
122 if [ "${COVERAGE}" == "--coverage" ] && [ "${CHANGED}" == "" ] && [ "${test}" == "sanity/5" ]; then
123 stub="--stub"
124 # trigger coverage reporting for stubs even if no other coverage data exists
125 mkdir -p tests/output/coverage/
126 else
127 stub=""
128 fi
129
130 if [ -d tests/output/coverage/ ]; then
131 if find tests/output/coverage/ -mindepth 1 -name '.*' -prune -o -print -quit | grep -q .; then
132 process_coverage='yes' # process existing coverage files
133 elif [ "${stub}" ]; then
134 process_coverage='yes' # process coverage when stubs are enabled
135 else
136 process_coverage=''
137 fi
138
139 if [ "${process_coverage}" ]; then
140 # use python 3.7 for coverage to avoid running out of memory during coverage xml processing
141 # only use it for coverage to avoid the additional overhead of setting up a virtual environment for a potential no-op job
142 virtualenv --python /usr/bin/python3.7 ~/ansible-venv
143 set +ux
144 . ~/ansible-venv/bin/activate
145 set -ux
146
147 # shellcheck disable=SC2086
148 ansible-test coverage xml --color -v --requirements --group-by command --group-by version ${stub:+"$stub"}
149 cp -a tests/output/reports/coverage=*.xml "$SHIPPABLE_RESULT_DIR/codecoverage/"
150
151 if [ "${ansible_version}" != "2.9" ]; then
152 # analyze and capture code coverage aggregated by integration test target
153 ansible-test coverage analyze targets generate -v "$SHIPPABLE_RESULT_DIR/testresults/coverage-analyze-targets.json"
154 fi
155
156 # upload coverage report to codecov.io only when using complete on-demand coverage
157 if [ "${COVERAGE}" == "--coverage" ] && [ "${CHANGED}" == "" ]; then
158 for file in tests/output/reports/coverage=*.xml; do
159 flags="${file##*/coverage=}"
160 flags="${flags%-powershell.xml}"
161 flags="${flags%.xml}"
162 # remove numbered component from stub files when converting to tags
163 flags="${flags//stub-[0-9]*/stub}"
164 flags="${flags//=/,}"
165 flags="${flags//[^a-zA-Z0-9_,]/_}"
166
167 bash <(curl -s https://ansible-ci-files.s3.us-east-1.amazonaws.com/codecov/codecov.sh) \
168 -f "${file}" \
169 -F "${flags}" \
170 -n "${test}" \
171 -t e27e1287-0583-48ee-8a1f-867be2b25b46 \
172 -X coveragepy \
173 -X gcov \
174 -X fix \
175 -X search \
176 -X xcode \
177 || echo "Failed to upload code coverage report to codecov.io: ${file}"
178 done
179 fi
180 fi
181 fi
182
183 if [ -d tests/output/junit/ ]; then
184 cp -aT tests/output/junit/ "$SHIPPABLE_RESULT_DIR/testresults/"
185 fi
186
187 if [ -d tests/output/data/ ]; then
188 cp -a tests/output/data/ "$SHIPPABLE_RESULT_DIR/testresults/"
189 fi
190
191 if [ -d tests/output/bot/ ]; then
192 cp -aT tests/output/bot/ "$SHIPPABLE_RESULT_DIR/testresults/"
193 fi
194 }
195
196 if [ "${SHIPPABLE_BUILD_ID:-}" ]; then trap cleanup EXIT; fi
197
198 if [[ "${COVERAGE:-}" == "--coverage" ]]; then
199 timeout=60
200 else
201 timeout=50
202 fi
203
204 ansible-test env --dump --show --timeout "${timeout}" --color -v
205
206 if [ "${SHIPPABLE_BUILD_ID:-}" ]; then "tests/utils/shippable/check_matrix.py"; fi
207 "tests/utils/shippable/${script}.sh" "${test}"