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