package runner
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"slices"
"testing"
)
// failingCheckout is a checkout that must not be used.
type failingCheckout struct{ called bool }
func (f *failingCheckout) Checkout(context.Context, string, string) error {
f.called = true
return errors.New("no account to fetch trunk as")
}
// A RUNNER NAMES ITS TASK TYPES AT START-UP FROM THE FORGE, with its
// worker token alone: it needs no account to read trunk's manifest, and
// a forge that cannot say leaves the runner reading trunk itself.
func TestARunnerReadsItsTaskTypesFromTheForgeAtStartUp(t *testing.T) {
var bearer string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/trunk/tasks" {
http.NotFound(w, r)
return
}
bearer = r.Header.Get("Authorization")
json.NewEncoder(w).Encode(map[string][]string{"tasks": {"lint", "test"}})
}))
defer srv.Close()
co := &failingCheckout{}
got, err := startupTasks(context.Background(), nil, t.Logf, srv.URL, "WT", "demo", co, t.TempDir())
if err != nil || co.called || bearer != "Bearer WT" {
t.Fatalf("FAIL: %v, checkout used %v, bearer %q; want the forge's answer by the worker token, no checkout", err, co.called, bearer)
}
if !slices.Contains(got, "lint") || !slices.Contains(got, "test") || len(got) != 3 {
t.Errorf("FAIL: the task types are %v; want the two names and the repository's job type", got)
}
// A forge without the route: trunk is read with the runner's own checkout.
old := httptest.NewServer(http.NotFoundHandler())
defer old.Close()
manifest := `{"tasks":{"fmt":["true"]}}`
got, err = startupTasks(context.Background(), nil, t.Logf, old.URL, "WT", "demo", fakeCheckout{map[string]string{"trunk": manifest}}, t.TempDir())
if err != nil || len(got) != 2 || !slices.Contains(got, "fmt") {
t.Errorf("FAIL: an older forge: %v, %v; want fmt and the job type from trunk", got, err)
}
if _, err := startupTasks(context.Background(), nil, t.Logf, old.URL, "WT", "demo", &failingCheckout{}, t.TempDir()); err == nil {
t.Errorf("FAIL: an older forge and a checkout that cannot read trunk started a runner")
}
// A forge that answers with a fault does not start it on a guess.
sick := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "busy", http.StatusServiceUnavailable)
}))
defer sick.Close()
co = &failingCheckout{}
if _, err := startupTasks(context.Background(), nil, t.Logf, sick.URL, "WT", "demo", co, t.TempDir()); err == nil || co.called {
t.Errorf("FAIL: a forge's fault was %v (checkout used %v); want an error and no fallback", err, co.called)
}
}
// A WORKER TOKEN THE FORGE HAS NO RECORD OF (minted before it kept one)
// is answered 401 by the task-names route. The runner does not exit: it
// reads trunk itself, as for an older forge, and says once that the forge
// does not know the token.
func TestARunnerWithAnUnrecordedTokenReadsTrunkItself(t *testing.T) {
forge := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Bearer realm="quarry runners"`)
http.Error(w, "unknown", http.StatusUnauthorized)
}))
defer forge.Close()
var logged []string
logf := func(format string, args ...any) { logged = append(logged, format) }
manifest := `{"tasks":{"fmt":["true"]}}`
got, err := startupTasks(context.Background(), nil, logf, forge.URL, "WT", "demo", fakeCheckout{map[string]string{"trunk": manifest}}, t.TempDir())
if err != nil || len(got) != 2 || !slices.Contains(got, "fmt") {
t.Errorf("FAIL: an unrecorded token: %v, %v; want trunk's names from the runner's own checkout", got, err)
}
if len(logged) != 1 {
t.Errorf("FAIL: %d lines logged, want one saying the forge does not know the token", len(logged))
}
// With no checkout that can read trunk, it stops, as it always did.
if _, err := startupTasks(context.Background(), nil, logf, forge.URL, "WT", "demo", &failingCheckout{}, t.TempDir()); err == nil {
t.Errorf("FAIL: an unrecorded token and a checkout that cannot read trunk started a runner")
}
}