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