| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // TestHubCIReads: both reads send the agent's bearer to the hub's path |
| 13 | // for the repository, ask for the branch and logs given, and decode the |
| 14 | // verdict. Anything the hub did not plainly answer (a refusal, a redirect, |
| 15 | // an oversized or unreadable body, another branch) is ErrCIRead, never a |
| 16 | // verdict. |
| 17 | func TestHubCIReads(t *testing.T) { |
| 18 | sha := strings.Repeat("2", 40) |
| 19 | var got, stolen []string |
| 20 | elsewhere := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | stolen = append(stolen, r.Header.Get("Authorization")) |
| 22 | _, _ = w.Write([]byte(`{"ns":"oficina","repo":"sample","branch":"equipo/tk1-fix-teh","state":"success"}`)) |
| 23 | })) |
| 24 | defer elsewhere.Close() |
| 25 | body := map[string]string{} |
| 26 | status := map[string]int{} |
| 27 | hub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | got = append(got, r.Method+" "+r.URL.Path+"?"+r.URL.RawQuery+" "+r.Header.Get("Authorization")) |
| 29 | if code := status[r.URL.Path]; code != 0 { |
| 30 | if code == http.StatusFound { |
| 31 | w.Header().Set("Location", elsewhere.URL+"/steal") |
| 32 | } |
| 33 | w.WriteHeader(code) |
| 34 | return |
| 35 | } |
| 36 | _, _ = w.Write([]byte(body[r.URL.Path])) |
| 37 | })) |
| 38 | defer hub.Close() |
| 39 | cred := Credential{Bearer: "agent-token"} |
| 40 | ctx := context.Background() |
| 41 | const repo, branch = "amadan.net/oficina/sample", "equipo/tk1-fix-teh" |
| 42 | |
| 43 | body["/api/v1/repos/oficina/sample/ci/hosted"] = `{"available":true,"on":true,"vcpu":32}` |
| 44 | if on, err := HubCIHosted(ctx, hub.Client(), hub.URL, cred, repo); err != nil || !on { |
| 45 | t.Fatalf("hosted %v %v", on, err) |
| 46 | } |
| 47 | body["/api/v1/repos/oficina/sample/ci/status"] = `{"ns":"oficina","repo":"sample","branch":"equipo/tk1-fix-teh","sha":"` + sha + `","state":"failure","label":"CI failed","summary":"step \"compile\" failed","updated_at":1794402264,"steps":[{"name":"compile","state":"failure","log":"budget size: 1 problem"},{"name":"check","state":"success","shards":[{"index":0,"name":"shard 0 of 2","state":"success"}]}]}` |
| 48 | st, err := HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsFailed) |
| 49 | if err != nil || st.SHA != sha || st.State != "failure" || st.UpdatedAt != 1794402264 || len(st.Steps) != 2 || st.Steps[0].Log != "budget size: 1 problem" || st.Steps[1].Shards[0].Name != "shard 0 of 2" { |
| 50 | t.Fatalf("status %+v %v", st, err) |
| 51 | } |
| 52 | want := []string{ |
| 53 | "GET /api/v1/repos/oficina/sample/ci/hosted? Bearer agent-token", |
| 54 | "GET /api/v1/repos/oficina/sample/ci/status?branch=equipo%2Ftk1-fix-teh&logs=failed Bearer agent-token", |
| 55 | } |
| 56 | if strings.Join(got, "\n") != strings.Join(want, "\n") { |
| 57 | t.Fatalf("requests\n%s", strings.Join(got, "\n")) |
| 58 | } |
| 59 | |
| 60 | refused := func(name string, err error) { |
| 61 | t.Helper() |
| 62 | if !errors.Is(err, ErrCIRead) { |
| 63 | t.Fatalf("%s: %v", name, err) |
| 64 | } |
| 65 | } |
| 66 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, "equipo/other", CILogsNone) |
| 67 | refused("another branch's answer", err) |
| 68 | for _, other := range []string{`"ns":"elsewhere","repo":"sample"`, `"ns":"oficina","repo":"other"`, `"repo":"sample"`} { |
| 69 | body["/api/v1/repos/oficina/sample/ci/status"] = `{` + other + `,"branch":"equipo/tk1-fix-teh","sha":"` + sha + `","state":"success"}` |
| 70 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) |
| 71 | refused("an answer about "+other, err) |
| 72 | } |
| 73 | body["/api/v1/repos/oficina/sample/ci/hosted"] = `{"available":true}` |
| 74 | _, err = HubCIHosted(ctx, hub.Client(), hub.URL, cred, repo) |
| 75 | refused("no on field", err) |
| 76 | body["/api/v1/repos/oficina/sample/ci/status"] = `{"ns":"oficina","repo":"sample","branch":"equipo/tk1-fix-teh","sha":"not-a-sha","state":"success"}` |
| 77 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) |
| 78 | refused("a malformed tip", err) |
| 79 | body["/api/v1/repos/oficina/sample/ci/status"] = `{"ns":"oficina","repo":"sample","branch":"equipo/tk1-fix-teh","sha":"` + sha + `","state":"success","log":"` + strings.Repeat("x", maxCIStatusBody) + `"}` |
| 80 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) |
| 81 | refused("an oversized body", err) |
| 82 | body["/api/v1/repos/oficina/sample/ci/status"] = `{"branch":` |
| 83 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) |
| 84 | refused("an unreadable body", err) |
| 85 | status["/api/v1/repos/oficina/sample/ci/status"] = http.StatusFound |
| 86 | got = nil |
| 87 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) |
| 88 | refused("a redirect", err) |
| 89 | if len(got) != 1 || len(stolen) != 0 { |
| 90 | t.Fatalf("the redirect was followed: %v %v", got, stolen) |
| 91 | } |
| 92 | status["/api/v1/repos/oficina/sample/ci/status"] = http.StatusForbidden |
| 93 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) |
| 94 | refused("a refusal", err) |
| 95 | got = nil |
| 96 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, "amadan.net/oficina/../x", branch, CILogsNone) |
| 97 | refused("a bad repository", err) |
| 98 | _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, "all") |
| 99 | refused("another logs mode", err) |
| 100 | _, err = HubCIHosted(ctx, hub.Client(), hub.URL, Credential{Bearer: "a\nb"}, repo) |
| 101 | refused("a bearer with a newline", err) |
| 102 | if len(got) != 0 { |
| 103 | t.Fatalf("asked the hub anyway: %v", got) |
| 104 | } |
| 105 | } |
| 106 | |