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