package forge import ( "context" "errors" "net/http" "net/http/httptest" "strings" "testing" ) // TestHubCIReads: both reads send the agent's bearer to the hub's path // for the repository, ask for the branch and logs given, and decode the // verdict. Anything the hub did not plainly answer (a refusal, a redirect, // an oversized or unreadable body, another branch) is ErrCIRead, never a // verdict. func TestHubCIReads(t *testing.T) { sha := strings.Repeat("2", 40) var got, stolen []string elsewhere := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { stolen = append(stolen, r.Header.Get("Authorization")) _, _ = w.Write([]byte(`{"ns":"oficina","repo":"sample","branch":"equipo/tk1-fix-teh","state":"success"}`)) })) defer elsewhere.Close() body := map[string]string{} status := map[string]int{} hub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { got = append(got, r.Method+" "+r.URL.Path+"?"+r.URL.RawQuery+" "+r.Header.Get("Authorization")) if code := status[r.URL.Path]; code != 0 { if code == http.StatusFound { w.Header().Set("Location", elsewhere.URL+"/steal") } w.WriteHeader(code) return } _, _ = w.Write([]byte(body[r.URL.Path])) })) defer hub.Close() cred := Credential{Bearer: "agent-token"} ctx := context.Background() const repo, branch = "amadan.net/oficina/sample", "equipo/tk1-fix-teh" body["/api/v1/repos/oficina/sample/ci/hosted"] = `{"available":true,"on":true,"vcpu":32}` if on, err := HubCIHosted(ctx, hub.Client(), hub.URL, cred, repo); err != nil || !on { t.Fatalf("hosted %v %v", on, err) } 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"}]}]}` st, err := HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsFailed) 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" { t.Fatalf("status %+v %v", st, err) } want := []string{ "GET /api/v1/repos/oficina/sample/ci/hosted? Bearer agent-token", "GET /api/v1/repos/oficina/sample/ci/status?branch=equipo%2Ftk1-fix-teh&logs=failed Bearer agent-token", } if strings.Join(got, "\n") != strings.Join(want, "\n") { t.Fatalf("requests\n%s", strings.Join(got, "\n")) } refused := func(name string, err error) { t.Helper() if !errors.Is(err, ErrCIRead) { t.Fatalf("%s: %v", name, err) } } _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, "equipo/other", CILogsNone) refused("another branch's answer", err) for _, other := range []string{`"ns":"elsewhere","repo":"sample"`, `"ns":"oficina","repo":"other"`, `"repo":"sample"`} { body["/api/v1/repos/oficina/sample/ci/status"] = `{` + other + `,"branch":"equipo/tk1-fix-teh","sha":"` + sha + `","state":"success"}` _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) refused("an answer about "+other, err) } body["/api/v1/repos/oficina/sample/ci/hosted"] = `{"available":true}` _, err = HubCIHosted(ctx, hub.Client(), hub.URL, cred, repo) refused("no on field", err) body["/api/v1/repos/oficina/sample/ci/status"] = `{"ns":"oficina","repo":"sample","branch":"equipo/tk1-fix-teh","sha":"not-a-sha","state":"success"}` _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) refused("a malformed tip", err) 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) + `"}` _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) refused("an oversized body", err) body["/api/v1/repos/oficina/sample/ci/status"] = `{"branch":` _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) refused("an unreadable body", err) status["/api/v1/repos/oficina/sample/ci/status"] = http.StatusFound got = nil _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) refused("a redirect", err) if len(got) != 1 || len(stolen) != 0 { t.Fatalf("the redirect was followed: %v %v", got, stolen) } status["/api/v1/repos/oficina/sample/ci/status"] = http.StatusForbidden _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, CILogsNone) refused("a refusal", err) got = nil _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, "amadan.net/oficina/../x", branch, CILogsNone) refused("a bad repository", err) _, err = HubCIStatus(ctx, hub.Client(), hub.URL, cred, repo, branch, "all") refused("another logs mode", err) _, err = HubCIHosted(ctx, hub.Client(), hub.URL, Credential{Bearer: "a\nb"}, repo) refused("a bearer with a newline", err) if len(got) != 0 { t.Fatalf("asked the hub anyway: %v", got) } }