Skip to content
quarry Artifact [455dd410d8]
Login

Artifact [455dd410d8]

Artifact 455dd410d861d7e1078d9b4a453ac94f6b9c7515890cdd9aeb13a7f203c509de:


package cibridge

import (
	"bufio"
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"
)

type seenReq struct {
	method, path, auth string
	body               map[string]any
}

// A runner on its owner's machine reaches the engine's four worker
// routes through the repository's URL; the poll is pinned to the
// repository's group; the connect stream arrives as it is written; and
// the engine's admin bearer is refused on this path.
func TestTheBridgeForwardsTheWorkerRoutesAndPinsTheGroup(t *testing.T) {
	seen := make(chan seenReq, 10)
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var body map[string]any
		json.NewDecoder(r.Body).Decode(&body)
		seen <- seenReq{r.Method, r.URL.Path, r.Header.Get("Authorization"), body}
		if r.URL.Path == "/v1/workers/connect" {
			w.Header().Set("Content-Type", "text/event-stream")
			fl := w.(http.Flusher)
			io.WriteString(w, "event: heartbeat\ndata: {}\n\n")
			fl.Flush()
			time.Sleep(200 * time.Millisecond)
			return
		}
		io.WriteString(w, `{"ok":true}`)
	}))
	defer engine.Close()
	h, err := Handler(engine.URL, "ADMIN", func(repo string) string { return "g-" + repo }, func(repo string) bool { return repo == "alpha" }, nil, nil)
	if err != nil {
		t.Fatal(err)
	}
	front := httptest.NewServer(h)
	defer front.Close()
	post := func(path, auth, body string) *http.Response {
		t.Helper()
		req, _ := http.NewRequest("POST", front.URL+path, strings.NewReader(body))
		if auth != "" {
			req.Header.Set("Authorization", "Bearer "+auth)
		}
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		return resp
	}

	// poll: pinned to the repository's group, token passed through.
	resp := post("/r/alpha/ci/bridge/v1/tasks/poll", "WT", `{"task_types":["t"],"max_tasks":1}`)
	resp.Body.Close()
	if resp.StatusCode != 200 {
		t.Fatalf("FAIL: poll = %d", resp.StatusCode)
	}
	got := <-seen
	if got.path != "/v1/tasks/poll" || got.auth != "Bearer WT" || got.body["worker_group"] != "g-alpha" {
		t.Fatalf("FAIL: engine saw %+v", got)
	}
	// A poll naming another repository's group is refused before the engine.
	resp = post("/r/alpha/ci/bridge/v1/tasks/poll", "WT", `{"task_types":["t"],"worker_group":"g-beta"}`)
	resp.Body.Close()
	if resp.StatusCode != 400 {
		t.Errorf("FAIL: a foreign group got %d", resp.StatusCode)
	}
	// resolve and logs keep the task id.
	for _, p := range []string{"resolve", "logs"} {
		resp = post("/r/alpha/ci/bridge/v1/tasks/task-7/"+p, "WT", `{}`)
		resp.Body.Close()
		if got := <-seen; got.path != "/v1/tasks/task-7/"+p {
			t.Errorf("FAIL: %s reached %s", p, got.path)
		}
	}
	// connect streams: the first event arrives before the engine finishes.
	start := time.Now()
	resp = post("/r/alpha/ci/bridge/v1/workers/connect", "WT", `{"worker_id":"w","task_types":["t"],"max_tasks":1}`)
	line, _ := bufio.NewReader(resp.Body).ReadString('\n')
	resp.Body.Close()
	// connect is pinned too, so the engine records the runner under this
	// repository's group and Settings → Runners can list it (dagnats#719).
	if got := <-seen; got.body["worker_group"] != "g-alpha" {
		t.Errorf("FAIL: connect reached the engine as %+v, without the repository's group", got.body)
	}
	if !strings.HasPrefix(line, "event: heartbeat") || time.Since(start) > 150*time.Millisecond {
		t.Errorf("FAIL: the stream was held back: %q after %s", line, time.Since(start))
	}
	// A connect naming another repository's group is refused before the engine.
	resp = post("/r/alpha/ci/bridge/v1/workers/connect", "WT", `{"worker_id":"w","task_types":["t"],"worker_group":"g-beta"}`)
	resp.Body.Close()
	if resp.StatusCode != 400 {
		t.Errorf("FAIL: a connect in a foreign group got %d", resp.StatusCode)
	}
	// The admin bearer, a missing token, an unknown repository: refused
	// here, never forwarded.
	for _, c := range []struct {
		path, auth string
		want       int
	}{
		{"/r/alpha/ci/bridge/v1/tasks/poll", "ADMIN", 403},
		{"/r/alpha/ci/bridge/v1/tasks/poll", "", 401},
		{"/r/nosuch/ci/bridge/v1/tasks/poll", "WT", 404},
	} {
		resp = post(c.path, c.auth, `{}`)
		resp.Body.Close()
		if resp.StatusCode != c.want {
			t.Errorf("FAIL: %s with %q = %d, want %d", c.path, c.auth, resp.StatusCode, c.want)
		}
	}
	select {
	case r := <-seen:
		t.Errorf("FAIL: a refused request reached the engine: %+v", r)
	default:
	}
}

