package mac import ( "context" "errors" "os" "path/filepath" "testing" "time" ) // Whole-branch fix 1, #2: a run's end is proven only with its user-temp // cleanup finished in time. A cleanup that failed or is still pending // leaves the run unterminated (Gate.Run, process.Wait), and the pending // cleanup stays the attempt's own, so its own proof (Verify) finishes it. // TestGateCleanupFailureIsUnterminated: the check passes, its cleanup of // T fails (T cannot be listed when it is cleaned): not terminated; once T // can be listed again the attempt's proof cleans it. func TestGateCleanupFailureIsUnterminated(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-wb-1", time.Minute, `touch `+quote(filepath.Join(temp, "left"))) if res.Code != 0 || res.Terminated || !g.Turns.Pending() { t.Fatalf("a check whose cleanup failed: %+v pending=%v", res, g.Turns.Pending()) } g.QuietSimulators = nil if err := os.Chmod(temp, 0o700); err != nil { t.Fatal(err) } if !g.Verify(id) || exists(filepath.Join(temp, "left")) || g.Turns.Pending() { t.Fatal("the attempt's proof did not finish its cleanup") } } // TestWaitCleanupPendingIsUnproven: an Engineering launch (process.Wait) // whose cleanup outlasts its deadline is unproven; its pending cleanup // is still owned, and the attempt's proof finishes it. func TestWaitCleanupPendingIsUnproven(t *testing.T) { _, temp := userTempFixture(t) turns := NewTurns(filepath.Join(t.TempDir(), "lock")) // The shutdown runs until it is told to stop, at the cleanup's // deadline, and a moment past it. x := &Executor{Turns: turns, UserTemp: temp, Grace: 300 * time.Millisecond, Settle: time.Second, CleanLimit: 300 * time.Millisecond, QuietSimulators: func(ctx context.Context) { select { case <-ctx.Done(): case <-time.After(10 * time.Second): } time.Sleep(50 * 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-wb-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 cleanup is still pending: %v", err) } if !turns.Pending() { t.Fatal("the pending cleanup went") } u.Give() x.QuietSimulators = nil if !x.Verify(r.ID()) || exists(filepath.Join(temp, "late")) || turns.Pending() { t.Fatal("the attempt's proof did not finish its cleanup") } }