From 608c5b3463efdb4e0cfd806aa01949daa23b0c52 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Tue, 19 Nov 2024 19:18:35 -0700 Subject: [PATCH] Get the grub.cfg contents from byte slice: This allows us to avoid having to write a file to disk in a unit test. Signed-off-by: Jacob Weinstock --- internal/iso/iso_test.go | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/internal/iso/iso_test.go b/internal/iso/iso_test.go index 7f848f2f..97c03735 100644 --- a/internal/iso/iso_test.go +++ b/internal/iso/iso_test.go @@ -1,6 +1,7 @@ package iso import ( + "bytes" "context" "fmt" "io" @@ -161,31 +162,14 @@ menuentry 'LinuxKit ISO Image' { if err != nil { t.Fatal(err) } - if err := os.WriteFile("patched.iso", isoContents, 0o644); err != nil { - t.Fatal(err) - } - defer os.Remove("patched.iso") - dd, err := diskfs.Open("patched.iso", diskfs.WithOpenMode(diskfs.ReadOnly)) - if err != nil { - t.Fatal(err) - } - defer dd.Close() - fs, err := dd.GetFilesystem(0) - if err != nil { - t.Fatal(err) - } - ff, err := fs.OpenFile("EFI/BOOT/GRUB.CFG", os.O_RDONLY) - if err != nil { - t.Fatal(err) - } - defer ff.Close() - grubCfgFile, err := io.ReadAll(ff) - if err != nil { - t.Fatal(err) + idx := bytes.Index(isoContents, []byte(wantGrubCfg)) + if idx == -1 { + t.Fatalf("could not find grub.cfg in the ISO") } + contents := isoContents[idx : idx+len(wantGrubCfg)] - if diff := cmp.Diff(wantGrubCfg, string(grubCfgFile)); diff != "" { + if diff := cmp.Diff(wantGrubCfg, string(contents)); diff != "" { t.Fatalf("unexpected grub.cfg file: %s", diff) } }