From 4de3360dfd19b70e57e8195cdc9a88f3f5de062b Mon Sep 17 00:00:00 2001 From: Ed Santiago Date: Mon, 15 Jul 2024 10:35:17 -0600 Subject: [PATCH] System tests: safe container/image/volume/etc names Many system tests use hardcoded names for containers, images, and everything. This has worked because system tests run serially. It will not work if we ever run in parallel. Create a new safename() helper, and use it as follows: myctr=c_$(safename) myvol1=v1_$(safename) ... Find current instances of hardcoded names, and replace with safe ones. Whether or not we ever end up parallelizing system tests, this is simply good practice. There are far too many instances to fix in one (reviewable) PR. This is commit 1 of N. Signed-off-by: Ed Santiago safename: consistent within same test, and, dashes Make safename() invocations consistent within the same test. This puts the onus on the caller to add a unique element when calling multiple times, e.g. "ctr1-$(safename)". This is not too much of a burden. Major benefit is making it easy for a reader to associate containers, pods, volumes, images within a given test. And, use dashes, not underscores. "podman generate kube" removes underscores, making it very difficult to do things like "podman inspect $podname" (because we need to generate "$podname_with_underscores_removed") Signed-off-by: Ed Santiago --- test/system/helpers.bash | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/system/helpers.bash b/test/system/helpers.bash index 12613f2d4b..6a35369788 100644 --- a/test/system/helpers.bash +++ b/test/system/helpers.bash @@ -763,6 +763,28 @@ function random_string() { head /dev/urandom | tr -dc a-zA-Z0-9 | head -c$length } +############## +# safename # Returns a pseudorandom string suitable for container/image/etc names +############## +# +# Name will include the bats test number and a pseudorandom element, +# eg "t123-xyz123". safename() will return the same string across +# multiple invocations within a given test; this makes it easier for +# a maintainer to see common name patterns. +# +# String is lower-case so it can be used as an image name +# +function safename() { + # FIXME: I don't think these can ever fail. Remove checks once I'm sure. + test -n "$BATS_SUITE_TMPDIR" + test -n "$BATS_SUITE_TEST_NUMBER" + safenamepath=$BATS_SUITE_TMPDIR/.safename.$BATS_SUITE_TEST_NUMBER + if [[ ! -e $safenamepath ]]; then + echo -n "t${BATS_SUITE_TEST_NUMBER}-$(random_string 8 | tr A-Z a-z)" >$safenamepath + fi + cat $safenamepath +} + ######################### # find_exec_pid_files # Returns nothing or exec_pid hash files #########################