From 16f5b7408e204f2386a07f22a0bb65664495fea5 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Mon, 18 Mar 2024 16:14:16 +0100 Subject: [PATCH] added tests for BootLoaderFindPath for x86/aarch64 Signed-off-by: Christian Goll --- internal/pkg/container/shimgrub.go | 11 +++-- internal/pkg/container/shimgrub_test.go | 63 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 internal/pkg/container/shimgrub_test.go diff --git a/internal/pkg/container/shimgrub.go b/internal/pkg/container/shimgrub.go index eceee4ba..b152234c 100644 --- a/internal/pkg/container/shimgrub.go +++ b/internal/pkg/container/shimgrub.go @@ -53,7 +53,7 @@ func ShimFind(container string) string { } else { container_path = "/" } - wwlog.Debug("Finding grub under paths: %s", container_path) + wwlog.Debug("Finding shim under paths: %s", container_path) return BootLoaderFindPath(container_path, shimNames, shimDirs) } @@ -76,10 +76,13 @@ find the path of the shim binary in container */ func BootLoaderFindPath(cpath string, names func() []string, paths func() []string) string { for _, bdir := range paths() { - wwlog.Debug("Checking shim directory: %s", bdir) + wwlog.Debug("Checking directory: %s", bdir) for _, bname := range names() { - wwlog.Debug("Checking for bootloader name: %s", bname) - shimPaths, _ := filepath.Glob(path.Join(cpath, bdir, bname)) + wwlog.Debug("Checking for bootloader name: %s", path.Join(cpath, bdir, bname)) + shimPaths, err := filepath.Glob(path.Join(cpath, bdir, bname)) + if err != nil { + wwlog.Debug("Got error when globing %s: %s", path.Join(cpath, bdir, bname), err) + } for _, shimPath := range shimPaths { wwlog.Debug("Checking for bootloader path: %s", shimPath) // Only succeeds if shimPath exists and, if a diff --git a/internal/pkg/container/shimgrub_test.go b/internal/pkg/container/shimgrub_test.go new file mode 100644 index 00000000..11d9f29d --- /dev/null +++ b/internal/pkg/container/shimgrub_test.go @@ -0,0 +1,63 @@ +package container + +import ( + "os" + "path" + "testing" + + // warewulfconf "github.com/warewulf/warewulf/internal/pkg/config" + + "github.com/stretchr/testify/assert" + warewulfconf "github.com/warewulf/warewulf/internal/pkg/config" + "github.com/warewulf/warewulf/internal/pkg/testenv" + "github.com/warewulf/warewulf/internal/pkg/wwlog" +) + +func Test_Find_ShimX86(t *testing.T) { + testenv.New(t) + conf := warewulfconf.Get() + wwlog.SetLogLevel(wwlog.DEBUG) + os.MkdirAll(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/lib64/efi/"), 0755) + shimF, err := os.Create(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/lib64/efi/shim.efi")) + assert.NoError(t, err) + shimF.WriteString("shim.efi") + assert.FileExists(t, path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/lib64/efi/shim.efi")) + shimPath := ShimFind("suse") + assert.Equal(t, path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/lib64/efi/shim.efi"), shimPath) +} +func Test_Find_ShimArch64(t *testing.T) { + testenv.New(t) + conf := warewulfconf.Get() + wwlog.SetLogLevel(wwlog.DEBUG) + os.MkdirAll(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/aarch64"), 0755) + shimF, err := os.Create(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/aarch64/shim.efi")) + assert.NoError(t, err) + shimF.WriteString("shim.efi") + assert.FileExists(t, path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/aarch64/shim.efi")) + shimPath := ShimFind("suse") + assert.Equal(t, path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/aarch64/shim.efi"), shimPath) +} +func Test_Find_GrubX86(t *testing.T) { + testenv.New(t) + conf := warewulfconf.Get() + wwlog.SetLogLevel(wwlog.DEBUG) + os.MkdirAll(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/x86_64"), 0755) + shimF, err := os.Create(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/x86_64/grub.efi")) + assert.NoError(t, err) + shimF.WriteString("grub.efi") + assert.FileExists(t, path.Join(conf.Paths.WWChrootdir, "suse/rootfs//usr/share/efi/x86_64/grub.efi")) + shimPath := GrubFind("suse") + assert.Equal(t, path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/x86_64/grub.efi"), shimPath) +} +func Test_Find_GrubAarch64(t *testing.T) { + testenv.New(t) + conf := warewulfconf.Get() + wwlog.SetLogLevel(wwlog.DEBUG) + os.MkdirAll(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/aarch64/"), 0755) + shimF, err := os.Create(path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/aarch64/grub.efi")) + assert.NoError(t, err) + shimF.WriteString("grub.efi") + assert.FileExists(t, path.Join(conf.Paths.WWChrootdir, "suse/usr/share/efi/aarch64/grub.efi")) + shimPath := GrubFind("suse") + assert.Equal(t, path.Join(conf.Paths.WWChrootdir, "suse/rootfs/usr/share/efi/aarch64//grub.efi"), shimPath) +}