| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // Whole-branch fix 1, #2: a run's end is proven only with its user-temp |
| 13 | // cleanup finished in time. A cleanup that failed or is still pending |
| 14 | // leaves the run unterminated (Gate.Run, process.Wait), and the pending |
| 15 | // cleanup stays the attempt's own, so its own proof (Verify) finishes it. |
| 16 | |
| 17 | // TestGateCleanupFailureIsUnterminated: the check passes, its cleanup of |
| 18 | // T fails (T cannot be listed when it is cleaned): not terminated; once T |
| 19 | // can be listed again the attempt's proof cleans it. |
| 20 | func TestGateCleanupFailureIsUnterminated(t *testing.T) { |
| 21 | g, dir := gateFixture(t, true) |
| 22 | _, temp := userTempFixture(t) |
| 23 | g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock")) |
| 24 | g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) } |
| 25 | t.Cleanup(func() { _ = os.Chmod(temp, 0o700) }) |
| 26 | res, id := run(g, dir, "checks-wb-1", time.Minute, `touch `+quote(filepath.Join(temp, "left"))) |
| 27 | if res.Code != 0 || res.Terminated || !g.Turns.Pending() { |
| 28 | t.Fatalf("a check whose cleanup failed: %+v pending=%v", res, g.Turns.Pending()) |
| 29 | } |
| 30 | g.QuietSimulators = nil |
| 31 | if err := os.Chmod(temp, 0o700); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if !g.Verify(id) || exists(filepath.Join(temp, "left")) || g.Turns.Pending() { |
| 35 | t.Fatal("the attempt's proof did not finish its cleanup") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // TestWaitCleanupPendingIsUnproven: an Engineering launch (process.Wait) |
| 40 | // whose cleanup outlasts its deadline is unproven; its pending cleanup |
| 41 | // is still owned, and the attempt's proof finishes it. |
| 42 | func TestWaitCleanupPendingIsUnproven(t *testing.T) { |
| 43 | _, temp := userTempFixture(t) |
| 44 | turns := NewTurns(filepath.Join(t.TempDir(), "lock")) |
| 45 | // The shutdown runs until it is told to stop, at the cleanup's |
| 46 | // deadline, and a moment past it. |
| 47 | x := &Executor{Turns: turns, UserTemp: temp, Grace: 300 * time.Millisecond, Settle: time.Second, CleanLimit: 300 * time.Millisecond, |
| 48 | QuietSimulators: func(ctx context.Context) { |
| 49 | select { |
| 50 | case <-ctx.Done(): |
| 51 | case <-time.After(10 * time.Second): |
| 52 | } |
| 53 | time.Sleep(50 * time.Millisecond) |
| 54 | }} |
| 55 | u, err := turns.Take(context.Background(), OpEngineering) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | 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-wb-1", func(string) error { return nil }) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | p := &process{run: r, stop: func() bool { return true }} |
| 64 | if _, err := p.Wait(context.Background()); !errors.Is(err, ErrUnproven) { |
| 65 | t.Fatalf("a launch whose cleanup is still pending: %v", err) |
| 66 | } |
| 67 | if !turns.Pending() { |
| 68 | t.Fatal("the pending cleanup went") |
| 69 | } |
| 70 | u.Give() |
| 71 | x.QuietSimulators = nil |
| 72 | if !x.Verify(r.ID()) || exists(filepath.Join(temp, "late")) || turns.Pending() { |
| 73 | t.Fatal("the attempt's proof did not finish its cleanup") |
| 74 | } |
| 75 | } |
| 76 | |