Bash functions style cleanup
[csit.git] / resources / libraries / bash / function / per_patch.sh
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 set -exuo pipefail
15
16 # This library defines functions used mainly by per patch entry scripts.
17 # Generally, the functions assume "common.sh" library has been sourced already.
18 # Keep functions ordered alphabetically, please.
19
20 function archive_test_results () {
21
22     # Arguments:
23     # - ${1}: Directory to archive to. Required. Parent has to exist.
24     # Variable set:
25     # - TARGET - Target directory.
26     # Variables read:
27     # - ARCHIVE_DIR - Path to where robot result files are created in.
28     # - VPP_DIR - Path to existing directory, root for to relative paths.
29     # Directories updated:
30     # - ${1} - Created, and robot and parsing files are moved/created there.
31     # Functions called:
32     # - die - Print to stderr and exit, defined in common.sh
33
34     set -exuo pipefail
35
36     cd "${VPP_DIR}" || die "Change directory command failed."
37     TARGET="$(readlink -f "$1")"
38     mkdir -p "${TARGET}" || die "Directory creation failed."
39     for filename in "output.xml" "log.html" "report.html"; do
40         mv "${ARCHIVE_DIR}/${filename}" "${TARGET}/${filename}" || {
41             die "Attempt to move '${filename}' failed."
42         }
43     done
44 }
45
46
47 function archive_parse_test_results () {
48
49     # Arguments:
50     # - ${1}: Directory to archive to. Required. Parent has to exist.
51     # Variables read:
52     # - TARGET - Target directory.
53     # Functions called:
54     # - die - Print to stderr and exit, defined in common.sh
55     # - archive_test_results - Archiving results.
56     # - parse_bmrr_results - See definition in this file.
57
58     set -exuo pipefail
59
60     archive_test_results "$1" || die
61     parse_bmrr_results "${TARGET}" || {
62         die "The function should have died on error."
63     }
64 }
65
66
67 function build_vpp_ubuntu_amd64 () {
68
69     # This function is using Vagrant script to build VPP with all dependencies
70     # that is ARCH/OS aware. VPP repo is SSOT for building mechanics and CSIT
71     # is consuming artifacts. This way if VPP will introduce change in building
72     # mechanics they will not be blocked by CSIT repo.
73     # Arguments:
74     # - ${1} - String identifier for echo, can be unset.
75     # Variables read:
76     # - VPP_DIR - Path to existing directory, parent to accessed directories.
77     # Directories updated:
78     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
79     # Functions called:
80     # - die - Print to stderr and exit, defined in common.sh
81
82     set -exuo pipefail
83
84     cd "${VPP_DIR}" || die "Change directory command failed."
85     echo 'Building using "make build-root/vagrant/build.sh"'
86     build-root/vagrant/"build.sh" || die "Vagrant VPP build script failed."
87     echo "*******************************************************************"
88     echo "* VPP ${1-} BUILD SUCCESSFULLY COMPLETED" || {
89         die "Argument not found."
90     }
91     echo "*******************************************************************"
92 }
93
94
95 function compare_test_results () {
96
97     # Variables read:
98     # - VPP_DIR - Path to directory with VPP git repo (at least built parts).
99     # - ARCHIVE_DIR - Path to where robot result files are created in.
100     # - PYTHON_SCRIPTS_DIR - Path to directory holding comparison utility.
101     # Directories recreated:
102     # - csit_parent - Sibling to csit directory, for holding results
103     #   of parent build.
104     # Functions called:
105     # - die - Print to stderr and exit, defined in common.sh
106     # - parse_bmrr_results - See definition in this file.
107     # Exit code:
108     # - 0 - If the comparison utility sees no regression (nor data error).
109     # - 1 - If the comparison utility sees a regression (or data error).
110
111     set -exuo pipefail
112
113     cd "${VPP_DIR}" || die "Change directory operation failed."
114     # Reusing CSIT main virtualenv.
115     pip install -r "${PYTHON_SCRIPTS_DIR}/perpatch_requirements.txt" || {
116         die "Perpatch Python requirements installation failed."
117     }
118     python "${PYTHON_SCRIPTS_DIR}/compare_perpatch.py"
119     # The exit code determines the vote result.
120 }
121
122
123 function download_builds () {
124
125     # This is mostly useful only for Sandbox testing, to avoid recompilation.
126     #
127     # Arguments:
128     # - ${1} - URL to download VPP builds from.
129     # Variables read:
130     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
131     # Directories created:
132     # - archive - Ends up empty, not to be confused with ${ARCHIVE_DIR}.
133     # - build_current - Holding built artifacts of the patch under test (PUT).
134     # - built_parent - Holding built artifacts of parent of PUT.
135     # Functions called:
136     # - die - Print to stderr and exit, defined in common.sh
137
138     set -exuo pipefail
139
140     cd "${VPP_DIR}" || die "Change directory operation failed."
141     dirs=("build-root" "build_parent" "build_current" "archive" "csit_current")
142     rm -rf ${dirs[@]} || {
143         die "Directory removal failed."
144     }
145     wget -N --progress=dot:giga "${1}" || die "Wget download failed."
146     unzip "archive.zip" || die "Archive extraction failed."
147     mv "archive/build_parent" ./ || die "Move operation failed."
148     mv "archive/build_current" ./ || die "Move operation failed."
149 }
150
151
152 function initialize_csit_dirs () {
153
154     # This could be in prepare_test, but download_builds also needs this.
155     #
156     # Variables read:
157     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
158     # Directories created:
159     # - csit_current - Holding test results of the patch under test (PUT).
160     # - csit_parent - Holding test results of parent of PUT.
161     # Functions called:
162     # - die - Print to stderr and exit, defined in common.sh
163
164     set -exuo pipefail
165
166     cd "${VPP_DIR}" || die "Change directory operation failed."
167     rm -rf "csit_current" "csit_parent" || {
168         die "Directory deletion failed."
169     }
170     mkdir -p "csit_current" "csit_parent" || {
171         die "Directory creation failed."
172     }
173 }
174
175
176 function parse_bmrr_results () {
177
178     # Currently "parsing" is just two greps.
179     # TODO: Re-use PAL parsing code, make parsing more general and centralized.
180     #
181     # Arguments:
182     # - ${1} - Path to (existing) directory holding robot output.xml result.
183     # Files read:
184     # - output.xml - From argument location.
185     # Files updated:
186     # - results.txt - (Re)created, in argument location.
187     # Functions called:
188     # - die - Print to stderr and exit, defined in common.sh
189
190     set -exuo pipefail
191
192     rel_dir="$(readlink -e "${1}")" || die "Readlink failed."
193     in_file="${rel_dir}/output.xml"
194     out_file="${rel_dir}/results.txt"
195     # TODO: Do we need to check echo exit code explicitly?
196     echo "Parsing ${in_file} putting results into ${out_file}"
197     echo "TODO: Re-use parts of PAL when they support subsample test parsing."
198     pattern='Maximum Receive Rate trial results in packets'
199     pattern+=' per second: .*\]</status>'
200     grep -o "${pattern}" "${in_file}" | grep -o '\[.*\]' > "${out_file}" || {
201         die "Some parsing grep command has failed."
202     }
203 }
204
205
206 function select_build () {
207
208     # Arguments:
209     # - ${1} - Path to directory to copy VPP artifacts from. Required.
210     # Variables read:
211     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
212     # - VPP_DIR - Path to existing directory, root for relative paths.
213     # Directories read:
214     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
215     # Directories updated:
216     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
217     # Functions called:
218     # - die - Print to stderr and exit, defined in common.sh
219
220     set -exuo pipefail
221
222     cd "${VPP_DIR}" || die "Change directory operation failed."
223     source_dir="$(readlink -e "$1")"
224     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
225     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
226     # TODO: Is there a nice way to create symlinks,
227     #   so that if job fails on robot, results can be archived?
228 }
229
230
231 function set_aside_commit_build_artifacts () {
232
233     # Function is copying VPP built artifacts from actual checkout commit for
234     # further use and clean git.
235     # Variables read:
236     # - VPP_DIR - Path to existing directory, parent to accessed directories.
237     # Directories read:
238     # - build-root - Existing directory with built VPP artifacts (also DPDK).
239     # Directories updated:
240     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
241     # - build_current - Old contents removed, content of build-root copied here.
242     # Functions called:
243     # - die - Print to stderr and exit, defined in common.sh
244
245     set -exuo pipefail
246
247     cd "${VPP_DIR}" || die "Change directory operation failed."
248     rm -rf "build_current" || die "Remove operation failed."
249     mkdir -p "build_current" || die "Directory creation failed."
250     mv "build-root"/*".deb" "build_current"/ || die "Move operation failed."
251     # The previous build could have left some incompatible leftovers,
252     # e.g. DPDK artifacts of different version (in build/external).
253     # Also, there usually is a copy of dpdk artifact in build-root.
254     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
255     # Finally, check out the parent commit.
256     git checkout HEAD~ || die "Git checkout operation failed."
257     # Display any other leftovers.
258     git status || die "Git status operation failed."
259 }
260
261
262 function set_aside_parent_build_artifacts () {
263
264     # Function is copying VPP built artifacts from parent checkout commit for
265     # further use. Checkout to parent is not part of this function.
266     # Variables read:
267     # - VPP_DIR - Path to existing directory, parent of accessed directories.
268     # Directories read:
269     # - build-root - Existing directory with built VPP artifacts (also DPDK).
270     # Directories updated:
271     # - build_parent - Old directory removed, build-root debs moved here.
272     # Functions called:
273     # - die - Print to stderr and exit, defined in common.sh
274
275     set -exuo pipefail
276
277     cd "${VPP_DIR}" || die "Change directory operation failed."
278     rm -rf "build_parent" || die "Remove failed."
279     mkdir -p "build_parent" || die "Directory creation operation failed."
280     mv "build-root"/*".deb" "build_parent"/ || die "Move operation failed."
281 }
282
283
284 function set_perpatch_dut () {
285
286     # Variables set:
287     # - DUT - CSIT test/ subdirectory containing suites to execute.
288
289     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
290
291     set -exuo pipefail
292
293     DUT="vpp"
294 }
295
296
297 function set_perpatch_vpp_dir () {
298
299     # Variables read:
300     # - CSIT_DIR - Path to existing root of local CSIT git repository.
301     # Variables set:
302     # - VPP_DIR - Path to existing root of local VPP git repository.
303     # Functions called:
304     # - die - Print to stderr and exit, defined in common.sh
305
306     set -exuo pipefail
307
308     # In perpatch, CSIT is cloned inside VPP clone.
309     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
310 }