m: {cli,installer}: runfiles through datafile.MustGet
This implements datafile, a small library to more ergonomically resolve
Bazel runfiles:
1. It also works in cases where a tool doesn't run through `bazel run`.
2. It provides a MustGet wrapper which returns already read bytes and
removes some boilerplate at the callsite.
3. It allows us to extend the library in the future to prepare special
'self-contained' builds of some binaries, for example to bundle the
installer kernel in metroctl.
We then use this library to simplify the installer and installer tests.
In the installer, we technically remove the ability to specify arbitrary
kernels/bundles on the command line, but is this functionality actually
useful?
Change-Id: I46155b9951729c810e0d36930b470edfdfd82943
Reviewed-on: https://review.monogon.dev/c/monogon/+/484
Reviewed-by: Mateusz Zalega <mateusz@monogon.tech>
diff --git a/metropolis/test/installer/main.go b/metropolis/test/installer/main.go
index b36c70d..0a5e5aa 100644
--- a/metropolis/test/installer/main.go
+++ b/metropolis/test/installer/main.go
@@ -31,12 +31,12 @@
"syscall"
"testing"
- "github.com/bazelbuild/rules_go/go/tools/bazel"
diskfs "github.com/diskfs/go-diskfs"
"github.com/diskfs/go-diskfs/disk"
"github.com/diskfs/go-diskfs/partition/gpt"
mctl "source.monogon.dev/metropolis/cli/metroctl/core"
+ "source.monogon.dev/metropolis/cli/pkg/datafile"
"source.monogon.dev/metropolis/node/build/mkimage/osimage"
"source.monogon.dev/metropolis/pkg/logbuffer"
"source.monogon.dev/metropolis/proto/api"
@@ -45,12 +45,6 @@
// Each variable in this block points to either a test dependency or a side
// effect. These variables are initialized in TestMain using Bazel.
var (
- // installerEFIPayload is a filesystem path pointing at the unified kernel
- // image dependency.
- installerEFIPayload string
- // testOSBundle is a filesystem path pointing at the Metropolis installation
- // bundle.
- testOSBundle string
// installerImage is a filesystem path pointing at the installer image that
// is generated during the test, and is removed afterwards.
installerImage string
@@ -165,64 +159,18 @@
}
func TestMain(m *testing.M) {
- // Initialize global variables holding filesystem paths pointing to runtime
- // dependencies and side effects.
- paths := []struct {
- // res is a pointer to the global variable initialized.
- res *string
- // dep states whether the path should be resolved as a dependency, rather
- // than a side effect.
- dep bool
- // src is a source path, based on which res is resolved. In case of
- // dependencies it must be a path relative to the repository root. For
- // side effects, it must be just a filename.
- src string
- }{
- {&installerEFIPayload, true, "metropolis/test/installer/kernel.efi"},
- {&testOSBundle, true, "metropolis/test/installer/testos/testos_bundle.zip"},
- {&installerImage, false, "installer.img"},
- {&nodeStorage, false, "stor.img"},
- }
- for _, p := range paths {
- if p.dep {
- res, err := bazel.Runfile(p.src)
- if err != nil {
- log.Fatal(err)
- }
- *p.res = res
- } else {
- od := os.Getenv("TEST_TMPDIR")
- // If od is empty, just use the working directory, which is set according
- // to the rundir attribute of go_test.
- *p.res = filepath.Join(od, p.src)
- }
- }
+ installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
+ nodeStorage = filepath.Join(os.Getenv("TEST_TMPDIR"), "stor.img")
- // Build the installer image with metroctl, given the EFI executable
- // generated by Metropolis buildsystem.
- installer, err := os.Open(installerEFIPayload)
- if err != nil {
- log.Fatalf("Couldn't open the installer EFI executable at %q: %v", installerEFIPayload, err)
- }
- info, err := installer.Stat()
- if err != nil {
- log.Fatalf("Couldn't stat the installer EFI executable: %v", err)
- }
- bundle, err := os.Open(testOSBundle)
- if err != nil {
- log.Fatalf("Failed to open TestOS bundle: %v", err)
- }
- bundleStat, err := bundle.Stat()
- if err != nil {
- log.Fatalf("Failed to stat() TestOS bundle: %v", err)
- }
+ installer := datafile.MustGet("metropolis/test/installer/kernel.efi")
+ bundle := datafile.MustGet("metropolis/test/installer/testos/testos_bundle.zip")
iargs := mctl.MakeInstallerImageArgs{
- Installer: installer,
- InstallerSize: uint64(info.Size()),
+ Installer: bytes.NewBuffer(installer),
+ InstallerSize: uint64(len(installer)),
TargetPath: installerImage,
NodeParams: &api.NodeParameters{},
- Bundle: bundle,
- BundleSize: uint64(bundleStat.Size()),
+ Bundle: bytes.NewBuffer(bundle),
+ BundleSize: uint64(len(bundle)),
}
if err := mctl.MakeInstallerImage(iargs); err != nil {
log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)