//go:build linux package thin import ( "context" "os" "path/filepath" "strconv" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/runner/mac/proc" "amadan.net/oficina/equipo/internal/runner/workspace" ) // #2 (Astra round 1): the worker's git (index-pack, checkout) is the // runner's own helper. A probe's turn that ends while the worker prepares // a step sweeps the runner user, and must leave that git running. The // table is scoped to this test's markers (a shared Linux machine). func TestProbeEndSparesWorkerGit(t *testing.T) { dir := t.TempDir() git, pidFile := filepath.Join(dir, "git"), filepath.Join(dir, "pid") script := "#!/bin/sh\necho $$ >> " + pidFile + "\n[ -n \"$GIT_INDEX_FILE\" ] && touch \"$GIT_INDEX_FILE\"\nsleep 1\necho ok\n" if err := os.WriteFile(git, []byte(script), 0700); err != nil { t.Fatal(err) } marker := "checks/own-" + strconv.Itoa(os.Getpid()) w := &Worker{Engine: &Engineer{O: ClaudeOptions{Runner: runner.Config{GitPath: git}}}, marker: marker} turns := mac.NewTurns("").Sweep(proc.Scoped("EQUIPO_ATTEMPT="+marker, "EQUIPO_ATTEMPT="+marker+"-probe")) for name, prep := range map[string]func(context.Context) error{ "index-pack": func(ctx context.Context) error { out, err := w.git(ctx, strings.NewReader("PACK"), "index-pack", "--stdin") if err == nil && out != "ok" { t.Errorf("git said %q", out) } return err }, "checkout": func(ctx context.Context) error { return w.checkout(ctx, workspace.TaskTree{Dir: filepath.Join(t.TempDir(), "tree"), GitDir: dir}, "0123456789abcdef0123456789abcdef01234567") }, } { t.Run(name, func(t *testing.T) { _ = os.Remove(pidFile) u, err := turns.Take(context.Background(), mac.OpProbe) if err != nil { t.Fatal(err) } defer u.Give() x := &mac.Executor{Turns: turns, Grace: 50 * time.Millisecond, Settle: 3 * time.Second, Poll: 20 * time.Millisecond} 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 }) if err != nil { t.Fatal(err) } got := make(chan error, 1) go func() { got <- prep(context.Background()) }() for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { if raw, _ := os.ReadFile(pidFile); len(raw) > 0 { break } } time.Sleep(100 * time.Millisecond) if !run.Stop() { t.Fatal("the probe's end not proven") } if err := <-got; err != nil { t.Fatalf("the worker's git was swept: %v", err) } }) } }