Files
pipelines/internal/server/run_worktree_test.go
T
alan 532ba48927 Run every pipeline run in its own worktree
A fresh run started from a repository's main checkout gets its own
worktree and branch next to the repository, so runs never share a tree
or see the operator's uncommitted work, and several can run at once.
Retries and runs started in a linked worktree keep their directory. A
clean, landed run tree is removed after success; anything else is kept.

The delivery gate commits what the land stage left uncommitted in a run
worktree, and no longer requires the operator's main checkout to be
clean: Git's fast-forward refuses safely when a local change conflicts.
2026-11-10 16:06:40 +00:00

194 lines
6.9 KiB
Go

package server
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func newRunWorktreeRepo(t *testing.T) (root, main string) {
t.Helper()
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
main = filepath.Join(root, "app")
runGit(t, root, "init", "-b", "main", main)
runGit(t, main, "config", "user.email", "test@example.invalid")
runGit(t, main, "config", "user.name", "test")
if err := os.MkdirAll(filepath.Join(main, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(main, "sub", "README"), []byte("base\n"), 0o600); err != nil {
t.Fatal(err)
}
runGit(t, main, "add", ".")
runGit(t, main, "commit", "-m", "base")
return root, main
}
func TestIsolateRunWorkspaceGivesAMainCheckoutRunItsOwnWorktree(t *testing.T) {
root, main := newRunWorktreeRepo(t)
// The operator's uncommitted work stays behind.
if err := os.WriteFile(filepath.Join(main, "draft"), []byte("mine\n"), 0o600); err != nil {
t.Fatal(err)
}
tree, err := isolateRunWorkspace(context.Background(), main, "Fix the thing!")
if err != nil {
t.Fatal(err)
}
if filepath.Dir(tree) != filepath.Join(root, "app-runs") {
t.Fatalf("tree %s is not under app-runs", tree)
}
if !strings.Contains(filepath.Base(tree), "fix-the-thing") {
t.Fatalf("tree name %s lacks the title", tree)
}
if _, err := os.Stat(filepath.Join(tree, "draft")); !os.IsNotExist(err) {
t.Fatal("the operator's uncommitted file reached the run worktree")
}
branch, err := gitOutput(tree, "symbolic-ref", "--short", "HEAD")
if err != nil || !strings.HasPrefix(branch, runWorktreeBranchPrefix) {
t.Fatalf("branch %q, %v", branch, err)
}
if !isRunWorktree(tree) {
t.Fatal("the new tree is not recognized as a run worktree")
}
// A subdirectory maps to the same subdirectory; a second run gets another tree.
sub, err := isolateRunWorkspace(context.Background(), filepath.Join(main, "sub"), "")
if err != nil {
t.Fatal(err)
}
if filepath.Base(sub) != "sub" || filepath.Dir(filepath.Dir(sub)) != filepath.Join(root, "app-runs") || filepath.Dir(sub) == tree {
t.Fatalf("subdirectory run went to %s", sub)
}
// A linked worktree (a retry, or an item's own tree) and a plain directory
// are used as they are.
if same, err := isolateRunWorkspace(context.Background(), tree, "x"); err != nil || same != tree {
t.Fatalf("linked worktree became %s, %v", same, err)
}
plain := t.TempDir()
if same, err := isolateRunWorkspace(context.Background(), plain, "x"); err != nil || same != plain {
t.Fatalf("plain directory became %s, %v", same, err)
}
if isRunWorktree(main) || isRunWorktree(plain) {
t.Fatal("a non-run tree was recognized as a run worktree")
}
}
func TestRemoveLandedRunWorktreeKeepsUnlandedWork(t *testing.T) {
_, main := newRunWorktreeRepo(t)
tree, err := isolateRunWorkspace(context.Background(), main, "work")
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(tree, "change"), []byte("x\n"), 0o600); err != nil {
t.Fatal(err)
}
if removeLandedRunWorktree(tree) {
t.Fatal("removed a tree with uncommitted work")
}
runGit(t, tree, "add", "change")
runGit(t, tree, "commit", "-m", "change")
if removeLandedRunWorktree(tree) {
t.Fatal("removed a tree whose commit is not on main")
}
branch, _ := gitOutput(tree, "symbolic-ref", "--short", "HEAD")
runGit(t, main, "merge", "--ff-only", branch)
if !removeLandedRunWorktree(tree) {
t.Fatal("kept a clean, landed run tree")
}
if _, err := os.Stat(tree); !os.IsNotExist(err) {
t.Fatal("tree still on disk")
}
if _, err := gitOutput(main, "rev-parse", "--verify", "refs/heads/"+branch); err == nil {
t.Fatal("landed branch was not deleted")
}
// An operator's own linked worktree is never removed.
own := filepath.Join(filepath.Dir(main), "own")
runGit(t, main, "worktree", "add", "-b", "own", own)
if removeLandedRunWorktree(own) {
t.Fatal("removed an operator worktree")
}
}
func TestDeliveryCommitsLeftoversAndIgnoresAnUncleanMainCheckout(t *testing.T) {
_, main := newRunWorktreeRepo(t)
tree, err := isolateRunWorkspace(context.Background(), main, "deliver")
if err != nil {
t.Fatal(err)
}
access, err := landingAccess(tree)
if err != nil {
t.Fatal(err)
}
// The land stage committed one file and left another; the operator has
// unrelated untracked and modified files in the main checkout.
if err := os.WriteFile(filepath.Join(tree, "committed"), []byte("a\n"), 0o600); err != nil {
t.Fatal(err)
}
runGit(t, tree, "add", "committed")
runGit(t, tree, "commit", "-m", "part")
if err := os.WriteFile(filepath.Join(tree, "left"), []byte("b\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(main, "scratch.md"), []byte("notes\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(main, "sub", "README"), []byte("edited\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := verifyDelivery(context.Background(), tree, access, []string{"test -f committed && test -f left"}, "Deliver\n\nPipelines run 1."); err != nil {
t.Fatal(err)
}
for _, name := range []string{"committed", "left"} {
if _, err := os.Stat(filepath.Join(main, name)); err != nil {
t.Fatalf("%s did not land in the main checkout: %v", name, err)
}
}
if got, _ := os.ReadFile(filepath.Join(main, "sub", "README")); string(got) != "edited\n" {
t.Fatal("the operator's edit was overwritten")
}
if subject, _ := gitOutput(main, "log", "-1", "--format=%s"); subject != "Deliver" {
t.Fatalf("leftover commit subject %q", subject)
}
// A conflicting local change in the main checkout refuses the
// fast-forward and changes nothing there.
if err := os.WriteFile(filepath.Join(tree, "sub", "README"), []byte("item\n"), 0o600); err != nil {
t.Fatal(err)
}
before, _ := gitOutput(main, "rev-parse", "HEAD")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if _, err := verifyDelivery(ctx, tree, access, []string{"true"}, "Conflict"); err == nil || !strings.Contains(err.Error(), "fast-forward") {
t.Fatalf("conflicting fast-forward: %v", err)
}
if after, _ := gitOutput(main, "rev-parse", "HEAD"); after != before {
t.Fatal("main moved despite the refused fast-forward")
}
if got, _ := os.ReadFile(filepath.Join(main, "sub", "README")); string(got) != "edited\n" {
t.Fatal("the operator's edit was overwritten")
}
// Outside a run worktree there is no message, so nothing is committed
// for the stage and an unclean item tree is still refused.
other := filepath.Join(filepath.Dir(main), "other")
runGit(t, main, "worktree", "add", "-b", "other", other)
if err := os.WriteFile(filepath.Join(other, "stray"), []byte("x\n"), 0o600); err != nil {
t.Fatal(err)
}
otherAccess, err := landingAccess(other)
if err != nil {
t.Fatal(err)
}
if _, err := verifyDelivery(context.Background(), other, otherAccess, []string{"true"}, ""); err == nil || !strings.Contains(err.Error(), "not clean") {
t.Fatalf("unclean operator tree: %v", err)
}
}