oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build testbuild
2
3package thin
4
5import (
6 "context"
7 "crypto/rand"
8 "encoding/hex"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "syscall"
14 "testing"
15 "time"
16
17 "amadan.net/oficina/equipo/internal/runner/mac"
18 "amadan.net/oficina/equipo/internal/runner/mac/creds"
19 "amadan.net/oficina/equipo/internal/runner/mac/proc"
20 "amadan.net/oficina/equipo/internal/wire"
21)
22
23// fakeCheckEnv starts this binary as a fake worker's checks gate running
24// a check that never ends, in the directory it names (the gate as
25// EQUIPO_TEST_FAKE_CHECK_SANDBOX plays it).
26const fakeCheckEnv = "EQUIPO_TEST_FAKE_CHECK"
27
28const fakeCheckAttempt = "checks/0123456789abcdef"
29
30func init() {
31 testHelper = func() (int, bool) {
32 dir := os.Getenv(fakeCheckEnv)
33 if dir == "" {
34 return 0, false
35 }
36 entry := proc.ScopeEnv + "=" + os.Getenv(proc.ScopeEnv)
37 turns := mac.NewTurns(filepath.Join(dir, "mac-gate.lock")).Sweep(proc.Scoped(entry))
38 g, err := fakeWorker{}.checksGate(dir, dir, os.Getenv(fakeCheckEnv+"_SANDBOX"), turns, entry)
39 tree := filepath.Join(dir, "tree")
40 if err != nil || os.MkdirAll(tree, 0o700) != nil {
41 return 2, true
42 }
43 g.Run(context.Background(), fakeCheckAttempt, tree, []string{"/bin/sleep", "600"}, 10*time.Minute, func(id string) error {
44 return os.WriteFile(filepath.Join(dir, "record"), []byte(id), 0o600)
45 })
46 return 0, true
47 }
48}
49
50// TestFakeWorkerCrashedCheck: a fake worker killed mid-check leaves its
51// turn's record incomplete. Playing a seatbelt Mac (the srt stand-in)
52// the turn ran nothing unsandboxed, so the restart's idle read proves
53// it over, as on a Mac. Without a sandbox it is never cleared at this
54// boot, even with nothing left of it: only a reboot clears it (task 9).
55func TestFakeWorkerCrashedCheck(t *testing.T) {
56 for _, c := range []struct {
57 name, sandbox string
58 idle bool
59 }{{"seatbelt", wire.SandboxSeatbelt, true}, {"unsandboxed", "", false}} {
60 t.Run(c.name, func(t *testing.T) {
61 dir := shortDir(t)
62 raw := make([]byte, 8)
63 _, _ = rand.Read(raw)
64 entry := proc.ScopeEnv + "=fc" + hex.EncodeToString(raw)
65 table := proc.Scoped(entry)
66 t.Cleanup(func() {
67 list, _ := table.List(time.Time{}, nil)
68 for _, q := range list {
69 _ = syscall.Kill(q.PID, syscall.SIGKILL)
70 }
71 })
72 self, err := os.Executable()
73 if err != nil {
74 t.Fatal(err)
75 }
76 cmd := exec.Command(self, "-test.run=^$")
77 cmd.Env = append(os.Environ(), fakeCheckEnv+"="+dir, fakeCheckEnv+"_SANDBOX="+c.sandbox, entry)
78 if err := cmd.Start(); err != nil {
79 t.Fatal(err)
80 }
81 var record string
82 for deadline := time.Now().Add(30 * time.Second); ; time.Sleep(20 * time.Millisecond) {
83 raw, _ := os.ReadFile(filepath.Join(dir, "record"))
84 if record = string(raw); strings.HasPrefix(record, "pg:") && !strings.HasPrefix(record, "pg:0:") {
85 break
86 }
87 if time.Now().After(deadline) {
88 cmd.Process.Kill()
89 cmd.Wait()
90 t.Fatal("the check never started")
91 }
92 }
93 // The runner dies mid-check; the check lives on.
94 _ = cmd.Process.Kill()
95 _ = cmd.Wait()
96 if list, _ := table.List(time.Time{}, []string{fakeCheckAttempt}); len(list) == 0 {
97 t.Fatal("nothing of the check outlived the runner")
98 }
99 turns := mac.NewTurns(filepath.Join(dir, "mac-gate.lock")).Sweep(table)
100 x := &mac.Executor{Turns: turns, Settle: 5 * time.Second}
101 if got := x.Verify(record); got != c.idle {
102 t.Fatalf("at this boot, proven %v, want %v", got, c.idle)
103 }
104 if list, _ := table.List(time.Time{}, []string{fakeCheckAttempt}); len(list) != 0 {
105 t.Fatalf("the restart left %d of the check's processes", len(list))
106 }
107 if c.idle {
108 return
109 }
110 // Nothing of it is left, yet the idle read may not clear it.
111 if again := x.Verify(record); again {
112 t.Fatal("an unsandboxed turn was cleared by the idle read")
113 }
114 if code, _ := turns.Unproven(); code == "" {
115 t.Fatal("the Mac does not say the attempt is unproven")
116 }
117 now, err := creds.Boot()
118 if err != nil {
119 t.Fatal(err)
120 }
121 creds.BootForTest(func() (proc.Boot, error) { return proc.Boot{ID: now.ID + "-next", Time: time.Now()}, nil })
122 t.Cleanup(func() { creds.BootForTest(nil) })
123 if !x.Verify(record) {
124 t.Fatal("a reboot did not clear the unsandboxed turn")
125 }
126 })
127 }
128}
129