oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package mac
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "slices"
9 "sync/atomic"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/runner/mac/proc"
14)
15
16// The follow-up to task 9: the two clearances of a turn whose end could
17// not be proven (its record incomplete, or what it owes). Synthetic
18// tables, rows made up 2026-11-29 in the darwin table's shape (see
19// sweep_test.go).
20
21var turnStart = time.Unix(1_800_000_000, 0)
22
23// clearRig is a turns over tb (its table when the turn began is
24// turnBegan's), with a boot that reads as boot.
25func clearRig(t *testing.T, tb Procs, boot proc.Boot, bootErr error) (*Turns, *Executor, *Own, []ProcID) {
26 t.Helper()
27 list, err := tb.List(time.Now().Add(time.Second), nil)
28 if err != nil {
29 t.Fatal(err)
30 }
31 var before []ProcID
32 for _, q := range list {
33 before = append(before, q.ID)
34 }
35 own := &Own{}
36 turns := NewTurns(filepath.Join(t.TempDir(), "lock")).Sweep(tb)
37 turns.own, turns.Settle = own, 300*time.Millisecond
38 turns.boot = func() (proc.Boot, error) { return boot, bootErr }
39 return turns, &Executor{Turns: turns, Settle: 300 * time.Millisecond}, own, before
40}
41
42// owed is an incomplete turn's obligation, begun at turnStart in boot-1,
43// in which nothing ran outside a sandbox.
44func owed(before []ProcID) proc.Obligation {
45 return proc.Obligation{Markers: []string{"attempt-c"}, Incomplete: true, Began: turnStart, BootID: "boot-1", Before: before, FlagsKnown: true}
46}
47
48var (
49 sameBoot = proc.Boot{ID: "boot-1", Time: turnStart.Add(-time.Hour)}
50 laterBoot = proc.Boot{ID: "boot-2", Time: turnStart.Add(time.Hour)}
51)
52
53// A reboot after the turn began proves it ended: an incomplete record
54// from an earlier boot is settled without a sweep (what runs now is this
55// boot's), what it owes is cleared, and Retry is allowed. The same for
56// an obligation already owed.
57func TestRebootClearsAnIncompleteTurn(t *testing.T) {
58 me := os.Getuid()
59 for _, viaRecord := range []bool{true, false} {
60 tb := parseTable(t, turnBegan)
61 turns, x, _, before := clearRig(t, tb, laterBoot, nil)
62 // A process of this boot, which no settling of the old turn may touch.
63 tb.add(row(98000, 1, 98000, 98000, 5000, me, ""), "/Users/paulequipo/w/now")
64 tb.mark(&tb.unkillable, 98000)
65 if viaRecord {
66 if err := turns.saveTurn(turnRecord{ID: "old", Op: OpEngineering, Runner: selfID(), Sweep: true, Before: before, Markers: []string{"attempt-c"}, Began: turnStart, BootID: "boot-1"}); err != nil {
67 t.Fatal(err)
68 }
69 } else if err := turns.debts.Owe(owed(before)); err != nil {
70 t.Fatal(err)
71 }
72 if !x.Verify(RecordID(0, "attempt-c")) {
73 t.Fatalf("record %v: not proven over after a reboot", viaRecord)
74 }
75 if code, _ := turns.Unproven(); code != "" || turns.Incomplete() {
76 t.Fatalf("record %v: still said unproven: %q", viaRecord, code)
77 }
78 if slices.Contains(tb.signalled(), 98000) {
79 t.Fatalf("record %v: settling an earlier boot's turn signalled this boot's process", viaRecord)
80 }
81 }
82}
83
84// No reboot after the turn began (the same boot, even if the clock moved
85// past the turn's start; or a boot read before it), or a boot that
86// cannot be read: unproven.
87func TestNoRebootKeepsItUnproven(t *testing.T) {
88 me := os.Getuid()
89 for name, c := range map[string]struct {
90 boot proc.Boot
91 err error
92 }{
93 "boot before the turn": {proc.Boot{ID: "boot-2", Time: turnStart.Add(-time.Hour)}, nil},
94 "same boot, the clock moved": {proc.Boot{ID: "boot-1", Time: turnStart.Add(time.Hour)}, nil},
95 // A read that half failed: whatever it returned proves nothing.
96 "boot unreadable": {laterBoot, errors.New("sysctl failed")},
97 } {
98 tb := parseTable(t, turnBegan)
99 turns, x, _, before := clearRig(t, tb, c.boot, c.err)
100 // Something unaccounted, so the idle read cannot clear it either.
101 tb.add(row(98010, 1, 98010, 98010, 5000, me, ""), "/Users/paulequipo/w/left")
102 if err := turns.debts.Owe(owed(before)); err != nil {
103 t.Fatal(err)
104 }
105 if x.Verify(RecordID(0, "attempt-c")) || !turns.Incomplete() {
106 t.Fatalf("%s: proven over", name)
107 }
108 }
109}
110
111// With no turn running, a whole read of the runner user's processes that
112// finds only the runner, its own helpers and processes from before the
113// turn proves the turn's processes gone: proven, Retry allowed.
114func TestIdleReadClears(t *testing.T) {
115 me, self := os.Getuid(), os.Getpid()
116 tb := parseTable(t, turnBegan)
117 turns, x, own, before := clearRig(t, tb, sameBoot, nil)
118 tb.add(row(98100, self, self, self, 6000, me, ""), "/usr/bin/xcrun")
119 own.Add(ProcID{PID: 98100, Start: 6000})
120 if err := turns.debts.Owe(owed(before)); err != nil {
121 t.Fatal(err)
122 }
123 if !x.Verify(RecordID(0, "attempt-c")) {
124 t.Fatal("a clean idle read did not prove it over")
125 }
126 if code, _ := turns.Unproven(); code != "" {
127 t.Fatalf("still said %q", code)
128 }
129}
130
131// Processes of the runner user no record accounts for keep it unproven,
132// and the Machines page gets their count, nothing more.
133func TestIdleReadCountsStrays(t *testing.T) {
134 me := os.Getuid()
135 tb := parseTable(t, turnBegan)
136 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
137 tb.add(row(98200, 1, 98200, 98200, 5000, me, ""), "/Users/paulequipo/w/a")
138 tb.add(row(98201, 1, 98201, 98201, 5100, me, ""), "/Users/paulequipo/w/b")
139 if err := turns.debts.Owe(owed(before)); err != nil {
140 t.Fatal(err)
141 }
142 if x.Verify(RecordID(0, "attempt-c")) {
143 t.Fatal("proven with unaccounted processes running")
144 }
145 if code, n := turns.Unproven(); code != "processes_left" || n != 2 {
146 t.Fatalf("said %q, %d", code, n)
147 }
148 if len(tb.signalled()) != 0 {
149 t.Fatalf("an idle read signalled: %v", tb.signalled())
150 }
151}
152
153// What the turn owes must be gone too, for every user: a group it
154// recorded, still alive under another user, keeps it unproven.
155func TestIdleReadNeedsObligationsGone(t *testing.T) {
156 tb := parseTable(t, turnBegan)
157 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
158 tb.add(row(98301, 1, 98300, 98301, 7000, 501, ""), "/bin/zsh")
159 o := owed(before)
160 o.Groups = proc.Span{98300: 6000}
161 if err := turns.debts.Owe(o); err != nil {
162 t.Fatal(err)
163 }
164 if x.Verify(RecordID(0, "attempt-c")) {
165 t.Fatal("proven with a recorded group alive under another user")
166 }
167}
168
169// failOnce fails the List call numbered at (counting from 1 once armed).
170type failOnce struct {
171 *table
172 n, at atomic.Int32
173}
174
175func (f *failOnce) List(deadline time.Time, markers []string) ([]ProcInfo, error) {
176 if at := f.at.Load(); at > 0 && f.n.Add(1) == at {
177 return nil, errors.New("sysctl failed")
178 }
179 return f.table.List(deadline, markers)
180}
181
182// A read that fails proves nothing, even if the next one is clean.
183func TestIdleReadFailedKeepsItUnproven(t *testing.T) {
184 tb := &failOnce{table: parseTable(t, turnBegan)}
185 turns, x, _, before := clearRig(t, tb, sameBoot, nil)
186 // The recovery turn's own read is the first; the idle reads follow.
187 tb.at.Store(2)
188 if err := turns.debts.Owe(owed(before)); err != nil {
189 t.Fatal(err)
190 }
191 if x.Verify(RecordID(0, "attempt-c")) {
192 t.Fatal("proven on a failed read")
193 }
194}
195
196// Identities are read again before the clearance is accepted: a helper's
197// pid taken by another process between the reads keeps it unproven.
198func TestIdleReadRechecksBeforeAccepting(t *testing.T) {
199 me, self := os.Getuid(), os.Getpid()
200 tb := parseTable(t, turnBegan)
201 turns, x, own, before := clearRig(t, tb, sameBoot, nil)
202 tb.add(row(98400, self, self, self, 6000, me, ""), "/usr/bin/xcrun")
203 own.Add(ProcID{PID: 98400, Start: 6000})
204 if err := turns.debts.Owe(owed(before)); err != nil {
205 t.Fatal(err)
206 }
207 // The recovery turn's own read is the first, the first idle read the
208 // second: right after it, the pid changes hands.
209 reads := 0
210 tb.afterList = func(tb *table, _ int, _ []string) {
211 if reads++; reads == 2 {
212 // The helper exits; its pid is reused at once by a process
213 // that is not the runner's.
214 tb.procs[98400] = row(98400, 1, 98400, 98400, 6500, me, "")
215 }
216 }
217 if x.Verify(RecordID(0, "attempt-c")) {
218 t.Fatal("proven though a pid changed hands between the reads")
219 }
220}
221
222// The idle clearance runs only holding the Mac's turn (no turn, a given
223// one, another Turns's): it refuses and clears nothing.
224func TestIdleClearanceOnlyUnderTheTurn(t *testing.T) {
225 tb := parseTable(t, turnBegan)
226 turns, _, _, before := clearRig(t, tb, laterBoot, nil)
227 if err := turns.debts.Owe(owed(before)); err != nil {
228 t.Fatal(err)
229 }
230 given, err := turns.Take(context.Background(), OpCheck)
231 if err != nil {
232 t.Fatal(err)
233 }
234 given.Give()
235 other, _, _, _ := clearRig(t, tb, laterBoot, nil)
236 foreign, err := other.Take(context.Background(), OpCheck)
237 if err != nil {
238 t.Fatal(err)
239 }
240 defer foreign.Give()
241 for name, u := range map[string]*Turn{"no turn": nil, "a given turn": given, "another's turn": foreign} {
242 if rebooted, met, err := turns.clearOwed(u, "attempt-c", time.Now().Add(time.Second)); !errors.Is(err, ErrNoTurn) || rebooted || met {
243 t.Fatalf("%s: rebooted %v, met %v, %v", name, rebooted, met, err)
244 }
245 }
246 if !turns.Incomplete() {
247 t.Fatal("cleared outside the turn")
248 }
249}
250
251// noSweep is a table that may not drive a runner-user sweep (Linux
252// outside a test's scope): it also proves nothing idle.
253type noSweep struct{ *table }
254
255func (noSweep) Sweeps() bool { return false }
256
257// An idle read needs a table that may sweep the runner user: where the
258// user is not dedicated to the runner, other processes prove nothing.
259func TestIdleReadNeedsASweepingTable(t *testing.T) {
260 tb := parseTable(t, turnBegan)
261 turns, _, _, before := clearRig(t, tb, sameBoot, nil)
262 if err := turns.debts.Owe(owed(before)); err != nil {
263 t.Fatal(err)
264 }
265 u, err := turns.Take(context.Background(), OpRecover)
266 if err != nil {
267 t.Fatal(err)
268 }
269 defer u.Give()
270 turns.sweep = noSweep{tb}
271 if _, met, err := turns.clearOwed(u, "attempt-c", time.Now().Add(time.Second)); err != nil || met || !turns.Incomplete() {
272 t.Fatalf("cleared on a table that may not sweep (met %v, %v)", met, err)
273 }
274}
275
276// selfID is this test process's identity (proc.Self).
277func selfID() ProcID { return proc.Self() }
278