// THE BRIDGE SAYS WHY IT REFUSED, IN A HEADER, FOR A RUNNER TO DECIDE ON.
//
// An unknown repository is "not served" (404) unless it is starting,
// which is a 503 with Retry-After; the engine's 404 and 409 on resolve
// and logs are "task gone" and "task resolved", and on poll they carry
// no reason.
func TestTheBridgeNamesItsRefusals(t *testing.T) {
	code := 404
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(code)
	}))
	defer engine.Close()
	h, err := Handler(engine.URL, "ADMIN", func(string) string { return "g" }, func(r string) bool { return r == "alpha" }, nil, nil,
		WithStarting(func(r string) bool { return r == "booting" }))
	if err != nil {
		t.Fatal(err)
	}
	front := httptest.NewServer(h)
	defer front.Close()
	do := func(path string) *http.Response {
		req, _ := http.NewRequest("POST", front.URL+path, strings.NewReader(`{}`))
		req.Header.Set("Authorization", "Bearer WT")
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		resp.Body.Close()
		return resp
	}
	for _, c := range []struct {
		name, path   string
		engine, want int
		reason       string
		retryAfter   bool
	}{
		{"unknown repository", "/r/nosuch/ci/bridge/v1/tasks/poll", 200, 404, ReasonRepoNotServed, false},
		{"repository starting", "/r/booting/ci/bridge/v1/tasks/poll", 200, 503, "", true},
		{"task gone", "/r/alpha/ci/bridge/v1/tasks/t1/resolve", 404, 404, ReasonTaskGone, false},
		{"task resolved", "/r/alpha/ci/bridge/v1/tasks/t1/resolve", 409, 409, ReasonTaskResolved, false},
		{"logs closed", "/r/alpha/ci/bridge/v1/tasks/t1/logs", 409, 409, ReasonTaskResolved, false},
		{"poll 404 is unexplained", "/r/alpha/ci/bridge/v1/tasks/poll", 404, 404, "", false},
	} {
		code = c.engine
		resp := do(c.path)
		if resp.StatusCode != c.want || resp.Header.Get(ReasonHeader) != c.reason || (resp.Header.Get("Retry-After") != "") != c.retryAfter {
			t.Errorf("FAIL: %s: status %d reason %q retry-after %q, want %d %q (retry-after %v)", c.name,
				resp.StatusCode, resp.Header.Get(ReasonHeader), resp.Header.Get("Retry-After"), c.want, c.reason, c.retryAfter)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// holds SAYS "NOT HELD" ONLY WHEN THE ENGINE'S HEARTBEAT ANSWER MEANS NO CLAIM.
//
// The engine's 404 (not in flight), 409 (run cancelled) and 403 (held by
// another token) are that. Its 401 (our token refused) is a plain 403 a
// runner must read as a failure, and a 500 is a 502: neither may read as
// a task the runner lost.
func TestHoldsNamesOnlyALostClaim(t *testing.T) {
	code := 200
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(code)
	}))
	defer engine.Close()
	tok := func(string, string) (map[string]string, string, error) { return nil, "", nil }
	h, err := Handler(engine.URL, "ADMIN", func(string) string { return "g" }, func(r string) bool { return r == "alpha" }, nil, tok)
	if err != nil {
		t.Fatal(err)
	}
	for _, c := range []struct {
		engine, want int
		reason       string
	}{
		{200, 200, ""},
		{404, 403, ReasonNotHeld},
		{409, 403, ReasonNotHeld},
		{403, 403, ReasonNotHeld},
		{401, 403, ""},
		{500, 502, ""},
		{503, 502, ""},
	} {
		code = c.engine
		req := httptest.NewRequest("POST", "/r/alpha/ci/bridge/v1/tasks/t1/token", strings.NewReader(`{}`))
		req.Header.Set("Authorization", "Bearer WT")
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, req)
		if rec.Code != c.want || rec.Header().Get(ReasonHeader) != c.reason {
			t.Errorf("FAIL: engine %d: status %d reason %q, want %d %q", c.engine, rec.Code, rec.Header().Get(ReasonHeader), c.want, c.reason)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A reason the engine sends itself never reaches the runner.
func TestAnEnginesOwnReasonHeaderIsNotPassedOn(t *testing.T) {
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set(ReasonHeader, ReasonTaskGone)
		w.WriteHeader(404)
	}))
	defer engine.Close()
	h, err := Handler(engine.URL, "ADMIN", func(string) string { return "g" }, func(r string) bool { return r == "alpha" }, nil, nil)
	if err != nil {
		t.Fatal(err)
	}
	for _, path := range []string{"/r/alpha/ci/bridge/v1/tasks/poll", "/r/alpha/ci/bridge/v1/tasks/t1/resolve"} {
		req := httptest.NewRequest("POST", path, strings.NewReader(`{}`))
		req.Header.Set("Authorization", "Bearer WT")
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, req)
		got := rec.Header().Get(ReasonHeader)
		if strings.HasSuffix(path, "poll") && got != "" {
			t.Errorf("FAIL: poll reached the client with reason %q", got)
		}
		if strings.HasSuffix(path, "resolve") && got != ReasonTaskGone {
			t.Errorf("FAIL: resolve carries %q, want the bridge's own %q", got, ReasonTaskGone)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}