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/node/core/metrics/BUILD.bazel b/metropolis/node/core/metrics/BUILD.bazel
index 30d08a7..9383afb 100644
--- a/metropolis/node/core/metrics/BUILD.bazel
+++ b/metropolis/node/core/metrics/BUILD.bazel
@@ -25,10 +25,12 @@
     name = "metrics_test",
     srcs = ["metrics_test.go"],
     data = [
-        # keep
         "//metropolis/node/core/metrics/fake_exporter",
     ],
     embed = [":metrics"],
+    x_defs = {
+        "xFakeExporterPath": "$(rlocationpath //metropolis/node/core/metrics/fake_exporter )",
+    },
     deps = [
         "//metropolis/node",
         "//metropolis/node/core/curator/proto/api",
diff --git a/metropolis/node/core/metrics/metrics_test.go b/metropolis/node/core/metrics/metrics_test.go
index 06b1d4e..fb5e3dc 100644
--- a/metropolis/node/core/metrics/metrics_test.go
+++ b/metropolis/node/core/metrics/metrics_test.go
@@ -23,9 +23,26 @@
 	"source.monogon.dev/osbase/supervisor"
 )
 
-func fakeExporter(name, value string) *Exporter {
-	path, _ := runfiles.Rlocation("_main/metropolis/node/core/metrics/fake_exporter/fake_exporter_/fake_exporter")
+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.
+	xFakeExporterPath string
+)
 
