FIX: Correct the packagecloud.io URL path
[csit.git] / resources / libraries / bash / function / common.sh
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 set -exuo pipefail
15
16 # This library defines functions used by multiple entry scripts.
17 # Keep functions ordered alphabetically, please.
18
19 # TODO: Add a link to bash style guide.
20 # TODO: Consider putting every die into a {} block,
21 #   the code might become more readable (but longer).
22
23
24 function activate_virtualenv () {
25
26     set -exuo pipefail
27
28     # Arguments:
29     # - ${1} - Non-empty path to existing directory for creating virtualenv in.
30     # Variables read:
31     # - CSIT_DIR - Path to existing root of local CSIT git repository.
32     # Variables set:
33     # - ENV_DIR - Path to the created virtualenv subdirectory.
34     # Variables exported:
35     # - PYTHONPATH - CSIT_DIR, as CSIT Python scripts usually need this.
36     # Functions called:
37     # - die - Print to stderr and exit.
38
39     # TODO: Do we really need to have ENV_DIR available as a global variable?
40
41     if [[ "${1-}" == "" ]]; then
42         die "Root location of virtualenv to create is not specified."
43     fi
44     ENV_DIR="${1}/env"
45     rm -rf "${ENV_DIR}" || die "Failed to clean previous virtualenv."
46
47     pip install --upgrade virtualenv || {
48         die "Virtualenv package install failed."
49     }
50     virtualenv "${ENV_DIR}" || {
51         die "Virtualenv creation failed."
52     }
53     set +u
54     source "${ENV_DIR}/bin/activate" || die "Virtualenv activation failed."
55     set -u
56     pip install -r "${CSIT_DIR}/requirements.txt" || {
57         die "CSIT requirements installation failed."
58     }
59
60     # Most CSIT Python scripts assume PYTHONPATH is set and exported.
61     export PYTHONPATH="${CSIT_DIR}" || die "Export failed."
62 }
63
64
65 function check_download_dir () {
66
67     set -exuo pipefail
68
69     # Fail if there are no files visible in ${DOWNLOAD_DIR}.
70     # TODO: Do we need this as a function, if it is (almost) a one-liner?
71     #
72     # Variables read:
73     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
74     # Directories read:
75     # - ${DOWNLOAD_DIR} - Has to be non-empty to proceed.
76     # Functions called:
77     # - die - Print to stderr and exit.
78
79     if [[ ! "$(ls -A "${DOWNLOAD_DIR}")" ]]; then
80         die "No artifacts downloaded!"
81     fi
82 }
83
84
85 function common_dirs () {
86
87     set -exuo pipefail
88
89     # Variables set:
90     # - BASH_FUNCTION_DIR - Path to existing directory this file is located in.
91     # - CSIT_DIR - Path to existing root of local CSIT git repository.
92     # - TOPOLOGIES_DIR - Path to existing directory with available tpologies.
93     # - RESOURCES_DIR - Path to existing CSIT subdirectory "resources".
94     # - TOOLS_DIR - Path to existing resources subdirectory "tools".
95     # - PYTHON_SCRIPTS_DIR - Path to existing tools subdirectory "scripts".
96     # - ARCHIVE_DIR - Path to created CSIT subdirectory "archive".
97     # - DOWNLOAD_DIR - Path to created CSIT subdirectory "download_dir".
98     # Functions called:
99     # - die - Print to stderr and exit.
100
101     BASH_FUNCTION_DIR="$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" || {
102         die "Some error during localizing this source directory."
103     }
104     # Current working directory could be in a different repo, e.g. VPP.
105     pushd "${BASH_FUNCTION_DIR}" || die "Pushd failed"
106     CSIT_DIR="$(readlink -e "$(git rev-parse --show-toplevel)")" || {
107         die "Readlink or git rev-parse failed."
108     }
109     popd || die "Popd failed."
110     TOPOLOGIES_DIR="$(readlink -e "${CSIT_DIR}/topologies/available")" || {
111         die "Readlink failed."
112     }
113     RESOURCES_DIR="$(readlink -e "${CSIT_DIR}/resources")" || {
114         die "Readlink failed."
115     }
116     TOOLS_DIR="$(readlink -e "${RESOURCES_DIR}/tools")" || {
117         die "Readlink failed."
118     }
119     PYTHON_SCRIPTS_DIR="$(readlink -e "${TOOLS_DIR}/scripts")" || {
120         die "Readlink failed."
121     }
122
123     ARCHIVE_DIR="$(readlink -f "${CSIT_DIR}/archive")" || {
124         die "Readlink failed."
125     }
126     mkdir -p "${ARCHIVE_DIR}" || die "Mkdir failed."
127     DOWNLOAD_DIR="$(readlink -f "${CSIT_DIR}/download_dir")" || {
128         die "Readlink failed."
129     }
130     mkdir -p "${DOWNLOAD_DIR}" || die "Mkdir failed."
131 }
132
133
134 function compose_pybot_arguments () {
135
136     set -exuo pipefail
137
138     # Variables read:
139     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
140     # - DUT - CSIT test/ subdirectory, set while processing tags.
141     # - TAGS - Array variable holding selected tag boolean expressions.
142     # - TOPOLOGIES_TAGS - Tag boolean expression filtering tests for topology.
143     # Variables set:
144     # - PYBOT_ARGS - String holding part of all arguments for pybot.
145     # - EXPANDED_TAGS - Array of strings pybot arguments compiled from tags.
146
147     # No explicit check needed with "set -u".
148     PYBOT_ARGS=("--loglevel" "TRACE" "--variable" "TOPOLOGY_PATH:${WORKING_TOPOLOGY}")
149     PYBOT_ARGS+=("--suite" "tests.${DUT}.perf")
150
151     EXPANDED_TAGS=()
152     for tag in "${TAGS[@]}"; do
153         if [[ ${tag} == "!"* ]]; then
154             EXPANDED_TAGS+=("--exclude" "${tag#$"!"}")
155         else
156             EXPANDED_TAGS+=("--include" "${TOPOLOGIES_TAGS}AND${tag}")
157         fi
158     done
159 }
160
161
162 function copy_archives () {
163
164     set -exuo pipefail
165
166     # Variables read:
167     # - WORKSPACE - Jenkins workspace, copy only if the value is not empty.
168     #   Can be unset, then it speeds up manual testing.
169     # - ARCHIVE_DIR - Path to directory with content to be copied.
170     # Directories updated:
171     # - ${WORKSPACE}/archives/ - Created if does not exist.
172     #   Content of ${ARCHIVE_DIR}/ is copied here.
173     # Functions called:
174     # - die - Print to stderr and exit.
175
176     # We will create additional archive if workspace variable is set.
177     # This way if script is running in jenkins all will be
178     # automatically archived to logs.fd.io.
179     if [[ -n "${WORKSPACE-}" ]]; then
180         mkdir -p "${WORKSPACE}/archives/" || die "Archives dir create failed."
181         cp -r "${ARCHIVE_DIR}"/* "${WORKSPACE}/archives" || die "Copy failed."
182     fi
183 }
184
185
186 function die () {
187     # Print the message to standard error end exit with error code specified
188     # by the second argument.
189     #
190     # Hardcoded values:
191     # - The default error message.
192     # Arguments:
193     # - ${1} - The whole error message, be sure to quote. Optional
194     # - ${2} - the code to exit with, default: 1.
195
196     set -x
197     set +eu
198     warn "${1:-Unspecified run-time error occurred!}"
199     exit "${2:-1}"
200 }
201
202
203 function die_on_pybot_error () {
204
205     set -exuo pipefail
206
207     # Source this fragment if you want to abort on any failed test case.
208     #
209     # Variables read:
210     # - PYBOT_EXIT_STATUS - Set by a pybot running fragment.
211     # Functions called:
212     # - die - Print to stderr and exit.
213
214     if [[ "${PYBOT_EXIT_STATUS}" != "0" ]]; then
215         die "${PYBOT_EXIT_STATUS}" "Test failures are present!"
216     fi
217 }
218
219
220 function get_test_code () {
221
222     set -exuo pipefail
223
224     # Arguments:
225     # - ${1} - Optional, argument of entry script (or empty as unset).
226     #   Test code value to override job name from environment.
227     # Variables read:
228     # - JOB_NAME - String affecting test selection, default if not argument.
229     # Variables set:
230     # - TEST_CODE - The test selection string from environment or argument.
231     # - NODENESS - Node multiplicity of desired testbed.
232     # - FLAVOR - Node flavor string, usually describing the processor.
233
234     TEST_CODE="${1-}" || die "Reading optional argument failed, somehow."
235     if [[ -z "${TEST_CODE}" ]]; then
236         TEST_CODE="${JOB_NAME-}" || die "Reading job name failed, somehow."
237     fi
238
239     case "${TEST_CODE}" in
240         *"2n-skx"*)
241             NODENESS="2n"
242             FLAVOR="skx"
243             ;;
244         *"3n-skx"*)
245             NODENESS="3n"
246             FLAVOR="skx"
247             ;;
248         *)
249             # Fallback to 3-node Haswell by default (backward compatibility)
250             NODENESS="3n"
251             FLAVOR="hsw"
252             ;;
253     esac
254 }
255
256
257 function get_test_tag_string () {
258
259     set -exuo pipefail
260
261     # Variables read:
262     # - GERRIT_EVENT_TYPE - Event type set by gerrit, can be unset.
263     # - GERRIT_EVENT_COMMENT_TEXT - Comment text, read for "comment-added" type.
264     # Variables set:
265     # - TEST_TAG_STRING - The string following "perftest" in gerrit comment,
266     #   or empty.
267
268     # TODO: ci-management scripts no longer need to perform this.
269
270     trigger=""
271     if [[ "${GERRIT_EVENT_TYPE-}" == "comment-added" ]]; then
272         # On parsing error, ${trigger} stays empty.
273         trigger="$(echo "${GERRIT_EVENT_COMMENT_TEXT}" \
274             | grep -oE '(perftest$|perftest[[:space:]].+$)')" || true
275     fi
276     # Set test tags as string.
277     TEST_TAG_STRING="${trigger#$"perftest"}"
278 }
279
280
281 function reserve_testbed () {
282
283     set -exuo pipefail
284
285     # Reserve physical testbed, perform cleanup, register trap to unreserve.
286     #
287     # Variables read:
288     # - TOPOLOGIES - Array of paths to topology yaml to attempt reservation on.
289     # - PYTHON_SCRIPTS_DIR - Path to directory holding the reservation script.
290     # Variables set:
291     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
292     # Functions called:
293     # - die - Print to stderr and exit.
294     # Traps registered:
295     # - EXIT - Calls cancel_all for ${WORKING_TOPOLOGY}.
296
297     while true; do
298         for topo in "${TOPOLOGIES[@]}"; do
299             set +e
300             python "${PYTHON_SCRIPTS_DIR}/topo_reservation.py" -t "${topo}"
301             result="$?"
302             set -e
303             if [[ "${result}" == "0" ]]; then
304                 WORKING_TOPOLOGY="${topo}"
305                 echo "Reserved: ${WORKING_TOPOLOGY}"
306                 trap "untrap_and_unreserve_testbed" EXIT || {
307                     message="TRAP ATTEMPT AND UNRESERVE FAILED, FIX MANUALLY."
308                     untrap_and_unreserve_testbed "${message}" || {
309                         die "Teardown should have died, not failed."
310                     }
311                     die "Trap attempt failed, unreserve succeeded. Aborting."
312                 }
313                 python "${PYTHON_SCRIPTS_DIR}/topo_cleanup.py" -t "${topo}" || {
314                     die "Testbed cleanup failed."
315                 }
316                 break
317             fi
318         done
319
320         if [[ -n "${WORKING_TOPOLOGY-}" ]]; then
321             # Exit the infinite while loop if we made a reservation.
322             break
323         fi
324
325         # Wait ~3minutes before next try.
326         sleep_time="$[ ( $RANDOM % 20 ) + 180 ]s" || {
327             die "Sleep time calculation failed."
328         }
329         echo "Sleeping ${sleep_time}"
330         sleep "${sleep_time}" || die "Sleep failed."
331     done
332 }
333
334
335 function run_pybot () {
336
337     set -exuo pipefail
338
339     # Currently, VPP-1361 causes occasional test failures.
340     # If real result is more important than time, we can retry few times.
341     # TODO: We should be retrying on test case level instead.
342
343     # Arguments:
344     # - ${1} - Optional number of pybot invocations to try to avoid failures.
345     #   Default: 1.
346     # Variables read:
347     # - CSIT_DIR - Path to existing root of local CSIT git repository.
348     # - ARCHIVE_DIR - Path to store robot result files in.
349     # - PYBOT_ARGS, EXPANDED_TAGS - See compose_pybot_arguments.sh
350     # Variables set:
351     # - PYBOT_EXIT_STATUS - Exit status of most recent pybot invocation.
352     # Functions called:
353     # - die - Print to stderr and exit.
354
355     # Set ${tries} as an integer variable, to fail on non-numeric input.
356     local -i "tries" || die "Setting type of variable failed."
357     tries="${1:-1}" || die "Argument evaluation failed."
358     all_options=("--outputdir" "${ARCHIVE_DIR}" "${PYBOT_ARGS[@]}")
359     all_options+=("${EXPANDED_TAGS[@]}")
360
361     while true; do
362         if [[ "${tries}" -le 0 ]]; then
363             break
364         else
365             tries="$((${tries} - 1))"
366         fi
367         pushd "${CSIT_DIR}" || die "Change directory operation failed."
368         set +e
369         # TODO: Make robot tests not require "$(pwd)" == "${CSIT_DIR}".
370         pybot "${all_options[@]}" "${CSIT_DIR}/tests/"
371         PYBOT_EXIT_STATUS="$?"
372         set -e
373         popd || die "Change directory operation failed."
374         if [[ "${PYBOT_EXIT_STATUS}" == "0" ]]; then
375             break
376         fi
377     done
378 }
379
380
381 function select_tags () {
382
383     set -exuo pipefail
384
385     # Variables read:
386     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
387     # - TEST_CODE - String affecting test selection, usually jenkins job name.
388     # - TEST_TAG_STRING - String selecting tags, from gerrit comment.
389     #   Can be unset.
390     # - TOPOLOGIES_DIR - Path to existing directory with available tpologies.
391     # Variables set:
392     # - TAGS - Array of processed tag boolean expressions.
393
394     # NIC SELECTION
395     # All topologies NICs
396     available=$(grep -hoPR "model: \K.*" "${TOPOLOGIES_DIR}"/* | sort -u)
397     # Selected topology NICs
398     reserved=$(grep -hoPR "model: \K.*" "${WORKING_TOPOLOGY}" | sort -u)
399     # All topologies NICs - Selected topology NICs
400     exclude_nics=($(comm -13 <(echo "${reserved}") <(echo "${available}")))
401
402     case "${TEST_CODE}" in
403         # Select specific performance tests based on jenkins job type variable.
404         *"ndrpdr-weekly"* )
405             test_tag_array=("ndrpdrAND64bAND1c"
406                             "ndrpdrAND78bAND1c")
407             ;;
408         *"mrr-daily"* | *"mrr-weekly"* )
409             test_tag_array=(# vic
410                             "mrrANDnic_cisco-vic-1227AND64b"
411                             "mrrANDnic_cisco-vic-1385AND64b"
412                             # memif
413                             "mrrANDmemifANDethAND64b"
414                             "mrrANDmemifANDethANDimix"
415                             # crypto
416                             "mrrANDipsecAND64b"
417                             # ip4 base
418                             "mrrANDip4baseAND64b"
419                             # ip4 scale FIB 2M
420                             "mrrANDip4fwdANDfib_2mAND64b"
421                             # ip4 scale FIB 200k
422                             "mrrANDip4fwdANDfib_200kANDnic_intel-*710AND64b"
423                             # ip4 scale FIB 20k
424                             "mrrANDip4fwdANDfib_20kANDnic_intel-*710AND64b"
425                             # ip4 scale ACL
426                             "mrrANDip4fwdANDacl1AND10k_flowsAND64b"
427                             "mrrANDip4fwdANDacl50AND10k_flowsAND64b"
428                             # ip4 scale NAT44
429                             "mrrANDip4fwdANDnat44ANDbaseAND64b"
430                             "mrrANDip4fwdANDnat44ANDsrc_user_4000AND64b"
431                             # ip4 features
432                             "mrrANDip4fwdANDfeatureANDnic_intel-*710AND64b"
433                             # TODO: Remove when tags in
434                             # tests/vpp/perf/ip4/*-ipolicemarkbase-*.robot
435                             # are fixed
436                             "mrrANDip4fwdANDpolice_markANDnic_intel-*710AND64b"
437                             # ip4 tunnels
438                             "mrrANDip4fwdANDencapANDip6unrlayANDip4ovrlayANDnic_intel-x520-da2AND64b"
439                             "mrrANDip4fwdANDencapANDnic_intel-*710AND64b"
440                             "mrrANDl2ovrlayANDencapANDnic_intel-*710AND64b"
441                             # ip6 base
442                             "mrrANDip6baseANDethAND78b"
443                             # ip6 features
444                             "mrrANDip6fwdANDfeatureANDnic_intel-*710AND78b"
445                             # ip6 scale FIB 2M
446                             "mrrANDip6fwdANDfib_2mANDnic_intel-*710AND78b"
447                             # ip6 scale FIB 200k
448                             "mrrANDip6fwdANDfib_200kANDnic_intel-*710AND78b"
449                             # ip6 scale FIB 20k
450                             "mrrANDip6fwdANDfib_20kANDnic_intel-*710AND78b"
451                             # ip6 tunnels
452                             "mrrANDip6fwdANDencapANDnic_intel-x520-da2AND78b"
453                             # l2xc base
454                             "mrrANDl2xcfwdANDbaseAND64b"
455                             # l2xc scale ACL
456                             "mrrANDl2xcANDacl1AND10k_flowsAND64b"
457                             "mrrANDl2xcANDacl50AND10k_flowsAND64b"
458                             # l2xc scale FIB 2M
459                             "mrrANDl2xcANDfib_2mAND64b"
460                             # l2xc scale FIB 200k
461                             "mrrANDl2xcANDfib_200kANDnic_intel-*710AND64b"
462                             # l2xc scale FIB 20k
463                             "mrrANDl2xcANDfib_20kANDnic_intel-*710AND64b"
464                             # l2bd base
465                             "mrrANDl2bdmaclrnANDbaseAND64b"
466                             # l2bd scale ACL
467                             "mrrANDl2bdmaclrnANDacl1AND10k_flowsAND64b"
468                             "mrrANDl2bdmaclrnANDacl50AND10k_flowsAND64b"
469                             # l2bd scale FIB 2M
470                             "mrrANDl2bdmaclrnANDfib_1mAND64b"
471                             # l2bd scale FIB 200k
472                             "mrrANDl2bdmaclrnANDfib_100kANDnic_intel-*710AND64b"
473                             # l2bd scale FIB 20k
474                             "mrrANDl2bdmaclrnANDfib_10kANDnic_intel-*710AND64b"
475                             # l2 patch base
476                             "mrrANDl2patchAND64b"
477                             # srv6
478                             "mrrANDsrv6ANDnic_intel-x520-da2AND78b"
479                             # vts
480                             "mrrANDvtsANDnic_intel-x520-da2AND114b"
481                             # vm vhost l2xc base
482                             "mrrANDvhostANDl2xcfwdANDbaseAND64b"
483                             "mrrANDvhostANDl2xcfwdANDbaseANDimix"
484                             # vm vhost l2bd base
485                             "mrrANDvhostANDl2bdmaclrnANDbaseAND64b"
486                             "mrrANDvhostANDl2bdmaclrnANDbaseANDimix"
487                             # vm vhost ip4 base
488                             "mrrANDvhostANDip4fwdANDbaseAND64b"
489                             "mrrANDvhostANDip4fwdANDbaseANDimix"
490                             # Exclude
491                             "!mrrANDip6baseANDdot1qAND78b"
492                             "!vhost_256ANDnic_intel-x520-da2"
493                             "!vhostANDnic_intel-xl710"
494                             "!cfs_opt"
495                             "!lbond_dpdk")
496             ;;
497         * )
498             if [[ -z "${TEST_TAG_STRING-}" ]]; then
499                 # If nothing is specified, we will run pre-selected tests by
500                 # following tags.
501                 test_tag_array=("mrrANDnic_intel-x710AND1cAND64bANDip4base"
502                                 "mrrANDnic_intel-x710AND1cAND78bANDip6base"
503                                 "mrrANDnic_intel-x710AND1cAND64bANDl2bdbase"
504                                 "mrrANDnic_intel-x710AND1cAND64bANDl2xcbase"
505                                 "!dot1q")
506             else
507                 # If trigger contains tags, split them into array.
508                 test_tag_array=(${TEST_TAG_STRING//:/ })
509             fi
510             ;;
511     esac
512
513     # Blacklisting certain tags per topology.
514     case "${TEST_CODE}" in
515         *"3n-hsw"*)
516             test_tag_array+=("!drv_avf")
517             ;;
518         *"2n-skx"*)
519             test_tag_array+=("!ipsechw")
520             ;;
521         *"3n-skx"*)
522             test_tag_array+=("!ipsechw")
523             ;;
524         *)
525             # Default to 3n-hsw due to compatibility.
526             test_tag_array+=("!drv_avf")
527             ;;
528     esac
529
530     # We will add excluded NICs.
531     test_tag_array+=("${exclude_nics[@]/#/!NIC_}")
532
533     TAGS=()
534
535     # We will prefix with perftest to prevent running other tests
536     # (e.g. Functional).
537     prefix="perftestAND"
538     if [[ "${TEST_CODE}" == "vpp-"* ]]; then
539         # Automatic prefixing for VPP jobs to limit the NIC used and
540         # traffic evaluation to MRR.
541         prefix="${prefix}mrrANDnic_intel-x710AND"
542     fi
543     for tag in "${test_tag_array[@]}"; do
544         if [[ ${tag} == "!"* ]]; then
545             # Exclude tags are not prefixed.
546             TAGS+=("${tag}")
547         else
548             TAGS+=("${prefix}${tag}")
549         fi
550     done
551 }
552
553
554 function select_topology () {
555
556     set -exuo pipefail
557
558     # Variables read:
559     # - NODENESS - Node multiplicity of testbed, either "2n" or "3n".
560     # - FLAVOR - Node flavor string, currently either "hsw" or "skx".
561     # - CSIT_DIR - Path to existing root of local CSIT git repository.
562     # - TOPOLOGIES_DIR - Path to existing directory with available tpologies.
563     # Variables set:
564     # - TOPOLOGIES - Array of paths to suitable topology yaml files.
565     # - TOPOLOGIES_TAGS - Tag expression selecting tests for the topology.
566     # Functions called:
567     # - die - Print to stderr and exit.
568
569     case_text="${NODENESS}_${FLAVOR}"
570     case "${case_text}" in
571         "3n_hsw")
572             TOPOLOGIES=(
573                         "${TOPOLOGIES_DIR}/lf_3n_hsw_testbed1.yaml"
574                         "${TOPOLOGIES_DIR}/lf_3n_hsw_testbed2.yaml"
575                         "${TOPOLOGIES_DIR}/lf_3n_hsw_testbed3.yaml"
576                        )
577             TOPOLOGIES_TAGS="3_node_single_link_topo"
578             ;;
579         "2n_skx")
580             TOPOLOGIES=(
581                         "${TOPOLOGIES_DIR}/lf_2n_skx_testbed21.yaml"
582                         #"${TOPOLOGIES_DIR}/lf_2n_skx_testbed22.yaml"
583                         #"${TOPOLOGIES_DIR}/lf_2n_skx_testbed23.yaml"
584                         "${TOPOLOGIES_DIR}/lf_2n_skx_testbed24.yaml"
585                        )
586             TOPOLOGIES_TAGS="2_node_*_link_topo"
587             ;;
588         "3n_skx")
589             TOPOLOGIES=(
590                         "${TOPOLOGIES_DIR}/lf_3n_skx_testbed31.yaml"
591                         "${TOPOLOGIES_DIR}/lf_3n_skx_testbed32.yaml"
592                        )
593             TOPOLOGIES_TAGS="3_node_*_link_topo"
594             ;;
595         *)
596             # No falling back to 3n_hsw default, that should have been done
597             # by the function which has set NODENESS and FLAVOR.
598             die "Unknown specification: ${case_text}"
599     esac
600
601     if [[ -z "${TOPOLOGIES-}" ]]; then
602         die "No applicable topology found!"
603     fi
604 }
605
606
607 function untrap_and_unreserve_testbed () {
608     # Use this as a trap function to ensure testbed does not remain reserved.
609     # Perhaps call directly before script exit, to free testbed for other jobs.
610     # This function is smart enough to avoid multiple unreservations (so safe).
611     # Topo cleanup is executed (call it best practice), ignoring failures.
612     #
613     # Hardcoded values:
614     # - default message to die with if testbed might remain reserved.
615     # Arguments:
616     # - ${1} - Message to die with if unreservation fails. Default hardcoded.
617     # Variables read (by inner function):
618     # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed.
619     # - PYTHON_SCRIPTS_DIR - Path to directory holding Python scripts.
620     # Variables written:
621     # - WORKING_TOPOLOGY - Set to empty string on successful unreservation.
622     # Trap unregistered:
623     # - EXIT - Failure to untrap is reported, but ignored otherwise.
624     # Functions called:
625     # - die - Print to stderr and exit.
626
627     set -xo pipefail
628     set +eu  # We do not want to exit early in a "teardown" function.
629     trap - EXIT || echo "Trap deactivation failed, continuing anyway."
630     wt="${WORKING_TOPOLOGY}"  # Just to avoid too long lines.
631     if [[ -z "${wt-}" ]]; then
632         set -eu
633         echo "Testbed looks unreserved already. Trap removal failed before?"
634     else
635         python "${PYTHON_SCRIPTS_DIR}/topo_cleanup.py" -t "${wt}" || true
636         python "${PYTHON_SCRIPTS_DIR}/topo_reservation.py" -c -t "${wt}" || {
637             die "${1:-FAILED TO UNRESERVE, FIX MANUALLY.}" 2
638         }
639         WORKING_TOPOLOGY=""
640         set -eu
641     fi
642 }
643
644
645 function warn () {
646     # Print the message to standard error.
647     #
648     # Arguments:
649     # - ${@} - The text of the message.
650
651     echo "$@" >&2
652 }