b6a572d654ecdf7a6c0492eb45761fd53c31b126
[csit.git] / resources / libraries / bash / function / per_patch.sh
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Copyright (c) 2021 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     file_list=("output.xml" "log.html" "report.html")
41     file_list+=("tests" "tests_output_raw.tar.xz")
42     for filename in "${file_list[@]}"; do
43         mv "${ARCHIVE_DIR}/${filename}" "${TARGET}/${filename}" || {
44             die "Attempt to move '${filename}' failed."
45         }
46     done
47 }
48
49
50 function archive_parse_test_results () {
51
52     # Arguments:
53     # - ${1}: Directory to archive to. Required. Parent has to exist.
54     # Variables read:
55     # - TARGET - Target directory.
56     # Functions called:
57     # - die - Print to stderr and exit, defined in common.sh
58     # - archive_test_results - Archiving results.
59     # - parse_bmrr_results - See definition in this file.
60
61     set -exuo pipefail
62
63     archive_test_results "$1" || die
64     parse_bmrr_results "${TARGET}" || {
65         die "The function should have died on error."
66     }
67 }
68
69
70 function build_vpp_ubuntu_amd64 () {
71
72     # This function is using make pkg-verify to build VPP with all dependencies
73     # that is ARCH/OS aware. VPP repo is SSOT for building mechanics and CSIT
74     # is consuming artifacts. This way if VPP will introduce change in building
75     # mechanics they will not be blocked by CSIT repo.
76     # Arguments:
77     # - ${1} - String identifier for echo, can be unset.
78     # Variables read:
79     # - MAKE_PARALLEL_FLAGS - Make flags when building VPP.
80     # - MAKE_PARALLEL_JOBS - Number of cores to use when building VPP.
81     # - VPP_DIR - Path to existing directory, parent to accessed directories.
82     # Directories updated:
83     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
84     # Functions called:
85     # - die - Print to stderr and exit, defined in common.sh
86
87     set -exuo pipefail
88
89     cd "${VPP_DIR}" || die "Change directory command failed."
90     if [ -n "${MAKE_PARALLEL_FLAGS-}" ]; then
91         echo "Building VPP. Number of cores for build set with" \
92              "MAKE_PARALLEL_FLAGS='${MAKE_PARALLEL_FLAGS}'."
93     elif [ -n "${MAKE_PARALLEL_JOBS-}" ]; then
94         echo "Building VPP. Number of cores for build set with" \
95              "MAKE_PARALLEL_JOBS='${MAKE_PARALLEL_JOBS}'."
96     else
97         echo "Building VPP. Number of cores not set, " \
98              "using build default ($(grep -c ^processor /proc/cpuinfo))."
99     fi
100
101     make UNATTENDED=y pkg-verify || die "VPP build with make pkg-verify failed."
102     echo "* VPP ${1-} BUILD SUCCESSFULLY COMPLETED" || {
103         die "Argument not found."
104     }
105 }
106
107
108 function compare_test_results () {
109
110     # Variables read:
111     # - VPP_DIR - Path to directory with VPP git repo (at least built parts).
112     # - ARCHIVE_DIR - Path to where robot result files are created in.
113     # - PYTHON_SCRIPTS_DIR - Path to directory holding comparison utility.
114     # Directories recreated:
115     # - csit_parent - Sibling to csit directory, for holding results
116     #   of parent build.
117     # Functions called:
118     # - die - Print to stderr and exit, defined in common.sh
119     # - parse_bmrr_results - See definition in this file.
120     # Exit code:
121     # - 0 - If the comparison utility sees no regression (nor data error).
122     # - 1 - If the comparison utility sees a regression (or data error).
123
124     set -exuo pipefail
125
126     cd "${VPP_DIR}" || die "Change directory operation failed."
127     # Reusing CSIT main virtualenv.
128     python3 "${TOOLS_DIR}/integrated/compare_perpatch.py"
129     # The exit code determines the vote result.
130 }
131
132
133 function initialize_csit_dirs () {
134
135     set -exuo pipefail
136
137     # Variables read:
138     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
139     # Directories created:
140     # - csit_current - Holding test results of the patch under test (PUT).
141     # - csit_parent - Holding test results of parent of PUT.
142     # Functions called:
143     # - die - Print to stderr and exit, defined in common.sh
144
145     set -exuo pipefail
146
147     cd "${VPP_DIR}" || die "Change directory operation failed."
148     rm -rf "csit_current" "csit_parent" || {
149         die "Directory deletion failed."
150     }
151     mkdir -p "csit_current" "csit_parent" || {
152         die "Directory creation failed."
153     }
154 }
155
156
157 function parse_bmrr_results () {
158
159     # Currently "parsing" is just two greps.
160     # TODO: Re-use PAL parsing code, make parsing more general and centralized.
161     #
162     # Arguments:
163     # - ${1} - Path to (existing) directory holding robot output.xml result.
164     # Files read:
165     # - output.xml - From argument location.
166     # Files updated:
167     # - results.txt - (Re)created, in argument location.
168     # Functions called:
169     # - die - Print to stderr and exit, defined in common.sh
170
171     set -exuo pipefail
172
173     rel_dir="$(readlink -e "${1}")" || die "Readlink failed."
174     in_file="${rel_dir}/output.xml"
175     out_file="${rel_dir}/results.txt"
176     # TODO: Do we need to check echo exit code explicitly?
177     echo "Parsing ${in_file} putting results into ${out_file}"
178     echo "TODO: Re-use parts of PAL when they support subsample test parsing."
179     pattern='Maximum Receive Rate trial results in packets'
180     pattern+=' per second: .*\]</status>'
181     grep -o "${pattern}" "${in_file}" | grep -o '\[.*\]' > "${out_file}" || {
182         die "Some parsing grep command has failed."
183     }
184 }
185
186
187 function select_build () {
188
189     # Arguments:
190     # - ${1} - Path to directory to copy VPP artifacts from. Required.
191     # Variables read:
192     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
193     # - VPP_DIR - Path to existing directory, root for relative paths.
194     # Directories read:
195     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
196     # Directories updated:
197     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
198     # Functions called:
199     # - die - Print to stderr and exit, defined in common.sh
200
201     set -exuo pipefail
202
203     cd "${VPP_DIR}" || die "Change directory operation failed."
204     source_dir="$(readlink -e "$1")"
205     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
206     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
207     # TODO: Is there a nice way to create symlinks,
208     #   so that if job fails on robot, results can be archived?
209 }
210
211
212 function set_aside_commit_build_artifacts () {
213
214     # Function is copying VPP built artifacts from actual checkout commit for
215     # further use and clean git.
216     # Variables read:
217     # - VPP_DIR - Path to existing directory, parent to accessed directories.
218     # Directories read:
219     # - build-root - Existing directory with built VPP artifacts (also DPDK).
220     # Directories updated:
221     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
222     # - build_current - Old contents removed, content of build-root copied here.
223     # Functions called:
224     # - die - Print to stderr and exit, defined in common.sh
225
226     set -exuo pipefail
227
228     cd "${VPP_DIR}" || die "Change directory operation failed."
229     rm -rf "build_current" || die "Remove operation failed."
230     mkdir -p "build_current" || die "Directory creation failed."
231     mv "build-root"/*".deb" "build_current"/ || die "Move operation failed."
232     # The previous build could have left some incompatible leftovers,
233     # e.g. DPDK artifacts of different version (in build/external).
234     # Also, there usually is a copy of dpdk artifact in build-root.
235     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
236     # Finally, check out the parent commit.
237     git checkout HEAD~ || die "Git checkout operation failed."
238     # Display any other leftovers.
239     git status || die "Git status operation failed."
240 }
241
242
243 function set_aside_parent_build_artifacts () {
244
245     # Function is copying VPP built artifacts from parent checkout commit for
246     # further use. Checkout to parent is not part of this function.
247     # Variables read:
248     # - VPP_DIR - Path to existing directory, parent of accessed directories.
249     # Directories read:
250     # - build-root - Existing directory with built VPP artifacts (also DPDK).
251     # Directories updated:
252     # - build_parent - Old directory removed, build-root debs moved here.
253     # Functions called:
254     # - die - Print to stderr and exit, defined in common.sh
255
256     set -exuo pipefail
257
258     cd "${VPP_DIR}" || die "Change directory operation failed."
259     rm -rf "build_parent" || die "Remove failed."
260     mkdir -p "build_parent" || die "Directory creation operation failed."
261     mv "build-root"/*".deb" "build_parent"/ || die "Move operation failed."
262 }
263
264
265 function set_perpatch_dut () {
266
267     # Variables set:
268     # - DUT - CSIT test/ subdirectory containing suites to execute.
269
270     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
271
272     set -exuo pipefail
273
274     DUT="vpp"
275 }
276
277
278 function set_perpatch_vpp_dir () {
279
280     # Variables read:
281     # - CSIT_DIR - Path to existing root of local CSIT git repository.
282     # Variables set:
283     # - VPP_DIR - Path to existing root of local VPP git repository.
284     # Functions called:
285     # - die - Print to stderr and exit, defined in common.sh
286
287     set -exuo pipefail
288
289     # In perpatch, CSIT is cloned inside VPP clone.
290     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
291 }