CSIT-1135: Scripts for VPP per-patch testing
[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 build_vpp_ubuntu_amd64 () {
25
26     set -exuo pipefail
27
28     # TODO: Make sure whether this works on other distros/archs too.
29
30     # Arguments:
31     # - ${1} - String identifier for echo, can be unset.
32     # Variables read:
33     # - VPP_DIR - Path to existing directory, parent to accessed directories.
34     # Directories updated:
35     # - ${VPP_DIR} - Whole subtree, many files (re)created by the build process.
36     # - ${VPP_DIR}/build-root - Final build artifacts for CSIT end up here.
37     # - ${VPP_DIR}/dpdk - The dpdk artifact is built, but moved to build-root/.
38     # Functions called:
39     # - die - Print to stderr and exit, defined in common.sh
40
41     cd "${VPP_DIR}" || die "Change directory command failed."
42     echo 'Building using "make build-root/vagrant/build.sh"'
43     # TODO: Do we want to support "${DRYRUN}" == "True"?
44     make UNATTENDED=yes install-dep || die "Make install-dep failed."
45     # The per_patch script calls this function twice, first for the new commit,
46     # then for its parent commit. On Jenkins, no dpdk is installed at first,
47     # locally it might have been installed. New dpdk is installed second call.
48     # If make detects installed vpp-dpdk-dev with matching version,
49     # it skips building vpp-dpdk-dkms entirely, but we need that file.
50     # On the other hand, if parent uses different dpdk version,
51     # The new vpp-dpdk-dkms is built, but the old one is not removed
52     # from the build directory if present. (Further functions move both,
53     # and during test dpkg decides on its own which version gets installed.)
54     # As per_patch is too dumb (yet) to detect any of that,
55     # the only safe solution is to clean build directory and force rebuild.
56     # TODO: Make this function smarter and skip DPDK rebuilds if possible.
57     cmd=("dpkg-query" "--showformat='$${Version}'" "--show" "vpp-dpdk-dev")
58     installed_deb_ver="$(sudo "${cmd[@]}" || true)"
59     if [[ -n "${installed_deb_ver}" ]]; then
60         sudo dpkg --purge "vpp-dpdk-dev" || {
61             die "Dpdk package uninstalation failed."
62         }
63     fi
64     make UNATTENDED=yes dpdk-install-dev || {
65         die "Make dpdk-install-dev failed."
66     }
67     build-root/vagrant/"build.sh" || die "Vagrant VPP build script failed."
68     # CSIT also needs the DPDK artifacts, which is not in build-root.
69     mv -v "dpdk/vpp-dpdk-dkms"*".deb" "build-root"/ || {
70         die "*.deb move failed."
71     }
72
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     rm -rf "csit_parent" || die "Remove operation failed."
101     mkdir -p "csit_parent" || die "Directory creation failed."
102     for filename in "output.xml" "log.html" "report.html"; do
103         mv "${ARCHIVE_DIR}/${filename}" "csit_parent/${filename}" || {
104             die "Attempt to move '${filename}' failed."
105         }
106     done
107     parse_bmrr_results "csit_parent" || {
108         die "The function should have died on error."
109     }
110
111     # Reusing CSIT main virtualenv.
112     pip install -r "${PYTHON_SCRIPTS_DIR}/perpatch_requirements.txt" || {
113         die "Perpatch Python requirements installation failed."
114     }
115     python "${PYTHON_SCRIPTS_DIR}/compare_perpatch.py"
116     # The exit code determines the vote result.
117 }
118
119
120 function download_builds () {
121
122     set -exuo pipefail
123
124     # This is mostly useful only for Sandbox testing, to avoid recompilation.
125     #
126     # Arguments:
127     # - ${1} - URL to download VPP builds from.
128     # Variables read:
129     # - VPP_DIR - Path to WORKSPACE, parent of created directories.
130     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
131     # Directories created:
132     # - archive - Ends up empty, not to be confused with ${ARCHIVE_DIR}.
133     # - build_new - Holding built artifacts of the patch under test (PUT).
134     # - built_parent - Holding built artifacts of parent of PUT.
135     # - csit_new - (Re)set to a symlink to archive robot results on failure.
136     # Functions called:
137     # - die - Print to stderr and exit, defined in common.sh
138
139     cd "${VPP_DIR}" || die "Change directory operation failed."
140     rm -rf "build-root" "build_parent" "build_new" "archive" "csit_new" || {
141         die "Directory removal failed."
142     }
143     wget -N --progress=dot:giga "${1}" || die "Wget download failed."
144     unzip "archive.zip" || die "Archive extraction failed."
145     mv "archive/build_parent" ./ || die "Move operation failed."
146     mv "archive/build_new" ./ || die "Move operation failed."
147     cp -r "build_new"/*".deb" "${DOWNLOAD_DIR}" || {
148         die "Copy operation failed."
149     }
150     # Create symlinks so that if job fails on robot, results can be archived.
151     ln -s "${ARCHIVE_DIR}" "csit_new" || die "Symbolic link creation failed."
152 }
153
154
155 function parse_bmrr_results () {
156
157     set -exuo pipefail
158
159     # Currently "parsing" is just two greps.
160     # TODO: Re-use PAL parsing code, make parsing more general and centralized.
161     #
162     # Arguments:
163     # - ${1} - Path to (existing) directory holding robot output.xml result.
164     # Files read:
165     # - output.xml - From argument location.
166     # Files updated:
167     # - results.txt - (Re)created, in argument location.
168     # Functions called:
169     # - die - Print to stderr and exit, defined in common.sh
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
175     # TODO: Do we need to check echo exit code explicitly?
176     echo "Parsing ${in_file} putting results into ${out_file}"
177     echo "TODO: Re-use parts of PAL when they support subsample test parsing."
178
179     pattern='Maximum Receive Rate trial results in packets'
180     pattern+=' per second: .*\]</status>'
181     grep -o "${pattern}" "${in_file}" | grep -o '\[.*\]' > "${out_file}" || {
182         die "Some parsing grep command has failed."
183     }
184 }
185
186
187 function prepare_build_parent () {
188
189     set -exuo pipefail
190
191     # Variables read:
192     # - VPP_DIR - Path to existing directory, parent to accessed directories.
193     # Directories read:
194     # - build-root - Existing directory with built VPP artifacts (also DPDK).
195     # Directories updated:
196     # - ${VPP_DIR} - A local git repository, parent commit gets checked out.
197     # - build_new - Old contents removed, content of build-root copied here.
198     # Functions called:
199     # - die - Print to stderr and exit, defined in common.sh
200
201     cd "${VPP_DIR}" || die "Change directory operation failed."
202     rm -rf "build_new" || die "Remove operation failed."
203     mkdir -p "build_new" || die "Directory creation failed."
204     mv "build-root"/*".deb" "build_new"/ || die "Move operation failed."
205     # The previous build could have left some incompatible leftovers,
206     # e.g. DPDK artifacts of different version.
207     # "make -C dpdk clean" does not actually remove such .deb file.
208     # Also, there usually is a copy of dpdk artifact in build-root.
209     git clean -dffx "dpdk"/ "build-root"/ || die "Git clean operation failed."
210     # Finally, check out the parent commit.
211     git checkout HEAD~ || die "Git checkout operation failed."
212     # Display any other leftovers.
213     git status || die "Git status operation failed."
214 }
215
216
217 function prepare_test_new () {
218
219     set -exuo pipefail
220
221     # Variables read:
222     # - VPP_DIR - Path to existing directory, parent of accessed directories.
223     # - DOWNLOAD_DIR - Path to directory where Robot takes builds to test from.
224     # - ARCHIVE_DIR - Path to where robot result files are created in.
225     # Directories read:
226     # - build-root - Existing directory with built VPP artifacts (also DPDK).
227     # Directories updated:
228     # - build_parent - Old directory removed, build-root moved to become this.
229     # - ${DOWNLOAD_DIR} - Old content removed, files from build_new copied here.
230     # - csit_new - Currently a symlink to to archive robot results on failure.
231     # Functions called:
232     # - die - Print to stderr and exit, defined in common.sh
233
234     cd "${VPP_DIR}" || die "Change directory operationf failed."
235     rm -rf "build_parent" "csit_new" "${DOWNLOAD_DIR}"/* || die "Remove failed."
236     mkdir -p "build_parent" || die "Directory creation operation failed."
237     mv "build-root"/*".deb" "build_parent"/ || die "Move operation failed."
238     cp "build_new"/*".deb" "${DOWNLOAD_DIR}" || die "Copy operation failed."
239     # Create symlinks so that if job fails on robot, results can be archived.
240     ln -s "${ARCHIVE_DIR}" "csit_new" || die "Symbolic link creation failed."
241 }
242
243
244 function prepare_test_parent () {
245
246     set -exuo pipefail
247
248     # Variables read:
249     # - VPP_DIR - Path to existing directory, parent of accessed directories.
250     # - CSIT_DIR - Path to existing root of local CSIT git repository.
251     # - ARCHIVE_DIR and DOWNLOAD_DIR - Paths to directories to update.
252     # Directories read:
253     # - build_parent - Build artifacts (to test next) are copied from here.
254     # Directories updated:
255     # - csit_new - Deleted, then recreated and latest robot results copied here.
256     # - ${CSIT_DIR} - Subjected to git reset and git clean.
257     # - ${ARCHIVE_DIR} - Created if not existing (if deleted by git clean).
258     # - ${DOWNLOAD_DIR} - Created after git clean, parent build copied here.
259     # - csit_parent - Currently a symlink to csit/ to archive robot results.
260     # Functions called:
261     # - die - Print to stderr and exit, defined in common.sh
262     # - parse_bmrr_results - See definition in this file.
263
264     cd "${VPP_DIR}" || die "Change directory operation failed."
265     rm -rf "csit_new" "csit_parent" || die "Remove operation failed."
266     mkdir -p "csit_new" || die "Create directory operation failed."
267     for filename in "output.xml" "log.html" "report.html"; do
268         mv "${ARCHIVE_DIR}/${filename}" "csit_new/${filename}" || {
269             die "Move operation of '${filename}' failed."
270         }
271     done
272     parse_bmrr_results "csit_new" || {
273         die "The function should have died on error."
274     }
275
276     pushd "${CSIT_DIR}" || die "Change directory operation failed."
277     git reset --hard HEAD || die "Git reset operation failed."
278     git clean -dffx || die "Git clean operation failed."
279     popd || die "Change directory operation failed."
280     mkdir -p "${ARCHIVE_DIR}" "${DOWNLOAD_DIR}" || die "Dir creation failed."
281
282     cp "build_parent"/*".deb" "${DOWNLOAD_DIR}"/ || die "Copy failed."
283     # Create symlinks so that if job fails on robot, results can be archived.
284     ln -s "${ARCHIVE_DIR}" "csit_parent" || die "Symlink creation failed."
285 }
286
287
288 function set_perpatch_dut () {
289
290     set -exuo pipefail
291
292     # Variables set:
293     # - DUT - CSIT test/ subdirectory containing suites to execute.
294
295     # TODO: Detect DUT from job name, when we have more than just VPP perpatch.
296
297     DUT="vpp"
298 }
299
300
301 function set_perpatch_vpp_dir () {
302
303     set -exuo pipefail
304
305     # Variables read:
306     # - CSIT_DIR - Path to existing root of local CSIT git repository.
307     # Variables set:
308     # - VPP_DIR - Path to existing root of local VPP git repository.
309     # Functions called:
310     # - die - Print to stderr and exit, defined in common.sh
311
312     # In perpatch, CSIT is cloned inside VPP clone.
313     VPP_DIR="$(readlink -e "${CSIT_DIR}/..")" || die "Readlink failed."
314 }