refactor(terraform): 2n_aws_c5n
[csit.git] / resources / libraries / bash / function / common.sh
1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Copyright (c) 2022 PANTHEON.tech and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -exuo pipefail
16
17 # This library defines functions used by multiple entry scripts.
18 # Keep functions ordered alphabetically, please.
19
20 # TODO: Add a link to bash style guide.
21 # TODO: Consider putting every die into a {} block,
22 #   the code might become more readable (but longer).
23
24
25 function activate_docker_topology () {
26
27     # Create virtual vpp-device topology. Output of the function is topology
28     # file describing created environment saved to a file.
29     #
30     # Variables read:
31     # - BASH_FUNCTION_DIR - Path to existing directory this file is located in.
32     # - TOPOLOGIES - Available topologies.
33     # - NODENESS - Node multiplicity of desired testbed.
34     # - FLAVOR - Node flavor string, usually describing the processor.
35     # - IMAGE_VER_FILE - Name of file that contains the image version.
36     # - CSIT_DIR - Directory where ${IMAGE_VER_FILE} is located.
37     # Variables set:
38     # - WORKING_TOPOLOGY - Path to topology file.
39
40     set -exuo pipefail
41
42     source "${BASH_FUNCTION_DIR}/device.sh" || {
43         die "Source failed!"
44     }
45     device_image="$(< ${CSIT_DIR}/${IMAGE_VER_FILE})"
46     case_text="${NODENESS}_${FLAVOR}"
47     case "${case_text}" in
48         "1n_skx" | "1n_tx2")
49             # We execute reservation over csit-shim-dcr (ssh) which runs sourced
50             # script's functions. Env variables are read from ssh output
51             # back to localhost for further processing.
52             # Shim and Jenkins executor are in the same network on the same host
53             # Connect to docker's default gateway IP and shim's exposed port
54             ssh="ssh root@172.17.0.1 -p 6022"
55             run="activate_wrapper ${NODENESS} ${FLAVOR} ${device_image}"
56             # The "declare -f" output is long and boring.
57             set +x
58             # backtics to avoid https://midnight-commander.org/ticket/2142
59             env_vars=`${ssh} "$(declare -f); ${run}"` || {
60                 die "Topology reservation via shim-dcr failed!"
61             }
62             set -x
63             set -a
64             source <(echo "$env_vars" | grep -v /usr/bin/docker) || {
65                 die "Source failed!"
66             }
67             set +a
68             ;;
69         "1n_vbox")
70             # We execute reservation on localhost. Sourced script automatially
71             # sets environment variables for further processing.
72             activate_wrapper "${NODENESS}" "${FLAVOR}" "${device_image}" || die
73             ;;
74         *)
75             die "Unknown specification: ${case_text}!"
76     esac
77
78     trap 'deactivate_docker_topology' EXIT || {
79          die "Trap attempt failed, please cleanup manually. Aborting!"
80     }
81
82     parse_env_variables || die "Parse of environment variables failed!"
83
84     # Replace all variables in template with those in environment.
85     source <(echo 'cat <<EOF >topo.yml'; cat ${TOPOLOGIES[0]}; echo EOF;) || {
86         die "Topology file create failed!"
87     }
88
89     WORKING_TOPOLOGY="${CSIT_DIR}/topologies/available/vpp_device.yaml"
90     mv topo.yml "${WORKING_TOPOLOGY}" || {
91         die "Topology move failed!"
92     }
93     cat ${WORKING_TOPOLOGY} | grep -v password || {
94         die "Topology read failed!"
95     }
96 }
97
98
99 function activate_virtualenv () {
100
101     # Update virtualenv pip package, delete and create virtualenv directory,
102     # activate the virtualenv, install requirements, set PYTHONPATH.
103
104     # Arguments:
105     # - ${1} - Path to existing directory for creating virtualenv in.
106     #          If missing or empty, ${CSIT_DIR} is used.
107     # - ${2} - Path to requirements file, ${CSIT_DIR}/requirements.txt if empty.
108     # Variables read:
109     # - CSIT_DIR - Path to existing root of local CSIT git repository.
110     # Variables exported:
111     # - PYTHONPATH - CSIT_DIR, as CSIT Python scripts usually need this.
112     # Functions called:
113     # - die - Print to stderr and exit.
114
115     set -exuo pipefail
116
117     root_path="${1-$CSIT_DIR}"
118     env_dir="${root_path}/env"
119     req_path=${2-$CSIT_DIR/requirements.txt}
120     rm -rf "${env_dir}" || die "Failed to clean previous virtualenv."
121     pip3 install virtualenv==20.0.20 || {
122         die "Virtualenv package install failed."
123     }
124     virtualenv --no-download --python=$(which python3) "${env_dir}" || {
125         die "Virtualenv creation for $(which python3) failed."
126     }
127     set +u
128     source "${env_dir}/bin/activate" || die "Virtualenv activation failed."
129     set -u
130     pip3 install -r "${req_path}" || {
131         die "Requirements installation failed."
132     }
133     # Most CSIT Python scripts assume PYTHONPATH is set and exported.
134     export PYTHONPATH="${CSIT_DIR}" || die "Export failed."
135 }
136
137
138 function archive_tests () {
139
140     # Create .tar.gz of generated/tests for archiving.
141     # To be run after generate_tests, kept separate to offer more flexibility.
142
143     # Directory read:
144     # - ${GENERATED_DIR}/tests - Tree of executed suites to archive.
145     # File rewriten:
146     # - ${ARCHIVE_DIR}/generated_tests.tar.gz - Archive of generated tests.
147
148     set -exuo pipefail
149
150     pushd "${ARCHIVE_DIR}" || die
151     tar czf "generated_tests.tar.gz" "${GENERATED_DIR}/tests" || true
152     popd || die
153 }
154
155
156 function check_download_dir () {
157
158     # Fail if there are no files visible in ${DOWNLOAD_DIR}.
159     #
160     # Variables read:
161     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
162     # Directories read:
163     # - ${DOWNLOAD_DIR} - Has to be non-empty to proceed.
164     # Functions called:
165     # - die - Print to stderr and exit.
166
167     set -exuo pipefail
168
169     if [[ ! "$(ls -A "${DOWNLOAD_DIR}")" ]]; then
170         die "No artifacts downloaded!"
171     fi
172 }
173
174
175 function check_prerequisites () {
176
177     # Fail if prerequisites are not met.
178     #
179     # Functions called:
180     # - installed - Check if application is installed/present in system.
181     # - die - Print to stderr and exit.
182
183     set -exuo pipefail
184
185     if ! installed sshpass; then
186         die "Please install sshpass before continue!"
187     fi
188 }
189
190
191 function common_dirs () {
192
193     # Set global variables, create some directories (without touching content).
194
195     # Variables set:
196     # - BASH_FUNCTION_DIR - Path to existing directory this file is located in.
197     # - CSIT_DIR - Path to existing root of local CSIT git repository.
198     # - TOPOLOGIES_DIR - Path to existing directory with available topologies.
199     # - JOB_SPECS_DIR - Path to existing directory with job test specifications.
200     # - RESOURCES_DIR - Path to existing CSIT subdirectory "resources".
201     # - TOOLS_DIR - Path to existing resources subdirectory "tools".
202     # - PYTHON_SCRIPTS_DIR - Path to existing tools subdirectory "scripts".
203     # - ARCHIVE_DIR - Path to created CSIT subdirectory "archives".
204     #   The name is chosen to match what ci-management expects.
205     # - DOWNLOAD_DIR - Path to created CSIT subdirectory "download_dir".
206     # - GENERATED_DIR - Path to created CSIT subdirectory "generated".
207     # Directories created if not present:
208     # ARCHIVE_DIR, DOWNLOAD_DIR, GENERATED_DIR.
209     # Functions called:
210     # - die - Print to stderr and exit.
211
212     set -exuo pipefail
213
214     this_file=$(readlink -e "${BASH_SOURCE[0]}") || {
215         die "Some error during locating of this source file."
216     }
217     BASH_FUNCTION_DIR=$(dirname "${this_file}") || {
218         die "Some error during dirname call."
219     }
220     # Current working directory could be in a different repo, e.g. VPP.
221     pushd "${BASH_FUNCTION_DIR}" || die "Pushd failed"
222     relative_csit_dir=$(git rev-parse --show-toplevel) || {
223         die "Git rev-parse failed."
224     }
225     CSIT_DIR=$(readlink -e "${relative_csit_dir}") || die "Readlink failed."
226     popd || die "Popd failed."
227     TOPOLOGIES_DIR=$(readlink -e "${CSIT_DIR}/topologies/available") || {
228         die "Readlink failed."
229     }
230     JOB_SPECS_DIR=$(readlink -e "${CSIT_DIR}/docs/job_specs") || {
231         die "Readlink failed."
232     }
233     RESOURCES_DIR=$(readlink -e "${CSIT_DIR}/resources") || {
234         die "Readlink failed."
235     }
236     TOOLS_DIR=$(readlink -e "${RESOURCES_DIR}/tools") || {
237         die "Readlink failed."
238     }
239     DOC_GEN_DIR=$(readlink -e "${TOOLS_DIR}/doc_gen") || {
240         die "Readlink failed."
241     }
242     PYTHON_SCRIPTS_DIR=$(readlink -e "${TOOLS_DIR}/scripts") || {
243         die "Readlink failed."
244     }
245
246     ARCHIVE_DIR=$(readlink -f "${CSIT_DIR}/archives") || {
247         die "Readlink failed."
248     }
249     mkdir -p "${ARCHIVE_DIR}" || die "Mkdir failed."
250     DOWNLOAD_DIR=$(readlink -f "${CSIT_DIR}/download_dir") || {
251         die "Readlink failed."
252     }
253     mkdir -p "${DOWNLOAD_DIR}" || die "Mkdir failed."
254     GENERATED_DIR=$(readlink -f "${CSIT_DIR}/generated") || {
255         die "Readlink failed."
256     }
257     mkdir -p "${GENERATED_DIR}" || die "Mkdir failed."
258 }
259
260
261 function compose_pybot_arguments () {
262
263     # Variables read:
264     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
265     # - DUT - CSIT test/ subdirectory, set while processing tags.
266     # - TAGS - Array variable holding selected tag boolean expressions.
267     # - TOPOLOGIES_TAGS - Tag boolean expression filtering tests for topology.
268     # - TEST_CODE - The test selection string from environment or argument.
269     # - SELECTION_MODE - Selection criteria [test, suite, include, exclude].
270     # Variables set:
271     # - PYBOT_ARGS - String holding part of all arguments for pybot.
272     # - EXPANDED_TAGS - Array of strings pybot arguments compiled from tags.
273
274     set -exuo pipefail
275
276     # No explicit check needed with "set -u".
277     PYBOT_ARGS=("--loglevel" "TRACE")
278     PYBOT_ARGS+=("--variable" "TOPOLOGY_PATH:${WORKING_TOPOLOGY}")
279
280     case "${TEST_CODE}" in
281         *"device"*)
282             PYBOT_ARGS+=("--suite" "tests.${DUT}.device")
283             ;;
284         *"perf"*)
285             PYBOT_ARGS+=("--suite" "tests.${DUT}.perf")
286             ;;
287         *)
288             die "Unknown specification: ${TEST_CODE}"
289     esac
290
291     EXPANDED_TAGS=()
292     for tag in "${TAGS[@]}"; do
293         if [[ ${tag} == "!"* ]]; then
294             EXPANDED_TAGS+=("--exclude" "${tag#$"!"}")
295         else
296             if [[ ${SELECTION_MODE} == "--test" ]]; then
297                 EXPANDED_TAGS+=("--test" "${tag}")
298             else
299                 EXPANDED_TAGS+=("--include" "${TOPOLOGIES_TAGS}AND${tag}")
300             fi
301         fi
302     done
303
304     if [[ ${SELECTION_MODE} == "--test" ]]; then
305         EXPANDED_TAGS+=("--include" "${TOPOLOGIES_TAGS}")
306     fi
307 }
308
309
310 function deactivate_docker_topology () {
311
312     # Deactivate virtual vpp-device topology by removing containers.
313     #
314     # Variables read:
315     # - NODENESS - Node multiplicity of desired testbed.
316     # - FLAVOR - Node flavor string, usually describing the processor.
317
318     set -exuo pipefail
319
320     case_text="${NODENESS}_${FLAVOR}"
321     case "${case_text}" in
322         "1n_skx" | "1n_tx2")
323             ssh="ssh root@172.17.0.1 -p 6022"
324             env_vars=$(env | grep CSIT_ | tr '\n' ' ' ) || die
325             # The "declare -f" output is long and boring.
326             set +x
327             ${ssh} "$(declare -f); deactivate_wrapper ${env_vars}" || {
328                 die "Topology cleanup via shim-dcr failed!"
329             }
330             set -x
331             ;;
332         "1n_vbox")
333             enter_mutex || die
334             clean_environment || {
335                 die "Topology cleanup locally failed!"
336             }
337             exit_mutex || die
338             ;;
339         *)
340             die "Unknown specification: ${case_text}!"
341     esac
342 }
343
344
345 function die () {
346
347     # Print the message to standard error end exit with error code specified
348     # by the second argument.
349     #
350     # Hardcoded values:
351     # - The default error message.
352     # Arguments:
353     # - ${1} - The whole error message, be sure to quote. Optional
354     # - ${2} - the code to exit with, default: 1.
355
356     set -x
357     set +eu
358     warn "${1:-Unspecified run-time error occurred!}"
359     exit "${2:-1}"
360 }
361
362
363 function die_on_pybot_error () {
364
365     # Source this fragment if you want to abort on any failed test case.
366     #
367     # Variables read:
368     # - PYBOT_EXIT_STATUS - Set by a pybot running fragment.
369     # Functions called:
370     # - die - Print to stderr and exit.
371
372     set -exuo pipefail
373
374     if [[ "${PYBOT_EXIT_STATUS}" != "0" ]]; then
375         die "Test failures are present!" "${PYBOT_EXIT_STATUS}"
376     fi
377 }
378
379
380 function generate_tests () {
381
382     # Populate ${GENERATED_DIR}/tests based on ${CSIT_DIR}/tests/.
383     # Any previously existing content of ${GENERATED_DIR}/tests is wiped before.
384     # The generation is done by executing any *.py executable
385     # within any subdirectory after copying.
386
387     # This is a separate function, because this code is called
388     # both by autogen checker and entries calling run_pybot.
389
390     # Directories read:
391     # - ${CSIT_DIR}/tests - Used as templates for the generated tests.
392     # Directories replaced:
393     # - ${GENERATED_DIR}/tests - Overwritten by the generated tests.
394
395     set -exuo pipefail
396
397     rm -rf "${GENERATED_DIR}/tests" || die
398     cp -r "${CSIT_DIR}/tests" "${GENERATED_DIR}/tests" || die
399     cmd_line=("find" "${GENERATED_DIR}/tests" "-type" "f")
400     cmd_line+=("-executable" "-name" "*.py")
401     # We sort the directories, so log output can be compared between runs.
402     file_list=$("${cmd_line[@]}" | sort) || die
403
404     for gen in ${file_list}; do
405         directory="$(dirname "${gen}")" || die
406         filename="$(basename "${gen}")" || die
407         pushd "${directory}" || die
408         ./"${filename}" || die
409         popd || die
410     done
411 }
412
413
414 function get_test_code () {
415
416     # Arguments:
417     # - ${1} - Optional, argument of entry script (or empty as unset).
418     #   Test code value to override job name from environment.
419     # Variables read:
420     # - JOB_NAME - String affecting test selection, default if not argument.
421     # Variables set:
422     # - TEST_CODE - The test selection string from environment or argument.
423     # - NODENESS - Node multiplicity of desired testbed.
424     # - FLAVOR - Node flavor string, usually describing the processor.
425
426     set -exuo pipefail
427
428     TEST_CODE="${1-}" || die "Reading optional argument failed, somehow."
429     if [[ -z "${TEST_CODE}" ]]; then
430         TEST_CODE="${JOB_NAME-}" || die "Reading job name failed, somehow."
431     fi
432
433     case "${TEST_CODE}" in
434         *"1n-vbox"*)
435             NODENESS="1n"
436             FLAVOR="vbox"
437             ;;
438         *"1n-skx"*)
439             NODENESS="1n"
440             FLAVOR="skx"
441             ;;
442        *"1n-tx2"*)
443             NODENESS="1n"
444             FLAVOR="tx2"
445             ;;
446         *"2n-aws"*)
447             NODENESS="2n"
448             FLAVOR="aws"
449             ;;
450         *"3n-aws"*)
451             NODENESS="3n"
452             FLAVOR="aws"
453             ;;
454         *"2n-skx"*)
455             NODENESS="2n"
456             FLAVOR="skx"
457             ;;
458         *"3n-skx"*)
459             NODENESS="3n"
460             FLAVOR="skx"
461             ;;
462         *"2n-zn2"*)
463             NODENESS="2n"
464             FLAVOR="zn2"
465             ;;
466         *"2n-clx"*)
467             NODENESS="2n"
468             FLAVOR="clx"
469             ;;
470         *"2n-icx"*)
471             NODENESS="2n"
472             FLAVOR="icx"
473             ;;
474         *"3n-icx"*)
475             NODENESS="3n"
476             FLAVOR="icx"
477             ;;
478         *"2n-dnv"*)
479             NODENESS="2n"
480             FLAVOR="dnv"
481             ;;
482         *"3n-dnv"*)
483             NODENESS="3n"
484             FLAVOR="dnv"
485             ;;
486         *"2n-tx2"*)
487             NODENESS="2n"
488             FLAVOR="tx2"
489             ;;
490         *"3n-tsh"*)
491             NODENESS="3n"
492             FLAVOR="tsh"
493             ;;
494     esac
495 }
496
497
498 function get_test_tag_string () {
499
500     # Variables read:
501     # - GERRIT_EVENT_TYPE - Event type set by gerrit, can be unset.
502     # - GERRIT_EVENT_COMMENT_TEXT - Comment text, read for "comment-added" type.
503     # - TEST_CODE - The test selection string from environment or argument.
504     # Variables set:
505     # - TEST_TAG_STRING - The string following trigger word in gerrit comment.
506     #   May be empty, or even not set on event types not adding comment.
507
508     # TODO: ci-management scripts no longer need to perform this.
509
510     set -exuo pipefail
511
512     if [[ "${GERRIT_EVENT_TYPE-}" == "comment-added" ]]; then
513         case "${TEST_CODE}" in
514             *"device"*)
515                 trigger="devicetest"
516                 ;;
517             *"perf"*)
518                 trigger="perftest"
519                 ;;
520             *)
521                 die "Unknown specification: ${TEST_CODE}"
522         esac
523         # Ignore lines not containing the trigger word.
524         comment=$(fgrep "${trigger}" <<< "${GERRIT_EVENT_COMMENT_TEXT}" || true)
525         # The vpp-csit triggers trail stuff we are not interested in.
526         # Removing them and trigger word: https://unix.stackexchange.com/a/13472
527         # (except relying on \s whitespace, \S non-whitespace and . both).
528         # The last string is concatenated, only the middle part is expanded.
529         cmd=("grep" "-oP" '\S*'"${trigger}"'\S*\s\K.+$') || die "Unset trigger?"
530         # On parsing error, TEST_TAG_STRING probably stays empty.
531         TEST_TAG_STRING=$("${cmd[@]}" <<< "${comment}" || true)
532         if [[ -z "${TEST_TAG_STRING-}" ]]; then
533             # Probably we got a base64 encoded comment.
534             comment="${GERRIT_EVENT_COMMENT_TEXT}"
535             comment=$(base64 --decode <<< "${comment}" || true)
536             comment=$(fgrep "${trigger}" <<< "${comment}" || true)
537             TEST_TAG_STRING=$("${cmd[@]}" <<< "${comment}" || true)
538         fi
539         if [[ -n "${TEST_TAG_STRING-}" ]]; then
540             test_tag_array=(${TEST_TAG_STRING})
541             if [[ "${test_tag_array[0]}" == "icl" ]]; then
542                 export GRAPH_NODE_VARIANT="icl"
543                 TEST_TAG_STRING="${test_tag_array[@]:1}" || true
544             elif [[ "${test_tag_array[0]}" == "skx" ]]; then
545                 export GRAPH_NODE_VARIANT="skx"
546                 TEST_TAG_STRING="${test_tag_array[@]:1}" || true
547             fi
548         fi
549     fi
550 }
551
552
553 function installed () {
554
555     # Check if the given utility is installed. Fail if not installed.
556     #
557     # Duplicate of common.sh function, as this file is also used standalone.
558     #
559     # Arguments:
560     # - ${1} - Utility to check.
561     # Returns:
562     # - 0 - If command is installed.
563     # - 1 - If command is not installed.
564
565     set -exuo pipefail
566
567     command -v "${1}"
568 }
569
570
571 function move_archives () {
572
573     # Move archive directory to top of workspace, if not already there.
574     #
575     # ARCHIVE_DIR is positioned relative to CSIT_DIR,
576     # but in some jobs CSIT_DIR is not same as WORKSPACE
577     # (e.g. under VPP_DIR). To simplify ci-management settings,
578     # we want to move the data to the top. We do not want simple copy,
579     # as ci-management is eager with recursive search.
580     #
581     # As some scripts may call this function multiple times,
582     # the actual implementation use copying and deletion,
583     # so the workspace gets "union" of contents (except overwrites on conflict).
584     # The consequence is empty ARCHIVE_DIR remaining after this call.
585     #
586     # As the source directory is emptied,
587     # the check for dirs being different is essential.
588     #
589     # Variables read:
590     # - WORKSPACE - Jenkins workspace, move only if the value is not empty.
591     #   Can be unset, then it speeds up manual testing.
592     # - ARCHIVE_DIR - Path to directory with content to be moved.
593     # Directories updated:
594     # - ${WORKSPACE}/archives/ - Created if does not exist.
595     #   Content of ${ARCHIVE_DIR}/ is moved.
596     # Functions called:
597     # - die - Print to stderr and exit.
598
599     set -exuo pipefail
600
601     if [[ -n "${WORKSPACE-}" ]]; then
602         target=$(readlink -f "${WORKSPACE}/archives")
603         if [[ "${target}" != "${ARCHIVE_DIR}" ]]; then
604             mkdir -p "${target}" || die "Archives dir create failed."
605             cp -rf "${ARCHIVE_DIR}"/* "${target}" || die "Copy failed."
606             rm -rf "${ARCHIVE_DIR}"/* || die "Delete failed."
607         fi
608     fi
609 }
610
611
612 function post_process_robot_outputs () {
613
614     # Generate INFO level output_info.xml by rebot.
615     # Archive UTI raw json outputs.
616     #
617     # Variables read:
618     # - ARCHIVE_DIR - Path to post-processed files.
619
620     set -exuo pipefail
621
622     # Compress raw json outputs, as they will never be post-processed.
623     pushd "${ARCHIVE_DIR}" || die
624     if [ -d "tests" ]; then
625         # Use deterministic order.
626         options+=("--sort=name")
627         # We are keeping info outputs where they are.
628         # Assuming we want to move anything but info files (and dirs).
629         options+=("--exclude=*.info.json")
630         tar czf "generated_output_raw.tar.gz" "${options[@]}" "tests" || true
631         # Tar can remove when archiving, but chokes (not deterministically)
632         # on attempting to remove dirs (not empty as info files are there).
633         # So we need to delete the raw files manually.
634         find "tests" -type f -name "*.raw.json" -delete || true
635     fi
636     popd || die
637
638     # Generate INFO level output_info.xml for post-processing.
639     all_options=("--loglevel" "INFO")
640     all_options+=("--log" "none")
641     all_options+=("--report" "none")
642     all_options+=("--output" "${ARCHIVE_DIR}/output_info.xml")
643     all_options+=("${ARCHIVE_DIR}/output.xml")
644     rebot "${all_options[@]}" || true
645 }
646
647
648 function prepare_topology () {
649
650     # Prepare virtual testbed topology if needed based on flavor.
651
652     # Variables read:
653     # - TEST_CODE - String affecting test selection, usually jenkins job name.
654     # - NODENESS - Node multiplicity of testbed, either "2n" or "3n".
655     # - FLAVOR - Node flavor string, e.g. "clx" or "skx".
656     # Functions called:
657     # - die - Print to stderr and exit.
658     # - terraform_init - Terraform init topology.
659     # - terraform_apply - Terraform apply topology.
660
661     set -exuo pipefail
662
663     case_text="${NODENESS}_${FLAVOR}"
664     case "${case_text}" in
665         "2n_aws")
666             export TF_VAR_testbed_name="${TEST_CODE}"
667             terraform_init || die "Failed to call terraform init."
668             terraform_apply || die "Failed to call terraform apply."
669             ;;
670         "3n_aws")
671             export TF_VAR_testbed_name="${TEST_CODE}"
672             terraform_init || die "Failed to call terraform init."
673             terraform_apply || die "Failed to call terraform apply."
674             ;;
675     esac
676 }
677
678
679 function reserve_and_cleanup_testbed () {
680
681     # Reserve physical testbed, perform cleanup, register trap to unreserve.
682     # When cleanup fails, remove from topologies and keep retrying
683     # until all topologies are removed.
684     #
685     # Variables read:
686     # - TOPOLOGIES - Array of paths to topology yaml to attempt reservation on.
687     # - PYTHON_SCRIPTS_DIR - Path to directory holding the reservation script.
688     # - BUILD_TAG - Any string suitable as filename, identifying
689     #   test run executing this function. May be unset.
690     # Variables set:
691     # - TOPOLOGIES - Array of paths to topologies, with failed cleanups removed.
692     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
693     # Functions called:
694     # - die - Print to stderr and exit.
695     # - ansible_playbook - Perform an action using ansible, see ansible.sh
696     # Traps registered:
697     # - EXIT - Calls cancel_all for ${WORKING_TOPOLOGY}.
698
699     set -exuo pipefail
700
701     while true; do
702         for topo in "${TOPOLOGIES[@]}"; do
703             set +e
704             scrpt="${PYTHON_SCRIPTS_DIR}/topo_reservation.py"
705             opts=("-t" "${topo}" "-r" "${BUILD_TAG:-Unknown}")
706             python3 "${scrpt}" "${opts[@]}"
707             result="$?"
708             set -e
709             if [[ "${result}" == "0" ]]; then
710                 # Trap unreservation before cleanup check,
711                 # so multiple jobs showing failed cleanup improve chances
712                 # of humans to notice and fix.
713                 WORKING_TOPOLOGY="${topo}"
714                 echo "Reserved: ${WORKING_TOPOLOGY}"
715                 trap "untrap_and_unreserve_testbed" EXIT || {
716                     message="TRAP ATTEMPT AND UNRESERVE FAILED, FIX MANUALLY."
717                     untrap_and_unreserve_testbed "${message}" || {
718                         die "Teardown should have died, not failed."
719                     }
720                     die "Trap attempt failed, unreserve succeeded. Aborting."
721                 }
722                 # Cleanup + calibration checks
723                 set +e
724                 ansible_playbook "cleanup, calibration"
725                 result="$?"
726                 set -e
727                 if [[ "${result}" == "0" ]]; then
728                     break
729                 fi
730                 warn "Testbed cleanup failed: ${topo}"
731                 untrap_and_unreserve_testbed "Fail of unreserve after cleanup."
732             fi
733             # Else testbed is accessible but currently reserved, moving on.
734         done
735
736         if [[ -n "${WORKING_TOPOLOGY-}" ]]; then
737             # Exit the infinite while loop if we made a reservation.
738             warn "Reservation and cleanup successful."
739             break
740         fi
741
742         if [[ "${#TOPOLOGIES[@]}" == "0" ]]; then
743             die "Run out of operational testbeds!"
744         fi
745
746         # Wait ~3minutes before next try.
747         sleep_time="$[ ( ${RANDOM} % 20 ) + 180 ]s" || {
748             die "Sleep time calculation failed."
749         }
750         echo "Sleeping ${sleep_time}"
751         sleep "${sleep_time}" || die "Sleep failed."
752     done
753 }
754
755
756 function run_pybot () {
757
758     # Run pybot with options based on input variables.
759     # Generate INFO level output_info.xml by rebot.
760     # Archive UTI raw json outputs.
761     #
762     # Variables read:
763     # - CSIT_DIR - Path to existing root of local CSIT git repository.
764     # - ARCHIVE_DIR - Path to store robot result files in.
765     # - PYBOT_ARGS, EXPANDED_TAGS - See compose_pybot_arguments.sh
766     # - GENERATED_DIR - Tests are assumed to be generated under there.
767     # Variables set:
768     # - PYBOT_EXIT_STATUS - Exit status of most recent pybot invocation.
769     # Functions called:
770     # - die - Print to stderr and exit.
771
772     set -exuo pipefail
773
774     all_options=("--outputdir" "${ARCHIVE_DIR}" "${PYBOT_ARGS[@]}")
775     all_options+=("--noncritical" "EXPECTED_FAILING")
776     all_options+=("${EXPANDED_TAGS[@]}")
777
778     pushd "${CSIT_DIR}" || die "Change directory operation failed."
779     set +e
780     robot "${all_options[@]}" "${GENERATED_DIR}/tests/"
781     PYBOT_EXIT_STATUS="$?"
782     set -e
783
784     post_process_robot_outputs || die
785
786     popd || die "Change directory operation failed."
787 }
788
789
790 function select_arch_os () {
791
792     # Set variables affected by local CPU architecture and operating system.
793     #
794     # Variables set:
795     # - VPP_VER_FILE - Name of file in CSIT dir containing vpp stable version.
796     # - IMAGE_VER_FILE - Name of file in CSIT dir containing the image name.
797     # - PKG_SUFFIX - Suffix of OS package file name, "rpm" or "deb."
798
799     set -exuo pipefail
800
801     source /etc/os-release || die "Get OS release failed."
802
803     case "${ID}" in
804         "ubuntu"*)
805             case "${VERSION}" in
806                 *"LTS (Focal Fossa)"*)
807                     IMAGE_VER_FILE="VPP_DEVICE_IMAGE_UBUNTU"
808                     VPP_VER_FILE="VPP_STABLE_VER_UBUNTU_FOCAL"
809                     PKG_SUFFIX="deb"
810                     ;;
811                 *)
812                     die "Unsupported Ubuntu version!"
813                     ;;
814             esac
815             ;;
816         *)
817             die "Unsupported distro or OS!"
818             ;;
819     esac
820
821     arch=$(uname -m) || {
822         die "Get CPU architecture failed."
823     }
824
825     case "${arch}" in
826         "aarch64")
827             IMAGE_VER_FILE="${IMAGE_VER_FILE}_ARM"
828             ;;
829         *)
830             ;;
831     esac
832 }
833
834
835 function select_tags () {
836
837     # Variables read:
838     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
839     # - TEST_CODE - String affecting test selection, usually jenkins job name.
840     # - DUT - CSIT test/ subdirectory, set while processing tags.
841     # - TEST_TAG_STRING - String selecting tags, from gerrit comment.
842     #   Can be unset.
843     # - TOPOLOGIES_DIR - Path to existing directory with available tpologies.
844     # - BASH_FUNCTION_DIR - Directory with input files to process.
845     # Variables set:
846     # - TAGS - Array of processed tag boolean expressions.
847     # - SELECTION_MODE - Selection criteria [test, suite, include, exclude].
848
849     set -exuo pipefail
850
851     # NIC SELECTION
852     start_pattern='^  TG:'
853     end_pattern='^ \? \?[A-Za-z0-9]\+:'
854     # Remove the TG section from topology file
855     sed_command="/${start_pattern}/,/${end_pattern}/d"
856     # All topologies DUT NICs
857     available=$(sed "${sed_command}" "${TOPOLOGIES_DIR}"/* \
858                 | grep -hoP "model: \K.*" | sort -u)
859     # Selected topology DUT NICs
860     reserved=$(sed "${sed_command}" "${WORKING_TOPOLOGY}" \
861                | grep -hoP "model: \K.*" | sort -u)
862     # All topologies DUT NICs - Selected topology DUT NICs
863     exclude_nics=($(comm -13 <(echo "${reserved}") <(echo "${available}"))) || {
864         die "Computation of excluded NICs failed."
865     }
866
867     # Select default NIC tag.
868     case "${TEST_CODE}" in
869         *"3n-dnv"* | *"2n-dnv"*)
870             default_nic="nic_intel-x553"
871             ;;
872         *"3n-tsh"*)
873             default_nic="nic_intel-x520-da2"
874             ;;
875         *"3n-icx"* | *"2n-icx"*)
876             default_nic="nic_intel-xxv710"
877             ;;
878         *"3n-skx"* | *"2n-skx"* | *"2n-clx"* | *"2n-zn2"*)
879             default_nic="nic_intel-xxv710"
880             ;;
881         *"2n-tx2"* | *"mrr-daily-master")
882             default_nic="nic_intel-xl710"
883             ;;
884         *"2n-aws"* | *"3n-aws"*)
885             default_nic="nic_amazon-nitro-50g"
886             ;;
887         *)
888             default_nic="nic_intel-x710"
889             ;;
890     esac
891
892     sed_nic_sub_cmd="sed s/\${default_nic}/${default_nic}/"
893     awk_nics_sub_cmd=""
894     awk_nics_sub_cmd+='gsub("xxv710","25ge2p1xxv710");'
895     awk_nics_sub_cmd+='gsub("x710","10ge2p1x710");'
896     awk_nics_sub_cmd+='gsub("xl710","40ge2p1xl710");'
897     awk_nics_sub_cmd+='gsub("x520-da2","10ge2p1x520");'
898     awk_nics_sub_cmd+='gsub("x553","10ge2p1x553");'
899     awk_nics_sub_cmd+='gsub("cx556a","100ge2p1cx556a");'
900     awk_nics_sub_cmd+='gsub("e810cq","100ge2p1e810cq");'
901     awk_nics_sub_cmd+='gsub("vic1227","10ge2p1vic1227");'
902     awk_nics_sub_cmd+='gsub("vic1385","40ge2p1vic1385");'
903     awk_nics_sub_cmd+='gsub("nitro-50g","50ge1p1ENA");'
904     awk_nics_sub_cmd+='if ($9 =="drv_avf") drv="avf-";'
905     awk_nics_sub_cmd+='else if ($9 =="drv_rdma_core") drv ="rdma-";'
906     awk_nics_sub_cmd+='else if ($9 =="drv_af_xdp") drv ="af-xdp-";'
907     awk_nics_sub_cmd+='else drv="";'
908     awk_nics_sub_cmd+='if ($1 =="-") cores="";'
909     awk_nics_sub_cmd+='else cores=$1;'
910     awk_nics_sub_cmd+='print "*"$7"-" drv $11"-"$5"."$3"-" cores "-" drv $11"-"$5'
911
912     # Tag file directory shorthand.
913     tfd="${JOB_SPECS_DIR}"
914     case "${TEST_CODE}" in
915         # Select specific performance tests based on jenkins job type variable.
916         *"device"* )
917             readarray -t test_tag_array <<< $(grep -v "#" \
918                 ${tfd}/vpp_device/${DUT}-${NODENESS}-${FLAVOR}.md |
919                 awk {"$awk_nics_sub_cmd"} || echo "devicetest") || die
920             SELECTION_MODE="--test"
921             ;;
922         *"ndrpdr-weekly"* )
923             readarray -t test_tag_array <<< $(grep -v "#" \
924                 ${tfd}/mlr_weekly/${DUT}-${NODENESS}-${FLAVOR}.md |
925                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
926             SELECTION_MODE="--test"
927             ;;
928         *"mrr-daily"* )
929             readarray -t test_tag_array <<< $(grep -v "#" \
930                 ${tfd}/mrr_daily/${DUT}-${NODENESS}-${FLAVOR}.md |
931                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
932             SELECTION_MODE="--test"
933             ;;
934         *"mrr-weekly"* )
935             readarray -t test_tag_array <<< $(grep -v "#" \
936                 ${tfd}/mrr_weekly/${DUT}-${NODENESS}-${FLAVOR}.md |
937                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
938             SELECTION_MODE="--test"
939             ;;
940         *"report-iterative"* )
941             test_sets=(${TEST_TAG_STRING//:/ })
942             # Run only one test set per run
943             report_file=${test_sets[0]}.md
944             readarray -t test_tag_array <<< $(grep -v "#" \
945                 ${tfd}/report_iterative/${NODENESS}-${FLAVOR}/${report_file} |
946                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
947             SELECTION_MODE="--test"
948             ;;
949         *"report-coverage"* )
950             test_sets=(${TEST_TAG_STRING//:/ })
951             # Run only one test set per run
952             report_file=${test_sets[0]}.md
953             readarray -t test_tag_array <<< $(grep -v "#" \
954                 ${tfd}/report_coverage/${NODENESS}-${FLAVOR}/${report_file} |
955                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
956             SELECTION_MODE="--test"
957             ;;
958         * )
959             if [[ -z "${TEST_TAG_STRING-}" ]]; then
960                 # If nothing is specified, we will run pre-selected tests by
961                 # following tags.
962                 test_tag_array=("mrrAND${default_nic}AND1cAND64bANDethip4-ip4base"
963                                 "mrrAND${default_nic}AND1cAND78bANDethip6-ip6base"
964                                 "mrrAND${default_nic}AND1cAND64bANDeth-l2bdbasemaclrn"
965                                 "mrrAND${default_nic}AND1cAND64bANDeth-l2xcbase"
966                                 "!drv_af_xdp" "!drv_avf")
967             else
968                 # If trigger contains tags, split them into array.
969                 test_tag_array=(${TEST_TAG_STRING//:/ })
970             fi
971             SELECTION_MODE="--include"
972             ;;
973     esac
974
975     # Blacklisting certain tags per topology.
976     #
977     # Reasons for blacklisting:
978     # - ipsechw - Blacklisted on testbeds without crypto hardware accelerator.
979     case "${TEST_CODE}" in
980         *"1n-vbox"*)
981             test_tag_array+=("!avf")
982             test_tag_array+=("!vhost")
983             test_tag_array+=("!flow")
984             ;;
985         *"1n_tx2"*)
986             test_tag_array+=("!flow")
987             ;;
988         *"2n-skx"*)
989             test_tag_array+=("!ipsechw")
990             ;;
991         *"3n-skx"*)
992             test_tag_array+=("!ipsechw")
993             # Not enough nic_intel-xxv710 to support double link tests.
994             test_tag_array+=("!3_node_double_link_topoANDnic_intel-xxv710")
995             ;;
996         *"2n-clx"*)
997             test_tag_array+=("!ipsechw")
998             ;;
999         *"2n-icx"*)
1000             test_tag_array+=("!ipsechw")
1001             ;;
1002         *"3n-icx"*)
1003             test_tag_array+=("!ipsechw")
1004             # Not enough nic_intel-xxv710 to support double link tests.
1005             test_tag_array+=("!3_node_double_link_topoANDnic_intel-xxv710")
1006             ;;
1007         *"2n-zn2"*)
1008             test_tag_array+=("!ipsechw")
1009             ;;
1010         *"2n-dnv"*)
1011             test_tag_array+=("!memif")
1012             test_tag_array+=("!srv6_proxy")
1013             test_tag_array+=("!vhost")
1014             test_tag_array+=("!vts")
1015             test_tag_array+=("!drv_avf")
1016             ;;
1017         *"2n-tx2"*)
1018             test_tag_array+=("!ipsechw")
1019             ;;
1020         *"3n-dnv"*)
1021             test_tag_array+=("!memif")
1022             test_tag_array+=("!srv6_proxy")
1023             test_tag_array+=("!vhost")
1024             test_tag_array+=("!vts")
1025             test_tag_array+=("!drv_avf")
1026             ;;
1027         *"3n-tsh"*)
1028             # 3n-tsh only has x520 NICs which don't work with AVF
1029             test_tag_array+=("!drv_avf")
1030             test_tag_array+=("!ipsechw")
1031             ;;
1032         *"2n-aws"* | *"3n-aws"*)
1033             test_tag_array+=("!ipsechw")
1034             ;;
1035     esac
1036
1037     # We will add excluded NICs.
1038     test_tag_array+=("${exclude_nics[@]/#/!NIC_}")
1039
1040     TAGS=()
1041     prefix=""
1042
1043     set +x
1044     if [[ "${TEST_CODE}" == "vpp-"* ]]; then
1045         if [[ "${TEST_CODE}" != *"device"* ]]; then
1046             # Automatic prefixing for VPP perf jobs to limit the NIC used and
1047             # traffic evaluation to MRR.
1048             if [[ "${TEST_TAG_STRING-}" == *"nic_"* ]]; then
1049                 prefix="${prefix}mrrAND"
1050             else
1051                 prefix="${prefix}mrrAND${default_nic}AND"
1052             fi
1053         fi
1054     fi
1055     for tag in "${test_tag_array[@]}"; do
1056         if [[ "${tag}" == "!"* ]]; then
1057             # Exclude tags are not prefixed.
1058             TAGS+=("${tag}")
1059         elif [[ "${tag}" == " "* || "${tag}" == *"perftest"* ]]; then
1060             # Badly formed tag expressions can trigger way too much tests.
1061             set -x
1062             warn "The following tag expression hints at bad trigger: ${tag}"
1063             warn "Possible cause: Multiple triggers in a single comment."
1064             die "Aborting to avoid triggering too many tests."
1065         elif [[ "${tag}" == *"OR"* ]]; then
1066             # If OR had higher precedence than AND, it would be useful here.
1067             # Some people think it does, thus triggering way too much tests.
1068             set -x
1069             warn "The following tag expression hints at bad trigger: ${tag}"
1070             warn "Operator OR has lower precedence than AND. Use space instead."
1071             die "Aborting to avoid triggering too many tests."
1072         elif [[ "${tag}" != "" && "${tag}" != "#"* ]]; then
1073             # Empty and comment lines are skipped.
1074             # Other lines are normal tags, they are to be prefixed.
1075             TAGS+=("${prefix}${tag}")
1076         fi
1077     done
1078     set -x
1079 }
1080
1081
1082 function select_topology () {
1083
1084     # Variables read:
1085     # - NODENESS - Node multiplicity of testbed, either "2n" or "3n".
1086     # - FLAVOR - Node flavor string, e.g. "clx" or "skx".
1087     # - CSIT_DIR - Path to existing root of local CSIT git repository.
1088     # - TOPOLOGIES_DIR - Path to existing directory with available topologies.
1089     # Variables set:
1090     # - TOPOLOGIES - Array of paths to suitable topology yaml files.
1091     # - TOPOLOGIES_TAGS - Tag expression selecting tests for the topology.
1092     # Functions called:
1093     # - die - Print to stderr and exit.
1094
1095     set -exuo pipefail
1096
1097     case_text="${NODENESS}_${FLAVOR}"
1098     case "${case_text}" in
1099         "1n_vbox")
1100             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*vpp_device*.template )
1101             TOPOLOGIES_TAGS="2_node_single_link_topo"
1102             ;;
1103         "1n_skx" | "1n_tx2")
1104             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*vpp_device*.template )
1105             TOPOLOGIES_TAGS="2_node_single_link_topo"
1106             ;;
1107         "2n_skx")
1108             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_skx*.yaml )
1109             TOPOLOGIES_TAGS="2_node_*_link_topo"
1110             ;;
1111         "2n_zn2")
1112             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_zn2*.yaml )
1113             TOPOLOGIES_TAGS="2_node_*_link_topo"
1114             ;;
1115         "3n_skx")
1116             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_skx*.yaml )
1117             TOPOLOGIES_TAGS="3_node_*_link_topo"
1118             ;;
1119         "3n_icx")
1120             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_icx*.yaml )
1121             TOPOLOGIES_TAGS="3_node_*_link_topo"
1122             ;;
1123         "2n_clx")
1124             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_clx*.yaml )
1125             TOPOLOGIES_TAGS="2_node_*_link_topo"
1126             ;;
1127         "2n_icx")
1128             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_icx*.yaml )
1129             TOPOLOGIES_TAGS="2_node_*_link_topo"
1130             ;;
1131         "2n_dnv")
1132             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_dnv*.yaml )
1133             TOPOLOGIES_TAGS="2_node_single_link_topo"
1134             ;;
1135         "3n_dnv")
1136             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_dnv*.yaml )
1137             TOPOLOGIES_TAGS="3_node_single_link_topo"
1138             ;;
1139         "3n_tsh")
1140             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_tsh*.yaml )
1141             TOPOLOGIES_TAGS="3_node_single_link_topo"
1142             ;;
1143         "2n_tx2")
1144             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_tx2*.yaml )
1145             TOPOLOGIES_TAGS="2_node_single_link_topo"
1146             ;;
1147         "2n_aws")
1148             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n-aws*.yaml )
1149             TOPOLOGIES_TAGS="2_node_single_link_topo"
1150             ;;
1151         "3n_aws")
1152             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n-aws*.yaml )
1153             TOPOLOGIES_TAGS="3_node_single_link_topo"
1154             ;;
1155         *)
1156             # No falling back to default, that should have been done
1157             # by the function which has set NODENESS and FLAVOR.
1158             die "Unknown specification: ${case_text}"
1159     esac
1160
1161     if [[ -z "${TOPOLOGIES-}" ]]; then
1162         die "No applicable topology found!"
1163     fi
1164 }
1165
1166
1167 function set_environment_variables () {
1168
1169     # Depending on testbed topology, overwrite defaults set in the
1170     # resources/libraries/python/Constants.py file
1171     #
1172     # Variables read:
1173     # - TEST_CODE - String affecting test selection, usually jenkins job name.
1174     # Variables set:
1175     # See specific cases
1176
1177     set -exuo pipefail
1178
1179     case "${TEST_CODE}" in
1180         *"2n-aws"* | *"3n-aws"*)
1181             # T-Rex 2.88 workaround for ENA NICs
1182             export TREX_RX_DESCRIPTORS_COUNT=1024
1183             export TREX_EXTRA_CMDLINE="--mbuf-factor 19"
1184             export TREX_CORE_COUNT=6
1185             # Settings to prevent duration stretching
1186             export PERF_TRIAL_STL_DELAY=0.1
1187             ;;
1188     esac
1189 }
1190
1191
1192 function untrap_and_unreserve_testbed () {
1193
1194     # Use this as a trap function to ensure testbed does not remain reserved.
1195     # Perhaps call directly before script exit, to free testbed for other jobs.
1196     # This function is smart enough to avoid multiple unreservations (so safe).
1197     # Topo cleanup is executed (call it best practice), ignoring failures.
1198     #
1199     # Hardcoded values:
1200     # - default message to die with if testbed might remain reserved.
1201     # Arguments:
1202     # - ${1} - Message to die with if unreservation fails. Default hardcoded.
1203     # Variables read (by inner function):
1204     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
1205     # - PYTHON_SCRIPTS_DIR - Path to directory holding Python scripts.
1206     # Variables written:
1207     # - WORKING_TOPOLOGY - Set to empty string on successful unreservation.
1208     # Trap unregistered:
1209     # - EXIT - Failure to untrap is reported, but ignored otherwise.
1210     # Functions called:
1211     # - die - Print to stderr and exit.
1212     # - ansible_playbook - Perform an action using ansible, see ansible.sh
1213
1214     set -xo pipefail
1215     set +eu  # We do not want to exit early in a "teardown" function.
1216     trap - EXIT || echo "Trap deactivation failed, continuing anyway."
1217     wt="${WORKING_TOPOLOGY}"  # Just to avoid too long lines.
1218     if [[ -z "${wt-}" ]]; then
1219         set -eu
1220         warn "Testbed looks unreserved already. Trap removal failed before?"
1221     else
1222         ansible_playbook "cleanup" || true
1223         python3 "${PYTHON_SCRIPTS_DIR}/topo_reservation.py" -c -t "${wt}" || {
1224             die "${1:-FAILED TO UNRESERVE, FIX MANUALLY.}" 2
1225         }
1226         case "${TEST_CODE}" in
1227             *"2n-aws"* | *"3n-aws"*)
1228                 terraform_destroy || die "Failed to call terraform destroy."
1229                 ;;
1230             *)
1231                 ;;
1232         esac
1233         WORKING_TOPOLOGY=""
1234         set -eu
1235     fi
1236 }
1237
1238
1239 function warn () {
1240
1241     # Print the message to standard error.
1242     #
1243     # Arguments:
1244     # - ${@} - The text of the message.
1245
1246     set -exuo pipefail
1247
1248     echo "$@" >&2
1249 }