package thin import ( "bytes" "context" "errors" "log/slog" "os" "os/exec" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // carlosBinary is a stand-in carlos that says words on stderr, as the // real one does when it refuses, and exits with code. func carlosBinary(t *testing.T, words string, code int) string { return carlosSaying(t, "", words, code) } // carlosSaying prints stdout, then says words on stderr, and exits with code. func carlosSaying(t *testing.T, stdout, words string, code int) string { t.Helper() dir := t.TempDir() said, printed := filepath.Join(dir, "stderr"), filepath.Join(dir, "stdout") if err := os.WriteFile(said, []byte(words), 0o600); err != nil { t.Fatal(err) } if err := os.WriteFile(printed, []byte(stdout), 0o600); err != nil { t.Fatal(err) } bin := filepath.Join(dir, "carlos") script := "#!/bin/sh\ncat '" + printed + "'\ncat '" + said + "' >&2\nexit " + itoa(int64(code)) + "\n" if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { t.Fatal(err) } return bin } func capturedLog(e *Engineer) *bytes.Buffer { var buf bytes.Buffer e.O.Logger = slog.New(slog.NewTextHandler(&buf, nil)) return &buf } // TestDevboxRefusalOnStderrNamesTheHeaders: carlos devbox refuses the // security headers on stderr. The step says preview_headers, so the // person is offered the preview without them, and the log keeps // carlos's own words. func TestDevboxRefusalOnStderrNamesTheHeaders(t *testing.T) { e, _, _, r := previewRig(t, mockups, nil) const refusal = "pack /stage: _headers: line 2: header \"Content-Security-Policy\" is not supported yet\n" e.Devbox = &DevboxOptions{Carlos: carlosBinary(t, refusal, 1), State: filepath.Join(e.O.Runner.StateDir, "devbox")} if err := os.MkdirAll(e.Devbox.State, 0o700); err != nil { t.Fatal(err) } log := capturedLog(e) rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) if rep.Reason != "preview_headers" { t.Fatalf("reason %q, want preview_headers: %+v", rep.Reason, rep) } if got := log.String(); !strings.Contains(got, "preview share") || !strings.Contains(got, "is not supported yet") || !strings.Contains(got, "exit status 1") { t.Fatalf("share failure not logged in carlos's words: %s", got) } } // TestDevboxUnshareReadsStderr: carlos says a link is gone on stderr. // The record goes quietly; any other refusal is logged in full and the // record stays. func TestDevboxUnshareReadsStderr(t *testing.T) { for _, c := range []struct { words string gone bool }{ {"no link called eq-tk1. see what exists with: carlos devbox list\n", true}, {"registrar unreachable\n", false}, } { d := &DevboxOptions{Carlos: carlosBinary(t, c.words, 1), State: t.TempDir()} if err := d.record("tk1", devboxRecord{Name: "eq-tk1"}); err != nil { t.Fatal(err) } e := &Engineer{Devbox: d} log := capturedLog(e) e.PreviewTaskEnded(context.Background(), "tk1") _, kept := d.recorded("tk1") if kept == c.gone { t.Fatalf("%q: record kept=%v", c.words, kept) } got := log.String() if c.gone && got != "" { t.Fatalf("a gone link logged: %s", got) } if !c.gone && (!strings.Contains(got, "preview unshare") || !strings.Contains(got, "registrar unreachable") || !strings.Contains(got, "exit status 1")) { t.Fatalf("unshare failure not logged in full: %s", got) } } } // TestCarlokuRefusalOnStderrReachesTheCaller: the Carloku path reads // carlos's stderr too. func TestCarlokuRefusalOnStderrReachesTheCaller(t *testing.T) { e, _, _, r := previewRig(t, mockups, nil) e.Preview.Run = nil e.Preview.Carlos = carlosBinary(t, "temporary apps, the most it can hold\n", 1) out, err := e.carlos(context.Background(), r, "apps", "list") if err == nil || !strings.Contains(out, "the most it can hold") { t.Fatalf("stderr lost: %q, %v", out, err) } } // TestSanitizeNamesEveryJoinedError: a joined error is logged as each of // its errors, an exit status as itself, not as *errors.joinError. func TestSanitizeNamesEveryJoinedError(t *testing.T) { exit := exec.Command("/bin/sh", "-c", "exit 3").Run() if got := sanitize(errors.Join(nil, exit)); got != "exit status 3" { t.Fatalf("joined exit: %q", got) } if got := sanitize(errors.Join(errors.New("secret words"), exit)); got != "*errors.errorString; exit status 3" { t.Fatalf("joined pair: %q", got) } } // TestRunCarlosOutputContract: success is stdout alone, stderr or not; a // failure is stdout, then carlos's words; stdout stays bounded. func TestRunCarlosOutputContract(t *testing.T) { out, err := runCarlos(exec.Command(carlosSaying(t, "shared\n", "warming up\n", 0))) if err != nil || out != "shared\n" { t.Fatalf("success: %q, %v", out, err) } out, err = runCarlos(exec.Command(carlosSaying(t, "partial\n", "refused\n", 1))) if err == nil || out != "partial\n\nrefused" || err.Error() != "exit status 1: refused" { t.Fatalf("failure: %q, %v", out, err) } out, err = runCarlos(exec.Command(carlosSaying(t, strings.Repeat("x", 65<<10), "", 0))) if err == nil || len(out) != 64<<10 { t.Fatalf("long stdout: %d bytes, %v", len(out), err) } } // TestRunCarlosClippedStderrNeverLogged: redaction needs whole text, so a // secret cut at the 4 KiB stderr limit could slip past it. Clipped words // are still matched against, but the logged error leaves them out. func TestRunCarlosClippedStderrNeverLogged(t *testing.T) { const secret = "Zq8vN3pR7tW2yB5dF9hK4mL6sX1cV0gJ8nQ3eT7u" words := "header \"X-Frame-Options\" is not supported yet " + strings.Repeat("a", 4000) + ` {"password":"` + secret + `"}` out, err := runCarlos(exec.Command(carlosSaying(t, "", words, 1))) if err == nil || !strings.Contains(out, "is not supported yet") { t.Fatalf("clipped words not matched: %v", err) } e := &Engineer{} log := capturedLog(e) e.logDetail("preview share", err) got := log.String() if strings.Contains(got, secret[:6]) || strings.Contains(got, "not supported") || !strings.Contains(got, "exit status 1") { t.Fatalf("clipped stderr logged: %s", got) } }