feat(perpatch): parse results from json
[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 build_vpp_ubuntu_amd64 () {
50
51     # This function is using make pkg-verify to build VPP with all dependencies
52     # that is ARCH/OS aware. VPP repo is SSOT for building mechanics and CSIT
53     # is consuming artifacts. This way if VPP will introduce change in building
54     # mechanics they will not be blocked by CSIT repo.
55     # Arguments:
56     # - ${1} - String identifier for echo, can be unset.
57     # Variables read:
58     # - MAKE_PARALLEL_FLAGS - Make flags when building VPP.
59     # - MAKE_PARALLEL_JOBS - Number of cores to use when building VPP.
60     # - VPP_DIR - Path to existing directory, parent to accessed directories.
61     # Directories updated:
62     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
63     # Functions called:
64     # - die - Print to stderr and exit, defined in common.sh
65
66     set -exuo pipefail
67
68     cd "${VPP_DIR}" || die "Change directory command failed."
69     if [ -n "${MAKE_PARALLEL_FLAGS-}" ]; then
70         echo "Building VPP. Number of cores for build set with" \
71              "MAKE_PARALLEL_FLAGS='${MAKE_PARALLEL_FLAGS}'."
72     elif [ -n "${MAKE_PARALLEL_JOBS-}" ]; then
73         echo "Building VPP. Number of cores for build set with" \
74              "MAKE_PARALLEL_JOBS='${MAKE_PARALLEL_JOBS}'."
75     else
76         echo "Building VPP. Number of cores not set, " \
77              "using build default ($(grep -c ^processor /proc/cpuinfo))."
78     fi
79
80     make UNATTENDED=y pkg-verify || die "VPP build with make pkg-verify failed."
81     echo "* VPP ${1-} BUILD SUCCESSFULLY COMPLETED" || {
82         die "Argument not found."
83     }
84 }
85
86
87 function compare_test_results () {
88
89     # Variables read:
90     # - VPP_DIR - Path to directory with VPP git repo (at least built parts).
91     # - ARCHIVE_DIR - Path to where robot result files are created in.
92     # - PYTHON_SCRIPTS_DIR - Path to directory holding comparison utility.
93     # Directories recreated:
94     # - csit_parent - Sibling to csit directory, for holding results
95     #   of parent build.
96     # Functions called:
97     # - die - Print to stderr and exit, defined in common.sh
98     # Exit code:
99     # - 0 - If the comparison utility sees no regression (nor data error).
100     # - 1 - If the comparison utility sees a regression (or data error).
101
102     set -exuo pipefail
103
104     cd "${VPP_DIR}" || die "Change directory operation failed."
105     # Reusing CSIT main virtualenv.
106     python3 "${TOOLS_DIR}/integrated/compare_perpatch.py"
107     # The exit code determines the vote result.
108 }
109
110
111 function initialize_csit_dirs () {
112
113     set -exuo pipefail
114
115     # Variables read:
116     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
117     # Directories created:
118     # - csit_current - Holding test results of the patch under test (PUT).
119     # - csit_parent - Holding test results of parent of PUT.
120     # Functions called:
121     # - die - Print to stderr and exit, defined in common.sh
122
123     set -exuo pipefail
124
125     cd "${VPP_DIR}" || die "Change directory operation failed."
126     rm -rf "csit_current" "csit_parent" || {
127         die "Directory deletion failed."
128     }
129     mkdir -p "csit_current" "csit_parent" || {
130         die "Directory creation failed."
131     }
132 }
133
134
135 function select_build () {
136
137     # Arguments:
138     # - ${1} - Path to directory to copy VPP artifacts from. Required.
139     # Variables read:
140     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
141     # - VPP_DIR - Path to existing directory, root for relative paths.
142     # Directories read:
143     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
144     # Directories updated:
145     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
146     # Functions called:
147     # - die - Print to stderr and exit, defined in common.sh
148
149     set -exuo pipefail
150
151     cd "${VPP_DIR}" || die "Change directory operation failed."
152     source_dir="$(readlink -e "$1")"
153     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
154     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
155     # TODO: Is there a nice way to create symlinks,
156     #   so that if job fails on robot, results can be archived?
157 }
158
159
160 function set_aside_commit_build_artifacts () {
161
162     # Function is copying VPP built artifacts from actual checkout commit for
163     # further use and clean git.
164     # Variables read:
165     # - VPP_DIR - Path to existing directory, parent to accessed directories.
166     # Directories read:
167     # - build-root - Existing directory with built VPP artifacts (also DPDK).
168     # Directories updated:
169     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
170     # - build_current - Old contents removed, content of build-root copied here.
171     # Functions called:
172     # - die - Print to stderr and exit, defined in common.sh
173
174     set -exuo pipefail
175
176     cd "${VPP_DIR}" || die "Change directory operation failed."
177     rm -rf "build_current" || die "Remove operation failed."
178     mkdir -p "build_current" || die "Directory creation failed."
179     mv "build-root"/*".deb" "build_current"/ || die "Move operation failed."
180     # The previous build could have left some incompatible leftovers,
181     # e.g. DPDK artifacts of different version (in build/external).
182     # Also, there usually is a copy of dpdk artifact in build-root.
183     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
184     # Finally, check out the parent commit.
185     git checkout HEAD~ || die "Git checkout operation failed."
186     # Display any other leftovers.
187     git status || die "Git status operation failed."
188 }
189
190
191 function set_aside_parent_build_artifacts () {
192
193     # Function is copying VPP built artifacts from parent checkout commit for
194     # further use. Checkout to parent is not part of this function.
195     # Variables read:
196     # - VPP_DIR - Path to existing directory, parent of accessed directories.
197     # Directories read:
198     # - build-root - Existing directory with built VPP artifacts (also DPDK).
199     # Directories updated:
200     # - build_parent - Old directory removed, build-root debs moved here.
201     # Functions called:
202     # - die - Print to stderr and exit, defined in common.sh
203
204     set -exuo pipefail
205
206     cd "${VPP_DIR}" || die "Change directory operation failed."
207     rm -rf "build_parent" || die "Remove failed."
208     mkdir -p "build_parent" || die "Directory creation operation failed."
209     mv "build-root"/*".deb" "build_parent"/ || die "Move operation failed."
210 }
211
212
213 function set_perpatch_dut () {
214
215     # Variables set:
216     # - DUT - CSIT test/ subdirectory containing suites to execute.
217
218     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
219
220     set -exuo pipefail
221
222     DUT="vpp"
223 }
224
225
226 function set_perpatch_vpp_dir () {
227
228     # Variables read:
229     # - CSIT_DIR - Path to existing root of local CSIT git repository.
230     # Variables set:
231     # - VPP_DIR - Path to existing root of local VPP git repository.
232     # Functions called:
233     # - die - Print to stderr and exit, defined in common.sh
234
235     set -exuo pipefail
236
237     # In perpatch, CSIT is cloned inside VPP clone.
238     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
239 }