treewide: override TMPDIR for tests that use qemu

When running QEMU with snapshot=on set, QEMU creates a copy of the
provided file in $TMPDIR. If $TMPDIR is set to /tmp, it will always
be overridden to /var/tmp. This creates an issue for us, as the
bazel tests only wire up /tmp, with /var/tmp being unaccessible
because of permissions. Bazel provides $TEST_TMPDIR for this
usecase, which we resolve to an absolute path and then override
$TMPDIR.

Change-Id: I767159211d3b3db2aafd0f4fc17d6d6480974f16
Reviewed-on: https://review.monogon.dev/c/monogon/+/4422
Tested-by: Jenkins CI
Reviewed-by: Leopold Schabel <leo@monogon.tech>
diff --git a/metropolis/cli/takeover/e2e/main_test.go b/metropolis/cli/takeover/e2e/main_test.go
index 482752a..556f300 100644
--- a/metropolis/cli/takeover/e2e/main_test.go
+++ b/metropolis/cli/takeover/e2e/main_test.go
@@ -14,6 +14,7 @@
 	"net"
 	"os"
 	"os/exec"
+	"path/filepath"
 	"strings"
 	"testing"
 	"time"
@@ -53,6 +54,18 @@
 			panic(err)
 		}
 	}
+	// When running QEMU with snapshot=on set, QEMU creates a copy of the
+	// provided file in $TMPDIR. If $TMPDIR is set to /tmp, it will always
+	// be overridden to /var/tmp. This creates an issue for us, as the
+	// bazel tests only wire up /tmp, with /var/tmp being unaccessible
+	// because of permissions. Bazel provides $TEST_TMPDIR for this
+	// usecase, which we resolve to an absolute path and then override
+	// $TMPDIR.
+	tmpDir, err := filepath.Abs(os.Getenv("TEST_TMPDIR"))
+	if err != nil {
+		panic(err)
+	}
+	os.Setenv("TMPDIR", tmpDir)
 }
 
 const GiB = 1024 * 1024 * 1024
diff --git a/metropolis/installer/test/run_test.go b/metropolis/installer/test/run_test.go
index 3da188a..daff8cd 100644
--- a/metropolis/installer/test/run_test.go
+++ b/metropolis/installer/test/run_test.go
@@ -54,6 +54,18 @@
 			panic(err)
 		}
 	}
+	// When running QEMU with snapshot=on set, QEMU creates a copy of the
+	// provided file in $TMPDIR. If $TMPDIR is set to /tmp, it will always
+	// be overridden to /var/tmp. This creates an issue for us, as the
+	// bazel tests only wire up /tmp, with /var/tmp being unaccessible
+	// because of permissions. Bazel provides $TEST_TMPDIR for this
+	// usecase, which we resolve to an absolute path and then override
+	// $TMPDIR.
+	tmpDir, err := filepath.Abs(os.Getenv("TEST_TMPDIR"))
+	if err != nil {
+		panic(err)
+	}
+	os.Setenv("TMPDIR", tmpDir)
 }
 
 // Each variable in this block points to either a test dependency or a side