package thin import ( "context" "errors" "log/slog" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runner/forge" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/wire" ) // httpHub sends the merge to a fake hub over HTTP, as the runner does. type httpHub struct { *hubForge url string client *http.Client } func (h httpHub) Merge(ctx context.Context, repo, branch string, req forge.MergeRequest) (forge.MergeResult, error) { return forge.HubMerge(ctx, h.client, h.url, forge.Credential{Bearer: "agent-bearer"}, repo, branch, req) } // brokenRemote cannot be read: its error names credentials. type brokenRemote struct{ *hubForge } func (brokenRemote) Remote(context.Context, string, Use) (workspace.Remote, error) { return workspace.Remote{}, errors.New("dial https://agent:s3cr3t-pw@amadan.net/x: Authorization: Bearer s3cr3t-hub-token refused") } // TestMergeStepKeepsTheHubAnswer: when the hub refuses or fails a merge, // the runner's log line has the hub's words and the step report has its // status, code and message (the log tail the task page shows for a // stopped step), redacted and bounded in both; the reason is as before. // When the repository cannot be read the hub is never asked, and the // report says why. func TestMergeStepKeepsTheHubAnswer(t *testing.T) { old := mergeBackoff mergeBackoff = time.Millisecond t.Cleanup(func() { mergeBackoff = old }) e, f, r, _ := hubRig(t) logs := &lockedBuffer{} e.O.Logger = slog.New(slog.NewTextHandler(logs, nil)) status, body := 0, "" srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(status) w.Write([]byte(body)) })) defer srv.Close() e.Forge = httpHub{hubForge: f, url: srv.URL, client: srv.Client()} secret := "Authorization: Bearer s3cr3t-hub-token; see https://agent:s3cr3t-pw@amadan.net/x" long := strings.Repeat("word ", 1000) + "END" for _, c := range []struct { status int body string tail, log string }{ {409, `{"error":"this branch is archived; ` + secret + `","reason":"archived"}`, "status: 409\ncode: archived\nmessage: this branch is archived; Authorization: ***; see https://***@amadan.net/x", "hub 409 archived: this branch is archived; Authorization: ***"}, {500, "
" + secret + "
", "status: 500\nmessage: Internal Server Error Authorization: ***", "hub 500: Internal Server Error Authorization: ***"}, {409, `{"error":"` + long + `","reason":"archived"}`, "status: 409\ncode: archived\nmessage: word word", "hub 409 archived: word word"}, } { status, body = c.status, c.body before := len(logs.Bytes()) rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) line := string(logs.Bytes()[before:]) if rep.State != wire.StepFailed || rep.Reason != "merge_failed" || !strings.HasPrefix(rep.LogTail, c.tail) { t.Fatalf("%d: report %+v", c.status, rep) } if !strings.Contains(line, "msg=merge") || !strings.Contains(line, c.log) { t.Fatalf("%d: log %q", c.status, line) } if strings.Contains(rep.LogTail+line, "s3cr3t") || strings.Contains(rep.LogTail+line, "END") || len(rep.LogTail) > 600 || len(line) > 800 { t.Fatalf("%d: not redacted or bounded: %q / %q", c.status, rep.LogTail, line) } } f.requests = 0 e.Clones.Forge = brokenRemote{f} before := len(logs.Bytes()) rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) line := string(logs.Bytes()[before:]) want := "reading the repository: dial https://***@amadan.net/x: Authorization: *** refused" if rep.Reason != "merge_failed" || !strings.HasPrefix(rep.LogTail, "error: ") || !strings.Contains(rep.LogTail, want) || !strings.Contains(line, want) || f.requests != 0 { t.Fatalf("unreadable: %+v %q (%d requests)", rep, line, f.requests) } if strings.Contains(rep.LogTail+line, "s3cr3t") { t.Fatalf("unreadable, not redacted: %q / %q", rep.LogTail, line) } } // TestMergeWatchLogsWhy: a default branch that cannot be read is logged // with the repository and git's own words, not the error's type. func TestMergeWatchLogsWhy(t *testing.T) { e, f, r, cand := hubRig(t) ctx := context.Background() if err := e.O.State.AcceptWatch(ctx, "w1", wire.WatchMergeCommand{Person: "p@x", TaskID: "tk1", Repo: r.Repo, Branch: r.Branch, CandidateSHA: cand}); err != nil { t.Fatal(err) } f.dir = filepath.Join(t.TempDir(), "missing.git") logs := &lockedBuffer{} w := &MergeWatch{State: e.O.State, Clones: e.Clones, Git: "/usr/bin/git", Logger: slog.New(slog.NewTextHandler(logs, nil))} w.Check(ctx) got := string(logs.Bytes()) if !strings.Contains(got, `msg="merge tip"`) || !strings.Contains(got, r.Repo) || !strings.Contains(got, "does not appear to be a git repository") { t.Fatalf("log %q", got) } }