50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
|
|
package mount
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSetupAndTeardownWithFakeCommands(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", `printf '/dev/loop9\n'`)
|
||
|
|
write("mount", `exit 0`)
|
||
|
|
write("umount", `exit 0`)
|
||
|
|
t.Setenv("PATH", scripts+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||
|
|
|
||
|
|
image := filepath.Join(dir, "image.img")
|
||
|
|
if err := os.WriteFile(image, make([]byte, 1024), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(image+"p1", []byte(""), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if got := partitionPath(image, 1); got != image+"p1" {
|
||
|
|
t.Fatalf("partitionPath existing = %q", got)
|
||
|
|
}
|
||
|
|
m, err := Setup(image)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("Setup: %v", err)
|
||
|
|
}
|
||
|
|
if m.LoopDevice != "/dev/loop9" || m.BootPath == "" || m.RootPath == "" {
|
||
|
|
t.Fatalf("unexpected mount: %#v", m)
|
||
|
|
}
|
||
|
|
if err := Teardown(m); err != nil {
|
||
|
|
t.Fatalf("Teardown: %v", err)
|
||
|
|
}
|
||
|
|
if got := partitionPath("/dev/loop9", 2); got != "/dev/loop92" && got != "/dev/loop9p2" {
|
||
|
|
t.Fatalf("partitionPath /dev = %q", got)
|
||
|
|
}
|
||
|
|
}
|