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 "os/exec"
8 "path/filepath"
9 "syscall"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/runner/mac/proc"
14)
15
16// Whole-branch fix 2, #1: the cleanup has its own bound (CleanLimit),
17// checked after its last act, the pending record's removal included. A
18// removal that returns late leaves the run unterminated, and its turn's
19// record stays as the evidence a later Verify proves from.
20func TestCleanupLateRecordRemovalIsUnproven(t *testing.T) {
21 _, temp := userTempFixture(t)
22 turns := NewTurns(filepath.Join(t.TempDir(), "lock"))
23 x := &Executor{Turns: turns, UserTemp: temp, Grace: 300 * time.Millisecond, Settle: time.Second, CleanLimit: 300 * time.Millisecond}
24 slow := true
25 turns.beforeDrop = func() {
26 if slow {
27 time.Sleep(500 * time.Millisecond)
28 }
29 }
30 u, err := turns.Take(context.Background(), OpEngineering)
31 if err != nil {
32 t.Fatal(err)
33 }
34 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 })
35 if err != nil {
36 t.Fatal(err)
37 }
38 p := &process{run: r, stop: func() bool { return true }}
39 if _, err := p.Wait(context.Background()); !errors.Is(err, ErrUnproven) {
40 t.Fatalf("a launch whose record removal came late: %v", err)
41 }
42 u.Give()
43 if _, ok, _ := turns.loadTurn(); !ok {
44 t.Fatal("the turn's record went with an unproven cleanup")
45 }
46 slow = false
47 if !x.Verify(r.ID()) || turns.Pending() {
48 t.Fatal("the attempt's proof did not finish")
49 }
50}
51
52// Whole-branch fix 2, #1: Verify's cleanup has the same bound, checked
53// after the pending record's removal too.
54func TestVerifyLateRecordRemovalIsUnproven(t *testing.T) {
55 g, dir := gateFixture(t, true)
56 _, temp := userTempFixture(t)
57 g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock"))
58 g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) }
59 t.Cleanup(func() { _ = os.Chmod(temp, 0o700) })
60 res, id := run(g, dir, "checks-wb2-1", time.Minute, `touch `+quote(filepath.Join(temp, "left")))
61 if res.Terminated {
62 t.Fatalf("%+v", res)
63 }
64 g.QuietSimulators = nil
65 if err := os.Chmod(temp, 0o700); err != nil {
66 t.Fatal(err)
67 }
68 x := g.executor()
69 x.CleanLimit = 300 * time.Millisecond
70 slow := true
71 g.Turns.beforeDrop = func() {
72 if slow {
73 time.Sleep(500 * time.Millisecond)
74 }
75 }
76 if x.Verify(id) {
77 t.Fatal("proven with its record removed past the cleanup's bound")
78 }
79 slow = false
80 if !x.Verify(id) {
81 t.Fatal("a later proof did not finish")
82 }
83}
84
85// Whole-branch fix 2, #2: a cleanup that failed keeps its turn's record
86// and its boot. After a reboot the attempt clears boot-qualified before
87// any old process id is looked at: its group number, now another
88// long-lived process's, is never checked or signalled, and the
89// completion is kept for every consumer.
90func TestFailedCleanupClearsAfterAReboot(t *testing.T) {
91 g, dir := gateFixture(t, true)
92 _, temp := userTempFixture(t)
93 g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock"))
94 boot := proc.Boot{ID: "boot-1", Time: time.Now().Add(-time.Hour)}
95 g.Turns.boot = func() (proc.Boot, error) { return boot, nil }
96 g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) }
97 t.Cleanup(func() { _ = os.Chmod(temp, 0o700) })
98 res, id := run(g, dir, "checks-wb2-2", time.Minute, `touch `+quote(filepath.Join(temp, "left")))
99 if res.Terminated || !g.Turns.Pending() {
100 t.Fatalf("%+v pending=%v", res, g.Turns.Pending())
101 }
102 if _, ok, _ := g.Turns.loadTurn(); !ok {
103 t.Fatal("the turn's record went with the failed cleanup")
104 }
105 // The reboot; the old group number now leads a long-lived process.
106 g.QuietSimulators = nil
107 if err := os.Chmod(temp, 0o700); err != nil {
108 t.Fatal(err)
109 }
110 boot = proc.Boot{ID: "boot-2", Time: time.Now()}
111 other := exec.Command("/bin/sleep", "60")
112 other.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
113 if err := other.Start(); err != nil {
114 t.Fatal(err)
115 }
116 t.Cleanup(func() { _ = other.Process.Kill(); _ = other.Wait() })
117 _, attempt, _ := proc.ParseRecord(id)
118 reused := RecordID(other.Process.Pid, attempt)
119 if !g.Verify(reused) || g.Turns.Pending() || exists(filepath.Join(temp, "left")) {
120 t.Fatal("a reboot did not clear the attempt whose cleanup failed")
121 }
122 if err := other.Process.Signal(syscall.Signal(0)); err != nil {
123 t.Fatalf("the process now holding the old group number was signalled: %v", err)
124 }
125 if !g.Verify(reused) {
126 t.Fatal("the reboot's completion was not kept for the next consumer")
127 }
128}
129
130// Whole-branch fix 2 (Luna's check of the detail parameters): P6 says
131// how many processes appeared, never their names (a name can be a path
132// or anything a process chose).
133func TestNewProcessesCounted(t *testing.T) {
134 before := map[string]int{"launchd": 1, "Finder": 1}
135 after := map[string]int{"launchd": 1, "Finder": 2, "sk-ant-oat01-forged": 1, "xcodebuild": 1}
136 if n := newNames(before, after); n != 3 {
137 t.Fatalf("%d new", n)
138 }
139}
140