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