| 1 | package mounts |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/bashref" |
| 12 | "github.com/sylvinus/agent-vm/internal/paths" |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | type sandbox struct { |
| 17 | root, home, proj, state string |
| 18 | } |
| 19 | |
| 20 | func newSandbox(t *testing.T) sandbox { |
| 21 | t.Helper() |
| 22 | root, _ := filepath.EvalSymlinks(t.TempDir()) |
| 23 | |
| 24 | |
| 25 | root = paths.Host(root) |
| 26 | s := sandbox{root: root, home: root + "/home", proj: root + "/home/proj"} |
| 27 | s.state = s.home + "/.agent-vm" |
| 28 | for _, d := range []string{s.proj + "/sub", s.state, root + "/data/a", root + "/data/b", root + "/bare.git/objects", root + "/bare.git/refs", root + "/repo/.git/hooks", s.home + "/.lima"} { |
| 29 | os.MkdirAll(d, 0o755) |
| 30 | } |
| 31 | os.WriteFile(root+"/bare.git/HEAD", []byte("ref: refs/heads/main\n"), 0o644) |
| 32 | os.WriteFile(root+"/data/file.txt", []byte("x"), 0o644) |
| 33 | os.Symlink(root+"/data/a", root+"/alink") |
| 34 | return s |
| 35 | } |
| 36 | |
| 37 | func (s sandbox) refs() []Ref { |
| 38 | return Refs(s.home, bashref.Root(), s.state, s.home+"/.lima") |
| 39 | } |
| 40 | |
| 41 | func (s sandbox) bash(t *testing.T, snippet string, args ...string) bashref.Result { |
| 42 | return bashref.Script(t, "", []string{"HOME=" + s.home, "AGENT_VM_STATE_DIR=" + s.state}, snippet, args...) |
| 43 | } |
| 44 | |
| 45 | func TestEntriesBash(t *testing.T) { |
| 46 | s := newSandbox(t) |
| 47 | lines := []string{ |
| 48 | "# a comment", |
| 49 | "", |
| 50 | " " + s.root + "/data/a ", |
| 51 | s.root + "/data/a:/mnt/a:rw", |
| 52 | s.root + "/data/b:/mnt/b:ro # trailing", |
| 53 | s.root + "/data/b:/mnt/a:rw", |
| 54 | s.root + "/data/a:/mnt/x/../y/:rw", |
| 55 | s.root + "/data/file.txt:/etc/f", |
| 56 | s.root + "/missing:/mnt/m", |
| 57 | "~/proj:/mnt/p", |
| 58 | s.home + ":/mnt/h", |
| 59 | s.state + ":/mnt/s", |
| 60 | s.root + "/bare.git:/mnt/bare:rw", |
| 61 | s.root + "/bare.git:/mnt/bare2:ro", |
| 62 | s.root + "/repo/.git/hooks:/mnt/hooks:rw", |
| 63 | s.root + "/data/a:" + filepath.Dir(s.proj) + ":ro", |
| 64 | s.root + "/data/a:" + s.proj + "/inside:ro", |
| 65 | s.root + "/data/a:rel/dir:ro", |
| 66 | s.root + "/data/a:/mnt/f1:rw:" + s.proj, |
| 67 | s.root + "/data/b:/mnt/f2:rw:/other/*", |
| 68 | s.root + "/data/b:/mnt/f3:rw:~/pr*", |
| 69 | s.root + "/data/b:/mnt/f4:rw:relative/p", |
| 70 | s.root + "/data/b:/mnt/f5:rw:", |
| 71 | s.root + "/data/b:/mnt/f6:/p", |
| 72 | s.root + "/data/b:/mnt/f7:rw:/p:x", |
| 73 | s.root + "/data/a:/mnt/q\"uote", |
| 74 | s.root + "/data/a:/mnt/tab\there", |
| 75 | "crlf:" + s.root + "/data/b:/mnt/crlf\r", |
| 76 | s.root + "/alink:/mnt/alink:rw", |
| 77 | } |
| 78 | content := strings.Join(lines, "\n") + "\n" |
| 79 | os.WriteFile(filepath.Join(s.state, "volumes"), []byte(content), 0o644) |
| 80 | |
| 81 | var warn bytes.Buffer |
| 82 | got := Entries(filepath.Join(s.state, "volumes"), s.proj, s.home, s.refs(), &warn) |
| 83 | var b strings.Builder |
| 84 | for _, e := range got { |
| 85 | fmt.Fprintf(&b, "%s|%s|%s|%s\n", e.Src, e.Dst, e.Mode, e.Line) |
| 86 | } |
| 87 | r := s.bash(t, `_agent_vm_volume_entries "$1"`, s.proj) |
| 88 | if b.String() != r.Stdout { |
| 89 | t.Errorf("entries:\n%s\nbash:\n%s", b.String(), r.Stdout) |
| 90 | } |
| 91 | if warn.String() != r.Stderr { |
| 92 | t.Errorf("warnings:\n%s\nbash:\n%s", warn.String(), r.Stderr) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestMatchesBash(t *testing.T) { |
| 97 | s := newSandbox(t) |
| 98 | for _, f := range []string{s.proj, s.proj + "/", s.home + "/*", "~/proj", "~", "~/p?oj", "~/[pq]roj", "~/[!p]roj", "/*", "/", "relative", "~other/proj", s.home + "/*/sub", "~/pro[", `~/pro\j`} { |
| 99 | var warn bytes.Buffer |
| 100 | got := Matches(f, s.proj, s.home, &warn) |
| 101 | r := s.bash(t, `_agent_vm_volume_matches "$1" "$2"`, f, s.proj) |
| 102 | if got != (r.Code == 0) || warn.String() != r.Stderr { |
| 103 | t.Errorf("Matches(%q) = %v %q, bash %d %q", f, got, warn.String(), r.Code, r.Stderr) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestUnsafeProjectBash(t *testing.T) { |
| 109 | s := newSandbox(t) |
| 110 | for _, d := range []string{s.proj, s.home, s.root, s.state, s.state + "/x", bashref.Root(), bashref.Root() + "/lib", s.home + "/.lima", |
| 111 | s.root + "/bare.git", s.root + "/bare.git/objects", s.root + "/repo/.git", s.root + "/repo/.GIT", s.root + "/repo/.git/hooks", s.root + "/repo", s.root + "/alink"} { |
| 112 | os.MkdirAll(d, 0o755) |
| 113 | why, bad := UnsafeProject(d, s.refs()) |
| 114 | r := s.bash(t, `_agent_vm_unsafe_project "$1"`, d) |
| 115 | if bad != (r.Code == 0) || (bad && why+"\n" != r.Stdout) { |
| 116 | t.Errorf("UnsafeProject(%q) = %q %v, bash %q %d", d, why, bad, r.Stdout, r.Code) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestProjectMountpointBash(t *testing.T) { |
| 122 | for _, c := range []struct{ rel, src string }{ |
| 123 | {"sub/new", "dir"}, {"deep/a/b", "dir"}, {"f.txt", "file"}, {"../out", "dir"}, {".", "dir"}, {"link/x", "dir"}, |
| 124 | {"sub", "file"}, {"plainfile", "dir"}, {"./sub/./x/", "dir"}, |
| 125 | } { |
| 126 | var gs, bs sandbox |
| 127 | for _, s := range []*sandbox{&gs, &bs} { |
| 128 | *s = newSandbox(t) |
| 129 | os.Symlink(s.root+"/data/a", s.proj+"/link") |
| 130 | os.WriteFile(s.proj+"/plainfile", nil, 0o644) |
| 131 | } |
| 132 | src := func(s sandbox) string { |
| 133 | if c.src == "dir" { |
| 134 | return s.root + "/data/a" |
| 135 | } |
| 136 | return s.root + "/data/file.txt" |
| 137 | } |
| 138 | var warn bytes.Buffer |
| 139 | p, ok := ProjectMountpoint(gs.proj, c.rel, src(gs), &warn) |
| 140 | r := bs.bash(t, `_agent_vm_project_mountpoint "$@"`, bs.proj, c.rel, src(bs)) |
| 141 | norm := func(s, root string) string { return strings.ReplaceAll(s, root, "ROOT") } |
| 142 | if ok != (r.Code == 0) || (ok && norm(p, gs.root)+"\n" != norm(r.Stdout, bs.root)) || norm(warn.String(), gs.root) != norm(r.Stderr, bs.root) { |
| 143 | t.Errorf("ProjectMountpoint(%q, %s) = %q %v %q, bash %q %d %q", c.rel, c.src, p, ok, warn.String(), r.Stdout, r.Code, r.Stderr) |
| 144 | } |
| 145 | if _, err := os.Stat(gs.root + "/data/a/x"); err == nil { |
| 146 | t.Errorf("%q made something through the link", c.rel) |
| 147 | } |
| 148 | } |
| 149 | } |