tests: Respect TEST_TMPDIR for temporary directories
authorIgor Opaniuk <igor.opaniuk@foundries.io>
Mon, 9 Mar 2026 11:04:07 +0000 (12:04 +0100)
committerIgor Opaniuk <igor.opaniuk@foundries.io>
Mon, 16 Mar 2026 14:26:16 +0000 (15:26 +0100)
Several C tests hardcoded /var/tmp as the base path for temporary
working directories, ignoring the TEST_TMPDIR environment variable
used by the shell test suite.

This caused tests to create their ostree repos on the overlayfs
filesystem even when TEST_TMPDIR points to a real filesystem,
bypassing the intended workaround for overlayfs's inaccurate
free-space reporting. As a result, ostree's min-free-space-percent
check (default 3%) would fire when writing content objects, making
tests fail in containerized environments where the rootfs is overlayfs.

Fix by reading TEST_TMPDIR at runtime and falling back to /var/tmp
when it is not set, consistent with how the shell test suite handles
this. Affected tests:
- tests/test-libarchive-import.c
- tests/test-basic-c.c

Signed-off-by: Igor Opaniuk <igor.opaniuk@foundries.io>
tests/libostreetest.c
tests/libostreetest.h
tests/test-basic-c.c
tests/test-libarchive-import.c

index a19535610edabf59880e03493a3c03920977c399..075456a20b3724f6e3073bde9e019dfce4bbaaac 100644 (file)
 #include "libglnx.h"
 #include "libostreetest.h"
 
+/* Return a newly-allocated mkdtemp/g_mkdtemp template path under TEST_TMPDIR
+ * (falling back to /var/tmp), e.g. "ostree-xattrs-test.XXXXXX". */
+char *
+ot_test_tmpdir_template (const char *basename_template)
+{
+  const char *test_tmpdir = g_getenv ("TEST_TMPDIR") ?: "/var/tmp";
+  return g_build_filename (test_tmpdir, basename_template, NULL);
+}
+
 /* This function hovers in a quantum superposition of horrifying and
  * beautiful.  Future generations may interpret it as modern art.
  */
index e593ac13e312af247513aeb2925784c94d940077..e2a077a7ad805dacd00a230bb12479a7a5011519 100644 (file)
@@ -28,6 +28,8 @@ G_BEGIN_DECLS
 
 gboolean ot_test_run_libtest (const char *cmd, GError **error);
 
+char *ot_test_tmpdir_template (const char *basename_template);
+
 OstreeRepo *ot_test_setup_repo (GCancellable *cancellable, GError **error);
 
 gboolean ot_check_relabeling (gboolean *can_relabel, GError **error);
index a27dbc8fe8d098734558710f38ea66a254768ec3..555c5802dac6495526552329826a33b141402cb6 100644 (file)
@@ -525,8 +525,9 @@ test_read_xattrs (void)
   g_auto (GLnxTmpDir) tmpd = {
     0,
   };
-  // Use /var/tmp to hope we get xattr support
-  glnx_mkdtempat (AT_FDCWD, "/var/tmp/ostree-xattrs-test.XXXXXX", 0700, &tmpd, error);
+  // Use TEST_TMPDIR (or /var/tmp as fallback) to hope we get xattr support
+  g_autofree char *tmpd_tmpl = ot_test_tmpdir_template ("ostree-xattrs-test.XXXXXX");
+  glnx_mkdtempat (AT_FDCWD, tmpd_tmpl, 0700, &tmpd, error);
   g_assert_no_error (local_error);
 
   const char value[] = "foo";
@@ -539,8 +540,8 @@ test_read_xattrs (void)
 
     if (r != 0)
       {
-        g_autofree gchar *message = g_strdup_printf (
-            "Unable to set extended attributes in /var/tmp: %s", g_strerror (errno));
+        g_autofree gchar *message = g_strdup_printf ("Unable to set extended attributes in %s: %s",
+                                                     tmpd_tmpl, g_strerror (errno));
         g_test_skip (message);
         return;
       }
index 5643db70e930744be8c62995299f3f534a4906b4..d7524821083ec1f9ce679561b0f058880adb0462 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "libostreetest.h"
 #include "ostree-libarchive-private.h"
 #include <archive.h>
 #include <archive_entry.h>
@@ -47,7 +48,7 @@ test_data_init (TestData *td)
   uid_t uid = getuid ();
   gid_t gid = getgid ();
 
-  td->tmpd = g_mkdtemp (g_strdup ("/var/tmp/test-libarchive-import-XXXXXX"));
+  td->tmpd = g_mkdtemp (ot_test_tmpdir_template ("test-libarchive-import-XXXXXX"));
   g_assert_cmpint (0, ==, chdir (td->tmpd));
 
   td->fd = openat (AT_FDCWD, "foo.tar.gz", O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0644);