oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "log/slog"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/equipo/internal/wire"
15)
16
17// carlosBinary is a stand-in carlos that says words on stderr, as the
18// real one does when it refuses, and exits with code.
19func carlosBinary(t *testing.T, words string, code int) string {
20 return carlosSaying(t, "", words, code)
21}
22
23// carlosSaying prints stdout, then says words on stderr, and exits with code.
24func carlosSaying(t *testing.T, stdout, words string, code int) string {
25 t.Helper()
26 dir := t.TempDir()
27 said, printed := filepath.Join(dir, "stderr"), filepath.Join(dir, "stdout")
28 if err := os.WriteFile(said, []byte(words), 0o600); err != nil {
29 t.Fatal(err)
30 }
31 if err := os.WriteFile(printed, []byte(stdout), 0o600); err != nil {
32 t.Fatal(err)
33 }
34 bin := filepath.Join(dir, "carlos")
35 script := "#!/bin/sh\ncat '" + printed + "'\ncat '" + said + "' >&2\nexit " + itoa(int64(code)) + "\n"
36 if err := os.WriteFile(bin, []byte(script), 0o700); err != nil {
37 t.Fatal(err)
38 }
39 return bin
40}
41
42func capturedLog(e *Engineer) *bytes.Buffer {
43 var buf bytes.Buffer
44 e.O.Logger = slog.New(slog.NewTextHandler(&buf, nil))
45 return &buf
46}
47
48// TestDevboxRefusalOnStderrNamesTheHeaders: carlos devbox refuses the
49// security headers on stderr. The step says preview_headers, so the
50// person is offered the preview without them, and the log keeps
51// carlos's own words.
52func TestDevboxRefusalOnStderrNamesTheHeaders(t *testing.T) {
53 e, _, _, r := previewRig(t, mockups, nil)
54 const refusal = "pack /stage: _headers: line 2: header \"Content-Security-Policy\" is not supported yet\n"
55 e.Devbox = &DevboxOptions{Carlos: carlosBinary(t, refusal, 1), State: filepath.Join(e.O.Runner.StateDir, "devbox")}
56 if err := os.MkdirAll(e.Devbox.State, 0o700); err != nil {
57 t.Fatal(err)
58 }
59 log := capturedLog(e)
60 rep := e.Exec(context.Background(), r, func(wire.StepReport) {})
61 if rep.Reason != "preview_headers" {
62 t.Fatalf("reason %q, want preview_headers: %+v", rep.Reason, rep)
63 }
64 if got := log.String(); !strings.Contains(got, "preview share") || !strings.Contains(got, "is not supported yet") || !strings.Contains(got, "exit status 1") {
65 t.Fatalf("share failure not logged in carlos's words: %s", got)
66 }
67}
68
69// TestDevboxUnshareReadsStderr: carlos says a link is gone on stderr.
70// The record goes quietly; any other refusal is logged in full and the
71// record stays.
72func TestDevboxUnshareReadsStderr(t *testing.T) {
73 for _, c := range []struct {
74 words string
75 gone bool
76 }{
77 {"no link called eq-tk1. see what exists with: carlos devbox list\n", true},
78 {"registrar unreachable\n", false},
79 } {
80 d := &DevboxOptions{Carlos: carlosBinary(t, c.words, 1), State: t.TempDir()}
81 if err := d.record("tk1", devboxRecord{Name: "eq-tk1"}); err != nil {
82 t.Fatal(err)
83 }
84 e := &Engineer{Devbox: d}
85 log := capturedLog(e)
86 e.PreviewTaskEnded(context.Background(), "tk1")
87 _, kept := d.recorded("tk1")
88 if kept == c.gone {
89 t.Fatalf("%q: record kept=%v", c.words, kept)
90 }
91 got := log.String()
92 if c.gone && got != "" {
93 t.Fatalf("a gone link logged: %s", got)
94 }
95 if !c.gone && (!strings.Contains(got, "preview unshare") || !strings.Contains(got, "registrar unreachable") || !strings.Contains(got, "exit status 1")) {
96 t.Fatalf("unshare failure not logged in full: %s", got)
97 }
98 }
99}
100
101// TestCarlokuRefusalOnStderrReachesTheCaller: the Carloku path reads
102// carlos's stderr too.
103func TestCarlokuRefusalOnStderrReachesTheCaller(t *testing.T) {
104 e, _, _, r := previewRig(t, mockups, nil)
105 e.Preview.Run = nil
106 e.Preview.Carlos = carlosBinary(t, "temporary apps, the most it can hold\n", 1)
107 out, err := e.carlos(context.Background(), r, "apps", "list")
108 if err == nil || !strings.Contains(out, "the most it can hold") {
109 t.Fatalf("stderr lost: %q, %v", out, err)
110 }
111}
112
113// TestSanitizeNamesEveryJoinedError: a joined error is logged as each of
114// its errors, an exit status as itself, not as *errors.joinError.
115func TestSanitizeNamesEveryJoinedError(t *testing.T) {
116 exit := exec.Command("/bin/sh", "-c", "exit 3").Run()
117 if got := sanitize(errors.Join(nil, exit)); got != "exit status 3" {
118 t.Fatalf("joined exit: %q", got)
119 }
120 if got := sanitize(errors.Join(errors.New("secret words"), exit)); got != "*errors.errorString; exit status 3" {
121 t.Fatalf("joined pair: %q", got)
122 }
123}
124
125// TestRunCarlosOutputContract: success is stdout alone, stderr or not; a
126// failure is stdout, then carlos's words; stdout stays bounded.
127func TestRunCarlosOutputContract(t *testing.T) {
128 out, err := runCarlos(exec.Command(carlosSaying(t, "shared\n", "warming up\n", 0)))
129 if err != nil || out != "shared\n" {
130 t.Fatalf("success: %q, %v", out, err)
131 }
132 out, err = runCarlos(exec.Command(carlosSaying(t, "partial\n", "refused\n", 1)))
133 if err == nil || out != "partial\n\nrefused" || err.Error() != "exit status 1: refused" {
134 t.Fatalf("failure: %q, %v", out, err)
135 }
136 out, err = runCarlos(exec.Command(carlosSaying(t, strings.Repeat("x", 65<<10), "", 0)))
137 if err == nil || len(out) != 64<<10 {
138 t.Fatalf("long stdout: %d bytes, %v", len(out), err)
139 }
140}
141
142// TestRunCarlosClippedStderrNeverLogged: redaction needs whole text, so a
143// secret cut at the 4 KiB stderr limit could slip past it. Clipped words
144// are still matched against, but the logged error leaves them out.
145func TestRunCarlosClippedStderrNeverLogged(t *testing.T) {
146 const secret = "Zq8vN3pR7tW2yB5dF9hK4mL6sX1cV0gJ8nQ3eT7u"
147 words := "header \"X-Frame-Options\" is not supported yet " + strings.Repeat("a", 4000) + ` {"password":"` + secret + `"}`
148 out, err := runCarlos(exec.Command(carlosSaying(t, "", words, 1)))
149 if err == nil || !strings.Contains(out, "is not supported yet") {
150 t.Fatalf("clipped words not matched: %v", err)
151 }
152 e := &Engineer{}
153 log := capturedLog(e)
154 e.logDetail("preview share", err)
155 got := log.String()
156 if strings.Contains(got, secret[:6]) || strings.Contains(got, "not supported") || !strings.Contains(got, "exit status 1") {
157 t.Fatalf("clipped stderr logged: %s", got)
158 }
159}
160