| 1 | //go:build linux |
| 2 | |
| 3 | package thin |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/runner" |
| 15 | "amadan.net/oficina/equipo/internal/runner/mac" |
| 16 | "amadan.net/oficina/equipo/internal/runner/mac/proc" |
| 17 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 18 | ) |
| 19 | |
| 20 | // #2 (Astra round 1): the worker's git (index-pack, checkout) is the |
| 21 | // runner's own helper. A probe's turn that ends while the worker prepares |
| 22 | // a step sweeps the runner user, and must leave that git running. The |
| 23 | // table is scoped to this test's markers (a shared Linux machine). |
| 24 | func TestProbeEndSparesWorkerGit(t *testing.T) { |
| 25 | dir := t.TempDir() |
| 26 | git, pidFile := filepath.Join(dir, "git"), filepath.Join(dir, "pid") |
| 27 | script := "#!/bin/sh\necho $$ >> " + pidFile + "\n[ -n \"$GIT_INDEX_FILE\" ] && touch \"$GIT_INDEX_FILE\"\nsleep 1\necho ok\n" |
| 28 | if err := os.WriteFile(git, []byte(script), 0700); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | marker := "checks/own-" + strconv.Itoa(os.Getpid()) |
| 32 | w := &Worker{Engine: &Engineer{O: ClaudeOptions{Runner: runner.Config{GitPath: git}}}, marker: marker} |
| 33 | turns := mac.NewTurns("").Sweep(proc.Scoped("EQUIPO_ATTEMPT="+marker, "EQUIPO_ATTEMPT="+marker+"-probe")) |
| 34 | for name, prep := range map[string]func(context.Context) error{ |
| 35 | "index-pack": func(ctx context.Context) error { |
| 36 | out, err := w.git(ctx, strings.NewReader("PACK"), "index-pack", "--stdin") |
| 37 | if err == nil && out != "ok" { |
| 38 | t.Errorf("git said %q", out) |
| 39 | } |
| 40 | return err |
| 41 | }, |
| 42 | "checkout": func(ctx context.Context) error { |
| 43 | return w.checkout(ctx, workspace.TaskTree{Dir: filepath.Join(t.TempDir(), "tree"), GitDir: dir}, "0123456789abcdef0123456789abcdef01234567") |
| 44 | }, |
| 45 | } { |
| 46 | t.Run(name, func(t *testing.T) { |
| 47 | _ = os.Remove(pidFile) |
| 48 | u, err := turns.Take(context.Background(), mac.OpProbe) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | defer u.Give() |
| 53 | x := &mac.Executor{Turns: turns, Grace: 50 * time.Millisecond, Settle: 3 * time.Second, Poll: 20 * time.Millisecond} |
| 54 | run, err := x.Start(u.Context(context.Background()), mac.Command{Argv: []string{"/bin/true"}, Env: []string{"PATH=/usr/bin:/bin", "EQUIPO_ATTEMPT=" + marker + "-probe"}}, marker+"-probe", func(string) error { return nil }) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | got := make(chan error, 1) |
| 59 | go func() { got <- prep(context.Background()) }() |
| 60 | for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { |
| 61 | if raw, _ := os.ReadFile(pidFile); len(raw) > 0 { |
| 62 | break |
| 63 | } |
| 64 | } |
| 65 | time.Sleep(100 * time.Millisecond) |
| 66 | if !run.Stop() { |
| 67 | t.Fatal("the probe's end not proven") |
| 68 | } |
| 69 | if err := <-got; err != nil { |
| 70 | t.Fatalf("the worker's git was swept: %v", err) |
| 71 | } |
| 72 | }) |
| 73 | } |
| 74 | } |
| 75 | |