treewide: replace hardcoded runfiles paths

We hardcoded some of the runfiles paths to find specific files. This replaces the hardcoded paths by a call to rlocationpath. This prevents running a target without the correct dependencies at build time instead of at runtime

Change-Id: I7ce56935ac80be6b28b824ccb0781ab401bd6521
Reviewed-on: https://review.monogon.dev/c/monogon/+/3301
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/metropolis/installer/test/BUILD.bazel b/metropolis/installer/test/BUILD.bazel
index 7f71218..3f74cde 100644
--- a/metropolis/installer/test/BUILD.bazel
+++ b/metropolis/installer/test/BUILD.bazel
@@ -1,24 +1,26 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
+load("@io_bazel_rules_go//go:def.bzl", "go_test")
 load("//metropolis/node/build:efi.bzl", "efi_unified_kernel_image")
 
 go_test(
-    name = "installer",
+    name = "test_test",
     size = "medium",
+    srcs = ["run_test.go"],
     data = [
         ":kernel",
         "//metropolis/installer/test/testos:testos_bundle",
-        "//third_party/edk2:firmware",
+        "//third_party/edk2:OVMF_CODE.fd",
+        "//third_party/edk2:OVMF_VARS.fd",
         "@qemu//:qemu-x86_64-softmmu",
     ],
-    embed = [":test"],
-    rundir = ".",
-)
-
-go_library(
-    name = "test",
-    srcs = ["main.go"],
     importpath = "source.monogon.dev/metropolis/installer/test",
     visibility = ["//visibility:private"],
+    x_defs = {
+        "xOvmfVarsPath": "$(rlocationpath //third_party/edk2:OVMF_VARS.fd )",
+        "xOvmfCodePath": "$(rlocationpath //third_party/edk2:OVMF_CODE.fd )",
+        "xQemuPath": "$(rlocationpath @qemu//:qemu-x86_64-softmmu )",
+        "xInstallerPath": "$(rlocationpath :kernel )",
+        "xBundlePath": "$(rlocationpath //metropolis/installer/test/testos:testos_bundle )",
+    },
     deps = [
         "//metropolis/cli/metroctl/core",
         "//metropolis/node/build/mkimage/osimage",
diff --git a/metropolis/installer/test/main.go b/metropolis/installer/test/run_test.go
similarity index 92%
rename from metropolis/installer/test/main.go
rename to metropolis/installer/test/run_test.go
index f2c1f46..fef8e20 100644
--- a/metropolis/installer/test/main.go
+++ b/metropolis/installer/test/run_test.go
@@ -42,6 +42,30 @@
 	"source.monogon.dev/osbase/cmd"
 )
 
+var (
+	// These are filled by bazel at linking time with the canonical path of
+	// their corresponding file. Inside the init function we resolve it
+	// with the rules_go runfiles package to the real path.
+	xOvmfCodePath  string
+	xOvmfVarsPath  string
+	xQemuPath      string
+	xInstallerPath string
+	xBundlePath    string
+)
+
+func init() {
+	var err error
+	for _, path := range []*string{
+		&xOvmfCodePath, &xOvmfVarsPath, &xQemuPath,
+		&xInstallerPath, &xBundlePath,
+	} {
+		*path, err = runfiles.Rlocation(*path)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 // Each variable in this block points to either a test dependency or a side
 // effect. These variables are initialized in TestMain using Bazel.
 var (
@@ -57,31 +81,19 @@
 // QEMU is killed shortly after the string is found, or when the context is
 // cancelled.
 func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
-	ovmfVarsPath, err := runfiles.Rlocation("edk2/OVMF_VARS.fd")
-	if err != nil {
-		return false, err
-	}
-	ovmfCodePath, err := runfiles.Rlocation("edk2/OVMF_CODE.fd")
-	if err != nil {
-		return false, err
-	}
-	qemuPath, err := runfiles.Rlocation("qemu/qemu-x86_64-softmmu")
-	if err != nil {
-		return false, err
-	}
 	defaultArgs := []string{
 		"-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
 		"-m", "512",
 		"-smp", "2",
 		"-cpu", "host",
-		"-drive", "if=pflash,format=raw,snapshot=on,file=" + ovmfCodePath,
-		"-drive", "if=pflash,format=raw,readonly=on,file=" + ovmfVarsPath,
+		"-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
+		"-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
 		"-serial", "stdio",
 		"-no-reboot",
 	}
 	qemuArgs := append(defaultArgs, args...)
 	pf := cmd.TerminateIfFound(expectedOutput, nil)
-	return cmd.RunCommand(ctx, qemuPath, qemuArgs, pf)
+	return cmd.RunCommand(ctx, xQemuPath, qemuArgs, pf)
 }
 
 // runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
@@ -136,20 +148,12 @@
 func TestMain(m *testing.M) {
 	installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
 
-	installerPath, err := runfiles.Rlocation("_main/metropolis/installer/test/kernel.efi")
-	if err != nil {
-		log.Fatal(err)
-	}
-	installer, err := os.ReadFile(installerPath)
+	installer, err := os.ReadFile(xInstallerPath)
 	if err != nil {
 		log.Fatal(err)
 	}
 
-	bundlePath, err := runfiles.Rlocation("_main/metropolis/installer/test/testos/testos_bundle.zip")
-	if err != nil {
-		log.Fatal(err)
-	}
-	bundle, err := os.ReadFile(bundlePath)
+	bundle, err := os.ReadFile(xBundlePath)
 	if err != nil {
 		log.Fatal(err)
 	}