package mac import ( "context" "errors" "os" "os/exec" "path/filepath" "syscall" "testing" "time" "amadan.net/oficina/equipo/internal/runner/mac/proc" ) // Whole-branch fix 2, #1: the cleanup has its own bound (CleanLimit), // checked after its last act, the pending record's removal included. A // removal that returns late leaves the run unterminated, and its turn's // record stays as the evidence a later Verify proves from. func TestCleanupLateRecordRemovalIsUnproven(t *testing.T) { _, temp := userTempFixture(t) turns := NewTurns(filepath.Join(t.TempDir(), "lock")) x := &Executor{Turns: turns, UserTemp: temp, Grace: 300 * time.Millisecond, Settle: time.Second, CleanLimit: 300 * time.Millisecond} slow := true turns.beforeDrop = func() { if slow { time.Sleep(500 * time.Millisecond) } } u, err := turns.Take(context.Background(), OpEngineering) if err != nil { t.Fatal(err) } r, err := x.Start(u.Context(context.Background()), Command{Argv: []string{"/bin/sh", "-c", "touch " + quote(filepath.Join(temp, "late"))}, Env: []string{"PATH=/usr/bin:/bin"}, Dir: t.TempDir()}, "eng-wb2-1", func(string) error { return nil }) if err != nil { t.Fatal(err) } p := &process{run: r, stop: func() bool { return true }} if _, err := p.Wait(context.Background()); !errors.Is(err, ErrUnproven) { t.Fatalf("a launch whose record removal came late: %v", err) } u.Give() if _, ok, _ := turns.loadTurn(); !ok { t.Fatal("the turn's record went with an unproven cleanup") } slow = false if !x.Verify(r.ID()) || turns.Pending() { t.Fatal("the attempt's proof did not finish") } } // Whole-branch fix 2, #1: Verify's cleanup has the same bound, checked // after the pending record's removal too. func TestVerifyLateRecordRemovalIsUnproven(t *testing.T) { g, dir := gateFixture(t, true) _, temp := userTempFixture(t) g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock")) g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) } t.Cleanup(func() { _ = os.Chmod(temp, 0o700) }) res, id := run(g, dir, "checks-wb2-1", time.Minute, `touch `+quote(filepath.Join(temp, "left"))) if res.Terminated { t.Fatalf("%+v", res) } g.QuietSimulators = nil if err := os.Chmod(temp, 0o700); err != nil { t.Fatal(err) } x := g.executor() x.CleanLimit = 300 * time.Millisecond slow := true g.Turns.beforeDrop = func() { if slow { time.Sleep(500 * time.Millisecond) } } if x.Verify(id) { t.Fatal("proven with its record removed past the cleanup's bound") } slow = false if !x.Verify(id) { t.Fatal("a later proof did not finish") } } // Whole-branch fix 2, #2: a cleanup that failed keeps its turn's record // and its boot. After a reboot the attempt clears boot-qualified before // any old process id is looked at: its group number, now another // long-lived process's, is never checked or signalled, and the // completion is kept for every consumer. func TestFailedCleanupClearsAfterAReboot(t *testing.T) { g, dir := gateFixture(t, true) _, temp := userTempFixture(t) g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock")) boot := proc.Boot{ID: "boot-1", Time: time.Now().Add(-time.Hour)} g.Turns.boot = func() (proc.Boot, error) { return boot, nil } g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) } t.Cleanup(func() { _ = os.Chmod(temp, 0o700) }) res, id := run(g, dir, "checks-wb2-2", time.Minute, `touch `+quote(filepath.Join(temp, "left"))) if res.Terminated || !g.Turns.Pending() { t.Fatalf("%+v pending=%v", res, g.Turns.Pending()) } if _, ok, _ := g.Turns.loadTurn(); !ok { t.Fatal("the turn's record went with the failed cleanup") } // The reboot; the old group number now leads a long-lived process. g.QuietSimulators = nil if err := os.Chmod(temp, 0o700); err != nil { t.Fatal(err) } boot = proc.Boot{ID: "boot-2", Time: time.Now()} other := exec.Command("/bin/sleep", "60") other.SysProcAttr = &syscall.SysProcAttr{Setsid: true} if err := other.Start(); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = other.Process.Kill(); _ = other.Wait() }) _, attempt, _ := proc.ParseRecord(id) reused := RecordID(other.Process.Pid, attempt) if !g.Verify(reused) || g.Turns.Pending() || exists(filepath.Join(temp, "left")) { t.Fatal("a reboot did not clear the attempt whose cleanup failed") } if err := other.Process.Signal(syscall.Signal(0)); err != nil { t.Fatalf("the process now holding the old group number was signalled: %v", err) } if !g.Verify(reused) { t.Fatal("the reboot's completion was not kept for the next consumer") } } // Whole-branch fix 2 (Luna's check of the detail parameters): P6 says // how many processes appeared, never their names (a name can be a path // or anything a process chose). func TestNewProcessesCounted(t *testing.T) { before := map[string]int{"launchd": 1, "Finder": 1} after := map[string]int{"launchd": 1, "Finder": 2, "sk-ant-oat01-forged": 1, "xcodebuild": 1} if n := newNames(before, after); n != 3 { t.Fatalf("%d new", n) } }