package forge import ( "context" "errors" "os" "os/exec" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/testci" ) func gitT(t *testing.T, dir string, env []string, args ...string) string { t.Helper() testci.Tools(t, "git", "python3") cmd := exec.Command("git", args...) cmd.Dir = dir 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...)...) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("git %v: %v %s", args, err, out) } return strings.TrimSpace(string(out)) } // branchRig is a remote with main, and the runner's private repository // holding a candidate commit on top of main. type branchRig struct { remote, gitdir, root, base, commit, commit2 string confirms int } func newBranchRig(t *testing.T) *branchRig { t.Helper() dir := t.TempDir() r := &branchRig{remote: filepath.Join(dir, "remote.git"), gitdir: filepath.Join(dir, "private.git"), root: filepath.Join(dir, "records")} seed := filepath.Join(dir, "seed") gitT(t, dir, nil, "init", "-q", "-b", "main", seed) os.WriteFile(filepath.Join(seed, "README.md"), []byte("teh panes\n"), 0600) gitT(t, seed, nil, "add", "-A") gitT(t, seed, nil, "commit", "-q", "-m", "seed") gitT(t, dir, nil, "clone", "-q", "--bare", seed, r.remote) gitT(t, dir, nil, "clone", "-q", "--bare", seed, r.gitdir) r.base = gitT(t, seed, nil, "rev-parse", "HEAD") work := filepath.Join(dir, "work") os.MkdirAll(work, 0700) env := []string{"GIT_DIR=" + r.gitdir, "GIT_WORK_TREE=" + work} gitT(t, work, env, "checkout", "-q", "-f", r.base, "--", ".") os.WriteFile(filepath.Join(work, "README.md"), []byte("the panes\n"), 0600) gitT(t, work, env, "add", "-A") tree := gitT(t, work, env, "write-tree") r.commit = gitT(t, work, env, "commit-tree", tree, "-p", r.base, "-m", "fix") r.commit2 = gitT(t, work, env, "commit-tree", tree, "-p", r.base, "-m", "another fix") return r } func (r *branchRig) push(branch string) BranchPush { 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"}, Remote: func(context.Context) (workspace.Remote, error) { return workspace.Remote{URL: r.remote}, nil }, Confirm: func(context.Context) error { r.confirms++; return nil }} } func (r *branchRig) head(t *testing.T, branch string) string { out, err := exec.Command("git", "--git-dir="+r.remote, "rev-parse", "--verify", "-q", "refs/heads/"+branch).Output() if err != nil { return "" } return strings.TrimSpace(string(out)) } func TestPushBranch(t *testing.T) { ctx := context.Background() r := newBranchRig(t) const branch = "equipo/tk1-fix-the-typo" if err := PushBranch(ctx, r.push(branch)); err != nil { t.Fatal(err) } if got := r.head(t, branch); got != r.commit || r.confirms != 1 { t.Fatalf("remote %s confirms %d", got, r.confirms) } if got := r.head(t, "main"); got != r.base { t.Fatalf("main moved to %s", got) } // Again for the same attempt: reconciled from the remote, no second push. if err := PushBranch(ctx, r.push(branch)); err != nil || r.confirms != 1 { t.Fatalf("repeat: %v confirms %d", err, r.confirms) } // The record binds the key to its commit. other := r.push(branch) other.Commit = r.commit2 if err := PushBranch(ctx, other); !errors.Is(err, ErrPushRefused) { t.Fatalf("another commit under the same key: %v", err) } } // TestPushBranchReconcilesACutPush: the runner died after the push // landed and before it recorded so. The next call reads the remote and // succeeds without pushing (the lease check would refuse it). func TestPushBranchReconcilesACutPush(t *testing.T) { ctx := context.Background() r := newBranchRig(t) const branch = "equipo/tk1-cut" b := r.push(branch) os.MkdirAll(r.root, 0700) if err := b.save(pushRecord{Key: b.Key, Repo: b.Repo, Branch: b.Branch, Commit: b.Commit, Phase: "push_started"}); err != nil { t.Fatal(err) } gitT(t, r.root, []string{"GIT_DIR=" + r.gitdir}, "push", "-q", r.remote, r.commit+":refs/heads/"+branch) b.Confirm = func(context.Context) error { return errors.New("no lease") } if err := PushBranch(ctx, b); err != nil { t.Fatalf("reconcile: %v", err) } rec, _, _ := b.load() if rec.Phase != "published" { t.Fatalf("record %+v", rec) } // A cut push that did not land is pushed again, under the lease. b2 := r.push("equipo/tk1-not-landed") b2.Key = "tk1/s1/2" if err := b2.save(pushRecord{Key: b2.Key, Repo: b2.Repo, Branch: b2.Branch, Commit: b2.Commit, Phase: "push_started"}); err != nil { t.Fatal(err) } if err := PushBranch(ctx, b2); err != nil || r.head(t, b2.Branch) != r.commit || r.confirms != 1 { t.Fatalf("re-push: %v confirms %d", err, r.confirms) } } func TestPushBranchRefuses(t *testing.T) { ctx := context.Background() r := newBranchRig(t) for name, mutate := range map[string]func(*BranchPush){ "default branch": func(b *BranchPush) { b.Branch, b.Default = "equipo/tk1-x", "equipo/tk1-x" }, "not a task branch": func(b *BranchPush) { b.Branch = "main" }, "another branch": func(b *BranchPush) { b.Branch = "feature/x" }, "not from the base": func(b *BranchPush) { b.Base, b.Commit = b.Commit, b.Base }, "another person": func(b *BranchPush) { b.Person = "mallory@example.com" }, "bad repository": func(b *BranchPush) { b.Repo = "amadan.net/../x" }, "unknown commit": func(b *BranchPush) { b.Commit = strings.Repeat("e", 40) }, "remote for another": func(b *BranchPush) { b.Remote = func(context.Context) (workspace.Remote, error) { return workspace.Remote{URL: "https://amadan.net/other/x", GitPassword: "p"}, nil } }, } { b := r.push("equipo/tk1-refused") b.Key = "refuse/" + name mutate(&b) if err := PushBranch(ctx, b); !errors.Is(err, ErrPushRefused) { t.Fatalf("%s: %v", name, err) } } if r.confirms != 0 || r.head(t, "equipo/tk1-refused") != "" { t.Fatalf("pushed: confirms %d", r.confirms) } // No lease, no push. b := r.push("equipo/tk1-no-lease") b.Confirm = func(context.Context) error { return errors.New("superseded") } if err := PushBranch(ctx, b); !errors.Is(err, ErrPushUncertain) || r.head(t, b.Branch) != "" { t.Fatalf("pushed without a lease: %v", err) } } // personCommit pushes a commit of the person's own onto branch in the // remote, on top of parent. func (r *branchRig) personCommit(t *testing.T, branch, parent string) string { t.Helper() tree := gitT(t, r.root, []string{"GIT_DIR=" + r.remote}, "rev-parse", parent+"^{tree}") sha := gitT(t, r.root, []string{"GIT_DIR=" + r.remote}, "commit-tree", tree, "-p", parent, "-m", "the person's own work") gitT(t, r.root, []string{"GIT_DIR=" + r.remote}, "update-ref", "refs/heads/"+branch, sha) return sha } // TestPushBranchNeverOverwritesOthers: the push landed but its answer // was lost; then the person advanced the branch. Reconciling finds a // head that is neither the expected predecessor nor the candidate: a // conflict, and the person's commit stays. func TestPushBranchNeverOverwritesOthers(t *testing.T) { ctx := context.Background() r := newBranchRig(t) const branch = "equipo/tk1-lost-answer" b := r.push(branch) if err := PushBranch(ctx, b); err != nil { t.Fatal(err) } // The answer was lost: the record still says the push started. rec, _, _ := b.load() rec.Phase = "push_started" if err := b.save(rec); err != nil { t.Fatal(err) } theirs := r.personCommit(t, branch, r.commit) if err := PushBranch(ctx, b); !errors.Is(err, ErrPushConflict) { t.Fatalf("reconcile over the person's commit: %v", err) } if got := r.head(t, branch); got != theirs { t.Fatalf("branch at %s, the person's commit was %s", got, theirs) } // A new attempt on a branch someone else moved does not force over it // either; one only Equipo pushed (an earlier attempt) it may replace. b2 := r.push(branch) b2.Key, b2.Commit = "tk1/s1/2", r.commit2 if err := PushBranch(ctx, b2); !errors.Is(err, ErrPushConflict) || r.head(t, branch) != theirs { t.Fatalf("attempt 2 over the person's commit: %v", err) } const other = "equipo/tk2-retry" first := r.push(other) first.Key = "tk2/s1/1" if err := PushBranch(ctx, first); err != nil { t.Fatal(err) } second := r.push(other) second.Key, second.Commit = "tk2/s1/2", r.commit2 if err := PushBranch(ctx, second); err != nil || r.head(t, other) != r.commit2 { t.Fatalf("attempt 2 over attempt 1: %v", err) } // A branch that appeared from elsewhere before the first push. const foreign = "equipo/tk3-foreign" r.personCommit(t, foreign, r.base) b3 := r.push(foreign) b3.Key = "tk3/s1/1" if err := PushBranch(ctx, b3); !errors.Is(err, ErrPushConflict) { t.Fatalf("over a branch Equipo never pushed: %v", err) } } // TestPushBranchLeaseHolds: the branch moves between reading its head // and pushing; the lease refuses the push and the person's commit stays. func TestPushBranchLeaseHolds(t *testing.T) { r := newBranchRig(t) const branch = "equipo/tk4-race" b := r.push(branch) b.Key = "tk4/s1/1" var theirs string b.beforePush = func() { theirs = r.personCommit(t, branch, r.base) } if err := PushBranch(context.Background(), b); !errors.Is(err, ErrPushConflict) || r.head(t, branch) != theirs { t.Fatalf("pushed over a branch that moved: %v (head %s, theirs %s)", err, r.head(t, branch), theirs) } } // TestPushBranchReconcilesWithoutTheRepository: the push landed and the // runner lost its private repository before the report was durable. A // recorded push that is already on the remote reconciles as published // without the local objects. func TestPushBranchReconcilesWithoutTheRepository(t *testing.T) { r := newBranchRig(t) const branch = "equipo/tk5-gone" b := r.push(branch) b.Key = "tk5/s1/1" if err := PushBranch(context.Background(), b); err != nil { t.Fatal(err) } rec, _, _ := b.load() rec.Phase = "push_started" b.save(rec) if err := os.RemoveAll(r.gitdir); err != nil { t.Fatal(err) } b.Confirm = func(context.Context) error { return errors.New("no push expected") } if err := PushBranch(context.Background(), b); err != nil { t.Fatalf("reconcile without the repository: %v", err) } } // A repository with no commits: the candidate is its initial commit (no // base, no parent) and the push creates the task branch only; the // default branch stays unborn. Without a base only a root commit is // pushed: a commit with a parent always has a base to name. func TestPushBranchInitialCommit(t *testing.T) { ctx := context.Background() dir := t.TempDir() r := &branchRig{remote: filepath.Join(dir, "remote.git"), gitdir: filepath.Join(dir, "private.git"), root: filepath.Join(dir, "records")} gitT(t, dir, nil, "init", "-q", "--bare", "-b", "main", r.remote) gitT(t, dir, nil, "init", "-q", "--bare", "-b", "main", r.gitdir) work := filepath.Join(dir, "work") os.MkdirAll(work, 0700) env := []string{"GIT_DIR=" + r.gitdir, "GIT_WORK_TREE=" + work} os.WriteFile(filepath.Join(work, "index.html"), []byte("

Barber

\n"), 0600) gitT(t, work, env, "add", "-A") tree := gitT(t, work, env, "write-tree") root := gitT(t, work, env, "commit-tree", tree, "-m", "mockups") child := gitT(t, work, env, "commit-tree", tree, "-p", root, "-m", "more") const branch = "equipo/tk1-barber-mockups" p := r.push(branch) p.Base, p.Commit = "", child if err := PushBranch(ctx, p); !errors.Is(err, ErrPushRefused) || r.head(t, branch) != "" { t.Fatalf("a commit with a parent and no base: %v", err) } p = r.push(branch) p.Base, p.Commit, p.Key = "", root, "tk1/s1/2" if err := PushBranch(ctx, p); err != nil { t.Fatal(err) } if got := r.head(t, branch); got != root || r.confirms != 1 { t.Fatalf("remote %s confirms %d", got, r.confirms) } if got := r.head(t, "main"); got != "" { t.Fatalf("main was created at %s", got) } } func TestCopyPushRequiresReviewedHead(t *testing.T) { r := newBranchRig(t) b := r.push("equipo/tk1-copy") if err := PushBranch(context.Background(), b); err != nil { t.Fatal(err) } b.Key = "tk1/copy/1" b.Commit = r.commit2 b.ExpectedHead = r.base if err := PushBranch(context.Background(), b); !errors.Is(err, ErrPushConflict) { t.Fatalf("published over unreviewed own head: %v", err) } }