Debian Codegraph @

ansible_collections/community/general/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 "$(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 install dependencies
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 # Nothing further should be added to this list.
82 # This is to prevent modules or plugins in this collection having a runtime dependency on other collections.
83 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"
84 # NOTE: we're installing with git to work around Galaxy being a huge PITA (https://github.com/ansible/galaxy/issues/2429)
85 # retry ansible-galaxy -vvv collection install community.internal_test_tools
86
87 if [ "${script}" != "sanity" ] && [ "${script}" != "units" ]; then
88 # To prevent Python dependencies on other collections only install other collections for integration tests
89 retry git clone --depth=1 --single-branch https://github.com/ansible-collections/ansible.posix.git "${ANSIBLE_COLLECTIONS_PATHS}/ansible_collections/ansible/posix"
90 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"
91 retry git clone --depth=1 --single-branch https://github.com/ansible-collections/community.docker.git "${ANSIBLE_COLLECTIONS_PATHS}/ansible_collections/community/docker"
92 # NOTE: we're installing with git to work around Galaxy being a huge PITA (https://github.com/ansible/galaxy/issues/2429)
93 # retry ansible-galaxy -vvv collection install ansible.posix
94 # retry ansible-galaxy -vvv collection install community.crypto
95 # retry ansible-galaxy -vvv collection install community.docker
96 fi
97 # END: HACK
98
99 export PYTHONIOENCODING='utf-8'
100
101 if [ "${JOB_TRIGGERED_BY_NAME:-}" == "nightly-trigger" ]; then
102 COVERAGE=yes
103 COMPLETE=yes
104 fi
105
106 if [ -n "${COVERAGE:-}" ]; then
107 # on-demand coverage reporting triggered by setting the COVERAGE environment variable to a non-empty value
108 export COVERAGE="--coverage"
109 elif [[ "${COMMIT_MESSAGE}" =~ ci_coverage ]]; then
110 # on-demand coverage reporting triggered by having 'ci_coverage' in the latest commit message
111 export COVERAGE="--coverage"
112 else
113 # on-demand coverage reporting disabled (default behavior, always-on coverage reporting remains enabled)
114 export COVERAGE="--coverage-check"
115 fi
116
117 if [ -n "${COMPLETE:-}" ]; then
118 # disable change detection triggered by setting the COMPLETE environment variable to a non-empty value
119 export CHANGED=""
120 elif [[ "${COMMIT_MESSAGE}" =~ ci_complete ]]; then
121 # disable change detection triggered by having 'ci_complete' in the latest commit message
122 export CHANGED=""
123 else
124 # enable change detection (default behavior)
125 export CHANGED="--changed"
126 fi
127
128 if [ "${IS_PULL_REQUEST:-}" == "true" ]; then
129 # run unstable tests which are targeted by focused changes on PRs
130 export UNSTABLE="--allow-unstable-changed"
131 else
132 # do not run unstable tests outside PRs
133 export UNSTABLE=""
134 fi
135
136 # remove empty core/extras module directories from PRs created prior to the repo-merge
137 find plugins -type d -empty -print -delete
138
139 function cleanup
140 {
141 # 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
142 if [ "${COVERAGE}" == "--coverage" ] && [ "${CHANGED}" == "" ] && [ "${test}" == "sanity/5" ]; then
143 stub="--stub"
144 # trigger coverage reporting for stubs even if no other coverage data exists
145 mkdir -p tests/output/coverage/
146 else
147 stub=""
148 fi
149
150 if [ -d tests/output/coverage/ ]; then
151 if find tests/output/coverage/ -mindepth 1 -name '.*' -prune -o -print -quit | grep -q .; then
152 process_coverage='yes' # process existing coverage files
153 elif [ "${stub}" ]; then
154 process_coverage='yes' # process coverage when stubs are enabled
155 else
156 process_coverage=''
157 fi
158
159 if [ "${process_coverage}" ]; then
160 # use python 3.7 for coverage to avoid running out of memory during coverage xml processing
161 # only use it for coverage to avoid the additional overhead of setting up a virtual environment for a potential no-op job
162 virtualenv --python /usr/bin/python3.7 ~/ansible-venv
163 set +ux
164 . ~/ansible-venv/bin/activate
165 set -ux
166
167 # shellcheck disable=SC2086
168 ansible-test coverage xml --color -v --requirements --group-by command --group-by version ${stub:+"$stub"}
169 cp -a tests/output/reports/coverage=*.xml "$SHIPPABLE_RESULT_DIR/codecoverage/"
170
171 # analyze and capture code coverage aggregated by integration test target
172 ansible-test coverage analyze targets generate -v "$SHIPPABLE_RESULT_DIR/testresults/coverage-analyze-targets.json"
173
174 # upload coverage report to codecov.io only when using complete on-demand coverage
175 if [ "${COVERAGE}" == "--coverage" ] && [ "${CHANGED}" == "" ]; then
176 for file in tests/output/reports/coverage=*.xml; do
177 flags="${file##*/coverage=}"
178 flags="${flags%-powershell.xml}"
179 flags="${flags%.xml}"
180 # remove numbered component from stub files when converting to tags
181 flags="${flags//stub-[0-9]*/stub}"
182 flags="${flags//=/,}"
183 flags="${flags//[^a-zA-Z0-9_,]/_}"
184
185 bash <(curl -s https://ansible-ci-files.s3.us-east-1.amazonaws.com/codecov/codecov.sh) \
186 -f "${file}" \
187 -F "${flags}" \
188 -n "${test}" \
189 -t 20636cf5-4d6a-4b9a-8d2d-6f22ebbaa752 \
190 -X coveragepy \
191 -X gcov \
192 -X fix \
193 -X search \
194 -X xcode \
195 || echo "Failed to upload code coverage report to codecov.io: ${file}"
196 done
197 fi
198 fi
199 fi
200
201 if [ -d tests/output/junit/ ]; then
202 cp -aT tests/output/junit/ "$SHIPPABLE_RESULT_DIR/testresults/"
203 fi
204
205 if [ -d tests/output/data/ ]; then
206 cp -a tests/output/data/ "$SHIPPABLE_RESULT_DIR/testresults/"
207 fi
208
209 if [ -d tests/output/bot/ ]; then
210 cp -aT tests/output/bot/ "$SHIPPABLE_RESULT_DIR/testresults/"
211 fi
212 }
213
214 if [ "${SHIPPABLE_BUILD_ID:-}" ]; then trap cleanup EXIT; fi
215
216 if [[ "${COVERAGE:-}" == "--coverage" ]]; then
217 timeout=60
218 else
219 timeout=50
220 fi
221
222 ansible-test env --dump --show --timeout "${timeout}" --color -v
223
224 if [ "${SHIPPABLE_BUILD_ID:-}" ]; then "tests/utils/shippable/check_matrix.py"; fi
225 "tests/utils/shippable/${script}.sh" "${test}" "${ansible_version}"