//go:build testbuild package thin import ( "context" "crypto/rand" "encoding/hex" "os" "os/exec" "path/filepath" "strings" "syscall" "testing" "time" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/runner/mac/creds" "amadan.net/oficina/equipo/internal/runner/mac/proc" "amadan.net/oficina/equipo/internal/wire" ) // fakeCheckEnv starts this binary as a fake worker's checks gate running // a check that never ends, in the directory it names (the gate as // EQUIPO_TEST_FAKE_CHECK_SANDBOX plays it). const fakeCheckEnv = "EQUIPO_TEST_FAKE_CHECK" const fakeCheckAttempt = "checks/0123456789abcdef" func init() { testHelper = func() (int, bool) { dir := os.Getenv(fakeCheckEnv) if dir == "" { return 0, false } entry := proc.ScopeEnv + "=" + os.Getenv(proc.ScopeEnv) turns := mac.NewTurns(filepath.Join(dir, "mac-gate.lock")).Sweep(proc.Scoped(entry)) g, err := fakeWorker{}.checksGate(dir, dir, os.Getenv(fakeCheckEnv+"_SANDBOX"), turns, entry) tree := filepath.Join(dir, "tree") if err != nil || os.MkdirAll(tree, 0o700) != nil { return 2, true } g.Run(context.Background(), fakeCheckAttempt, tree, []string{"/bin/sleep", "600"}, 10*time.Minute, func(id string) error { return os.WriteFile(filepath.Join(dir, "record"), []byte(id), 0o600) }) return 0, true } } // TestFakeWorkerCrashedCheck: a fake worker killed mid-check leaves its // turn's record incomplete. Playing a seatbelt Mac (the srt stand-in) // the turn ran nothing unsandboxed, so the restart's idle read proves // it over, as on a Mac. Without a sandbox it is never cleared at this // boot, even with nothing left of it: only a reboot clears it (task 9). func TestFakeWorkerCrashedCheck(t *testing.T) { for _, c := range []struct { name, sandbox string idle bool }{{"seatbelt", wire.SandboxSeatbelt, true}, {"unsandboxed", "", false}} { t.Run(c.name, func(t *testing.T) { dir := shortDir(t) raw := make([]byte, 8) _, _ = rand.Read(raw) entry := proc.ScopeEnv + "=fc" + hex.EncodeToString(raw) table := proc.Scoped(entry) t.Cleanup(func() { list, _ := table.List(time.Time{}, nil) for _, q := range list { _ = syscall.Kill(q.PID, syscall.SIGKILL) } }) self, err := os.Executable() if err != nil { t.Fatal(err) } cmd := exec.Command(self, "-test.run=^$") cmd.Env = append(os.Environ(), fakeCheckEnv+"="+dir, fakeCheckEnv+"_SANDBOX="+c.sandbox, entry) if err := cmd.Start(); err != nil { t.Fatal(err) } var record string for deadline := time.Now().Add(30 * time.Second); ; time.Sleep(20 * time.Millisecond) { raw, _ := os.ReadFile(filepath.Join(dir, "record")) if record = string(raw); strings.HasPrefix(record, "pg:") && !strings.HasPrefix(record, "pg:0:") { break } if time.Now().After(deadline) { cmd.Process.Kill() cmd.Wait() t.Fatal("the check never started") } } // The runner dies mid-check; the check lives on. _ = cmd.Process.Kill() _ = cmd.Wait() if list, _ := table.List(time.Time{}, []string{fakeCheckAttempt}); len(list) == 0 { t.Fatal("nothing of the check outlived the runner") } turns := mac.NewTurns(filepath.Join(dir, "mac-gate.lock")).Sweep(table) x := &mac.Executor{Turns: turns, Settle: 5 * time.Second} if got := x.Verify(record); got != c.idle { t.Fatalf("at this boot, proven %v, want %v", got, c.idle) } if list, _ := table.List(time.Time{}, []string{fakeCheckAttempt}); len(list) != 0 { t.Fatalf("the restart left %d of the check's processes", len(list)) } if c.idle { return } // Nothing of it is left, yet the idle read may not clear it. if again := x.Verify(record); again { t.Fatal("an unsandboxed turn was cleared by the idle read") } if code, _ := turns.Unproven(); code == "" { t.Fatal("the Mac does not say the attempt is unproven") } now, err := creds.Boot() if err != nil { t.Fatal(err) } creds.BootForTest(func() (proc.Boot, error) { return proc.Boot{ID: now.ID + "-next", Time: time.Now()}, nil }) t.Cleanup(func() { creds.BootForTest(nil) }) if !x.Verify(record) { t.Fatal("a reboot did not clear the unsandboxed turn") } }) } }