Skip to content
quarry Artifact [6f809d394c]
Login

Artifact [6f809d394c]

Artifact 6f809d394cd0de9989b3e578408fe0e0f94d25f4650c0bd6f2492abc00e4be0c:


package runner

import (
	"bytes"
	"context"
	"errors"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"sync/atomic"
	"testing"
	"time"
)

// A CHECKOUT THE FORGE REFUSES IS A STEP IN THE LOG THAT SAYS WHAT.
//
// A pair's archive refused with a page of HTML used to fail the job
// with that page's first bytes and a hash cut to twelve characters,
// "pair:09eb9fa", which names neither tip. The log says which subject
// could not be fetched and the status received; never the credential,
// never a URL.
func TestARefusedCheckoutNamesWhatAndTheStatus(t *testing.T) {
	const secret = "s3cret-sync-token"
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("<!doctype html><html><body>not found</body></html>"))
	}))
	defer srv.Close()
	branch, target := strings.Repeat("a", 64), strings.Repeat("b", 64)
	r, err := New(Config{
		Bridge: "http://127.0.0.1:1", Token: "WT", Tasks: []string{"t"},
		WorkDir: t.TempDir(), Logf: t.Logf,
		Checkout:      ArchiveCheckout{URL: srv.URL + "/r/demo", User: "runner", Password: secret},
		CheckoutRetry: []time.Duration{time.Millisecond},
	})
	if err != nil {
		t.Fatal(err)
	}
	var logs bytes.Buffer
	err = r.execute(context.Background(), "task", "t", "pair:"+branch+":"+target, "", "", nil, &logs, &logs)
	if err == nil {
		t.Fatal("FAIL: a refused checkout passed")
	}
	var refused *ArchiveRefused
	if !errors.As(err, &refused) || refused.Status != http.StatusNotFound {
		t.Errorf("FAIL: the failure is %v, want an ArchiveRefused with the status", err)
	}
	for name, text := range map[string]string{"the log": logs.String(), "the error": err.Error()} {
		for _, want := range []string{"404", branch[:12], target[:12]} {
			if !strings.Contains(text, want) {
				t.Errorf("FAIL: %s does not contain %q:\n%s", name, want, text)
			}
		}
		for _, banned := range []string{secret, srv.URL, "<html", "<!doctype", "?"} {
			if strings.Contains(text, banned) {
				t.Errorf("FAIL: %s contains %q:\n%s", name, banned, text)
			}
		}
	}
	if !strings.Contains(logs.String(), "checkout failed") || strings.Count(logs.String(), "$ checkout") != 2 {
		t.Errorf("FAIL: the log does not end on a failed checkout after its retries:\n%s", logs.String())
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A TRANSPORT FAILURE NAMES THE FORGE'S SCHEME AND HOST, NEVER A PATH.
func TestAnUnreachableForgeIsLoggedWithoutItsPath(t *testing.T) {
	srv := httptest.NewServer(http.NotFoundHandler())
	base := srv.URL
	srv.Close()
	r, err := New(Config{
		Bridge: "http://127.0.0.1:1", Token: "WT", Tasks: []string{"t"},
		WorkDir: t.TempDir(), Logf: t.Logf,
		Checkout:      ArchiveCheckout{URL: base + "/r/an-owner/a-repo", User: "runner", Password: "s3cret"},
		CheckoutRetry: []time.Duration{time.Millisecond},
	})
	if err != nil {
		t.Fatal(err)
	}
	var logs bytes.Buffer
	err = r.execute(context.Background(), "task", "t", strings.Repeat("c", 64), "", "", nil, &logs, &logs)
	if err == nil {
		t.Fatal("FAIL: an unreachable forge passed")
	}
	for name, text := range map[string]string{"the log": logs.String(), "the error": err.Error()} {
		if !strings.Contains(text, "could not reach "+base) {
			t.Errorf("FAIL: %s does not name the forge's scheme and host:\n%s", name, text)
		}
		for _, banned := range []string{"/r/an-owner", "/archive/", "s3cret"} {
			if strings.Contains(text, banned) {
				t.Errorf("FAIL: %s contains %q:\n%s", name, banned, text)
			}
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// THE CHANGED PATHS COME FROM THE FORGE'S BRIDGE FOR THE TASK THE
// RUNNER HOLDS, so a private repository's runner needs no read
// credential on the API; a forge without the route leaves the runner's
// own lookups.
func TestChangedPathsComeFromTheBridgeAndFallBackWithoutIt(t *testing.T) {
	var asked []string
	withRoute := true
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		asked = append(asked, req.URL.Path)
		if !withRoute {
			http.NotFound(w, req)
			return
		}
		w.Write([]byte(`{"changed":["a.txt","d/b.txt"],"touched":null}`))
	}))
	defer srv.Close()
	own := 0
	newRunner := func() *Runner {
		r, err := New(Config{
			Bridge: srv.URL, Token: "WT", Tasks: []string{"t"}, WorkDir: t.TempDir(), Logf: t.Logf,
			Checkout: fakeCheckout{},
			Changed: func(context.Context, string) ([]string, error) {
				own++
				return []string{"own.txt"}, nil
			},
		})
		if err != nil {
			t.Fatal(err)
		}
		return r
	}
	read := func(env map[string]string, key string) string {
		b, _ := os.ReadFile(env[key])
		return strings.TrimSpace(string(b))
	}
	var logs bytes.Buffer
	env := newRunner().changedList(context.Background(), "run1.job", "pair:"+strings.Repeat("a", 64)+":"+strings.Repeat("b", 64), t.TempDir(), &logs)
	if got := read(env, "QUARRY_CHANGED"); got != "a.txt\nd/b.txt" || own != 0 {
		t.Errorf("FAIL: QUARRY_CHANGED = %q with %d own lookups; want the forge's list and none:\n%s", got, own, logs.String())
	}
	if len(asked) != 1 || !strings.HasSuffix(asked[0], "/v1/tasks/run1.job/changed") {
		t.Errorf("FAIL: the bridge was asked %v", asked)
	}
	withRoute = false
	env = newRunner().changedList(context.Background(), "run2.job", strings.Repeat("c", 64), t.TempDir(), &logs)
	if got := read(env, "QUARRY_CHANGED"); got != "own.txt" || own != 1 {
		t.Errorf("FAIL: with no route QUARRY_CHANGED = %q after %d own lookups; want the runner's own", got, own)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A BRANCH-ONLY RUNNER LEARNS THE RUN'S BRANCH FROM THE FORGE'S BRIDGE,
// so a private repository, whose API refuses its sync token, still
// runs its branch. Only a forge without the route leaves the runner's
// own lookup; a bridge that fails or cannot say is a refusal, never a
// call to the API, so nothing is charged to the account's limiter.
func TestABranchOnlyRunnerAsksTheBridgeForTheBranch(t *testing.T) {
	branch, status := "feat", http.StatusOK
	var asked atomic.Int32
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		asked.Add(1)
		if status != http.StatusOK {
			http.Error(w, "no", status)
			return
		}
		w.Write([]byte(`{"branch":"` + branch + `"}`))
	}))
	defer srv.Close()
	own := 0
	r, err := New(Config{
		Bridge: srv.URL, Token: "WT", Tasks: []string{"t"}, WorkDir: t.TempDir(), Logf: t.Logf,
		Checkout: fakeCheckout{}, Branch: "feat",
		InfoRetry: []time.Duration{time.Millisecond, time.Millisecond},
		BranchOf: func(context.Context, string) (string, error) {
			own++
			return "", errors.New("the forge answered 404")
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	ci, ctx := strings.Repeat("c", 64), context.Background()
	if err := r.onBranch(ctx, "a.job", ci); err != nil || own != 0 {
		t.Errorf("FAIL: a run on the branch: %v after %d own lookups; want it accepted from the bridge", err, own)
	}
	branch = "other"
	if err := r.onBranch(ctx, "b.job", ci); err == nil {
		t.Errorf("FAIL: a run on another branch was accepted")
	}
	// 503 is asked again, then an empty branch is a refusal: no API call.
	status = http.StatusServiceUnavailable
	before := asked.Load()
	if err := r.onBranch(ctx, "c.job", ci); err == nil || own != 0 || asked.Load()-before != 3 {
		t.Errorf("FAIL: a failing bridge: %v, %d own lookups, %d bridge asks; want a refusal, none, 3", err, own, asked.Load()-before)
	}
	status, branch = http.StatusOK, ""
	if err := r.onBranch(ctx, "d.job", ci); err == nil || own != 0 {
		t.Errorf("FAIL: an empty branch: %v after %d own lookups; want a refusal and no API call", err, own)
	}
	// A forge without the route is an older one: the runner's own lookup.
	status = http.StatusNotFound
	if err := r.onBranch(ctx, "e.job", ci); err == nil || own != 1 {
		t.Errorf("FAIL: with no route: %v after %d own lookups; want the runner's own lookup", err, own)
	}
	// Fetched once per task however many ask.
	status, branch = http.StatusOK, "feat"
	before = asked.Load()
	r.onBranch(ctx, "f.job", ci)
	r.changedList(ctx, "f.job", ci, t.TempDir(), io.Discard)
	if asked.Load()-before != 1 {
		t.Errorf("FAIL: the bridge was asked %d times for one task, want once", asked.Load()-before)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// THE RUNNER'S OWN FORGE LOOKUPS CARRY NO CREDENTIAL, so even the
// fallback for an older forge can never be charged to an account.
func TestTheForgeFallbackSendsNoCredential(t *testing.T) {
	var seen atomic.Value
	seen.Store("")
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		seen.Store(req.Header.Get("Authorization"))
		http.NotFound(w, req)
	}))
	defer srv.Close()
	if _, err := (ForgeBranch{API: srv.URL + "/api/v1/r/demo"}).BranchOf(context.Background(), strings.Repeat("a", 64)); err == nil {
		t.Fatal("FAIL: a 404 passed")
	}
	if seen.Load().(string) != "" {
		t.Errorf("FAIL: the lookup carried an Authorization header")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}