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 <santiago@redhat.com>

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 <santiago@redhat.com>
This commit is contained in:
Ed Santiago 2024-07-15 10:35:17 -06:00 • committed by Lokesh Mandvekar
parent ec8e61e60a
commit 4de3360dfd
No known key found for this signature in database
GPG key ID: 1C1EDD7CC7C3A0DD

View file

@ -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
#########################