feat(density): Delete ipsec nfv_density tests
[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 main_bisect_loop () {
114
115     # Perform the iterative part of bisect entry script.
116     #
117     # The logic is too complex to remain in the entry script.
118     #
119     # At the start, the loop assumes git bisect old/new has just been executed,
120     # and verified more iterations are needed.
121     # The iteration cleans the build directory and builds the new mid commit.
122     # Then, testbed is reserved, tests run, and testbed unreserved.
123     # Results are moved from default to archive location
124     # (indexed by iteration number) and analyzed.
125     # The new adjective ("old" or "new") is selected,
126     # and git bisect with the adjective is executed.
127     # The symlinks csit_early and csit_late are updated to tightest bounds.
128     # The git.log file is examined and if the bisect is finished, loop ends.
129
130     iteration=0
131     while true
132     do
133         let iteration+=1
134         git clean -dffx "build"/ "build-root"/ || die
135         build_vpp_ubuntu "MIDDLE" || die
136         select_build "build-root" || die
137         check_download_dir || die
138         reserve_and_cleanup_testbed || die
139         run_robot || die
140         move_test_results "csit_middle/${iteration}" || die
141         untrap_and_unreserve_testbed || die
142         rm -vf "csit_mid" || die
143         ln -s -T "csit_middle/${iteration}" "csit_mid" || die
144         set +e
145         python3 "${TOOLS_DIR}/integrated/compare_bisect.py"
146         bisect_rc="${?}"
147         set -e
148         if [[ "${bisect_rc}" == "3" ]]; then
149             adjective="new"
150             rm -v "csit_late" || die
151             ln -s -T "csit_middle/${iteration}" "csit_late" || die
152         elif [[ "${bisect_rc}" == "0" ]]; then
153             adjective="old"
154             rm -v "csit_early" || die
155             ln -s -T "csit_middle/${iteration}" "csit_early" || die
156         else
157             die "Unexpected return code: ${bisect_rc}"
158         fi
159         git bisect "${adjective}" | tee "git.log" || die
160         git describe || die
161         git status || die
162         if head -n 1 "git.log" | cut -b -11 | fgrep -q "Bisecting:"; then
163             echo "Still bisecting..."
164         else
165             echo "Bisecting done."
166             break
167         fi
168     done
169 }
170
171
172 function move_test_results () {
173
174     # Arguments:
175     # - ${1}: Directory to archive to. Required. Parent has to exist.
176     # Variable set:
177     # - TARGET - Target archival directory, equivalent to the argument.
178     # Variables read:
179     # - ARCHIVE_DIR - Path to where robot result files are created in.
180     # - VPP_DIR - Path to existing directory, root for to relative paths.
181     # Directories updated:
182     # - ${1} - Created, and robot and parsing files are moved/created there.
183     # Functions called:
184     # - die - Print to stderr and exit, defined in common.sh
185
186     set -exuo pipefail
187
188     cd "${VPP_DIR}" || die "Change directory command failed."
189     TARGET="$(readlink -f "$1")"
190     mkdir -p "${TARGET}" || die "Directory creation failed."
191     file_list=("output.xml" "log.html" "report.html" "tests")
192     for filename in "${file_list[@]}"; do
193         mv "${ARCHIVE_DIR}/${filename}" "${TARGET}/${filename}" || die
194     done
195 }
196
197
198 function select_build () {
199
200     # Arguments:
201     # - ${1} - Path to directory to copy VPP artifacts from. Required.
202     # Variables read:
203     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
204     # - VPP_DIR - Path to existing directory, root for relative paths.
205     # Directories read:
206     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
207     # Directories updated:
208     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
209     # Functions called:
210     # - die - Print to stderr and exit, defined in common.sh
211
212     set -exuo pipefail
213
214     cd "${VPP_DIR}" || die "Change directory operation failed."
215     source_dir="$(readlink -e "$1")"
216     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
217     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
218     # TODO: Is there a nice way to create symlinks,
219     #   so that if job fails on robot, results can be archived?
220 }
221
222
223 function set_aside_build_artifacts () {
224
225     # Function used to save VPP .deb artifacts from currently finished build.
226     #
227     # After the artifacts are copied to the target directory,
228     # the main git tree is cleaned up to not interfere with next build.
229     #
230     # Arguments:
231     # - ${1} - String to derive the target directory name from. Required.
232     # Variables read:
233     # - VPP_DIR - Path to existing directory, parent to accessed directories.
234     # Directories read:
235     # - build-root - Existing directory with built VPP artifacts (also DPDK).
236     # Directories updated:
237     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
238     # - build_${1} - Old contents removed, content of build-root copied here.
239     # Functions called:
240     # - die - Print to stderr and exit, defined in common.sh
241
242     set -exuo pipefail
243
244     cd "${VPP_DIR}" || die "Change directory operation failed."
245     dir_name="build_${1}" || die
246     rm -rf "${dir_name}" || die "Remove operation failed."
247     mkdir -p "${dir_name}" || die "Directory creation failed."
248     mv "build-root"/*".deb" "${dir_name}"/ || die "Move operation failed."
249     # The previous build could have left some incompatible leftovers,
250     # e.g. DPDK artifacts of different version (in build/external).
251     # Also, there usually is a copy of dpdk artifact in build-root.
252     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
253     git status || die
254 }
255
256
257 function set_perpatch_dut () {
258
259     # Variables set:
260     # - DUT - CSIT test/ subdirectory containing suites to execute.
261
262     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
263
264     set -exuo pipefail
265
266     DUT="vpp"
267 }
268
269
270 function set_perpatch_vpp_dir () {
271
272     # Variables read:
273     # - CSIT_DIR - Path to existing root of local CSIT git repository.
274     # Variables set:
275     # - VPP_DIR - Path to existing root of local VPP git repository.
276     # Functions called:
277     # - die - Print to stderr and exit, defined in common.sh
278
279     set -exuo pipefail
280
281     # In perpatch, CSIT is cloned inside VPP clone.
282     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
283 }