| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 13 | "amadan.net/oficina/equipo/internal/testci" |
| 14 | ) |
| 15 | |
| 16 | func gitT(t *testing.T, dir string, env []string, args ...string) string { |
| 17 | t.Helper() |
| 18 | testci.Tools(t, "git", "python3") |
| 19 | cmd := exec.Command("git", args...) |
| 20 | cmd.Dir = dir |
| 21 | cmd.Env = append(os.Environ(), append([]string{"GIT_CONFIG_GLOBAL=/dev/null", "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com"}, env...)...) |
| 22 | out, err := cmd.CombinedOutput() |
| 23 | if err != nil { |
| 24 | t.Fatalf("git %v: %v %s", args, err, out) |
| 25 | } |
| 26 | return strings.TrimSpace(string(out)) |
| 27 | } |
| 28 | |
| 29 | // branchRig is a remote with main, and the runner's private repository |
| 30 | // holding a candidate commit on top of main. |
| 31 | type branchRig struct { |
| 32 | remote, gitdir, root, base, commit, commit2 string |
| 33 | confirms int |
| 34 | } |
| 35 | |
| 36 | func newBranchRig(t *testing.T) *branchRig { |
| 37 | t.Helper() |
| 38 | dir := t.TempDir() |
| 39 | r := &branchRig{remote: filepath.Join(dir, "remote.git"), gitdir: filepath.Join(dir, "private.git"), root: filepath.Join(dir, "records")} |
| 40 | seed := filepath.Join(dir, "seed") |
| 41 | gitT(t, dir, nil, "init", "-q", "-b", "main", seed) |
| 42 | os.WriteFile(filepath.Join(seed, "README.md"), []byte("teh panes\n"), 0600) |
| 43 | gitT(t, seed, nil, "add", "-A") |
| 44 | gitT(t, seed, nil, "commit", "-q", "-m", "seed") |
| 45 | gitT(t, dir, nil, "clone", "-q", "--bare", seed, r.remote) |
| 46 | gitT(t, dir, nil, "clone", "-q", "--bare", seed, r.gitdir) |
| 47 | r.base = gitT(t, seed, nil, "rev-parse", "HEAD") |
| 48 | work := filepath.Join(dir, "work") |
| 49 | os.MkdirAll(work, 0700) |
| 50 | env := []string{"GIT_DIR=" + r.gitdir, "GIT_WORK_TREE=" + work} |
| 51 | gitT(t, work, env, "checkout", "-q", "-f", r.base, "--", ".") |
| 52 | os.WriteFile(filepath.Join(work, "README.md"), []byte("the panes\n"), 0600) |
| 53 | gitT(t, work, env, "add", "-A") |
| 54 | tree := gitT(t, work, env, "write-tree") |
| 55 | r.commit = gitT(t, work, env, "commit-tree", tree, "-p", r.base, "-m", "fix") |
| 56 | r.commit2 = gitT(t, work, env, "commit-tree", tree, "-p", r.base, "-m", "another fix") |
| 57 | return r |
| 58 | } |
| 59 | |
| 60 | func (r *branchRig) push(branch string) BranchPush { |
| 61 | return BranchPush{Root: r.root, Git: "/usr/bin/git", GitDir: r.gitdir, Repo: "amadan.net/oficina/sample", Branch: branch, Default: "main", Base: r.base, Commit: r.commit, Key: "tk1/s1/1", Person: "owner@example.com", Owners: []string{"owner@example.com"}, |
| 62 | Remote: func(context.Context) (workspace.Remote, error) { return workspace.Remote{URL: r.remote}, nil }, |
| 63 | Confirm: func(context.Context) error { r.confirms++; return nil }} |
| 64 | } |
| 65 | |
| 66 | func (r *branchRig) head(t *testing.T, branch string) string { |
| 67 | out, err := exec.Command("git", "--git-dir="+r.remote, "rev-parse", "--verify", "-q", "refs/heads/"+branch).Output() |
| 68 | if err != nil { |
| 69 | return "" |
| 70 | } |
| 71 | return strings.TrimSpace(string(out)) |
| 72 | } |
| 73 | |
| 74 | func TestPushBranch(t *testing.T) { |
| 75 | ctx := context.Background() |
| 76 | r := newBranchRig(t) |
| 77 | const branch = "equipo/tk1-fix-the-typo" |
| 78 | if err := PushBranch(ctx, r.push(branch)); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | if got := r.head(t, branch); got != r.commit || r.confirms != 1 { |
| 82 | t.Fatalf("remote %s confirms %d", got, r.confirms) |
| 83 | } |
| 84 | if got := r.head(t, "main"); got != r.base { |
| 85 | t.Fatalf("main moved to %s", got) |
| 86 | } |
| 87 | // Again for the same attempt: reconciled from the remote, no second push. |
| 88 | if err := PushBranch(ctx, r.push(branch)); err != nil || r.confirms != 1 { |
| 89 | t.Fatalf("repeat: %v confirms %d", err, r.confirms) |
| 90 | } |
| 91 | // The record binds the key to its commit. |
| 92 | other := r.push(branch) |
| 93 | other.Commit = r.commit2 |
| 94 | if err := PushBranch(ctx, other); !errors.Is(err, ErrPushRefused) { |
| 95 | t.Fatalf("another commit under the same key: %v", err) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // TestPushBranchReconcilesACutPush: the runner died after the push |
| 100 | // landed and before it recorded so. The next call reads the remote and |
| 101 | // succeeds without pushing (the lease check would refuse it). |
| 102 | func TestPushBranchReconcilesACutPush(t *testing.T) { |
| 103 | ctx := context.Background() |
| 104 | r := newBranchRig(t) |
| 105 | const branch = "equipo/tk1-cut" |
| 106 | b := r.push(branch) |
| 107 | os.MkdirAll(r.root, 0700) |
| 108 | if err := b.save(pushRecord{Key: b.Key, Repo: b.Repo, Branch: b.Branch, Commit: b.Commit, Phase: "push_started"}); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | gitT(t, r.root, []string{"GIT_DIR=" + r.gitdir}, "push", "-q", r.remote, r.commit+":refs/heads/"+branch) |
| 112 | b.Confirm = func(context.Context) error { return errors.New("no lease") } |
| 113 | if err := PushBranch(ctx, b); err != nil { |
| 114 | t.Fatalf("reconcile: %v", err) |
| 115 | } |
| 116 | rec, _, _ := b.load() |
| 117 | if rec.Phase != "published" { |
| 118 | t.Fatalf("record %+v", rec) |
| 119 | } |
| 120 | // A cut push that did not land is pushed again, under the lease. |
| 121 | b2 := r.push("equipo/tk1-not-landed") |
| 122 | b2.Key = "tk1/s1/2" |
| 123 | if err := b2.save(pushRecord{Key: b2.Key, Repo: b2.Repo, Branch: b2.Branch, Commit: b2.Commit, Phase: "push_started"}); err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | if err := PushBranch(ctx, b2); err != nil || r.head(t, b2.Branch) != r.commit || r.confirms != 1 { |
| 127 | t.Fatalf("re-push: %v confirms %d", err, r.confirms) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestPushBranchRefuses(t *testing.T) { |
| 132 | ctx := context.Background() |
| 133 | r := newBranchRig(t) |
| 134 | for name, mutate := range map[string]func(*BranchPush){ |
| 135 | "default branch": func(b *BranchPush) { b.Branch, b.Default = "equipo/tk1-x", "equipo/tk1-x" }, |
| 136 | "not a task branch": func(b *BranchPush) { b.Branch = "main" }, |
| 137 | "another branch": func(b *BranchPush) { b.Branch = "feature/x" }, |
| 138 | "not from the base": func(b *BranchPush) { b.Base, b.Commit = b.Commit, b.Base }, |
| 139 | "another person": func(b *BranchPush) { b.Person = "mallory@example.com" }, |
| 140 | "bad repository": func(b *BranchPush) { b.Repo = "amadan.net/../x" }, |
| 141 | "unknown commit": func(b *BranchPush) { b.Commit = strings.Repeat("e", 40) }, |
| 142 | "remote for another": func(b *BranchPush) { |
| 143 | b.Remote = func(context.Context) (workspace.Remote, error) { |
| 144 | return workspace.Remote{URL: "https://amadan.net/other/x", GitPassword: "p"}, nil |
| 145 | } |
| 146 | }, |
| 147 | } { |
| 148 | b := r.push("equipo/tk1-refused") |
| 149 | b.Key = "refuse/" + name |
| 150 | mutate(&b) |
| 151 | if err := PushBranch(ctx, b); !errors.Is(err, ErrPushRefused) { |
| 152 | t.Fatalf("%s: %v", name, err) |
| 153 | } |
| 154 | } |
| 155 | if r.confirms != 0 || r.head(t, "equipo/tk1-refused") != "" { |
| 156 | t.Fatalf("pushed: confirms %d", r.confirms) |
| 157 | } |
| 158 | // No lease, no push. |
| 159 | b := r.push("equipo/tk1-no-lease") |
| 160 | b.Confirm = func(context.Context) error { return errors.New("superseded") } |
| 161 | if err := PushBranch(ctx, b); !errors.Is(err, ErrPushUncertain) || r.head(t, b.Branch) != "" { |
| 162 | t.Fatalf("pushed without a lease: %v", err) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // personCommit pushes a commit of the person's own onto branch in the |
| 167 | // remote, on top of parent. |
| 168 | func (r *branchRig) personCommit(t *testing.T, branch, parent string) string { |
| 169 | t.Helper() |
| 170 | tree := gitT(t, r.root, []string{"GIT_DIR=" + r.remote}, "rev-parse", parent+"^{tree}") |
| 171 | sha := gitT(t, r.root, []string{"GIT_DIR=" + r.remote}, "commit-tree", tree, "-p", parent, "-m", "the person's own work") |
| 172 | gitT(t, r.root, []string{"GIT_DIR=" + r.remote}, "update-ref", "refs/heads/"+branch, sha) |
| 173 | return sha |
| 174 | } |
| 175 | |
| 176 | // TestPushBranchNeverOverwritesOthers: the push landed but its answer |
| 177 | // was lost; then the person advanced the branch. Reconciling finds a |
| 178 | // head that is neither the expected predecessor nor the candidate: a |
| 179 | // conflict, and the person's commit stays. |
| 180 | func TestPushBranchNeverOverwritesOthers(t *testing.T) { |
| 181 | ctx := context.Background() |
| 182 | r := newBranchRig(t) |
| 183 | const branch = "equipo/tk1-lost-answer" |
| 184 | b := r.push(branch) |
| 185 | if err := PushBranch(ctx, b); err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | // The answer was lost: the record still says the push started. |
| 189 | rec, _, _ := b.load() |
| 190 | rec.Phase = "push_started" |
| 191 | if err := b.save(rec); err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | theirs := r.personCommit(t, branch, r.commit) |
| 195 | if err := PushBranch(ctx, b); !errors.Is(err, ErrPushConflict) { |
| 196 | t.Fatalf("reconcile over the person's commit: %v", err) |
| 197 | } |
| 198 | if got := r.head(t, branch); got != theirs { |
| 199 | t.Fatalf("branch at %s, the person's commit was %s", got, theirs) |
| 200 | } |
| 201 | // A new attempt on a branch someone else moved does not force over it |
| 202 | // either; one only Equipo pushed (an earlier attempt) it may replace. |
| 203 | b2 := r.push(branch) |
| 204 | b2.Key, b2.Commit = "tk1/s1/2", r.commit2 |
| 205 | if err := PushBranch(ctx, b2); !errors.Is(err, ErrPushConflict) || r.head(t, branch) != theirs { |
| 206 | t.Fatalf("attempt 2 over the person's commit: %v", err) |
| 207 | } |
| 208 | const other = "equipo/tk2-retry" |
| 209 | first := r.push(other) |
| 210 | first.Key = "tk2/s1/1" |
| 211 | if err := PushBranch(ctx, first); err != nil { |
| 212 | t.Fatal(err) |
| 213 | } |
| 214 | second := r.push(other) |
| 215 | second.Key, second.Commit = "tk2/s1/2", r.commit2 |
| 216 | if err := PushBranch(ctx, second); err != nil || r.head(t, other) != r.commit2 { |
| 217 | t.Fatalf("attempt 2 over attempt 1: %v", err) |
| 218 | } |
| 219 | // A branch that appeared from elsewhere before the first push. |
| 220 | const foreign = "equipo/tk3-foreign" |
| 221 | r.personCommit(t, foreign, r.base) |
| 222 | b3 := r.push(foreign) |
| 223 | b3.Key = "tk3/s1/1" |
| 224 | if err := PushBranch(ctx, b3); !errors.Is(err, ErrPushConflict) { |
| 225 | t.Fatalf("over a branch Equipo never pushed: %v", err) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // TestPushBranchLeaseHolds: the branch moves between reading its head |
| 230 | // and pushing; the lease refuses the push and the person's commit stays. |
| 231 | func TestPushBranchLeaseHolds(t *testing.T) { |
| 232 | r := newBranchRig(t) |
| 233 | const branch = "equipo/tk4-race" |
| 234 | b := r.push(branch) |
| 235 | b.Key = "tk4/s1/1" |
| 236 | var theirs string |
| 237 | b.beforePush = func() { theirs = r.personCommit(t, branch, r.base) } |
| 238 | if err := PushBranch(context.Background(), b); !errors.Is(err, ErrPushConflict) || r.head(t, branch) != theirs { |
| 239 | t.Fatalf("pushed over a branch that moved: %v (head %s, theirs %s)", err, r.head(t, branch), theirs) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // TestPushBranchReconcilesWithoutTheRepository: the push landed and the |
| 244 | // runner lost its private repository before the report was durable. A |
| 245 | // recorded push that is already on the remote reconciles as published |
| 246 | // without the local objects. |
| 247 | func TestPushBranchReconcilesWithoutTheRepository(t *testing.T) { |
| 248 | r := newBranchRig(t) |
| 249 | const branch = "equipo/tk5-gone" |
| 250 | b := r.push(branch) |
| 251 | b.Key = "tk5/s1/1" |
| 252 | if err := PushBranch(context.Background(), b); err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | rec, _, _ := b.load() |
| 256 | rec.Phase = "push_started" |
| 257 | b.save(rec) |
| 258 | if err := os.RemoveAll(r.gitdir); err != nil { |
| 259 | t.Fatal(err) |
| 260 | } |
| 261 | b.Confirm = func(context.Context) error { return errors.New("no push expected") } |
| 262 | if err := PushBranch(context.Background(), b); err != nil { |
| 263 | t.Fatalf("reconcile without the repository: %v", err) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // A repository with no commits: the candidate is its initial commit (no |
| 268 | // base, no parent) and the push creates the task branch only; the |
| 269 | // default branch stays unborn. Without a base only a root commit is |
| 270 | // pushed: a commit with a parent always has a base to name. |
| 271 | func TestPushBranchInitialCommit(t *testing.T) { |
| 272 | ctx := context.Background() |
| 273 | dir := t.TempDir() |
| 274 | r := &branchRig{remote: filepath.Join(dir, "remote.git"), gitdir: filepath.Join(dir, "private.git"), root: filepath.Join(dir, "records")} |
| 275 | gitT(t, dir, nil, "init", "-q", "--bare", "-b", "main", r.remote) |
| 276 | gitT(t, dir, nil, "init", "-q", "--bare", "-b", "main", r.gitdir) |
| 277 | work := filepath.Join(dir, "work") |
| 278 | os.MkdirAll(work, 0700) |
| 279 | env := []string{"GIT_DIR=" + r.gitdir, "GIT_WORK_TREE=" + work} |
| 280 | os.WriteFile(filepath.Join(work, "index.html"), []byte("<h1>Barber</h1>\n"), 0600) |
| 281 | gitT(t, work, env, "add", "-A") |
| 282 | tree := gitT(t, work, env, "write-tree") |
| 283 | root := gitT(t, work, env, "commit-tree", tree, "-m", "mockups") |
| 284 | child := gitT(t, work, env, "commit-tree", tree, "-p", root, "-m", "more") |
| 285 | const branch = "equipo/tk1-barber-mockups" |
| 286 | p := r.push(branch) |
| 287 | p.Base, p.Commit = "", child |
| 288 | if err := PushBranch(ctx, p); !errors.Is(err, ErrPushRefused) || r.head(t, branch) != "" { |
| 289 | t.Fatalf("a commit with a parent and no base: %v", err) |
| 290 | } |
| 291 | p = r.push(branch) |
| 292 | p.Base, p.Commit, p.Key = "", root, "tk1/s1/2" |
| 293 | if err := PushBranch(ctx, p); err != nil { |
| 294 | t.Fatal(err) |
| 295 | } |
| 296 | if got := r.head(t, branch); got != root || r.confirms != 1 { |
| 297 | t.Fatalf("remote %s confirms %d", got, r.confirms) |
| 298 | } |
| 299 | if got := r.head(t, "main"); got != "" { |
| 300 | t.Fatalf("main was created at %s", got) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | func TestCopyPushRequiresReviewedHead(t *testing.T) { |
| 305 | r := newBranchRig(t) |
| 306 | b := r.push("equipo/tk1-copy") |
| 307 | if err := PushBranch(context.Background(), b); err != nil { |
| 308 | t.Fatal(err) |
| 309 | } |
| 310 | b.Key = "tk1/copy/1" |
| 311 | b.Commit = r.commit2 |
| 312 | b.ExpectedHead = r.base |
| 313 | if err := PushBranch(context.Background(), b); !errors.Is(err, ErrPushConflict) { |
| 314 | t.Fatalf("published over unreviewed own head: %v", err) |
| 315 | } |
| 316 | } |
| 317 | |