test(metis): cover writer and mount failure paths

This commit is contained in:
codex 2026-04-21 05:24:11 -03:00
parent c1de38b7a1
commit 5ddff4f385
2 changed files with 117 additions and 0 deletions

View File

@ -47,3 +47,84 @@ func TestSetupAndTeardownWithFakeCommands(t *testing.T) {
t.Fatalf("partitionPath /dev = %q", got) t.Fatalf("partitionPath /dev = %q", got)
} }
} }
func TestSetupReportsLoopAndTempDirFailures(t *testing.T) {
dir := t.TempDir()
scripts := filepath.Join(dir, "bin")
if err := os.MkdirAll(scripts, 0o755); err != nil {
t.Fatal(err)
}
write := func(name, body string) {
path := filepath.Join(scripts, name)
if err := os.WriteFile(path, []byte("#!/usr/bin/env bash\nset -eu\n"+body+"\n"), 0o755); err != nil {
t.Fatalf("write %s: %v", name, err)
}
}
write("losetup", `exit 2`)
write("mount", `exit 0`)
write("umount", `exit 0`)
t.Setenv("PATH", scripts+string(os.PathListSeparator)+os.Getenv("PATH"))
if _, err := Setup(filepath.Join(dir, "image.img")); err == nil {
t.Fatal("expected losetup failure")
}
tmpFile := filepath.Join(dir, "tmp-file")
if err := os.WriteFile(tmpFile, []byte("not-a-dir"), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("TMPDIR", tmpFile)
if _, err := Setup("/dev/loop9"); err == nil {
t.Fatal("expected temp-dir creation failure")
}
}
func TestSetupReportsMountFailures(t *testing.T) {
t.Run("boot", func(t *testing.T) {
dir := t.TempDir()
scripts := filepath.Join(dir, "bin")
if err := os.MkdirAll(scripts, 0o755); err != nil {
t.Fatal(err)
}
write := func(name, body string) {
path := filepath.Join(scripts, name)
if err := os.WriteFile(path, []byte("#!/usr/bin/env bash\nset -eu\n"+body+"\n"), 0o755); err != nil {
t.Fatalf("write %s: %v", name, err)
}
}
write("mount", `exit 3`)
write("umount", `exit 0`)
write("losetup", `exit 0`)
t.Setenv("PATH", scripts+string(os.PathListSeparator)+os.Getenv("PATH"))
if _, err := Setup("/dev/loop9"); err == nil {
t.Fatal("expected boot mount failure")
}
})
t.Run("root", func(t *testing.T) {
dir := t.TempDir()
scripts := filepath.Join(dir, "bin")
if err := os.MkdirAll(scripts, 0o755); err != nil {
t.Fatal(err)
}
state := filepath.Join(dir, "mount-count")
write := func(name, body string) {
path := filepath.Join(scripts, name)
if err := os.WriteFile(path, []byte("#!/usr/bin/env bash\nset -eu\n"+body+"\n"), 0o755); err != nil {
t.Fatalf("write %s: %v", name, err)
}
}
write("mount", `state="`+state+`"
count=0
if [[ -f "$state" ]]; then count="$(cat "$state")"; fi
count=$((count + 1))
printf '%s' "$count" > "$state"
if [[ "$count" == "2" ]]; then exit 4; fi
exit 0`)
write("umount", `exit 0`)
write("losetup", `exit 0`)
t.Setenv("PATH", scripts+string(os.PathListSeparator)+os.Getenv("PATH"))
if _, err := Setup("/dev/loop9"); err == nil {
t.Fatal("expected root mount failure")
}
})
}

View File

@ -39,3 +39,39 @@ func TestWriteImageWithProgressBranches(t *testing.T) {
t.Fatal("expected missing source error") t.Fatal("expected missing source error")
} }
} }
func TestCopyFileErrorBranches(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src.img")
if err := os.WriteFile(src, []byte("writer-errors"), 0o644); err != nil {
t.Fatal(err)
}
blockedParent := filepath.Join(dir, "blocked-parent")
if err := os.WriteFile(blockedParent, []byte("not-a-dir"), 0o644); err != nil {
t.Fatal(err)
}
if err := copyFile(context.Background(), src, filepath.Join(blockedParent, "dest.img"), 0, nil); err == nil {
t.Fatal("expected destination parent mkdir failure")
}
if err := copyFile(context.Background(), filepath.Join(dir, "missing.img"), filepath.Join(dir, "dest.img"), 0, nil); err == nil {
t.Fatal("expected source open failure")
}
if err := copyFile(context.Background(), src, dir, 0, nil); err == nil {
t.Fatal("expected destination create failure")
}
sourceDir := filepath.Join(dir, "source-dir")
if err := os.Mkdir(sourceDir, 0o755); err != nil {
t.Fatal(err)
}
if err := copyFile(context.Background(), sourceDir, filepath.Join(dir, "dir-read.img"), 0, nil); err == nil {
t.Fatal("expected directory read failure")
}
if _, err := os.Stat("/dev/full"); err != nil {
t.Skip("/dev/full is required for deterministic write-error coverage")
}
if err := copyFile(context.Background(), src, "/dev/full", int64(len("writer-errors")), nil); err == nil {
t.Fatal("expected /dev/full write failure")
}
}