244b6f448ce92adec91db4eb7a0e600694f1bebe
[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     # TODO: Make sure whether this works on other distros/archs too.
58
59     # Arguments:
60     # - ${1} - String identifier for echo, can be unset.
61     # Variables read:
62     # - VPP_DIR - Path to existing directory, parent to accessed directories.
63     # Directories updated:
64     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
65     # - ${VPP_DIR}/build-root - Final build artifacts for CSIT end up here.
66     # - ${VPP_DIR}/dpdk - The dpdk artifact is built, but moved to build-root/.
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     # TODO: Do we want to support "${DRYRUN}" == "True"?
73     make UNATTENDED=yes install-dep || die "Make install-dep failed."
74     # The per_patch script calls this function twice, first for the new commit,
75     # then for its parent commit. On Jenkins, no dpdk is installed at first,
76     # locally it might have been installed. New dpdk is installed second call.
77     # If make detects installed vpp-ext-deps with matching version,
78     # it skips building vpp-ext-deps entirely, but we need that file.
79     # On the other hand, if parent uses different dpdk version,
80     # The new vpp-ext-deps is built, but the old one is not removed
81     # from the build directory if present. (Further functions move both,
82     # and during test dpkg would decide randomly which version gets installed.)
83     # As per_patch is too dumb (yet) to detect any of that,
84     # the only safe solution is to clean build directory and force rebuild.
85     # TODO: Make this function smarter and skip DPDK rebuilds if possible.
86     cmd=("dpkg-query" "--showformat='$${Version}'" "--show" "vpp-ext-deps")
87     installed_deb_ver="$(sudo "${cmd[@]}" || true)"
88     if [[ -n "${installed_deb_ver}" ]]; then
89         sudo dpkg --purge "vpp-ext-deps" || {
90             die "DPDK package uninstalation failed."
91         }
92     fi
93     make UNATTENDED=yes install-ext-deps || {
94         die "Make install-ext-deps failed."
95     }
96     build-root/vagrant/"build.sh" || die "Vagrant VPP build script failed."
97     # CSIT also needs the external dependency artifacts, which is not in
98     # build-root.
99     mv -v "build/external/vpp-ext-deps"*".deb" "build-root"/ || {
100         die "*.deb move failed."
101     }
102     echo "*******************************************************************"
103     echo "* VPP ${1-} BUILD SUCCESSFULLY COMPLETED" || {
104         die "Argument not found."
105     }
106     echo "*******************************************************************"
107 }
108
109
110 function compare_test_results () {
111
112     set -exuo pipefail
113
114     # Variables read:
115     # - VPP_DIR - Path to directory with VPP git repo (at least built parts).
116     # - ARCHIVE_DIR - Path to where robot result files are created in.
117     # - PYTHON_SCRIPTS_DIR - Path to directory holding comparison utility.
118     # Directories recreated:
119     # - csit_parent - Sibling to csit directory, for holding results
120     #   of parent build.
121     # Functions called:
122     # - die - Print to stderr and exit, defined in common.sh
123     # - parse_bmrr_results - See definition in this file.
124     # Exit code:
125     # - 0 - If the comparison utility sees no regression (nor data error).
126     # - 1 - If the comparison utility sees a regression (or data error).
127
128     cd "${VPP_DIR}" || die "Change directory operation failed."
129     # Reusing CSIT main virtualenv.
130     pip install -r "${PYTHON_SCRIPTS_DIR}/perpatch_requirements.txt" || {
131         die "Perpatch Python requirements installation failed."
132     }
133     python "${PYTHON_SCRIPTS_DIR}/compare_perpatch.py"
134     # The exit code determines the vote result.
135 }
136
137
138 function download_builds () {
139
140     set -exuo pipefail
141
142     # This is mostly useful only for Sandbox testing, to avoid recompilation.
143     #
144     # Arguments:
145     # - ${1} - URL to download VPP builds from.
146     # Variables read:
147     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
148     # Directories created:
149     # - archive - Ends up empty, not to be confused with ${ARCHIVE_DIR}.
150     # - build_new - Holding built artifacts of the patch under test (PUT).
151     # - built_parent - Holding built artifacts of parent of PUT.
152     # Functions called:
153     # - die - Print to stderr and exit, defined in common.sh
154
155     cd "${VPP_DIR}" || die "Change directory operation failed."
156     rm -rf "build-root" "build_parent" "build_new" "archive" "csit_new" || {
157         die "Directory removal failed."
158     }
159     wget -N --progress=dot:giga "${1}" || die "Wget download failed."
160     unzip "archive.zip" || die "Archive extraction failed."
161     mv "archive/build_parent" ./ || die "Move operation failed."
162     mv "archive/build_new" ./ || die "Move operation failed."
163 }
164
165
166 function initialize_csit_dirs () {
167
168     set -exuo pipefail
169
170     # This could be in prepare_test, but download_builds also needs this.
171     #
172     # Variables read:
173     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
174     # Directories created:
175     # - csit_new - Holding test results of the patch under test (PUT).
176     # - csit_parent - Holding test results of parent of PUT.
177     # Functions called:
178     # - die - Print to stderr and exit, defined in common.sh
179
180     cd "${VPP_DIR}" || die "Change directory operation failed."
181     rm -rf "csit_new" "csit_parent" || {
182         die "Directory deletion failed."
183     }
184     mkdir -p "csit_new" "csit_parent" || {
185         die "Directory creation failed."
186     }
187 }
188
189
190 function parse_bmrr_results () {
191
192     set -exuo pipefail
193
194     # Currently "parsing" is just two greps.
195     # TODO: Re-use PAL parsing code, make parsing more general and centralized.
196     #
197     # Arguments:
198     # - ${1} - Path to (existing) directory holding robot output.xml result.
199     # Files read:
200     # - output.xml - From argument location.
201     # Files updated:
202     # - results.txt - (Re)created, in argument location.
203     # Functions called:
204     # - die - Print to stderr and exit, defined in common.sh
205
206     rel_dir="$(readlink -e "${1}")" || die "Readlink failed."
207     in_file="${rel_dir}/output.xml"
208     out_file="${rel_dir}/results.txt"
209     # TODO: Do we need to check echo exit code explicitly?
210     echo "Parsing ${in_file} putting results into ${out_file}"
211     echo "TODO: Re-use parts of PAL when they support subsample test parsing."
212     pattern='Maximum Receive Rate trial results in packets'
213     pattern+=' per second: .*\]</status>'
214     grep -o "${pattern}" "${in_file}" | grep -o '\[.*\]' > "${out_file}" || {
215         die "Some parsing grep command has failed."
216     }
217 }
218
219
220 function prepare_build_parent () {
221
222     set -exuo pipefail
223
224     # Variables read:
225     # - VPP_DIR - Path to existing directory, parent to accessed directories.
226     # Directories read:
227     # - build-root - Existing directory with built VPP artifacts (also DPDK).
228     # Directories updated:
229     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
230     # - build_new - Old contents removed, content of build-root copied here.
231     # Functions called:
232     # - die - Print to stderr and exit, defined in common.sh
233
234     cd "${VPP_DIR}" || die "Change directory operation failed."
235     rm -rf "build_new" || die "Remove operation failed."
236     mkdir -p "build_new" || die "Directory creation failed."
237     mv "build-root"/*".deb" "build_new"/ || die "Move operation failed."
238     # The previous build could have left some incompatible leftovers,
239     # e.g. DPDK artifacts of different version (in build/external).
240     # Also, there usually is a copy of dpdk artifact in build-root.
241     git clean -dffx "build"/ "build-root"/ || die "Git clean operation failed."
242     # Finally, check out the parent commit.
243     git checkout HEAD~ || die "Git checkout operation failed."
244     # Display any other leftovers.
245     git status || die "Git status operation failed."
246 }
247
248
249 function prepare_test () {
250
251     set -exuo pipefail
252
253     # Variables read:
254     # - VPP_DIR - Path to existing directory, parent of accessed directories.
255     # Directories read:
256     # - build-root - Existing directory with built VPP artifacts (also DPDK).
257     # Directories updated:
258     # - build_parent - Old directory removed, build-root debs moved here.
259     # Functions called:
260     # - die - Print to stderr and exit, defined in common.sh
261
262     cd "${VPP_DIR}" || die "Change directory operation failed."
263     rm -rf "build_parent" || die "Remove failed."
264     mkdir -p "build_parent" || die "Directory creation operation failed."
265     mv "build-root"/*".deb" "build_parent"/ || die "Move operation failed."
266 }
267
268
269 function select_build () {
270
271     set -exuo pipefail
272
273     # Arguments:
274     # - ${1} - Path to directory to copy VPP artifacts from. Required.
275     # Variables read:
276     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
277     # - VPP_DIR - Path to existing directory, root for relative paths.
278     # Directories read:
279     # - ${1} - Existing directory with built new VPP artifacts (and DPDK).
280     # Directories updated:
281     # - ${DOWNLOAD_DIR} - Old content removed, .deb files from ${1} copied here.
282     # Functions called:
283     # - die - Print to stderr and exit, defined in common.sh
284
285     cd "${VPP_DIR}" || die "Change directory operation failed."
286     source_dir="$(readlink -e "$1")"
287     rm -rf "${DOWNLOAD_DIR}"/* || die "Cleanup of download dir failed."
288     cp "${source_dir}"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
289     # TODO: Is there a nice way to create symlinks,
290     #   so that if job fails on robot, results can be archived?
291 }
292
293
294 function set_perpatch_dut () {
295
296     set -exuo pipefail
297
298     # Variables set:
299     # - DUT - CSIT test/ subdirectory containing suites to execute.
300
301     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
302
303     DUT="vpp"
304 }
305
306
307 function set_perpatch_vpp_dir () {
308
309     set -exuo pipefail
310
311     # Variables read:
312     # - CSIT_DIR - Path to existing root of local CSIT git repository.
313     # Variables set:
314     # - VPP_DIR - Path to existing root of local VPP git repository.
315     # Functions called:
316     # - die - Print to stderr and exit, defined in common.sh
317
318     # In perpatch, CSIT is cloned inside VPP clone.
319     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
320 }