From 1d14bb7a8a7621bbb3bda3bc3825be6ab84e6d88 Mon Sep 17 00:00:00 2001
From: Ed Santiago <santiago@redhat.com>
Date: Mon, 17 Jul 2023 10:50:44 -0600
Subject: [PATCH] quadlet systest: fix broken tmpdir references

quadlet volume-path system test was making invalid assumptions
about $TMPDIR, causing test to fail when TMPDIR=/var/tmp or /dev/shm

Much more complicated than it should be, because we need to
find out the systemd value of %T.

Minor cleanup too.

Signed-off-by: Ed Santiago <santiago@redhat.com>
---
 test/system/252-quadlet.bats | 38 ++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/test/system/252-quadlet.bats b/test/system/252-quadlet.bats
index d4badd194a..b13fd8f8b1 100644
--- a/test/system/252-quadlet.bats
+++ b/test/system/252-quadlet.bats
@@ -751,9 +751,35 @@ EOF
     remove_secret $SECRET_NAME
 }
 
-@test "quadlet - volume path using specifier" {
-    local tmp_path=$(mktemp -d --tmpdir=$PODMAN_TMPDIR quadlet.volume.XXXXXX)
-    local tmp_dir=${tmp_path#/tmp/}
+@test "quadlet - volume path using systemd %T specifier" {
+    # "specifier" is systemd-speak for "replaceable fields"; see systemd.unit(5)
+    #
+    # Step 1: determine what systemd is using for %T. There does not
+    # seem to be any systemctly way to find this.
+    local service=get-percent-t.$(random_string 10).service
+    local unitfile=${UNIT_DIR}/$service
+    cat >$unitfile <<EOF
+[Unit]
+Description=Get the value of percent T
+
+[Service]
+ExecStart=/bin/bash -c "echo --==%T==--"
+EOF
+    systemctl daemon-reload
+    systemctl start $service
+    echo "$_LOG_PROMPT journalctl -u $service"
+    run journalctl -u $service
+    echo "$output"
+    percent_t=$(expr "$output" : ".* --==\(.*\)==--")
+    # Clean up. Don't bother to systemctl-reload, service_setup does that below.
+    rm -f $unitfile
+
+    # Sanity check: just make sure it's not "/"
+    assert "${#percent_t}" -ge 4 "sanity check: length of %T ($percent_t)"
+
+    # Step 2: Make a subdirectory in %T, and in there, a scratch file
+    local tmp_path=$(mktemp -d --tmpdir=${percent_t} quadlet.volume.XXXXXX)
+    local tmp_subdir=$(basename $tmp_path)
     local file_name="f$(random_string 10).txt"
     local file_content="data_$(random_string 15)"
     echo $file_content > $tmp_path/$file_name
@@ -762,7 +788,7 @@ EOF
     cat > $quadlet_file <<EOF
 [Container]
 Image=$IMAGE
-Volume=%T/$tmp_dir:/test_content:Z
+Volume=%T/$tmp_subdir:/test_content:Z
 Exec=sh -c "echo STARTED CONTAINER; echo "READY=1" | socat -u STDIN unix-sendto:\$NOTIFY_SOCKET; top"
 Notify=yes
 EOF
@@ -770,8 +796,8 @@ EOF
     run_quadlet "$quadlet_file"
     service_setup $QUADLET_SERVICE_NAME
 
-    run_podman exec $QUADLET_CONTAINER_NAME /bin/sh -c "cat /test_content/$file_name"
-    is "$output" $file_content
+    run_podman exec $QUADLET_CONTAINER_NAME cat /test_content/$file_name
+    is "$output" "$file_content" "contents of testfile in container volume"
 
     rm -rf $tmp_path
 }