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