Skip to content

Commit

Permalink
Get the grub.cfg contents from byte slice:
Browse files Browse the repository at this point in the history
This allows us to avoid having to write a
file to disk in a unit test.

Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed Nov 20, 2024
1 parent 8815ac8 commit 6a07cea
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions internal/iso/iso_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iso

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 6a07cea

Please sign in to comment.