Add tox.ini and few checker scripts
[csit.git] / resources / libraries / bash / function / common.sh
1 # Copyright (c) 2019 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 by multiple entry scripts.
17 # Keep functions ordered alphabetically, please.
18
19 # TODO: Add a link to bash style guide.
20 # TODO: Consider putting every die into a {} block,
21 #   the code might become more readable (but longer).
22
23
24 function activate_docker_topology () {
25     # Create virtual vpp-device topology. Output of the function is topology
26     # file describing created environment saved to a file.
27     #
28     # Variables read:
29     # - BASH_FUNCTION_DIR - Path to existing directory this file is located in.
30     # - TOPOLOGIES - Available topologies.
31     # - NODENESS - Node multiplicity of desired testbed.
32     # - FLAVOR - Node flavor string, usually describing the processor.
33     # Variables set:
34     # - WORKING_TOPOLOGY - Path to topology file.
35
36     set -exuo pipefail
37
38     source "${BASH_FUNCTION_DIR}/device.sh" || {
39         die "Source failed!"
40     }
41
42     device_image="$(< ${CSIT_DIR}/VPP_DEVICE_IMAGE)"
43     case_text="${NODENESS}_${FLAVOR}"
44     case "${case_text}" in
45         "1n_skx")
46             # We execute reservation over csit-shim-dcr (ssh) which runs sourced
47             # script's functions. Env variables are read from ssh output
48             # back to localhost for further processing.
49             hostname=$(grep search /etc/resolv.conf | cut -d' ' -f3)
50             ssh="ssh root@${hostname} -p 6022"
51             run="activate_wrapper ${NODENESS} ${FLAVOR} ${device_image}"
52             env_vars=$(${ssh} "$(declare -f); ${run}") || {
53                 die "Topology reservation via shim-dcr failed!"
54             }
55             set -a
56             source <(echo "$env_vars" | grep -v /usr/bin/docker) || {
57                 die "Source failed!"
58             }
59             set +a
60             ;;
61         "1n_vbox")
62             # We execute reservation on localhost. Sourced script automatially
63             # sets environment variables for further processing.
64             activate_wrapper "${NODENESS}" "${FLAVOR}" "${device_image}" || die
65             ;;
66         *)
67             die "Unknown specification: ${case_text}!"
68     esac
69
70     trap 'deactivate_docker_topology' EXIT || {
71          die "Trap attempt failed, please cleanup manually. Aborting!"
72     }
73
74     # Replace all variables in template with those in environment.
75     source <(echo 'cat <<EOF >topo.yml'; cat ${TOPOLOGIES[0]}; echo EOF;) || {
76         die "Topology file create failed!"
77     }
78
79     WORKING_TOPOLOGY="/tmp/topology.yaml"
80     mv topo.yml "${WORKING_TOPOLOGY}" || {
81         die "Topology move failed!"
82     }
83     cat ${WORKING_TOPOLOGY} | grep -v password || {
84         die "Topology read failed!"
85     }
86 }
87
88
89 function activate_virtualenv () {
90
91     set -exuo pipefail
92
93     # Update virtualenv pip package, delete and create virtualenv directory,
94     # activate the virtualenv, install requirements, set PYTHONPATH.
95
96     # Arguments:
97     # - ${1} - Path to existing directory for creating virtualenv in.
98     #          If missing or empty, ${CSIT_DIR} is used.
99     # - ${2} - Path to requirements file, ${CSIT_DIR}/requirements.txt if empty.
100     # Variables read:
101     # - CSIT_DIR - Path to existing root of local CSIT git repository.
102     # Variables exported:
103     # - PYTHONPATH - CSIT_DIR, as CSIT Python scripts usually need this.
104     # Functions called:
105     # - die - Print to stderr and exit.
106
107     # TODO: Do we want the callers to be able to set the env dir name?
108     # TODO: + In that case, do we want to support env switching?
109     # TODO:   + In that case we want to make env_dir global.
110     # TODO: Do we want the callers to override PYTHONPATH loaction?
111
112     root_path="${1-$CSIT_DIR}"
113     env_dir="${root_path}/env"
114     req_path=${2-$CSIT_DIR/requirements.txt}
115     rm -rf "${env_dir}" || die "Failed to clean previous virtualenv."
116     pip install --upgrade virtualenv || {
117         die "Virtualenv package install failed."
118     }
119     virtualenv "${env_dir}" || {
120         die "Virtualenv creation failed."
121     }
122     set +u
123     source "${env_dir}/bin/activate" || die "Virtualenv activation failed."
124     set -u
125     pip install --upgrade -r "${req_path}" || {
126         die "Requirements installation failed."
127     }
128     # Most CSIT Python scripts assume PYTHONPATH is set and exported.
129     export PYTHONPATH="${root_path}" || die "Export failed."
130 }
131
132
133 function check_download_dir () {
134
135     set -exuo pipefail
136
137     # Fail if there are no files visible in ${DOWNLOAD_DIR}.
138     # TODO: Do we need this as a function, if it is (almost) a one-liner?
139     #
140     # Variables read:
141     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
142     # Directories read:
143     # - ${DOWNLOAD_DIR} - Has to be non-empty to proceed.
144     # Functions called:
145     # - die - Print to stderr and exit.
146
147     if [[ ! "$(ls -A "${DOWNLOAD_DIR}")" ]]; then
148         die "No artifacts downloaded!"
149     fi
150 }
151
152
153 function cleanup_topo () {
154
155     set -exuo pipefail
156
157     # Variables read:
158     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
159     # - PYTHON_SCRIPTS_DIR - Path to directory holding the reservation script.
160
161     python "${PYTHON_SCRIPTS_DIR}/topo_cleanup.py" -t "${WORKING_TOPOLOGY}"
162     # Not using "|| die" as some callers might want to ignore errors,
163     # e.g. in teardowns, such as unreserve.
164 }
165
166
167 function common_dirs () {
168
169     set -exuo pipefail
170
171     # Variables set:
172     # - BASH_FUNCTION_DIR - Path to existing directory this file is located in.
173     # - CSIT_DIR - Path to existing root of local CSIT git repository.
174     # - TOPOLOGIES_DIR - Path to existing directory with available tpologies.
175     # - RESOURCES_DIR - Path to existing CSIT subdirectory "resources".
176     # - TOOLS_DIR - Path to existing resources subdirectory "tools".
177     # - PYTHON_SCRIPTS_DIR - Path to existing tools subdirectory "scripts".
178     # - ARCHIVE_DIR - Path to created CSIT subdirectory "archive".
179     # - DOWNLOAD_DIR - Path to created CSIT subdirectory "download_dir".
180     # Functions called:
181     # - die - Print to stderr and exit.
182
183     BASH_FUNCTION_DIR="$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" || {
184         die "Some error during localizing this source directory."
185     }
186     # Current working directory could be in a different repo, e.g. VPP.
187     pushd "${BASH_FUNCTION_DIR}" || die "Pushd failed"
188     CSIT_DIR="$(readlink -e "$(git rev-parse --show-toplevel)")" || {
189         die "Readlink or git rev-parse failed."
190     }
191     popd || die "Popd failed."
192     TOPOLOGIES_DIR="$(readlink -e "${CSIT_DIR}/topologies/available")" || {
193         die "Readlink failed."
194     }
195     RESOURCES_DIR="$(readlink -e "${CSIT_DIR}/resources")" || {
196         die "Readlink failed."
197     }
198     TOOLS_DIR="$(readlink -e "${RESOURCES_DIR}/tools")" || {
199         die "Readlink failed."
200     }
201     PYTHON_SCRIPTS_DIR="$(readlink -e "${TOOLS_DIR}/scripts")" || {
202         die "Readlink failed."
203     }
204
205     ARCHIVE_DIR="$(readlink -f "${CSIT_DIR}/archive")" || {
206         die "Readlink failed."
207     }
208     mkdir -p "${ARCHIVE_DIR}" || die "Mkdir failed."
209     DOWNLOAD_DIR="$(readlink -f "${CSIT_DIR}/download_dir")" || {
210         die "Readlink failed."
211     }
212     mkdir -p "${DOWNLOAD_DIR}" || die "Mkdir failed."
213 }
214
215
216 function compose_pybot_arguments () {
217
218     set -exuo pipefail
219
220     # Variables read:
221     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
222     # - DUT - CSIT test/ subdirectory, set while processing tags.
223     # - TAGS - Array variable holding selected tag boolean expressions.
224     # - TOPOLOGIES_TAGS - Tag boolean expression filtering tests for topology.
225     # - TEST_CODE - The test selection string from environment or argument.
226     # Variables set:
227     # - PYBOT_ARGS - String holding part of all arguments for pybot.
228     # - EXPANDED_TAGS - Array of strings pybot arguments compiled from tags.
229
230     # No explicit check needed with "set -u".
231     PYBOT_ARGS=("--loglevel" "TRACE")
232     PYBOT_ARGS+=("--variable" "TOPOLOGY_PATH:${WORKING_TOPOLOGY}")
233
234     case "${TEST_CODE}" in
235         *"device"*)
236             PYBOT_ARGS+=("--suite" "tests.${DUT}.device")
237             ;;
238         *"func"*)
239             PYBOT_ARGS+=("--suite" "tests.${DUT}.func")
240             ;;
241         *"perf"*)
242             PYBOT_ARGS+=("--suite" "tests.${DUT}.perf")
243             ;;
244         *)
245             die "Unknown specification: ${TEST_CODE}"
246     esac
247
248     EXPANDED_TAGS=()
249     for tag in "${TAGS[@]}"; do
250         if [[ ${tag} == "!"* ]]; then
251             EXPANDED_TAGS+=("--exclude" "${tag#$"!"}")
252         else
253             EXPANDED_TAGS+=("--include" "${TOPOLOGIES_TAGS}AND${tag}")
254         fi
255     done
256 }
257
258
259 function copy_archives () {
260
261     set -exuo pipefail
262
263     # Variables read:
264     # - WORKSPACE - Jenkins workspace, copy only if the value is not empty.
265     #   Can be unset, then it speeds up manual testing.
266     # - ARCHIVE_DIR - Path to directory with content to be copied.
267     # Directories updated:
268     # - ${WORKSPACE}/archives/ - Created if does not exist.
269     #   Content of ${ARCHIVE_DIR}/ is copied here.
270     # Functions called:
271     # - die - Print to stderr and exit.
272
273     # We will create additional archive if workspace variable is set.
274     # This way if script is running in jenkins all will be
275     # automatically archived to logs.fd.io.
276     if [[ -n "${WORKSPACE-}" ]]; then
277         mkdir -p "${WORKSPACE}/archives/" || die "Archives dir create failed."
278         cp -rf "${ARCHIVE_DIR}"/* "${WORKSPACE}/archives" || die "Copy failed."
279     fi
280 }
281
282
283 function deactivate_docker_topology () {
284     # Deactivate virtual vpp-device topology by removing containers.
285     #
286     # Variables read:
287     # - NODENESS - Node multiplicity of desired testbed.
288     # - FLAVOR - Node flavor string, usually describing the processor.
289
290     set -exuo pipefail
291
292     case_text="${NODENESS}_${FLAVOR}"
293     case "${case_text}" in
294         "1n_skx")
295             hostname=$(grep search /etc/resolv.conf | cut -d' ' -f3)
296             ssh="ssh root@${hostname} -p 6022"
297             env_vars="$(env | grep CSIT_ | tr '\n' ' ' )"
298             ${ssh} "$(declare -f); deactivate_wrapper ${env_vars}" || {
299                 die "Topology cleanup via shim-dcr failed!"
300             }
301             ;;
302         "1n_vbox")
303             enter_mutex || die
304             clean_environment || {
305                 die "Topology cleanup locally failed!"
306             }
307             exit_mutex || die
308             ;;
309         *)
310             die "Unknown specification: ${case_text}!"
311     esac
312 }
313
314
315 function die () {
316     # Print the message to standard error end exit with error code specified
317     # by the second argument.
318     #
319     # Hardcoded values:
320     # - The default error message.
321     # Arguments:
322     # - ${1} - The whole error message, be sure to quote. Optional
323     # - ${2} - the code to exit with, default: 1.
324
325     set -x
326     set +eu
327     warn "${1:-Unspecified run-time error occurred!}"
328     exit "${2:-1}"
329 }
330
331
332 function die_on_pybot_error () {
333
334     set -exuo pipefail
335
336     # Source this fragment if you want to abort on any failed test case.
337     #
338     # Variables read:
339     # - PYBOT_EXIT_STATUS - Set by a pybot running fragment.
340     # Functions called:
341     # - die - Print to stderr and exit.
342
343     if [[ "${PYBOT_EXIT_STATUS}" != "0" ]]; then
344         die "${PYBOT_EXIT_STATUS}" "Test failures are present!"
345     fi
346 }
347
348
349 function get_test_code () {
350
351     set -exuo pipefail
352
353     # Arguments:
354     # - ${1} - Optional, argument of entry script (or empty as unset).
355     #   Test code value to override job name from environment.
356     # Variables read:
357     # - JOB_NAME - String affecting test selection, default if not argument.
358     # Variables set:
359     # - TEST_CODE - The test selection string from environment or argument.
360     # - NODENESS - Node multiplicity of desired testbed.
361     # - FLAVOR - Node flavor string, usually describing the processor.
362
363     TEST_CODE="${1-}" || die "Reading optional argument failed, somehow."
364     if [[ -z "${TEST_CODE}" ]]; then
365         TEST_CODE="${JOB_NAME-}" || die "Reading job name failed, somehow."
366     fi
367
368     case "${TEST_CODE}" in
369         *"1n-vbox"*)
370             NODENESS="1n"
371             FLAVOR="vbox"
372             ;;
373         *"1n-skx"*)
374             NODENESS="1n"
375             FLAVOR="skx"
376             ;;
377         *"2n-skx"*)
378             NODENESS="2n"
379             FLAVOR="skx"
380             ;;
381         *"3n-skx"*)
382             NODENESS="3n"
383             FLAVOR="skx"
384             ;;
385         *)
386             # Fallback to 3-node Haswell by default (backward compatibility)
387             NODENESS="3n"
388             FLAVOR="hsw"
389             ;;
390     esac
391 }
392
393
394 function get_test_tag_string () {
395
396     set -exuo pipefail
397
398     # Variables read:
399     # - GERRIT_EVENT_TYPE - Event type set by gerrit, can be unset.
400     # - GERRIT_EVENT_COMMENT_TEXT - Comment text, read for "comment-added" type.
401     # - TEST_CODE - The test selection string from environment or argument.
402     # Variables set:
403     # - TEST_TAG_STRING - The string following "perftest" in gerrit comment,
404     #   or empty.
405
406     # TODO: ci-management scripts no longer need to perform this.
407
408     trigger=""
409     if [[ "${GERRIT_EVENT_TYPE-}" == "comment-added" ]]; then
410         case "${TEST_CODE}" in
411             *"device"*)
412                 # On parsing error, ${trigger} stays empty.
413                 trigger="$(echo "${GERRIT_EVENT_COMMENT_TEXT}" \
414                     | grep -oE '(devicetest$|devicetest[[:space:]].+$)')" \
415                     || true
416                 # Set test tags as string.
417                 TEST_TAG_STRING="${trigger#$"devicetest"}"
418                 ;;
419             *"perf"*)
420                 # On parsing error, ${trigger} stays empty.
421                 trigger="$(echo "${GERRIT_EVENT_COMMENT_TEXT}" \
422                     | grep -oE '(perftest$|perftest[[:space:]].+$)')" \
423                     || true
424                 # Set test tags as string.
425                 TEST_TAG_STRING="${trigger#$"perftest"}"
426                 ;;
427             *)
428                 die "Unknown specification: ${TEST_CODE}"
429         esac
430     fi
431 }
432
433
434 function reserve_testbed () {
435
436     set -exuo pipefail
437
438     # Reserve physical testbed, perform cleanup, register trap to unreserve.
439     #
440     # Variables read:
441     # - TOPOLOGIES - Array of paths to topology yaml to attempt reservation on.
442     # - PYTHON_SCRIPTS_DIR - Path to directory holding the reservation script.
443     # Variables set:
444     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
445     # Functions called:
446     # - die - Print to stderr and exit.
447     # Traps registered:
448     # - EXIT - Calls cancel_all for ${WORKING_TOPOLOGY}.
449
450     while true; do
451         for topo in "${TOPOLOGIES[@]}"; do
452             set +e
453             python "${PYTHON_SCRIPTS_DIR}/topo_reservation.py" -t "${topo}"
454             result="$?"
455             set -e
456             if [[ "${result}" == "0" ]]; then
457                 WORKING_TOPOLOGY="${topo}"
458                 echo "Reserved: ${WORKING_TOPOLOGY}"
459                 trap "untrap_and_unreserve_testbed" EXIT || {
460                     message="TRAP ATTEMPT AND UNRESERVE FAILED, FIX MANUALLY."
461                     untrap_and_unreserve_testbed "${message}" || {
462                         die "Teardown should have died, not failed."
463                     }
464                     die "Trap attempt failed, unreserve succeeded. Aborting."
465                 }
466                 cleanup_topo || {
467                     die "Testbed cleanup failed."
468                 }
469                 break
470             fi
471         done
472
473         if [[ -n "${WORKING_TOPOLOGY-}" ]]; then
474             # Exit the infinite while loop if we made a reservation.
475             break
476         fi
477
478         # Wait ~3minutes before next try.
479         sleep_time="$[ ( $RANDOM % 20 ) + 180 ]s" || {
480             die "Sleep time calculation failed."
481         }
482         echo "Sleeping ${sleep_time}"
483         sleep "${sleep_time}" || die "Sleep failed."
484     done
485 }
486
487
488 function run_pybot () {
489
490     set -exuo pipefail
491
492     # Currently, VPP-1361 causes occasional test failures.
493     # If real result is more important than time, we can retry few times.
494     # TODO: We should be retrying on test case level instead.
495
496     # Arguments:
497     # - ${1} - Optional number of pybot invocations to try to avoid failures.
498     #   Default: 1.
499     # Variables read:
500     # - CSIT_DIR - Path to existing root of local CSIT git repository.
501     # - ARCHIVE_DIR - Path to store robot result files in.
502     # - PYBOT_ARGS, EXPANDED_TAGS - See compose_pybot_arguments.sh
503     # Variables set:
504     # - PYBOT_EXIT_STATUS - Exit status of most recent pybot invocation.
505     # Functions called:
506     # - die - Print to stderr and exit.
507
508     # Set ${tries} as an integer variable, to fail on non-numeric input.
509     local -i "tries" || die "Setting type of variable failed."
510     tries="${1:-1}" || die "Argument evaluation failed."
511     all_options=("--outputdir" "${ARCHIVE_DIR}" "${PYBOT_ARGS[@]}")
512     all_options+=("${EXPANDED_TAGS[@]}")
513
514     while true; do
515         if [[ "${tries}" -le 0 ]]; then
516             break
517         else
518             tries="$((${tries} - 1))"
519         fi
520         pushd "${CSIT_DIR}" || die "Change directory operation failed."
521         set +e
522         # TODO: Make robot tests not require "$(pwd)" == "${CSIT_DIR}".
523         pybot "${all_options[@]}" "${CSIT_DIR}/tests/"
524         PYBOT_EXIT_STATUS="$?"
525         set -e
526         popd || die "Change directory operation failed."
527         if [[ "${PYBOT_EXIT_STATUS}" == "0" ]]; then
528             break
529         fi
530     done
531 }
532
533
534 function select_tags () {
535
536     set -exuo pipefail
537
538     # Variables read:
539     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
540     # - TEST_CODE - String affecting test selection, usually jenkins job name.
541     # - TEST_TAG_STRING - String selecting tags, from gerrit comment.
542     #   Can be unset.
543     # - TOPOLOGIES_DIR - Path to existing directory with available tpologies.
544     # Variables set:
545     # - TAGS - Array of processed tag boolean expressions.
546
547     # NIC SELECTION
548     # All topologies NICs
549     available=$(grep -hoPR "model: \K.*" "${TOPOLOGIES_DIR}"/* | sort -u)
550     # Selected topology NICs
551     reserved=$(grep -hoPR "model: \K.*" "${WORKING_TOPOLOGY}" | sort -u)
552     # All topologies NICs - Selected topology NICs
553     exclude_nics=($(comm -13 <(echo "${reserved}") <(echo "${available}")))
554
555     case "${TEST_CODE}" in
556         # Select specific performance tests based on jenkins job type variable.
557         *"ndrpdr-weekly"* )
558             test_tag_array=("ndrpdrAND64bAND1c"
559                             "ndrpdrAND78bAND1c")
560             ;;
561         *"mrr-daily"* )
562             test_tag_array=(# vic
563                             "mrrANDnic_cisco-vic-1227AND64b"
564                             "mrrANDnic_cisco-vic-1385AND64b"
565                             # memif
566                             "mrrANDmemifANDethAND64b"
567                             "mrrANDmemifANDethANDimix"
568                             # crypto
569                             "mrrANDipsecAND64b"
570                             # ip4 base
571                             "mrrANDip4baseAND64b"
572                             # ip4 scale FIB 2M
573                             "mrrANDip4fwdANDfib_2mAND64b"
574                             # ip4 scale FIB 200k
575                             "mrrANDip4fwdANDfib_200kANDnic_intel-*710AND64b"
576                             # ip4 scale FIB 20k
577                             "mrrANDip4fwdANDfib_20kANDnic_intel-*710AND64b"
578                             # ip4 scale ACL
579                             "mrrANDip4fwdANDacl1AND10k_flowsAND64b"
580                             "mrrANDip4fwdANDacl50AND10k_flowsAND64b"
581                             # ip4 scale NAT44
582                             "mrrANDip4fwdANDnat44ANDbaseAND64b"
583                             "mrrANDip4fwdANDnat44ANDsrc_user_4000AND64b"
584                             # ip4 features
585                             "mrrANDip4fwdANDfeatureANDnic_intel-*710AND64b"
586                             # TODO: Remove when tags in
587                             # tests/vpp/perf/ip4/*-ipolicemarkbase-*.robot
588                             # are fixed
589                             "mrrANDip4fwdANDpolice_markANDnic_intel-*710AND64b"
590                             # ip4 tunnels
591                             "mrrANDip4fwdANDencapANDip6unrlayANDip4ovrlayANDnic_intel-x520-da2AND64b"
592                             "mrrANDip4fwdANDencapANDnic_intel-*710AND64b"
593                             "mrrANDl2ovrlayANDencapANDnic_intel-*710AND64b"
594                             # ip6 base
595                             "mrrANDip6baseANDethAND78b"
596                             # ip6 features
597                             "mrrANDip6fwdANDfeatureANDnic_intel-*710AND78b"
598                             # ip6 scale FIB 2M
599                             "mrrANDip6fwdANDfib_2mANDnic_intel-*710AND78b"
600                             # ip6 scale FIB 200k
601                             "mrrANDip6fwdANDfib_200kANDnic_intel-*710AND78b"
602                             # ip6 scale FIB 20k
603                             "mrrANDip6fwdANDfib_20kANDnic_intel-*710AND78b"
604                             # ip6 tunnels
605                             "mrrANDip6fwdANDencapANDnic_intel-x520-da2AND78b"
606                             # l2xc base
607                             "mrrANDl2xcfwdANDbaseAND64b"
608                             # l2xc scale ACL
609                             "mrrANDl2xcANDacl1AND10k_flowsAND64b"
610                             "mrrANDl2xcANDacl50AND10k_flowsAND64b"
611                             # l2xc scale FIB 2M
612                             "mrrANDl2xcANDfib_2mAND64b"
613                             # l2xc scale FIB 200k
614                             "mrrANDl2xcANDfib_200kANDnic_intel-*710AND64b"
615                             # l2xc scale FIB 20k
616                             "mrrANDl2xcANDfib_20kANDnic_intel-*710AND64b"
617                             # l2bd base
618                             "mrrANDl2bdmaclrnANDbaseAND64b"
619                             # l2bd scale ACL
620                             "mrrANDl2bdmaclrnANDacl1AND10k_flowsAND64b"
621                             "mrrANDl2bdmaclrnANDacl50AND10k_flowsAND64b"
622                             # l2bd scale FIB 2M
623                             "mrrANDl2bdmaclrnANDfib_1mAND64b"
624                             # l2bd scale FIB 200k
625                             "mrrANDl2bdmaclrnANDfib_100kANDnic_intel-*710AND64b"
626                             # l2bd scale FIB 20k
627                             "mrrANDl2bdmaclrnANDfib_10kANDnic_intel-*710AND64b"
628                             # l2 patch base
629                             "mrrANDl2patchAND64b"
630                             # srv6
631                             "mrrANDsrv6ANDnic_intel-x520-da2AND78b"
632                             # vts
633                             "mrrANDvtsANDnic_intel-x520-da2AND114b"
634                             # vm vhost l2xc base
635                             "mrrANDvhostANDl2xcfwdANDbaseAND64b"
636                             "mrrANDvhostANDl2xcfwdANDbaseANDimix"
637                             # vm vhost l2bd base
638                             "mrrANDvhostANDl2bdmaclrnANDbaseAND64b"
639                             "mrrANDvhostANDl2bdmaclrnANDbaseANDimix"
640                             # vm vhost ip4 base
641                             "mrrANDvhostANDip4fwdANDbaseAND64b"
642                             "mrrANDvhostANDip4fwdANDbaseANDimix"
643                             # DPDK
644                             "mrrANDdpdkAND64b"
645                             # Exclude
646                             "!mrrANDip6baseANDdot1qAND78b"
647                             "!vhost_256ANDnic_intel-x520-da2"
648                             "!vhostANDnic_intel-xl710"
649                             "!cfs_opt"
650                             "!lbond_dpdk"
651                             "!nf_density")
652             ;;
653         *"mrr-weekly"* )
654             test_tag_array=(# NF Density tests
655                             "mrrANDnf_densityAND64b"
656                             "mrrANDnf_densityANDimix"
657                             # DPDK
658                             "mrrANDdpdkAND64b")
659             ;;
660         * )
661             if [[ -z "${TEST_TAG_STRING-}" ]]; then
662                 # If nothing is specified, we will run pre-selected tests by
663                 # following tags.
664                 test_tag_array=("mrrANDnic_intel-x710AND1cAND64bANDip4base"
665                                 "mrrANDnic_intel-x710AND1cAND78bANDip6base"
666                                 "mrrANDnic_intel-x710AND1cAND64bANDl2bdbase"
667                                 "mrrANDnic_intel-x710AND1cAND64bANDl2xcbase"
668                                 "!dot1q")
669             else
670                 # If trigger contains tags, split them into array.
671                 test_tag_array=(${TEST_TAG_STRING//:/ })
672             fi
673             ;;
674     esac
675
676     # Blacklisting certain tags per topology.
677     case "${TEST_CODE}" in
678         *"3n-hsw"*)
679             test_tag_array+=("!drv_avf")
680             ;;
681         *"2n-skx"*)
682             test_tag_array+=("!ipsechw")
683             ;;
684         *"3n-skx"*)
685             test_tag_array+=("!ipsechw")
686             ;;
687         *)
688             # Default to 3n-hsw due to compatibility.
689             test_tag_array+=("!drv_avf")
690             ;;
691     esac
692
693     # We will add excluded NICs.
694     test_tag_array+=("${exclude_nics[@]/#/!NIC_}")
695
696     TAGS=()
697
698     # We will prefix with perftest to prevent running other tests
699     # (e.g. Functional).
700     prefix="perftestAND"
701     if [[ "${TEST_CODE}" == "vpp-"* ]]; then
702         # Automatic prefixing for VPP jobs to limit the NIC used and
703         # traffic evaluation to MRR.
704         prefix="${prefix}mrrANDnic_intel-x710AND"
705     fi
706     for tag in "${test_tag_array[@]}"; do
707         if [[ ${tag} == "!"* ]]; then
708             # Exclude tags are not prefixed.
709             TAGS+=("${tag}")
710         else
711             TAGS+=("${prefix}${tag}")
712         fi
713     done
714 }
715
716
717 function select_vpp_device_tags () {
718
719     set -exuo pipefail
720
721     # Variables read:
722     # - TEST_CODE - String affecting test selection, usually jenkins job name.
723     # - TEST_TAG_STRING - String selecting tags, from gerrit comment.
724     #   Can be unset.
725     # Variables set:
726     # - TAGS - Array of processed tag boolean expressions.
727
728     case "${TEST_CODE}" in
729         # Select specific performance tests based on jenkins job type variable.
730         * )
731             if [[ -z "${TEST_TAG_STRING-}" ]]; then
732                 # If nothing is specified, we will run pre-selected tests by
733                 # following tags. Items of array will be concatenated by OR
734                 # in Robot Framework.
735                 test_tag_array=()
736             else
737                 # If trigger contains tags, split them into array.
738                 test_tag_array=(${TEST_TAG_STRING//:/ })
739             fi
740             ;;
741     esac
742
743     TAGS=()
744
745     # We will prefix with perftest to prevent running other tests
746     # (e.g. Functional).
747     prefix="devicetestAND"
748     if [[ "${TEST_CODE}" == "vpp-"* ]]; then
749         # Automatic prefixing for VPP jobs to limit testing.
750         prefix="${prefix}"
751     fi
752     for tag in "${test_tag_array[@]}"; do
753         if [[ ${tag} == "!"* ]]; then
754             # Exclude tags are not prefixed.
755             TAGS+=("${tag}")
756         else
757             TAGS+=("${prefix}${tag}")
758         fi
759     done
760 }
761
762
763 function select_topology () {
764
765     set -exuo pipefail
766
767     # Variables read:
768     # - NODENESS - Node multiplicity of testbed, either "2n" or "3n".
769     # - FLAVOR - Node flavor string, currently either "hsw" or "skx".
770     # - CSIT_DIR - Path to existing root of local CSIT git repository.
771     # - TOPOLOGIES_DIR - Path to existing directory with available topologies.
772     # Variables set:
773     # - TOPOLOGIES - Array of paths to suitable topology yaml files.
774     # - TOPOLOGIES_TAGS - Tag expression selecting tests for the topology.
775     # Functions called:
776     # - die - Print to stderr and exit.
777
778     case_text="${NODENESS}_${FLAVOR}"
779     case "${case_text}" in
780         "1n_vbox")
781             TOPOLOGIES=(
782                         "${TOPOLOGIES_DIR}/vpp_device.template"
783                        )
784             TOPOLOGIES_TAGS="2_node_single_link_topo"
785             ;;
786         "1n_skx")
787             TOPOLOGIES=(
788                         "${TOPOLOGIES_DIR}/vpp_device.template"
789                        )
790             TOPOLOGIES_TAGS="2_node_single_link_topo"
791             ;;
792         "2n_skx")
793             TOPOLOGIES=(
794                         "${TOPOLOGIES_DIR}/lf_2n_skx_testbed21.yaml"
795                         #"${TOPOLOGIES_DIR}/lf_2n_skx_testbed22.yaml"
796                         "${TOPOLOGIES_DIR}/lf_2n_skx_testbed23.yaml"
797                         "${TOPOLOGIES_DIR}/lf_2n_skx_testbed24.yaml"
798                        )
799             TOPOLOGIES_TAGS="2_node_*_link_topo"
800             ;;
801         "3n_skx")
802             TOPOLOGIES=(
803                         "${TOPOLOGIES_DIR}/lf_3n_skx_testbed31.yaml"
804                         "${TOPOLOGIES_DIR}/lf_3n_skx_testbed32.yaml"
805                        )
806             TOPOLOGIES_TAGS="3_node_*_link_topo"
807             ;;
808         "3n_hsw")
809             TOPOLOGIES=(
810                         "${TOPOLOGIES_DIR}/lf_3n_hsw_testbed1.yaml"
811                         "${TOPOLOGIES_DIR}/lf_3n_hsw_testbed2.yaml"
812                         "${TOPOLOGIES_DIR}/lf_3n_hsw_testbed3.yaml"
813                        )
814             TOPOLOGIES_TAGS="3_node_single_link_topo"
815             ;;
816         *)
817             # No falling back to 3n_hsw default, that should have been done
818             # by the function which has set NODENESS and FLAVOR.
819             die "Unknown specification: ${case_text}"
820     esac
821
822     if [[ -z "${TOPOLOGIES-}" ]]; then
823         die "No applicable topology found!"
824     fi
825 }
826
827
828 function untrap_and_unreserve_testbed () {
829     # Use this as a trap function to ensure testbed does not remain reserved.
830     # Perhaps call directly before script exit, to free testbed for other jobs.
831     # This function is smart enough to avoid multiple unreservations (so safe).
832     # Topo cleanup is executed (call it best practice), ignoring failures.
833     #
834     # Hardcoded values:
835     # - default message to die with if testbed might remain reserved.
836     # Arguments:
837     # - ${1} - Message to die with if unreservation fails. Default hardcoded.
838     # Variables read (by inner function):
839     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
840     # - PYTHON_SCRIPTS_DIR - Path to directory holding Python scripts.
841     # Variables written:
842     # - WORKING_TOPOLOGY - Set to empty string on successful unreservation.
843     # Trap unregistered:
844     # - EXIT - Failure to untrap is reported, but ignored otherwise.
845     # Functions called:
846     # - die - Print to stderr and exit.
847
848     set -xo pipefail
849     set +eu  # We do not want to exit early in a "teardown" function.
850     trap - EXIT || echo "Trap deactivation failed, continuing anyway."
851     wt="${WORKING_TOPOLOGY}"  # Just to avoid too long lines.
852     if [[ -z "${wt-}" ]]; then
853         set -eu
854         warn "Testbed looks unreserved already. Trap removal failed before?"
855     else
856         cleanup_topo || true
857         python "${PYTHON_SCRIPTS_DIR}/topo_reservation.py" -c -t "${wt}" || {
858             die "${1:-FAILED TO UNRESERVE, FIX MANUALLY.}" 2
859         }
860         WORKING_TOPOLOGY=""
861         set -eu
862     fi
863 }
864
865
866 function warn () {
867     # Print the message to standard error.
868     #
869     # Arguments:
870     # - ${@} - The text of the message.
871
872     echo "$@" >&2
873 }