gunk-dev/the-valley

snapshot_test.go

sourcebc00697b19b5main's published tip history
7.7 KiB
1package main
2
3import (
4 "crypto/sha256"
5 "fmt"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "the-valley/approval"
13)
14
15// floorCommit is a commit whose tree holds one floor document,
16// policy/instance/floor.cue, saying text, on parents.
17func (s *scratchRepo) floorCommit(text string, parents ...string) string {
18 s.t.Helper()
19 blob := s.input(text, "hash-object", "-w", "--stdin")
20 instance := s.input("100644 blob "+blob+"\tfloor.cue\n", "mktree")
21 policy := s.input("040000 tree "+instance+"\tinstance\n", "mktree")
22 tree := s.input("040000 tree "+policy+"\tpolicy\n", "mktree")
23 args := []string{"commit-tree", tree, "-m", "a floor"}
24 for _, p := range parents {
25 args = append(args, "-p", p)
26 }
27 return s.git(args...)
28}
29
30// repoFiles is every path under dir with its mode, modification time,
31// size and, for a file, the SHA-256 of its content, which any write under
32// dir would change.
33func repoFiles(t *testing.T, dir string) map[string]string {
34 t.Helper()
35 files := map[string]string{}
36 err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
37 if err != nil {
38 return err
39 }
40 info, err := d.Info()
41 if err != nil {
42 return err
43 }
44 files[path] = fmt.Sprint(info.Mode(), info.ModTime(), info.Size())
45 if info.Mode().IsRegular() {
46 content, err := os.ReadFile(path)
47 if err != nil {
48 return err
49 }
50 files[path] += fmt.Sprintf(" %x", sha256.Sum256(content))
51 }
52 return nil
53 })
54 if err != nil {
55 t.Fatal(err)
56 }
57 return files
58}
59
60// repoFilesUnchanged fails the test when dir's files are not before.
61func repoFilesUnchanged(t *testing.T, dir string, before map[string]string) {
62 t.Helper()
63 after := repoFiles(t, dir)
64 for path, was := range before {
65 if after[path] != was {
66 t.Errorf("%s changed: %q, then %q", path, was, after[path])
67 }
68 }
69 for path := range after {
70 if _, ok := before[path]; !ok {
71 t.Errorf("%s was written", path)
72 }
73 }
74}
75
76// readOnlyTree takes write away from every file and directory under dir,
77// and gives it back when the test ends.
78func readOnlyTree(t *testing.T, dir string) {
79 t.Helper()
80 modes := map[string]fs.FileMode{}
81 if err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
82 if err != nil {
83 return err
84 }
85 info, err := d.Info()
86 if err != nil {
87 return err
88 }
89 modes[path] = info.Mode().Perm()
90 return nil
91 }); err != nil {
92 t.Fatal(err)
93 }
94 for path, mode := range modes {
95 if err := os.Chmod(path, mode&^0o222); err != nil {
96 t.Fatal(err)
97 }
98 }
99 t.Cleanup(func() {
100 for path, mode := range modes {
101 os.Chmod(path, mode)
102 }
103 })
104}
105
106// A snapshot is the commit its ref held when it was taken. The floor, a
107// document and the instance identity are read at that commit after the
108// ref has moved to a commit holding another floor and another root. The
109// reads write nothing: they succeed in a repository nobody may write,
110// and leave every file in it as it was.
111func TestASnapshotReadsTheCommitItResolved(t *testing.T) {
112 s := newScratchRepo(t)
113 base := s.git("rev-parse", "refs/heads/main")
114 first := s.floorCommit("package verification\n// first\n", base)
115 s.git("update-ref", "refs/heads/main", first)
116 snap, err := takeSnapshot(s.repo, "refs/heads/main")
117 if err != nil {
118 t.Fatal(err)
119 }
120 if snap.commit != first {
121 t.Fatalf("the snapshot is of %s, not main's %s", snap.commit, first)
122 }
123
124 other := s.commit("an unrelated root")
125 moved := s.floorCommit("package verification\n// moved\n", first, other)
126 s.git("update-ref", "refs/heads/main", moved)
127
128 // Nobody may write the repository from here on, the test included.
129 if os.Geteuid() == 0 {
130 t.Log("running as root, which writes whatever the modes say; the listing alone shows no write")
131 }
132 readOnlyTree(t, s.repo)
133 before := repoFiles(t, s.repo)
134
135 if got, err := snap.read("policy/instance/floor.cue"); err != nil || string(got) != "package verification\n// first\n" {
136 t.Errorf("the snapshot's floor document reads %q, %v", got, err)
137 }
138 dir, done, err := snap.floor("policy/instance").Open(t.Context(), "valley-snapshot-test")
139 if err != nil {
140 t.Fatal(err)
141 }
142 if got, err := os.ReadFile(filepath.Join(dir, "floor.cue")); err != nil || !strings.Contains(string(got), "first") {
143 t.Errorf("the snapshot's floor opens as %q, %v", got, err)
144 }
145 done()
146 want, err := approval.InstanceID("sha1", []string{base})
147 if err != nil {
148 t.Fatal(err)
149 }
150 if got, err := snap.instanceID(); err != nil || got != want {
151 t.Errorf("the snapshot's identity is %q, %v, not its commit's %q", got, err, want)
152 }
153 again, err := takeSnapshot(s.repo, "refs/heads/main")
154 if err != nil || again.commit != moved {
155 t.Errorf("a new snapshot is of %q, %v, not main's %s", again.commit, err, moved)
156 }
157 repoFilesUnchanged(t, s.repo, before)
158}
159
160// A snapshot is taken of a full ref name holding a commit. A short name, a
161// ref the repository does not hold and a ref holding a tree are refused,
162// and so is a path the commit does not hold as a file.
163func TestASnapshotIsOfACommit(t *testing.T) {
164 s := newScratchRepo(t)
165 tree := s.git("rev-parse", "refs/heads/main^{tree}")
166 s.git("update-ref", "refs/the-valley/signers", tree)
167 for _, ref := range []string{"main", "refs/heads/absent", "refs/the-valley/signers"} {
168 if snap, err := takeSnapshot(s.repo, ref); err == nil {
169 t.Errorf("%s was taken as a snapshot of %s", ref, snap.commit)
170 }
171 }
172 snap, err := takeSnapshot(s.repo, "refs/heads/main")
173 if err != nil {
174 t.Fatal(err)
175 }
176 s.git("update-ref", "refs/heads/main", s.floorCommit("package verification\n"))
177 if snap, err = takeSnapshot(s.repo, "refs/heads/main"); err != nil {
178 t.Fatal(err)
179 }
180 for _, path := range []string{"absent.cue", "policy/instance", "policy"} {
181 if got, err := snap.read(path); err == nil {
182 t.Errorf("%s read as %q", path, got)
183 }
184 }
185}
186
187// In a partial clone, a snapshot reads only what the repository holds. A
188// blob the clone left to its promisor remote fails the read, of the
189// document and of the floor alike, and nothing is fetched into the
190// repository: every file in it is as it was. The commit and its history
191// are held, so the instance identity reads. A git read with lazy fetching
192// on does fetch the blob, which shows the clone would have.
193func TestASnapshotFetchesNothingFromAPromisorRemote(t *testing.T) {
194 src := newScratchRepo(t)
195 base := src.git("rev-parse", "refs/heads/main")
196 src.git("update-ref", "refs/heads/main", src.floorCommit("package verification\n// promised\n", base))
197 src.git("config", "uploadpack.allowFilter", "true")
198 part := filepath.Join(t.TempDir(), "part.git")
199 if _, err := git(t.TempDir(), "clone", "--quiet", "--bare", "--filter=blob:none", "file://"+src.repo, part); err != nil {
200 t.Fatal(err)
201 }
202 if promisor, err := gitLine(part, "config", "remote.origin.promisor"); err != nil || promisor != "true" {
203 t.Fatalf("the clone has no promisor remote (%q, %v), so this test proves nothing", promisor, err)
204 }
205 blob := src.git("rev-parse", "refs/heads/main:policy/instance/floor.cue")
206
207 before := repoFiles(t, part)
208 snap, err := takeSnapshot(part, "refs/heads/main")
209 if err != nil {
210 t.Fatal(err)
211 }
212 if got, err := snap.read("policy/instance/floor.cue"); err == nil {
213 t.Errorf("a blob the clone does not hold read as %q", got)
214 }
215 if dir, done, err := snap.floor("policy/instance").Open(t.Context(), "valley-snapshot-test"); err == nil {
216 done()
217 t.Errorf("a floor the clone does not hold opened at %s", dir)
218 }
219 want, err := approval.InstanceID("sha1", []string{base})
220 if err != nil {
221 t.Fatal(err)
222 }
223 if got, err := snap.instanceID(); err != nil || got != want {
224 t.Errorf("the partial clone's identity is %q, %v, not %q", got, err, want)
225 }
226 repoFilesUnchanged(t, part, before)
227
228 if _, err := git(part, "cat-file", "blob", blob); err != nil {
229 t.Fatalf("git with lazy fetching on did not fetch the blob either, so this test proves nothing: %v", err)
230 }
231}