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