oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package proc
2
3import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "sync"
8 "testing"
9 "time"
10
11 "amadan.net/rastrillo/rastrillo/budget"
12)
13
14func TestMain(m *testing.M) { os.Exit(budget.Main(m)) }
15
16// Only a path the system alone can write counts for a relaunch: under
17// /System (not its volumes: /System/Volumes/Data is every user's data),
18// /usr/libexec or /usr/sbin, exactly, with no climbing out.
19func TestSystemPath(t *testing.T) {
20 for p, want := range map[string]bool{
21 "/System/Library/PrivateFrameworks/X.framework/XPCServices/Y.xpc/Contents/MacOS/Y": true,
22 "/usr/libexec/trustd": true,
23 "/System/Volumes/Data/Users/paulequipo/x": false,
24 "/System/Volumes/Preboot/Cryptexes/OS/x": false,
25 "/System/Library/../../Users/paulequipo/x": false,
26 "/usr/libexec/./x": false,
27 "/usr/libexec/..": false,
28 "/system/Library/x": false,
29 "/usr/sbin/cfprefsd": true,
30 "/Users/paulequipo/.local/lib/equipo-runner/x": false,
31 "/System": false,
32 "": false,
33 } {
34 if SystemPath(p) != want {
35 t.Errorf("%q: %v", p, !want)
36 }
37 }
38}
39
40// #4: helpers start concurrently (capability queries beside a gate's
41// caffeinate): every registration lands, with no race (run with -race).
42func TestOwnConcurrentStarts(t *testing.T) {
43 o := &Own{}
44 var wg sync.WaitGroup
45 var mu sync.Mutex
46 var cmds []*exec.Cmd
47 for range 16 {
48 wg.Add(1)
49 go func() {
50 defer wg.Done()
51 c := exec.Command("/bin/sleep", "1")
52 if err := o.Start(c, Platform); err != nil {
53 t.Error(err)
54 return
55 }
56 mu.Lock()
57 cmds = append(cmds, c)
58 mu.Unlock()
59 }()
60 }
61 wg.Add(1)
62 go func() { defer wg.Done(); _, _, _ = o.Snapshot(time.Now().Add(5*time.Second), Platform, nil) }()
63 wg.Wait()
64 for _, c := range cmds {
65 q, ok := Platform.Lookup(c.Process.Pid)
66 if !ok || !o.Owns(q.ID) {
67 t.Errorf("helper %d not on record", c.Process.Pid)
68 }
69 _ = c.Process.Kill()
70 _ = c.Wait()
71 }
72}
73
74// #13: every read-modify-write of the debts file is serialized across
75// processes: another instance's obligation written in the gap of a
76// Discharge is never lost.
77func TestDebtsSerializedAcrossInstances(t *testing.T) {
78 p := filepath.Join(t.TempDir(), "owed")
79 a, b := &Debts{Path: p}, &Debts{Path: p}
80 if err := a.Owe(Obligation{Markers: []string{"attempt-a"}}); err != nil {
81 t.Fatal(err)
82 }
83 done := make(chan error, 1)
84 a.between = func() {
85 go func() { done <- b.Owe(Obligation{Markers: []string{"attempt-b"}}) }()
86 time.Sleep(200 * time.Millisecond)
87 }
88 if err := a.Discharge("attempt-a", time.Time{}); err != nil {
89 t.Fatal(err)
90 }
91 a.between = nil
92 if err := <-done; err != nil {
93 t.Fatal(err)
94 }
95 if !b.Owes("attempt-b") || a.Owes("attempt-a") {
96 t.Fatal("another instance's obligation was lost, or the discharged one stayed")
97 }
98}
99
100// Fix round 2, #4: a lock that comes after the deadline runs nothing,
101// and a discharge whose work outlasts it removes nothing.
102func TestDebtsUnderTheDeadline(t *testing.T) {
103 d := &Debts{Path: filepath.Join(t.TempDir(), "owed")}
104 if err := d.Owe(Obligation{Markers: []string{"attempt-a"}}); err != nil {
105 t.Fatal(err)
106 }
107 d.LockHook = func(int) error { time.Sleep(150 * time.Millisecond); return nil }
108 ran := false
109 if err := d.lockedBy(time.Now().Add(50*time.Millisecond), func() error { ran = true; return nil }); err == nil || ran {
110 t.Fatalf("a lock taken after its deadline ran its work (err %v)", err)
111 }
112 d.LockHook = nil
113 d.between = func() { time.Sleep(150 * time.Millisecond) }
114 if err := d.Discharge("attempt-a", time.Now().Add(50*time.Millisecond)); err == nil {
115 t.Fatal("a discharge that outlasted its deadline succeeded")
116 }
117 d.between = nil
118 if !d.Owes("attempt-a") {
119 t.Fatal("a late discharge removed the obligation")
120 }
121}
122
123// Round 3, #3: the deadline covers the save itself (the write, its sync
124// and rename). A discharge whose save ends late fails and leaves the
125// debt; a reboot's completion saved late fails, and stays as the
126// evidence a later proof finishes from.
127func TestDebtsSaveUnderTheDeadline(t *testing.T) {
128 dir := t.TempDir()
129 slow := false
130 delay := func() {
131 if slow {
132 slow = false
133 time.Sleep(150 * time.Millisecond)
134 }
135 }
136 d := &Debts{Path: filepath.Join(dir, "owed"), saved: delay}
137 if err := d.Owe(Obligation{Markers: []string{"attempt-a"}}); err != nil {
138 t.Fatal(err)
139 }
140 slow = true
141 if err := d.Discharge("attempt-a", time.Now().Add(50*time.Millisecond)); err == nil {
142 t.Fatal("a discharge whose save ended after its deadline succeeded")
143 }
144 if !d.Owes("attempt-a") {
145 t.Fatal("a late discharge erased the debt")
146 }
147
148 c := &Debts{Path: filepath.Join(dir, "owed-c"), saved: delay}
149 if err := c.Owe(Obligation{Markers: []string{"attempt-b"}, BootID: "boot-1"}); err != nil {
150 t.Fatal(err)
151 }
152 slow = true
153 if err := c.Complete("attempt-b", "boot-2", time.Now().Add(50*time.Millisecond)); err == nil {
154 t.Fatal("a completion saved after its deadline succeeded")
155 }
156 all, err := c.load()
157 if err != nil || len(all) != 1 || all[0].ProvenIn != "boot-2" {
158 t.Fatalf("a late completion was not kept for the next proof: %+v %v", all, err)
159 }
160}
161