m/installer/test: attempt to fix heisenbug

Change-Id: Ib4dae5bec7c99b88d5b9d4e5ac90ece09fdc7c77
Reviewed-on: https://review.monogon.dev/c/monogon/+/1365
Tested-by: Jenkins CI
Reviewed-by: Leopold Schabel <leo@monogon.tech>
diff --git a/metropolis/installer/test/main.go b/metropolis/installer/test/main.go
index 5e37b7d..3963654 100644
--- a/metropolis/installer/test/main.go
+++ b/metropolis/installer/test/main.go
@@ -28,6 +28,7 @@
 	"path/filepath"
 	"syscall"
 	"testing"
+	"time"
 
 	diskfs "github.com/diskfs/go-diskfs"
 	"github.com/diskfs/go-diskfs/disk"
@@ -46,9 +47,6 @@
 	// installerImage is a filesystem path pointing at the installer image that
 	// is generated during the test, and is removed afterwards.
 	installerImage string
-	// nodeStorage is a filesystem path pointing at the VM block device image
-	// Metropolis is installed to during the test. The file is removed afterwards.
-	nodeStorage string
 )
 
 // runQemu starts a new QEMU process, expecting the given output to appear
@@ -83,6 +81,11 @@
 // getStorage creates a sparse file, given a size expressed in mebibytes, and
 // returns a path to that file. It may return an error.
 func getStorage(size int64) (string, error) {
+	nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
+	if err != nil {
+		return "", err
+	}
+	nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
 	image, err := os.Create(nodeStorage)
 	if err != nil {
 		return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
@@ -119,7 +122,6 @@
 
 func TestMain(m *testing.M) {
 	installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
-	nodeStorage = filepath.Join(os.Getenv("TEST_TMPDIR"), "stor.img")
 
 	installer := datafile.MustGet("metropolis/installer/test/kernel.efi")
 	bundle := datafile.MustGet("metropolis/installer/test/testos/testos_bundle.zip")
@@ -266,9 +268,10 @@
 	if err := checkEspContents(storage); err != nil {
 		t.Error(err.Error())
 	}
-	storage.File.Close()
+	//storage.File.Close()
 	// Run QEMU again. Expect TestOS to launch successfully.
 	expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
+	time.Sleep(time.Second)
 	result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
 	if err != nil {
 		t.Error(err.Error())