feat(bootstrap): refactor functions before bisect
[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
22 function build_vpp_ubuntu () {
23
24     # This function is using make pkg-verify to build VPP with all dependencies
25     # that is ARCH/OS aware. VPP repo is SSOT for building mechanics and CSIT
26     # is consuming artifacts. This way if VPP will introduce change in building
27     # mechanics they will not be blocked by CSIT repo.
28     #
29     # Arguments:
30     # - ${1} - String identifier for echo, can be unset.
31     # Variables read:
32     # - MAKE_PARALLEL_FLAGS - Make flags when building VPP.
33     # - MAKE_PARALLEL_JOBS - Number of cores to use when building VPP.
34     # - VPP_DIR - Path to existing directory, parent to accessed directories.
35     # Directories updated:
36     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
37     # Functions called:
38     # - die - Print to stderr and exit, defined in common.sh
39
40     set -exuo pipefail
41
42     cd "${VPP_DIR}" || die "Change directory command failed."
43     if [ -n "${MAKE_PARALLEL_FLAGS-}" ]; then
44         echo "Building VPP. Number of cores for build set with" \
45              "MAKE_PARALLEL_FLAGS='${MAKE_PARALLEL_FLAGS}'."
46     elif [ -n "${MAKE_PARALLEL_JOBS-}" ]; then
47         echo "Building VPP. Number of cores for build set with" \
48              "MAKE_PARALLEL_JOBS='${MAKE_PARALLEL_JOBS}'."
49     else
50         echo "Building VPP. Number of cores not set, " \
51              "using build default ($(grep -c ^processor /proc/cpuinfo))."
52     fi
53
54     make UNATTENDED=y pkg-verify || die "VPP build with make pkg-verify failed."
55     echo "* VPP ${1-} BUILD SUCCESSFULLY COMPLETED" || {
56         die "Argument not found."
57     }
58 }
59
60
61 function compare_test_results () {
62
63     # Variables read:
64     # - VPP_DIR - Path to directory with VPP git repo (at least built parts).
65     # - ARCHIVE_DIR - Path to where robot result files are created in.
66     # - PYTHON_SCRIPTS_DIR - Path to directory holding comparison utility.
67     # Directories recreated:
68     # - csit_parent - Sibling to csit directory, for holding results
69     #   of parent build.
70     # Functions called:
71     # - die - Print to stderr and exit, defined in common.sh
72     # Exit code:
73     # - 0 - If the comparison utility sees no regression (nor data error).
74     # - 1 - If the comparison utility sees a regression (or data error).
75
76     set -exuo pipefail
77
78     cd "${VPP_DIR}" || die "Change directory operation failed."
79     # Reusing CSIT main virtualenv.
80     python3 "${TOOLS_DIR}/integrated/compare_perpatch.py"
81     # The exit code determines the vote result.
82 }
83
84
85 function initialize_csit_dirs () {
86
87     set -exuo pipefail
88
89     # Variables read:
90     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
91     # Directories created:
92     # - csit_{part} - See the caller what it is used for.
93     # Functions called:
94     # - die - Print to stderr and exit, defined in common.sh
95
96     set -exuo pipefail
97
98     cd "${VPP_DIR}" || die "Change directory operation failed."
99     while true; do
100         if [[ ${#} < 1 ]]; then
101             # All directories created.
102             break
103         fi
104         name_part="${1}" || die
105         shift || die
106         dir_name="csit_${name_part}" || die
107         rm -rf "${dir_name}" || die "Directory deletion failed."
108         mkdir -p "${dir_name}" || die "Directory creation failed."
109     done
110 }
111
112
113 function move_test_results () {
114
115     # Arguments:
116     # - ${1}: Directory to archive to. Required. Parent has to exist.
117     # Variable set:
118     # - TARGET - Target archival directory, equivalent to the argument.
119     # Variables read:
120     # - ARCHIVE_DIR - Path to where robot result files are created in.
121     # - VPP_DIR - Path to existing directory, root for to relative paths.
122     # Directories updated:
123     # - ${1} - Created, and robot and parsing files are moved/created there.
124     # Functions called:
125     # - die - Print to stderr and exit, defined in common.sh
126
127     set -exuo pipefail
128
129     cd "${VPP_DIR}" || die "Change directory command failed."
130     TARGET="$(readlink -f "$1")"
131     mkdir -p "${TARGET}" || die "Directory creation failed."
132     file_list=("output.xml" "log.html" "report.html" "tests")
133     for filename in "${file_list[@]}"; do
134         mv "${ARCHIVE_DIR}/${filename}" "${TARGET}/${filename}" || die
135     done
136 }
137
138
139 function select_build () {
140
141     # Arguments:
142     # - ${1} - Path to directory to copy VPP artifacts from. Required.
143     # Variables read:
144     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
145     # - VPP_DIR - Path to existing directory, root for relative paths.
146     # Directories read:
147     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
148     # Directories updated:
149     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
150     # Functions called:
151     # - die - Print to stderr and exit, defined in common.sh
152
153     set -exuo pipefail
154
155     cd "${VPP_DIR}" || die "Change directory operation failed."
156     source_dir="$(readlink -e "$1")"
157     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
158     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
159     # TODO: Is there a nice way to create symlinks,
160     #   so that if job fails on robot, results can be archived?
161 }
162
163
164 function set_aside_build_artifacts () {
165
166     # Function used to save VPP .deb artifacts from currently finished build.
167     #
168     # After the artifacts are copied to the target directory,
169     # the main git tree is cleaned up to not interfere with next build.
170     #
171     # Arguments:
172     # - ${1} - String to derive the target directory name from. Required.
173     # Variables read:
174     # - VPP_DIR - Path to existing directory, parent to accessed directories.
175     # Directories read:
176     # - build-root - Existing directory with built VPP artifacts (also DPDK).
177     # Directories updated:
178     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
179     # - build_${1} - Old contents removed, content of build-root copied here.
180     # Functions called:
181     # - die - Print to stderr and exit, defined in common.sh
182
183     set -exuo pipefail
184
185     cd "${VPP_DIR}" || die "Change directory operation failed."
186     dir_name="build_${1}" || die
187     rm -rf "${dir_name}" || die "Remove operation failed."
188     mkdir -p "${dir_name}" || die "Directory creation failed."
189     mv "build-root"/*".deb" "${dir_name}"/ || die "Move operation failed."
190     # The previous build could have left some incompatible leftovers,
191     # e.g. DPDK artifacts of different version (in build/external).
192     # Also, there usually is a copy of dpdk artifact in build-root.
193     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
194     git status || die
195 }
196
197
198 function set_perpatch_dut () {
199
200     # Variables set:
201     # - DUT - CSIT test/ subdirectory containing suites to execute.
202
203     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
204
205     set -exuo pipefail
206
207     DUT="vpp"
208 }
209
210
211 function set_perpatch_vpp_dir () {
212
213     # Variables read:
214     # - CSIT_DIR - Path to existing root of local CSIT git repository.
215     # Variables set:
216     # - VPP_DIR - Path to existing root of local VPP git repository.
217     # Functions called:
218     # - die - Print to stderr and exit, defined in common.sh
219
220     set -exuo pipefail
221
222     # In perpatch, CSIT is cloned inside VPP clone.
223     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
224 }