perpatch: Echo MAKE_PARALLEL_* var before build
[csit.git] / resources / libraries / bash / function / per_patch.sh
1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 # Copyright (c) 2020 PANTHEON.tech s.r.o.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -exuo pipefail
16
17 # This library defines functions used mainly by per patch entry scripts.
18 # Generally, the functions assume "common.sh" library has been sourced already.
19 # Keep functions ordered alphabetically, please.
20
21 function archive_test_results () {
22
23     # Arguments:
24     # - ${1}: Directory to archive to. Required. Parent has to exist.
25     # Variable set:
26     # - TARGET - Target directory.
27     # Variables read:
28     # - ARCHIVE_DIR - Path to where robot result files are created in.
29     # - VPP_DIR - Path to existing directory, root for to relative paths.
30     # Directories updated:
31     # - ${1} - Created, and robot and parsing files are moved/created there.
32     # Functions called:
33     # - die - Print to stderr and exit, defined in common.sh
34
35     set -exuo pipefail
36
37     cd "${VPP_DIR}" || die "Change directory command failed."
38     TARGET="$(readlink -f "$1")"
39     mkdir -p "${TARGET}" || die "Directory creation failed."
40     for filename in "output.xml" "log.html" "report.html"; do
41         mv "${ARCHIVE_DIR}/${filename}" "${TARGET}/${filename}" || {
42             die "Attempt to move '${filename}' failed."
43         }
44     done
45 }
46
47
48 function archive_parse_test_results () {
49
50     # Arguments:
51     # - ${1}: Directory to archive to. Required. Parent has to exist.
52     # Variables read:
53     # - TARGET - Target directory.
54     # Functions called:
55     # - die - Print to stderr and exit, defined in common.sh
56     # - archive_test_results - Archiving results.
57     # - parse_bmrr_results - See definition in this file.
58
59     set -exuo pipefail
60
61     archive_test_results "$1" || die
62     parse_bmrr_results "${TARGET}" || {
63         die "The function should have died on error."
64     }
65 }
66
67
68 function build_vpp_ubuntu_amd64 () {
69
70     # This function is using make pkg-verify to build VPP with all dependencies
71     # that is ARCH/OS aware. VPP repo is SSOT for building mechanics and CSIT
72     # is consuming artifacts. This way if VPP will introduce change in building
73     # mechanics they will not be blocked by CSIT repo.
74     # Arguments:
75     # - ${1} - String identifier for echo, can be unset.
76     # Variables read:
77     # - MAKE_PARALLEL_FLAGS - Make flags when building VPP.
78     # - MAKE_PARALLEL_JOBS - Number of cores to use when building VPP.
79     # - VPP_DIR - Path to existing directory, parent to accessed directories.
80     # Directories updated:
81     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
82     # Functions called:
83     # - die - Print to stderr and exit, defined in common.sh
84
85     set -exuo pipefail
86
87     cd "${VPP_DIR}" || die "Change directory command failed."
88     if [ -n "${MAKE_PARALLEL_FLAGS-}" ]; then
89         echo "Building VPP. Number of cores for build set with" \
90              "MAKE_PARALLEL_FLAGS='${MAKE_PARALLEL_FLAGS}'."
91     elif [ -n "${MAKE_PARALLEL_JOBS-}" ]; then
92         echo "Building VPP. Number of cores for build set with" \
93              "MAKE_PARALLEL_JOBS='${MAKE_PARALLEL_JOBS}'."
94     else
95         echo "Building VPP. Number of cores not set, " \
96              "using build default ($(grep -c ^processor /proc/cpuinfo))."
97     fi
98
99     make UNATTENDED=y pkg-verify || die "VPP build using make pkg-verify failed."
100     echo "* VPP ${1-} BUILD SUCCESSFULLY COMPLETED" || {
101         die "Argument not found."
102     }
103 }
104
105
106 function compare_test_results () {
107
108     # Variables read:
109     # - VPP_DIR - Path to directory with VPP git repo (at least built parts).
110     # - ARCHIVE_DIR - Path to where robot result files are created in.
111     # - PYTHON_SCRIPTS_DIR - Path to directory holding comparison utility.
112     # Directories recreated:
113     # - csit_parent - Sibling to csit directory, for holding results
114     #   of parent build.
115     # Functions called:
116     # - die - Print to stderr and exit, defined in common.sh
117     # - parse_bmrr_results - See definition in this file.
118     # Exit code:
119     # - 0 - If the comparison utility sees no regression (nor data error).
120     # - 1 - If the comparison utility sees a regression (or data error).
121
122     set -exuo pipefail
123
124     cd "${VPP_DIR}" || die "Change directory operation failed."
125     # Reusing CSIT main virtualenv.
126     python3 "${TOOLS_DIR}/integrated/compare_perpatch.py"
127     # The exit code determines the vote result.
128 }
129
130
131 function download_builds () {
132
133     # This is mostly useful only for Sandbox testing, to avoid recompilation.
134     #
135     # Arguments:
136     # - ${1} - URL to download VPP builds from.
137     # Variables read:
138     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
139     # Directories created:
140     # - archive - Ends up empty, not to be confused with ${ARCHIVE_DIR}.
141     # - build_current - Holding built artifacts of the patch under test (PUT).
142     # - built_parent - Holding built artifacts of parent of PUT.
143     # Functions called:
144     # - die - Print to stderr and exit, defined in common.sh
145
146     set -exuo pipefail
147
148     cd "${VPP_DIR}" || die "Change directory operation failed."
149     dirs=("build-root" "build_parent" "build_current" "archive" "csit_current")
150     rm -rf ${dirs[@]} || {
151         die "Directory removal failed."
152     }
153     wget -N --progress=dot:giga "${1}" || die "Wget download failed."
154     unzip "archive.zip" || die "Archive extraction failed."
155     mv "archive/build_parent" ./ || die "Move operation failed."
156     mv "archive/build_current" ./ || die "Move operation failed."
157 }
158
159
160 function initialize_csit_dirs () {
161
162     # This could be in prepare_test, but download_builds also needs this.
163     #
164     # Variables read:
165     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
166     # Directories created:
167     # - csit_current - Holding test results of the patch under test (PUT).
168     # - csit_parent - Holding test results of parent of PUT.
169     # Functions called:
170     # - die - Print to stderr and exit, defined in common.sh
171
172     set -exuo pipefail
173
174     cd "${VPP_DIR}" || die "Change directory operation failed."
175     rm -rf "csit_current" "csit_parent" || {
176         die "Directory deletion failed."
177     }
178     mkdir -p "csit_current" "csit_parent" || {
179         die "Directory creation failed."
180     }
181 }
182
183
184 function parse_bmrr_results () {
185
186     # Currently "parsing" is just two greps.
187     # TODO: Re-use PAL parsing code, make parsing more general and centralized.
188     #
189     # Arguments:
190     # - ${1} - Path to (existing) directory holding robot output.xml result.
191     # Files read:
192     # - output.xml - From argument location.
193     # Files updated:
194     # - results.txt - (Re)created, in argument location.
195     # Functions called:
196     # - die - Print to stderr and exit, defined in common.sh
197
198     set -exuo pipefail
199
200     rel_dir="$(readlink -e "${1}")" || die "Readlink failed."
201     in_file="${rel_dir}/output.xml"
202     out_file="${rel_dir}/results.txt"
203     # TODO: Do we need to check echo exit code explicitly?
204     echo "Parsing ${in_file} putting results into ${out_file}"
205     echo "TODO: Re-use parts of PAL when they support subsample test parsing."
206     pattern='Maximum Receive Rate trial results in packets'
207     pattern+=' per second: .*\]</status>'
208     grep -o "${pattern}" "${in_file}" | grep -o '\[.*\]' > "${out_file}" || {
209         die "Some parsing grep command has failed."
210     }
211 }
212
213
214 function select_build () {
215
216     # Arguments:
217     # - ${1} - Path to directory to copy VPP artifacts from. Required.
218     # Variables read:
219     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
220     # - VPP_DIR - Path to existing directory, root for relative paths.
221     # Directories read:
222     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
223     # Directories updated:
224     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
225     # Functions called:
226     # - die - Print to stderr and exit, defined in common.sh
227
228     set -exuo pipefail
229
230     cd "${VPP_DIR}" || die "Change directory operation failed."
231     source_dir="$(readlink -e "$1")"
232     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
233     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
234     # TODO: Is there a nice way to create symlinks,
235     #   so that if job fails on robot, results can be archived?
236 }
237
238
239 function set_aside_commit_build_artifacts () {
240
241     # Function is copying VPP built artifacts from actual checkout commit for
242     # further use and clean git.
243     # Variables read:
244     # - VPP_DIR - Path to existing directory, parent to accessed directories.
245     # Directories read:
246     # - build-root - Existing directory with built VPP artifacts (also DPDK).
247     # Directories updated:
248     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
249     # - build_current - Old contents removed, content of build-root copied here.
250     # Functions called:
251     # - die - Print to stderr and exit, defined in common.sh
252
253     set -exuo pipefail
254
255     cd "${VPP_DIR}" || die "Change directory operation failed."
256     rm -rf "build_current" || die "Remove operation failed."
257     mkdir -p "build_current" || die "Directory creation failed."
258     mv "build-root"/*".deb" "build_current"/ || die "Move operation failed."
259     # The previous build could have left some incompatible leftovers,
260     # e.g. DPDK artifacts of different version (in build/external).
261     # Also, there usually is a copy of dpdk artifact in build-root.
262     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
263     # Finally, check out the parent commit.
264     git checkout HEAD~ || die "Git checkout operation failed."
265     # Display any other leftovers.
266     git status || die "Git status operation failed."
267 }
268
269
270 function set_aside_parent_build_artifacts () {
271
272     # Function is copying VPP built artifacts from parent checkout commit for
273     # further use. Checkout to parent is not part of this function.
274     # Variables read:
275     # - VPP_DIR - Path to existing directory, parent of accessed directories.
276     # Directories read:
277     # - build-root - Existing directory with built VPP artifacts (also DPDK).
278     # Directories updated:
279     # - build_parent - Old directory removed, build-root debs moved here.
280     # Functions called:
281     # - die - Print to stderr and exit, defined in common.sh
282
283     set -exuo pipefail
284
285     cd "${VPP_DIR}" || die "Change directory operation failed."
286     rm -rf "build_parent" || die "Remove failed."
287     mkdir -p "build_parent" || die "Directory creation operation failed."
288     mv "build-root"/*".deb" "build_parent"/ || die "Move operation failed."
289 }
290
291
292 function set_perpatch_dut () {
293
294     # Variables set:
295     # - DUT - CSIT test/ subdirectory containing suites to execute.
296
297     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
298
299     set -exuo pipefail
300
301     DUT="vpp"
302 }
303
304
305 function set_perpatch_vpp_dir () {
306
307     # Variables read:
308     # - CSIT_DIR - Path to existing root of local CSIT git repository.
309     # Variables set:
310     # - VPP_DIR - Path to existing root of local VPP git repository.
311     # Functions called:
312     # - die - Print to stderr and exit, defined in common.sh
313
314     set -exuo pipefail
315
316     # In perpatch, CSIT is cloned inside VPP clone.
317     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
318 }