+func init() {
+	var err error
+	for _, path := range []*string{
+		&xFakeExporterPath,
+	} {
+		*path, err = runfiles.Rlocation(*path)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
+func fakeExporter(name, value string) *Exporter {
 	p, closer, err := freeport.AllocateTCPPort()
 	if err != nil {
 		panic(err)
@@ -36,7 +53,7 @@
 	return &Exporter{
 		Name:       name,
 		Port:       port,
-		Executable: path,
+		Executable: xFakeExporterPath,
 		Arguments: []string{
 			"-listen", "127.0.0.1:" + port.PortString(),
 			"-value", value,
diff --git a/metropolis/node/core/update/e2e/BUILD.bazel b/metropolis/node/core/update/e2e/BUILD.bazel
index b96006c..abf3ba1 100644
--- a/metropolis/node/core/update/e2e/BUILD.bazel
+++ b/metropolis/node/core/update/e2e/BUILD.bazel
@@ -5,7 +5,8 @@
     srcs = ["e2e_test.go"],
     data = [
         # For emulation
-        "//third_party/edk2:firmware",
+        "//third_party/edk2:OVMF_CODE.fd",
+        "//third_party/edk2:OVMF_VARS.fd",
         # For the initial image creation
         "//metropolis/node/core/update/e2e/testos:verity_rootfs_x",
         "//metropolis/node/core/update/e2e/testos:kernel_efi_x",
@@ -14,6 +15,16 @@
         "//metropolis/node/core/update/e2e/testos:testos_bundle_y",
         "//metropolis/node/core/update/e2e/testos:testos_bundle_z",
     ],
+    x_defs = {
+        "xBundleYPath": "$(rlocationpath //metropolis/node/core/update/e2e/testos:testos_bundle_y )",
+        "xBundleZPath": "$(rlocationpath //metropolis/node/core/update/e2e/testos:testos_bundle_z )",
+        "xOvmfVarsPath": "$(rlocationpath //third_party/edk2:OVMF_VARS.fd )",
+        "xOvmfCodePath": "$(rlocationpath //third_party/edk2:OVMF_CODE.fd )",
+        "xBootPath": "$(rlocationpath //metropolis/node/core/update/e2e/testos:kernel_efi_x )",
+        "xSystemXPath": "$(rlocationpath //metropolis/node/core/update/e2e/testos:verity_rootfs_x )",
+        # TODO(tim): Hardcoded because of https://github.com/monogon-dev/monogon/issues/316
+        "xAbloaderPath": "_main/metropolis/node/core/abloader/abloader_bin.efi",
+    },
     deps = [
         "//metropolis/node/build/mkimage/osimage",
         "//osbase/blkio",
diff --git a/metropolis/node/core/update/e2e/e2e_test.go b/metropolis/node/core/update/e2e/e2e_test.go
index a62c3d7..3df42e2 100644
--- a/metropolis/node/core/update/e2e/e2e_test.go
+++ b/metropolis/node/core/update/e2e/e2e_test.go
@@ -23,6 +23,33 @@
 	"source.monogon.dev/osbase/blockdev"
 )
 
+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.
+	xBundleYPath  string
+	xBundleZPath  string
+	xOvmfVarsPath string
+	xOvmfCodePath string
+	xBootPath     string
+	xSystemXPath  string
+	xAbloaderPath string
+)
+
+func init() {
+	var err error
+	for _, path := range []*string{
+		&xBundleYPath, &xBundleZPath, &xOvmfVarsPath,
+		&xOvmfCodePath, &xBootPath, &xSystemXPath,
+		&xAbloaderPath,
+	} {
+		*path, err = runfiles.Rlocation(*path)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 const Mi = 1024 * 1024
 
 var variantRegexp = regexp.MustCompile(`TESTOS_VARIANT=([A-Z])`)
@@ -137,16 +164,8 @@
 	}
 
 	m := http.NewServeMux()
-	bundleYPath, err := runfiles.Rlocation("_main/metropolis/node/core/update/e2e/testos/testos_bundle_y.zip")
-	if err != nil {
-		t.Fatal(err)
-	}
-	b.bundlePaths["Y"] = bundleYPath
-	bundleZPath, err := runfiles.Rlocation("_main/metropolis/node/core/update/e2e/testos/testos_bundle_z.zip")
-	if err != nil {
-		t.Fatal(err)
-	}
-	b.bundlePaths["Z"] = bundleZPath
+	b.bundlePaths["Y"] = xBundleYPath
+	b.bundlePaths["Z"] = xBundleZPath
 	m.HandleFunc("/bundle.bin", func(w http.ResponseWriter, req *http.Request) {
 		b.m.Lock()
 		bundleFilePath := b.bundleFilePath
@@ -175,38 +194,18 @@
 	t.Cleanup(func() { os.Remove(rootDevPath) })
 	defer rootDisk.Close()
 
-	ovmfVarsPath, err := runfiles.Rlocation("edk2/OVMF_VARS.fd")
-	if err != nil {
-		t.Fatal(err)
-	}
-	ovmfCodePath, err := runfiles.Rlocation("edk2/OVMF_CODE.fd")
-	if err != nil {
-		t.Fatal(err)
-	}
-	bootPath, err := runfiles.Rlocation("_main/metropolis/node/core/update/e2e/testos/kernel_efi_x.efi")
-	if err != nil {
-		t.Fatal(err)
-	}
-	boot, err := blkio.NewFileReader(bootPath)
+	boot, err := blkio.NewFileReader(xBootPath)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer boot.Close()
-	systemXPath, err := runfiles.Rlocation("_main/metropolis/node/core/update/e2e/testos/verity_rootfs_x.img")
-	if err != nil {
-		t.Fatal(err)
-	}
-	system, err := os.Open(systemXPath)
+	system, err := os.Open(xSystemXPath)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer system.Close()
 
-	abloaderPath, err := runfiles.Rlocation("_main/metropolis/node/core/abloader/abloader_bin.efi")
-	if err != nil {
-		t.Fatal(err)
-	}
-	loader, err := blkio.NewFileReader(abloaderPath)
+	loader, err := blkio.NewFileReader(xAbloaderPath)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -233,7 +232,7 @@
 	}
 	defer ovmfVars.Close()
 	t.Cleanup(func() { os.Remove(ovmfVars.Name()) })
-	ovmfVarsTmpl, err := os.Open(ovmfVarsPath)
+	ovmfVarsTmpl, err := os.Open(xOvmfVarsPath)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -245,7 +244,7 @@
 	qemuArgs := []string{
 		"-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults", "-m", "1024",
 		"-cpu", "max", "-smp", "sockets=1,cpus=1,cores=2,threads=2,maxcpus=4",
-		"-drive", "if=pflash,format=raw,readonly=on,file=" + ovmfCodePath,
+		"-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfCodePath,
 		"-drive", "if=pflash,format=raw,file=" + ovmfVars.Name(),
 		"-drive", "if=virtio,format=raw,cache=unsafe,file=" + rootDevPath,
 		"-netdev", fmt.Sprintf("user,id=net0,net=10.42.0.0/24,dhcpstart=10.42.0.10,%s", blobGuestFwd),