Debian Codegraph @

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

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