| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "log/slog" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 15 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 16 | "amadan.net/oficina/equipo/internal/wire" |
| 17 | ) |
| 18 | |
| 19 | // httpHub sends the merge to a fake hub over HTTP, as the runner does. |
| 20 | type httpHub struct { |
| 21 | *hubForge |
| 22 | url string |
| 23 | client *http.Client |
| 24 | } |
| 25 | |
| 26 | func (h httpHub) Merge(ctx context.Context, repo, branch string, req forge.MergeRequest) (forge.MergeResult, error) { |
| 27 | return forge.HubMerge(ctx, h.client, h.url, forge.Credential{Bearer: "agent-bearer"}, repo, branch, req) |
| 28 | } |
| 29 | |
| 30 | // brokenRemote cannot be read: its error names credentials. |
| 31 | type brokenRemote struct{ *hubForge } |
| 32 | |
| 33 | func (brokenRemote) Remote(context.Context, string, Use) (workspace.Remote, error) { |
| 34 | return workspace.Remote{}, errors.New("dial https://agent:s3cr3t-pw@amadan.net/x: Authorization: Bearer s3cr3t-hub-token refused") |
| 35 | } |
| 36 | |
| 37 | // TestMergeStepKeepsTheHubAnswer: when the hub refuses or fails a merge, |
| 38 | // the runner's log line has the hub's words and the step report has its |
| 39 | // status, code and message (the log tail the task page shows for a |
| 40 | // stopped step), redacted and bounded in both; the reason is as before. |
| 41 | // When the repository cannot be read the hub is never asked, and the |
| 42 | // report says why. |
| 43 | func TestMergeStepKeepsTheHubAnswer(t *testing.T) { |
| 44 | old := mergeBackoff |
| 45 | mergeBackoff = time.Millisecond |
| 46 | t.Cleanup(func() { mergeBackoff = old }) |
| 47 | e, f, r, _ := hubRig(t) |
| 48 | logs := &lockedBuffer{} |
| 49 | e.O.Logger = slog.New(slog.NewTextHandler(logs, nil)) |
| 50 | status, body := 0, "" |
| 51 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 52 | w.WriteHeader(status) |
| 53 | w.Write([]byte(body)) |
| 54 | })) |
| 55 | defer srv.Close() |
| 56 | e.Forge = httpHub{hubForge: f, url: srv.URL, client: srv.Client()} |
| 57 | secret := "Authorization: Bearer s3cr3t-hub-token; see https://agent:s3cr3t-pw@amadan.net/x" |
| 58 | long := strings.Repeat("word ", 1000) + "END" |
| 59 | for _, c := range []struct { |
| 60 | status int |
| 61 | body string |
| 62 | tail, log string |
| 63 | }{ |
| 64 | {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: ***"}, |
| 65 | {500, "<html><body><h1>Internal Server Error</h1><p>" + secret + "</p></body></html>", "status: 500\nmessage: Internal Server Error Authorization: ***", "hub 500: Internal Server Error Authorization: ***"}, |
| 66 | {409, `{"error":"` + long + `","reason":"archived"}`, "status: 409\ncode: archived\nmessage: word word", "hub 409 archived: word word"}, |
| 67 | } { |
| 68 | status, body = c.status, c.body |
| 69 | before := len(logs.Bytes()) |
| 70 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 71 | line := string(logs.Bytes()[before:]) |
| 72 | if rep.State != wire.StepFailed || rep.Reason != "merge_failed" || !strings.HasPrefix(rep.LogTail, c.tail) { |
| 73 | t.Fatalf("%d: report %+v", c.status, rep) |
| 74 | } |
| 75 | if !strings.Contains(line, "msg=merge") || !strings.Contains(line, c.log) { |
| 76 | t.Fatalf("%d: log %q", c.status, line) |
| 77 | } |
| 78 | if strings.Contains(rep.LogTail+line, "s3cr3t") || strings.Contains(rep.LogTail+line, "END") || len(rep.LogTail) > 600 || len(line) > 800 { |
| 79 | t.Fatalf("%d: not redacted or bounded: %q / %q", c.status, rep.LogTail, line) |
| 80 | } |
| 81 | } |
| 82 | f.requests = 0 |
| 83 | e.Clones.Forge = brokenRemote{f} |
| 84 | before := len(logs.Bytes()) |
| 85 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 86 | line := string(logs.Bytes()[before:]) |
| 87 | want := "reading the repository: dial https://***@amadan.net/x: Authorization: *** refused" |
| 88 | if rep.Reason != "merge_failed" || !strings.HasPrefix(rep.LogTail, "error: ") || !strings.Contains(rep.LogTail, want) || !strings.Contains(line, want) || f.requests != 0 { |
| 89 | t.Fatalf("unreadable: %+v %q (%d requests)", rep, line, f.requests) |
| 90 | } |
| 91 | if strings.Contains(rep.LogTail+line, "s3cr3t") { |
| 92 | t.Fatalf("unreadable, not redacted: %q / %q", rep.LogTail, line) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // TestMergeWatchLogsWhy: a default branch that cannot be read is logged |
| 97 | // with the repository and git's own words, not the error's type. |
| 98 | func TestMergeWatchLogsWhy(t *testing.T) { |
| 99 | e, f, r, cand := hubRig(t) |
| 100 | ctx := context.Background() |
| 101 | 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 { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | f.dir = filepath.Join(t.TempDir(), "missing.git") |
| 105 | logs := &lockedBuffer{} |
| 106 | w := &MergeWatch{State: e.O.State, Clones: e.Clones, Git: "/usr/bin/git", Logger: slog.New(slog.NewTextHandler(logs, nil))} |
| 107 | w.Check(ctx) |
| 108 | got := string(logs.Bytes()) |
| 109 | if !strings.Contains(got, `msg="merge tip"`) || !strings.Contains(got, r.Repo) || !strings.Contains(got, "does not appear to be a git repository") { |
| 110 | t.Fatalf("log %q", got) |
| 111 | } |
| 112 | } |
| 113 | |