feat(docs): Move job_specs to resources
[csit.git] / resources / libraries / bash / function / common.sh
1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Copyright (c) 2023 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.15.1 || {
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}/resources/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         *"1n-aws"*)
447             NODENESS="1n"
448             FLAVOR="aws"
449             ;;
450         *"2n-aws"*)
451             NODENESS="2n"
452             FLAVOR="aws"
453             ;;
454         *"3n-aws"*)
455             NODENESS="3n"
456             FLAVOR="aws"
457             ;;
458         *"2n-zn2"*)
459             NODENESS="2n"
460             FLAVOR="zn2"
461             ;;
462         *"2n-clx"*)
463             NODENESS="2n"
464             FLAVOR="clx"
465             ;;
466         *"2n-icx"*)
467             NODENESS="2n"
468             FLAVOR="icx"
469             ;;
470         *"2n-spr"*)
471             NODENESS="2n"
472             FLAVOR="spr"
473             ;;
474         *"3n-icx"*)
475             NODENESS="3n"
476             FLAVOR="icx"
477             ;;
478         *"3n-spr"*)
479             NODENESS="3n"
480             FLAVOR="spr"
481             ;;
482         *"3n-snr"*)
483             NODENESS="3n"
484             FLAVOR="snr"
485             ;;
486         *"2n-tx2"*)
487             NODENESS="2n"
488             FLAVOR="tx2"
489             ;;
490         *"3n-tsh"*)
491             NODENESS="3n"
492             FLAVOR="tsh"
493             ;;
494         *"3n-alt"*)
495             NODENESS="3n"
496             FLAVOR="alt"
497             ;;
498     esac
499 }
500
501
502 function get_test_tag_string () {
503
504     # Variables read:
505     # - GERRIT_EVENT_TYPE - Event type set by gerrit, can be unset.
506     # - GERRIT_EVENT_COMMENT_TEXT - Comment text, read for "comment-added" type.
507     # - TEST_CODE - The test selection string from environment or argument.
508     # Variables set:
509     # - TEST_TAG_STRING - The string following trigger word in gerrit comment.
510     #   May be empty, or even not set on event types not adding comment.
511
512     # TODO: ci-management scripts no longer need to perform this.
513
514     set -exuo pipefail
515
516     if [[ "${GERRIT_EVENT_TYPE-}" == "comment-added" ]]; then
517         case "${TEST_CODE}" in
518             *"device"*)
519                 trigger="devicetest"
520                 ;;
521             *"perf"*)
522                 trigger="perftest"
523                 ;;
524             *)
525                 die "Unknown specification: ${TEST_CODE}"
526         esac
527         # Ignore lines not containing the trigger word.
528         comment=$(fgrep "${trigger}" <<< "${GERRIT_EVENT_COMMENT_TEXT}" || true)
529         # The vpp-csit triggers trail stuff we are not interested in.
530         # Removing them and trigger word: https://unix.stackexchange.com/a/13472
531         # (except relying on \s whitespace, \S non-whitespace and . both).
532         # The last string is concatenated, only the middle part is expanded.
533         cmd=("grep" "-oP" '\S*'"${trigger}"'\S*\s\K.+$') || die "Unset trigger?"
534         # On parsing error, TEST_TAG_STRING probably stays empty.
535         TEST_TAG_STRING=$("${cmd[@]}" <<< "${comment}" || true)
536         if [[ -z "${TEST_TAG_STRING-}" ]]; then
537             # Probably we got a base64 encoded comment.
538             comment="${GERRIT_EVENT_COMMENT_TEXT}"
539             comment=$(base64 --decode <<< "${comment}" || true)
540             comment=$(fgrep "${trigger}" <<< "${comment}" || true)
541             TEST_TAG_STRING=$("${cmd[@]}" <<< "${comment}" || true)
542         fi
543         if [[ -n "${TEST_TAG_STRING-}" ]]; then
544             test_tag_array=(${TEST_TAG_STRING})
545             if [[ "${test_tag_array[0]}" == "icl" ]]; then
546                 export GRAPH_NODE_VARIANT="icl"
547                 TEST_TAG_STRING="${test_tag_array[@]:1}" || true
548             elif [[ "${test_tag_array[0]}" == "skx" ]]; then
549                 export GRAPH_NODE_VARIANT="skx"
550                 TEST_TAG_STRING="${test_tag_array[@]:1}" || true
551             fi
552         fi
553     fi
554 }
555
556
557 function installed () {
558
559     # Check if the given utility is installed. Fail if not installed.
560     #
561     # Duplicate of common.sh function, as this file is also used standalone.
562     #
563     # Arguments:
564     # - ${1} - Utility to check.
565     # Returns:
566     # - 0 - If command is installed.
567     # - 1 - If command is not installed.
568
569     set -exuo pipefail
570
571     command -v "${1}"
572 }
573
574
575 function move_archives () {
576
577     # Move archive directory to top of workspace, if not already there.
578     #
579     # ARCHIVE_DIR is positioned relative to CSIT_DIR,
580     # but in some jobs CSIT_DIR is not same as WORKSPACE
581     # (e.g. under VPP_DIR). To simplify ci-management settings,
582     # we want to move the data to the top. We do not want simple copy,
583     # as ci-management is eager with recursive search.
584     #
585     # As some scripts may call this function multiple times,
586     # the actual implementation use copying and deletion,
587     # so the workspace gets "union" of contents (except overwrites on conflict).
588     # The consequence is empty ARCHIVE_DIR remaining after this call.
589     #
590     # As the source directory is emptied,
591     # the check for dirs being different is essential.
592     #
593     # Variables read:
594     # - WORKSPACE - Jenkins workspace, move only if the value is not empty.
595     #   Can be unset, then it speeds up manual testing.
596     # - ARCHIVE_DIR - Path to directory with content to be moved.
597     # Directories updated:
598     # - ${WORKSPACE}/archives/ - Created if does not exist.
599     #   Content of ${ARCHIVE_DIR}/ is moved.
600     # Functions called:
601     # - die - Print to stderr and exit.
602
603     set -exuo pipefail
604
605     if [[ -n "${WORKSPACE-}" ]]; then
606         target=$(readlink -f "${WORKSPACE}/archives")
607         if [[ "${target}" != "${ARCHIVE_DIR}" ]]; then
608             mkdir -p "${target}" || die "Archives dir create failed."
609             cp -rf "${ARCHIVE_DIR}"/* "${target}" || die "Copy failed."
610             rm -rf "${ARCHIVE_DIR}"/* || die "Delete failed."
611         fi
612     fi
613 }
614
615
616 function post_process_robot_outputs () {
617
618     # Generate INFO level output_info.xml by rebot.
619     #
620     # Variables read:
621     # - ARCHIVE_DIR - Path to post-processed files.
622
623     set -exuo pipefail
624
625     # Generate INFO level output_info.xml for post-processing.
626     all_options=("--loglevel" "INFO")
627     all_options+=("--log" "none")
628     all_options+=("--report" "none")
629     all_options+=("--output" "${ARCHIVE_DIR}/output_info.xml")
630     all_options+=("${ARCHIVE_DIR}/output.xml")
631     rebot "${all_options[@]}" || true
632 }
633
634
635 function prepare_topology () {
636
637     # Prepare virtual testbed topology if needed based on flavor.
638
639     # Variables read:
640     # - TEST_CODE - String affecting test selection, usually jenkins job name.
641     # - NODENESS - Node multiplicity of testbed, either "2n" or "3n".
642     # - FLAVOR - Node flavor string, e.g. "clx" or "skx".
643     # Functions called:
644     # - die - Print to stderr and exit.
645     # - terraform_init - Terraform init topology.
646     # - terraform_apply - Terraform apply topology.
647
648     set -exuo pipefail
649
650     case_text="${NODENESS}_${FLAVOR}"
651     case "${case_text}" in
652         "1n_aws" | "2n_aws" | "3n_aws")
653             export TF_VAR_testbed_name="${TEST_CODE}"
654             terraform_init || die "Failed to call terraform init."
655             terraform_apply || die "Failed to call terraform apply."
656             ;;
657     esac
658 }
659
660
661 function reserve_and_cleanup_testbed () {
662
663     # Reserve physical testbed, perform cleanup, register trap to unreserve.
664     # When cleanup fails, remove from topologies and keep retrying
665     # until all topologies are removed.
666     #
667     # Variables read:
668     # - TOPOLOGIES - Array of paths to topology yaml to attempt reservation on.
669     # - PYTHON_SCRIPTS_DIR - Path to directory holding the reservation script.
670     # - BUILD_TAG - Any string suitable as filename, identifying
671     #   test run executing this function. May be unset.
672     # Variables set:
673     # - TOPOLOGIES - Array of paths to topologies, with failed cleanups removed.
674     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
675     # Functions called:
676     # - die - Print to stderr and exit.
677     # - ansible_playbook - Perform an action using ansible, see ansible.sh
678     # Traps registered:
679     # - EXIT - Calls cancel_all for ${WORKING_TOPOLOGY}.
680
681     set -exuo pipefail
682
683     while true; do
684         for topo in "${TOPOLOGIES[@]}"; do
685             set +e
686             scrpt="${PYTHON_SCRIPTS_DIR}/topo_reservation.py"
687             opts=("-t" "${topo}" "-r" "${BUILD_TAG:-Unknown}")
688             python3 "${scrpt}" "${opts[@]}"
689             result="$?"
690             set -e
691             if [[ "${result}" == "0" ]]; then
692                 # Trap unreservation before cleanup check,
693                 # so multiple jobs showing failed cleanup improve chances
694                 # of humans to notice and fix.
695                 WORKING_TOPOLOGY="${topo}"
696                 echo "Reserved: ${WORKING_TOPOLOGY}"
697                 trap "untrap_and_unreserve_testbed" EXIT || {
698                     message="TRAP ATTEMPT AND UNRESERVE FAILED, FIX MANUALLY."
699                     untrap_and_unreserve_testbed "${message}" || {
700                         die "Teardown should have died, not failed."
701                     }
702                     die "Trap attempt failed, unreserve succeeded. Aborting."
703                 }
704                 # Cleanup + calibration checks
705                 set +e
706                 ansible_playbook "cleanup, calibration"
707                 result="$?"
708                 set -e
709                 if [[ "${result}" == "0" ]]; then
710                     break
711                 fi
712                 warn "Testbed cleanup failed: ${topo}"
713                 untrap_and_unreserve_testbed "Fail of unreserve after cleanup."
714             fi
715             # Else testbed is accessible but currently reserved, moving on.
716         done
717
718         if [[ -n "${WORKING_TOPOLOGY-}" ]]; then
719             # Exit the infinite while loop if we made a reservation.
720             warn "Reservation and cleanup successful."
721             break
722         fi
723
724         if [[ "${#TOPOLOGIES[@]}" == "0" ]]; then
725             die "Run out of operational testbeds!"
726         fi
727
728         # Wait ~3minutes before next try.
729         sleep_time="$[ ( ${RANDOM} % 20 ) + 180 ]s" || {
730             die "Sleep time calculation failed."
731         }
732         echo "Sleeping ${sleep_time}"
733         sleep "${sleep_time}" || die "Sleep failed."
734     done
735 }
736
737
738 function run_pybot () {
739
740     # Run pybot with options based on input variables.
741     # Generate INFO level output_info.xml by rebot.
742     #
743     # Variables read:
744     # - CSIT_DIR - Path to existing root of local CSIT git repository.
745     # - ARCHIVE_DIR - Path to store robot result files in.
746     # - PYBOT_ARGS, EXPANDED_TAGS - See compose_pybot_arguments.sh
747     # - GENERATED_DIR - Tests are assumed to be generated under there.
748     # Variables set:
749     # - PYBOT_EXIT_STATUS - Exit status of most recent pybot invocation.
750     # Functions called:
751     # - die - Print to stderr and exit.
752
753     set -exuo pipefail
754
755     all_options=("--outputdir" "${ARCHIVE_DIR}" "${PYBOT_ARGS[@]}")
756     all_options+=("${EXPANDED_TAGS[@]}")
757
758     pushd "${CSIT_DIR}" || die "Change directory operation failed."
759     set +e
760     robot "${all_options[@]}" "${GENERATED_DIR}/tests/"
761     PYBOT_EXIT_STATUS="$?"
762     set -e
763
764     post_process_robot_outputs || die
765
766     popd || die "Change directory operation failed."
767 }
768
769
770 function select_arch_os () {
771
772     # Set variables affected by local CPU architecture and operating system.
773     #
774     # Variables set:
775     # - VPP_VER_FILE - Name of file in CSIT dir containing vpp stable version.
776     # - IMAGE_VER_FILE - Name of file in CSIT dir containing the image name.
777     # - PKG_SUFFIX - Suffix of OS package file name, "rpm" or "deb."
778
779     set -exuo pipefail
780
781     source /etc/os-release || die "Get OS release failed."
782
783     case "${ID}" in
784         "ubuntu"*)
785             case "${VERSION}" in
786                 *"LTS (Jammy Jellyfish)"*)
787                     IMAGE_VER_FILE="VPP_DEVICE_IMAGE_UBUNTU_JAMMY"
788                     VPP_VER_FILE="VPP_STABLE_VER_UBUNTU_JAMMY"
789                     PKG_SUFFIX="deb"
790                     ;;
791                 *)
792                     die "Unsupported Ubuntu version!"
793                     ;;
794             esac
795             ;;
796         *)
797             die "Unsupported distro or OS!"
798             ;;
799     esac
800
801     arch=$(uname -m) || {
802         die "Get CPU architecture failed."
803     }
804
805     case "${arch}" in
806         "aarch64")
807             IMAGE_VER_FILE="${IMAGE_VER_FILE}_ARM"
808             ;;
809         *)
810             ;;
811     esac
812 }
813
814
815 function select_tags () {
816
817     # Variables read:
818     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
819     # - TEST_CODE - String affecting test selection, usually jenkins job name.
820     # - DUT - CSIT test/ subdirectory, set while processing tags.
821     # - TEST_TAG_STRING - String selecting tags, from gerrit comment.
822     #   Can be unset.
823     # - TOPOLOGIES_DIR - Path to existing directory with available tpologies.
824     # - BASH_FUNCTION_DIR - Directory with input files to process.
825     # Variables set:
826     # - TAGS - Array of processed tag boolean expressions.
827     # - SELECTION_MODE - Selection criteria [test, suite, include, exclude].
828
829     set -exuo pipefail
830
831     # NIC SELECTION
832     case "${TEST_CODE}" in
833         *"1n-aws"*)
834             start_pattern='^  SUT:'
835             ;;
836         *)
837             start_pattern='^  TG:'
838             ;;
839     esac
840     end_pattern='^ \? \?[A-Za-z0-9]\+:'
841     # Remove the sections from topology file
842     sed_command="/${start_pattern}/,/${end_pattern}/d"
843     # All topologies NICs
844     available=$(sed "${sed_command}" "${TOPOLOGIES_DIR}"/* \
845                 | grep -hoP "model: \K.*" | sort -u)
846     # Selected topology NICs
847     reserved=$(sed "${sed_command}" "${WORKING_TOPOLOGY}" \
848                | grep -hoP "model: \K.*" | sort -u)
849     # All topologies NICs - Selected topology NICs
850     exclude_nics=($(comm -13 <(echo "${reserved}") <(echo "${available}"))) || {
851         die "Computation of excluded NICs failed."
852     }
853
854     # Select default NIC tag.
855     case "${TEST_CODE}" in
856         *"3n-snr"*)
857             default_nic="nic_intel-e822cq"
858             ;;
859         *"3n-tsh"*)
860             default_nic="nic_intel-x520-da2"
861             ;;
862         *"3n-icx"* | *"2n-icx"*)
863             default_nic="nic_intel-xxv710"
864             ;;
865         *"3n-spr"* | *"2n-spr"*)
866             default_nic="nic_intel-e810cq"
867             ;;
868         *"2n-clx"* | *"2n-zn2"*)
869             default_nic="nic_intel-xxv710"
870             ;;
871         *"2n-tx2"* | *"3n-alt"*)
872             default_nic="nic_intel-xl710"
873             ;;
874         *"1n-aws"* | *"2n-aws"* | *"3n-aws"*)
875             default_nic="nic_amazon-nitro-50g"
876             ;;
877         *)
878             default_nic="nic_intel-x710"
879             ;;
880     esac
881
882     sed_nic_sub_cmd="sed s/\${default_nic}/${default_nic}/"
883     awk_nics_sub_cmd=""
884     awk_nics_sub_cmd+='gsub("xxv710","25ge2p1xxv710");'
885     awk_nics_sub_cmd+='gsub("x710","10ge2p1x710");'
886     awk_nics_sub_cmd+='gsub("xl710","40ge2p1xl710");'
887     awk_nics_sub_cmd+='gsub("x520-da2","10ge2p1x520");'
888     awk_nics_sub_cmd+='gsub("cx556a","100ge2p1cx556a");'
889     awk_nics_sub_cmd+='gsub("e810cq","100ge2p1e810cq");'
890     awk_nics_sub_cmd+='gsub("vic1227","10ge2p1vic1227");'
891     awk_nics_sub_cmd+='gsub("vic1385","40ge2p1vic1385");'
892     awk_nics_sub_cmd+='gsub("nitro-50g","50ge1p1ENA");'
893     awk_nics_sub_cmd+='if ($9 =="drv_avf") drv="avf-";'
894     awk_nics_sub_cmd+='else if ($9 =="drv_rdma_core") drv ="rdma-";'
895     awk_nics_sub_cmd+='else if ($9 =="drv_mlx5_core") drv ="mlx5-";'
896     awk_nics_sub_cmd+='else if ($9 =="drv_af_xdp") drv ="af-xdp-";'
897     awk_nics_sub_cmd+='else drv="";'
898     awk_nics_sub_cmd+='if ($1 =="-") cores="";'
899     awk_nics_sub_cmd+='else cores=$1;'
900     awk_nics_sub_cmd+='print "*"$7"-" drv $11"-"$5"."$3"-" cores "-" drv $11"-"$5'
901
902     # Tag file directory shorthand.
903     tfd="${JOB_SPECS_DIR}"
904     case "${TEST_CODE}" in
905         # Select specific performance tests based on jenkins job type variable.
906         *"device"* )
907             readarray -t test_tag_array <<< $(grep -v "#" \
908                 ${tfd}/vpp_device/${DUT}-${NODENESS}-${FLAVOR}.md |
909                 awk {"$awk_nics_sub_cmd"} || echo "devicetest") || die
910             SELECTION_MODE="--test"
911             ;;
912         *"hoststack-daily"* )
913             readarray -t test_tag_array <<< $(grep -v "#" \
914                 ${tfd}/hoststack_daily/${DUT}-${NODENESS}-${FLAVOR}.md |
915                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
916             SELECTION_MODE="--test"
917             ;;
918         *"ndrpdr-weekly"* )
919             readarray -t test_tag_array <<< $(grep -v "#" \
920                 ${tfd}/ndrpdr_weekly/${DUT}-${NODENESS}-${FLAVOR}.md |
921                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
922             SELECTION_MODE="--test"
923             ;;
924         *"mrr-daily"* )
925             readarray -t test_tag_array <<< $(grep -v "#" \
926                 ${tfd}/mrr_daily/${DUT}-${NODENESS}-${FLAVOR}.md |
927                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
928             SELECTION_MODE="--test"
929             ;;
930         *"mrr-weekly"* )
931             readarray -t test_tag_array <<< $(grep -v "#" \
932                 ${tfd}/mrr_weekly/${DUT}-${NODENESS}-${FLAVOR}.md |
933                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
934             SELECTION_MODE="--test"
935             ;;
936         *"report-iterative"* )
937             test_sets=(${TEST_TAG_STRING//:/ })
938             # Run only one test set per run
939             report_file=${test_sets[0]}.md
940             readarray -t test_tag_array <<< $(grep -v "#" \
941                 ${tfd}/report_iterative/${NODENESS}-${FLAVOR}/${report_file} |
942                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
943             SELECTION_MODE="--test"
944             ;;
945         *"report-coverage"* )
946             test_sets=(${TEST_TAG_STRING//:/ })
947             # Run only one test set per run
948             report_file=${test_sets[0]}.md
949             readarray -t test_tag_array <<< $(grep -v "#" \
950                 ${tfd}/report_coverage/${NODENESS}-${FLAVOR}/${report_file} |
951                 awk {"$awk_nics_sub_cmd"} || echo "perftest") || die
952             SELECTION_MODE="--test"
953             ;;
954         * )
955             if [[ -z "${TEST_TAG_STRING-}" ]]; then
956                 # If nothing is specified, we will run pre-selected tests by
957                 # following tags.
958                 test_tag_array=("mrrAND${default_nic}AND1cAND64bANDethip4-ip4base"
959                                 "mrrAND${default_nic}AND1cAND78bANDethip6-ip6base"
960                                 "mrrAND${default_nic}AND1cAND64bANDeth-l2bdbasemaclrn"
961                                 "mrrAND${default_nic}AND1cAND64bANDeth-l2xcbase"
962                                 "!drv_af_xdp" "!drv_avf")
963             else
964                 # If trigger contains tags, split them into array.
965                 test_tag_array=(${TEST_TAG_STRING//:/ })
966             fi
967             SELECTION_MODE="--include"
968             ;;
969     esac
970
971     # Blacklisting certain tags per topology.
972     #
973     # Reasons for blacklisting:
974     # - ipsechw - Blacklisted on testbeds without crypto hardware accelerator.
975     case "${TEST_CODE}" in
976         *"1n-vbox"*)
977             test_tag_array+=("!avf")
978             test_tag_array+=("!vhost")
979             test_tag_array+=("!flow")
980             ;;
981         *"1n_tx2"*)
982             test_tag_array+=("!flow")
983             ;;
984         *"2n-clx"*)
985             test_tag_array+=("!ipsechw")
986             ;;
987         *"2n-icx"*)
988             test_tag_array+=("!ipsechw")
989             ;;
990         *"2n-spr"*)
991             test_tag_array+=("!ipsechw")
992             ;;
993         *"2n-tx2"*)
994             test_tag_array+=("!ipsechw")
995             ;;
996         *"2n-zn2"*)
997             test_tag_array+=("!ipsechw")
998             ;;
999         *"3n-alt"*)
1000             test_tag_array+=("!ipsechw")
1001             ;;
1002         *"3n-icx"*)
1003             test_tag_array+=("!ipsechw")
1004             test_tag_array+=("!3_node_double_link_topoANDnic_intel-xxv710")
1005             ;;
1006         *"3n-snr"*)
1007             ;;
1008         *"3n-spr"*)
1009             test_tag_array+=("!ipsechw")
1010             ;;
1011         *"3n-tsh"*)
1012             test_tag_array+=("!drv_avf")
1013             test_tag_array+=("!ipsechw")
1014             ;;
1015         *"1n-aws"* | *"2n-aws"* | *"3n-aws"*)
1016             test_tag_array+=("!ipsechw")
1017             ;;
1018     esac
1019
1020     # We will add excluded NICs.
1021     test_tag_array+=("${exclude_nics[@]/#/!NIC_}")
1022
1023     TAGS=()
1024     prefix=""
1025
1026     set +x
1027     if [[ "${TEST_CODE}" == "vpp-"* ]]; then
1028         if [[ "${TEST_CODE}" != *"device"* ]]; then
1029             # Automatic prefixing for VPP perf jobs to limit the NIC used and
1030             # traffic evaluation to MRR.
1031             if [[ "${TEST_TAG_STRING-}" == *"nic_"* ]]; then
1032                 prefix="${prefix}mrrAND"
1033             else
1034                 prefix="${prefix}mrrAND${default_nic}AND"
1035             fi
1036         fi
1037     fi
1038     for tag in "${test_tag_array[@]}"; do
1039         if [[ "${tag}" == "!"* ]]; then
1040             # Exclude tags are not prefixed.
1041             TAGS+=("${tag}")
1042         elif [[ "${tag}" == " "* || "${tag}" == *"perftest"* ]]; then
1043             # Badly formed tag expressions can trigger way too much tests.
1044             set -x
1045             warn "The following tag expression hints at bad trigger: ${tag}"
1046             warn "Possible cause: Multiple triggers in a single comment."
1047             die "Aborting to avoid triggering too many tests."
1048         elif [[ "${tag}" == *"OR"* ]]; then
1049             # If OR had higher precedence than AND, it would be useful here.
1050             # Some people think it does, thus triggering way too much tests.
1051             set -x
1052             warn "The following tag expression hints at bad trigger: ${tag}"
1053             warn "Operator OR has lower precedence than AND. Use space instead."
1054             die "Aborting to avoid triggering too many tests."
1055         elif [[ "${tag}" != "" && "${tag}" != "#"* ]]; then
1056             # Empty and comment lines are skipped.
1057             # Other lines are normal tags, they are to be prefixed.
1058             TAGS+=("${prefix}${tag}")
1059         fi
1060     done
1061     set -x
1062 }
1063
1064
1065 function select_topology () {
1066
1067     # Variables read:
1068     # - NODENESS - Node multiplicity of testbed, either "2n" or "3n".
1069     # - FLAVOR - Node flavor string, e.g. "clx" or "skx".
1070     # - CSIT_DIR - Path to existing root of local CSIT git repository.
1071     # - TOPOLOGIES_DIR - Path to existing directory with available topologies.
1072     # Variables set:
1073     # - TOPOLOGIES - Array of paths to suitable topology yaml files.
1074     # - TOPOLOGIES_TAGS - Tag expression selecting tests for the topology.
1075     # Functions called:
1076     # - die - Print to stderr and exit.
1077
1078     set -exuo pipefail
1079
1080     case_text="${NODENESS}_${FLAVOR}"
1081     case "${case_text}" in
1082         "1n_vbox")
1083             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*vpp_device*.template )
1084             TOPOLOGIES_TAGS="2_node_single_link_topo"
1085             ;;
1086         "1n_skx" | "1n_tx2")
1087             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*vpp_device*.template )
1088             TOPOLOGIES_TAGS="2_node_single_link_topo"
1089             ;;
1090         "2n_skx")
1091             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_skx*.yaml )
1092             TOPOLOGIES_TAGS="2_node_*_link_topo"
1093             ;;
1094         "2n_zn2")
1095             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_zn2*.yaml )
1096             TOPOLOGIES_TAGS="2_node_*_link_topo"
1097             ;;
1098         "3n_skx")
1099             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_skx*.yaml )
1100             TOPOLOGIES_TAGS="3_node_*_link_topo"
1101             ;;
1102         "3n_icx")
1103             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_icx*.yaml )
1104             TOPOLOGIES_TAGS="3_node_*_link_topo"
1105             ;;
1106         "2n_clx")
1107             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_clx*.yaml )
1108             TOPOLOGIES_TAGS="2_node_*_link_topo"
1109             ;;
1110         "2n_icx")
1111             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_icx*.yaml )
1112             TOPOLOGIES_TAGS="2_node_*_link_topo"
1113             ;;
1114         "2n_spr")
1115             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_spr*.yaml )
1116             TOPOLOGIES_TAGS="2_node_*_link_topo"
1117             ;;
1118         "3n_snr")
1119             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_snr*.yaml )
1120             TOPOLOGIES_TAGS="3_node_single_link_topo"
1121             ;;
1122         "3n_tsh")
1123             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_tsh*.yaml )
1124             TOPOLOGIES_TAGS="3_node_single_link_topo"
1125             ;;
1126         "2n_tx2")
1127             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n_tx2*.yaml )
1128             TOPOLOGIES_TAGS="2_node_single_link_topo"
1129             ;;
1130         "3n_alt")
1131             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n_alt*.yaml )
1132             TOPOLOGIES_TAGS="3_node_single_link_topo"
1133             ;;
1134         "1n_aws")
1135             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*1n-aws*.yaml )
1136             TOPOLOGIES_TAGS="1_node_single_link_topo"
1137             ;;
1138         "2n_aws")
1139             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*2n-aws*.yaml )
1140             TOPOLOGIES_TAGS="2_node_single_link_topo"
1141             ;;
1142         "3n_aws")
1143             TOPOLOGIES=( "${TOPOLOGIES_DIR}"/*3n-aws*.yaml )
1144             TOPOLOGIES_TAGS="3_node_single_link_topo"
1145             ;;
1146         *)
1147             # No falling back to default, that should have been done
1148             # by the function which has set NODENESS and FLAVOR.
1149             die "Unknown specification: ${case_text}"
1150     esac
1151
1152     if [[ -z "${TOPOLOGIES-}" ]]; then
1153         die "No applicable topology found!"
1154     fi
1155 }
1156
1157
1158 function set_environment_variables () {
1159
1160     # Depending on testbed topology, overwrite defaults set in the
1161     # resources/libraries/python/Constants.py file
1162     #
1163     # Variables read:
1164     # - TEST_CODE - String affecting test selection, usually jenkins job name.
1165     # Variables set:
1166     # See specific cases
1167
1168     set -exuo pipefail
1169
1170     case "${TEST_CODE}" in
1171         *"1n-aws"* | *"2n-aws"* | *"3n-aws"*)
1172             # T-Rex 2.88+ workaround for ENA NICs.
1173             export TREX_RX_DESCRIPTORS_COUNT=1024
1174             export TREX_EXTRA_CMDLINE="--mbuf-factor 19"
1175             export TREX_CORE_COUNT=6
1176             # Settings to prevent duration stretching.
1177             export PERF_TRIAL_STL_DELAY=0.1
1178             ;;
1179         *"2n-zn2"*)
1180             # Maciek's workaround for Zen2 with lower amount of cores.
1181             export TREX_CORE_COUNT=14
1182     esac
1183 }
1184
1185
1186 function untrap_and_unreserve_testbed () {
1187
1188     # Use this as a trap function to ensure testbed does not remain reserved.
1189     # Perhaps call directly before script exit, to free testbed for other jobs.
1190     # This function is smart enough to avoid multiple unreservations (so safe).
1191     # Topo cleanup is executed (call it best practice), ignoring failures.
1192     #
1193     # Hardcoded values:
1194     # - default message to die with if testbed might remain reserved.
1195     # Arguments:
1196     # - ${1} - Message to die with if unreservation fails. Default hardcoded.
1197     # Variables read (by inner function):
1198     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
1199     # - PYTHON_SCRIPTS_DIR - Path to directory holding Python scripts.
1200     # Variables written:
1201     # - WORKING_TOPOLOGY - Set to empty string on successful unreservation.
1202     # Trap unregistered:
1203     # - EXIT - Failure to untrap is reported, but ignored otherwise.
1204     # Functions called:
1205     # - die - Print to stderr and exit.
1206     # - ansible_playbook - Perform an action using ansible, see ansible.sh
1207
1208     set -xo pipefail
1209     set +eu  # We do not want to exit early in a "teardown" function.
1210     trap - EXIT || echo "Trap deactivation failed, continuing anyway."
1211     wt="${WORKING_TOPOLOGY}"  # Just to avoid too long lines.
1212     if [[ -z "${wt-}" ]]; then
1213         set -eu
1214         warn "Testbed looks unreserved already. Trap removal failed before?"
1215     else
1216         ansible_playbook "cleanup" || true
1217         python3 "${PYTHON_SCRIPTS_DIR}/topo_reservation.py" -c -t "${wt}" || {
1218             die "${1:-FAILED TO UNRESERVE, FIX MANUALLY.}" 2
1219         }
1220         case "${TEST_CODE}" in
1221             *"1n-aws"* | *"2n-aws"* | *"3n-aws"*)
1222                 terraform_destroy || die "Failed to call terraform destroy."
1223                 ;;
1224             *)
1225                 ;;
1226         esac
1227         WORKING_TOPOLOGY=""
1228         set -eu
1229     fi
1230 }
1231
1232
1233 function warn () {
1234
1235     # Print the message to standard error.
1236     #
1237     # Arguments:
1238     # - ${@} - The text of the message.
1239
1240     set -exuo pipefail
1241
1242     echo "$@" >&2
1243 }