Skip to content
quarry Artifact [258a652aeb]
Login

Artifact [258a652aeb]

Artifact 258a652aeba84b07167325d6483cdecdbc6e87e6b2ad90691a1981b56bd61aff:


package install

import (
	"bytes"
	"context"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"errors"
	"regexp"
	"strings"
	"testing"
	"time"
)

func jsonUnmarshal(b []byte, v any) { _ = json.Unmarshal(b, v) }

type rig struct {
	sys   *fakeSys
	forge *testForge
	env   Env
	out   *bytes.Buffer
	vars  map[string]string
}

func newRig(t *testing.T) *rig {
	t.Helper()
	sys := newFakeSys()
	r := &rig{sys: sys, forge: newTestForge(t, "PROGRAM-BYTES"), out: &bytes.Buffer{}, vars: map[string]string{}}
	r.env = Env{
		Sys:     sys,
		HTTP:    r.forge.srv.Client(),
		Out:     r.out,
		Getenv:  func(k string) string { return r.vars[k] },
		Version: "v-test",
		GOOS:    "linux",
		Trunk:   func(context.Context, string) (bool, error) { return true, nil },
	}
	return r
}

func (r *rig) install(extra ...string) error {
	args := append([]string{r.forge.repoURL()}, extra...)
	return Install(context.Background(), r.env, args)
}

func exitCode(err error) int {
	if err == nil {
		return 0
	}
	var ee *ExitError
	if errors.As(err, &ee) {
		return ee.Code
	}
	return -1
}

const wantUnit = `[Unit]
Description=quarry runner for URL
After=network-online.target
Wants=network-online.target

[Service]
User=qr-suit
Group=qr-suit
WorkingDirectory=/var/lib/quarry-runner/suit
EnvironmentFile=/etc/quarry-runner/suit.env
Environment=HOME=/var/lib/quarry-runner/suit
ExecStart=/usr/local/lib/quarry-runner/quarry-runner --work /var/lib/quarry-runner/suit/work --max-tasks 2 --job-users qj-suit-1,qj-suit-2 URL
Restart=always
RestartSec=5
KillMode=mixed
TimeoutStopSec=90

[Install]
WantedBy=multi-user.target
`

func TestInstallCreatesExactlyTheRFCSet(t *testing.T) {
	r := newRig(t)
	if err := r.install("--code", "qri_abc"); err != nil {
		t.Fatalf("FAIL: install: %v\n%s", err, r.out)
	}
	type want struct {
		owner, group string
		mode         uint32
	}
	files := map[string]want{
		"/usr/local/lib/quarry-runner/quarry-runner":     {"root", "root", 0o755},
		"/var/lib/quarry-runner/suit":                    {"qr-suit", "qr-suit", 0o711},
		"/var/lib/quarry-runner/suit/work":               {"qr-suit", "qr-suit", 0o750},
		"/etc/sudoers.d/quarry-runner-suit":              {"root", "root", 0o440},
		"/etc/quarry-runner/suit.env":                    {"root", "root", 0o600},
		"/etc/quarry-runner/suit.json":                   {"root", "root", 0o644},
		"/etc/systemd/system/quarry-runner-suit.service": {"root", "root", 0o644},
	}
	for p, w := range files {
		x, ok := r.sys.files[p]
		if !ok {
			t.Errorf("FAIL: %s was not made", p)
			continue
		}
		if x.owner != w.owner || x.group != w.group || uint32(x.mode) != w.mode {
			t.Errorf("FAIL: %s is %s:%s %04o, want %s:%s %04o", p, x.owner, x.group, x.mode, w.owner, w.group, w.mode)
		}
	}
	for _, u := range []string{"qr-suit", "qj-suit-1", "qj-suit-2"} {
		if !r.sys.UserExists(u) || !r.sys.GroupExists(u) {
			t.Errorf("FAIL: user or group %s missing", u)
		}
	}
	for _, g := range []string{"qj-suit-1", "qj-suit-2"} {
		if !r.sys.InGroup("qr-suit", g) {
			t.Errorf("FAIL: qr-suit is not in %s", g)
		}
	}
	if got := r.sys.users["qr-suit"].Home; got != "/var/lib/quarry-runner/suit" {
		t.Errorf("FAIL: service home %q", got)
	}
	if got := r.sys.users["qj-suit-1"].Home; got != "" {
		t.Errorf("FAIL: a job user has home %q", got)
	}
	if got := string(r.sys.files["/etc/sudoers.d/quarry-runner-suit"].data); got != "qr-suit ALL=(qj-suit-1,qj-suit-2) NOPASSWD: ALL\n" {
		t.Errorf("FAIL: sudoers %q", got)
	}
	if got := string(r.sys.files["/etc/quarry-runner/suit.env"].data); got != "QUARRY_RUNNER_TOKEN=wt-secret-1\n" {
		t.Errorf("FAIL: env %q", got)
	}
	if got, want := string(r.sys.files["/etc/systemd/system/quarry-runner-suit.service"].data), strings.ReplaceAll(wantUnit, "URL", r.forge.repoURL()); got != want {
		t.Errorf("FAIL: unit text\n%s\nwant\n%s", got, want)
	}
	if string(r.sys.files["/usr/local/lib/quarry-runner/quarry-runner"].data) != "PROGRAM-BYTES" {
		t.Errorf("FAIL: the program is not a copy of the running binary")
	}
	var m map[string]any
	if err := json.Unmarshal(r.sys.files["/etc/quarry-runner/suit.json"].data, &m); err != nil {
		t.Fatalf("FAIL: manifest is not JSON: %v", err)
	}
	for _, k := range []string{"url", "name", "users", "groups", "paths", "token_id", "version"} {
		if _, ok := m[k]; !ok {
			t.Errorf("FAIL: manifest has no %q key", k)
		}
	}
	if m["token_id"] != "wt1" || m["version"] != "v-test" || m["url"] != r.forge.repoURL() {
		t.Errorf("FAIL: manifest %v", m)
	}
	// Order: visudo validates before the rule is moved into place.
	if v, mv := r.sys.indexOf("visudo -cf"), r.sys.indexOf("Rename /etc/sudoers.d"); v < 0 || mv < 0 || v > mv {
		t.Errorf("FAIL: visudo at %d, move at %d", v, mv)
	}
	if r.sys.indexOf("systemctl enable --now quarry-runner-suit.service") < 0 {
		t.Errorf("FAIL: the unit was not enabled and started: %v", r.sys.calls)
	}
	// Redeemed once, as this host, after creation and before the env file.
	if len(r.forge.redeems) != 1 {
		t.Fatalf("FAIL: redeemed %d times", len(r.forge.redeems))
	}
	rd := r.forge.redeems[0]
	if rd["code"] != "qri_abc" || rd["host"] != "build-box" {
		t.Errorf("FAIL: redeem body %v", rd)
	}
	if _, ok := rd["label"]; !ok {
		t.Errorf("FAIL: redeem body has no label: %v", rd)
	}
	if !r.sys.Exists("/etc/quarry-runner/suit.env") || r.sys.indexOf("AddUser") > r.sys.indexOf("WriteFile /etc/quarry-runner/suit.env") {
		t.Errorf("FAIL: users were not made before the env file")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestInstallPassesFlagsThroughToExecStart(t *testing.T) {
	r := newRig(t)
	if err := r.install("--code", "c", "--jobs", "3", "--checkout", "zit", "--zit", "/opt/zit", "--branch", "trunk", "--tasks", "a,b"); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	unit := string(r.sys.files["/etc/systemd/system/quarry-runner-suit.service"].data)
	want := "--work /var/lib/quarry-runner/suit/work --max-tasks 3 --job-users qj-suit-1,qj-suit-2,qj-suit-3 --checkout zit --zit /opt/zit --branch trunk --tasks a,b " + r.forge.repoURL()
	if !strings.Contains(unit, want) {
		t.Errorf("FAIL: ExecStart lacks %q in\n%s", want, unit)
	}
	if !r.sys.UserExists("qj-suit-3") {
		t.Errorf("FAIL: --jobs 3 made no third job user")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestSecondInstallIsANoOpRepair(t *testing.T) {
	r := newRig(t)
	if err := r.install("--code", "qri_abc"); err != nil {
		t.Fatalf("FAIL: first: %v", err)
	}
	before := r.sys.snapshot()
	r.sys.calls = nil
	// No code now: the token is already in the env file.
	if err := r.install(); err != nil {
		t.Fatalf("FAIL: second: %v\n%s", err, r.out)
	}
	if len(r.forge.redeems) != 1 {
		t.Errorf("FAIL: redeemed %d times, want once in all", len(r.forge.redeems))
	}
	for _, c := range []string{"AddUser", "AddGroup", "AddToGroup", "DelUser"} {
		if n := r.sys.called(c); n != 0 {
			t.Errorf("FAIL: second install made %d %s calls", n, c)
		}
	}
	if after := r.sys.snapshot(); after != before {
		t.Errorf("FAIL: second install changed the machine:\n%s\n---\n%s", before, after)
	}
	// A deleted file is repaired.
	delete(r.sys.files, "/etc/sudoers.d/quarry-runner-suit")
	if err := r.install(); err != nil {
		t.Fatalf("FAIL: repair: %v", err)
	}
	if !r.sys.Exists("/etc/sudoers.d/quarry-runner-suit") {
		t.Errorf("FAIL: the sudo rule was not repaired")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestFailureBeforeRedeemRollsBackAndSpendsNoCode(t *testing.T) {
	cases := map[string]func(r *rig){
		"visudo rejects the rule": func(r *rig) { r.sys.fail["visudo"] = errors.New("syntax") },
		"usermod fails":           func(r *rig) { r.sys.fail["AddToGroup"] = errors.New("boom") },
		"unit cannot be written":  func(r *rig) { r.sys.fail["WriteFile /etc/systemd"] = errors.New("disk") },
	}
	for name, setup := range cases {
		r := newRig(t)
		before := r.sys.snapshot()
		setup(r)
		if err := r.install("--code", "qri_abc"); err == nil {
			t.Errorf("FAIL: %s: install succeeded", name)
			continue
		}
		if n := len(r.forge.redeems); n != 0 {
			t.Errorf("FAIL: %s: redeem was called %d times", name, n)
		}
		if after := r.sys.snapshot(); after != before {
			t.Errorf("FAIL: %s: left behind:\n%s\n---\nwas:\n%s", name, after, before)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestARefusedRedeemRollsBack(t *testing.T) {
	r := newRig(t)
	before := r.sys.snapshot()
	r.forge.redeemErr = 404
	if err := r.install("--code", "qri_bad"); err == nil {
		t.Fatalf("FAIL: install succeeded on a refused code")
	}
	if after := r.sys.snapshot(); after != before {
		t.Errorf("FAIL: a refused code left the machine changed:\n%s", after)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestFailureAfterRedeemKeepsTheTokenAndNamesDoctor(t *testing.T) {
	r := newRig(t)
	r.sys.fail["systemctl enable"] = errors.New("no bus")
	err := r.install("--code", "qri_abc")
	if err == nil {
		t.Fatalf("FAIL: install succeeded")
	}
	if !strings.Contains(string(r.sys.files["/etc/quarry-runner/suit.env"].data), "wt-secret-1") {
		t.Errorf("FAIL: the token was not kept")
	}
	if !strings.Contains(err.Error(), "doctor") {
		t.Errorf("FAIL: the error does not point at doctor: %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestInstallRefusesWhatItCannotDo(t *testing.T) {
	cases := map[string]func(r *rig) []string{
		"not root":   func(r *rig) []string { r.sys.uid = 1000; return []string{"--code", "c"} },
		"other os":   func(r *rig) []string { r.env.GOOS = "freebsd"; return []string{"--code", "c"} },
		"no systemd": func(r *rig) []string { delete(r.sys.files, "/run/systemd/system"); return []string{"--code", "c"} },
		"no visudo":  func(r *rig) []string { r.sys.tools["visudo"] = false; return []string{"--code", "c"} },
		"user mode":  func(r *rig) []string { return []string{"--code", "c", "--user"} },
	}
	for name, setup := range cases {
		r := newRig(t)
		r.env.Prompt = func(string) (string, error) { return "", nil }
		args := setup(r)
		before := r.sys.snapshot()
		if err := r.install(args...); err == nil {
			t.Errorf("FAIL: %s: install succeeded", name)
		}
		if len(r.forge.redeems) != 0 || r.sys.snapshot() != before {
			t.Errorf("FAIL: %s: it changed something", name)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestTheCodeMayComeFromTheEnvironmentOrAPrompt(t *testing.T) {
	r := newRig(t)
	r.vars["QUARRY_INSTALL_CODE"] = "qri_env"
	if err := r.install(); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	if got := r.forge.redeems[0]["code"]; got != "qri_env" {
		t.Errorf("FAIL: redeemed %v", got)
	}
	r = newRig(t)
	r.env.Prompt = func(string) (string, error) { return " qri_typed\n", nil }
	if err := r.install("--code", "-"); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	if got := r.forge.redeems[0]["code"]; got != "qri_typed" {
		t.Errorf("FAIL: redeemed %v", got)
	}
	// A token in the environment and no code: nothing is redeemed.
	r = newRig(t)
	r.vars["QUARRY_RUNNER_TOKEN"] = r.forge.token
	if err := r.install(); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	if len(r.forge.redeems) != 0 {
		t.Errorf("FAIL: redeemed with a token in hand")
	}
	if got := string(r.sys.files["/etc/quarry-runner/suit.env"].data); got != "QUARRY_RUNNER_TOKEN=wt-secret-1\n" {
		t.Errorf("FAIL: env %q", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestNameCutsLongGroupsAndAppendsAHash(t *testing.T) {
	if got := Name("suit"); got != "suit" {
		t.Errorf("FAIL: Name(suit) = %q", got)
	}
	exact := strings.Repeat("a", 20)
	if got := Name(exact); got != exact {
		t.Errorf("FAIL: a 20-character group was cut: %q", got)
	}
	long := strings.Repeat("b", 30)
	sum := sha256.Sum256([]byte(long))
	if got, want := Name(long), strings.Repeat("b", 20)+hex.EncodeToString(sum[:])[:4]; got != want {
		t.Errorf("FAIL: Name(long) = %q, want %q", got, want)
	}
	if Name(long+"x") == Name(long) {
		t.Errorf("FAIL: two long groups share a name")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// ---- doctor ----

var statusEnd = regexp.MustCompile(`(ok|warn|FAIL)$`)

func (r *rig) doctor(args ...string) (int, string) {
	r.out.Reset()
	err := Doctor(context.Background(), r.env, args)
	return exitCode(err), r.out.String()
}

func installed(t *testing.T) *rig {
	t.Helper()
	r := newRig(t)
	if err := r.install("--code", "qri_abc"); err != nil {
		t.Fatalf("FAIL: setup install: %v\n%s", err, r.out)
	}
	return r
}

func TestDoctorOnAHealthyInstallIsTwelveOkLines(t *testing.T) {
	r := installed(t)
	code, out := r.doctor("suit")
	lines := strings.Split(strings.TrimSpace(out), "\n")
	if code != 0 || len(lines) != 12 {
		t.Fatalf("FAIL: exit %d with %d lines:\n%s", code, len(lines), out)
	}
	for i, l := range lines {
		if !strings.HasSuffix(l, "ok") {
			t.Errorf("FAIL: line %d does not end ok: %q", i+1, l)
		}
	}
	// The URL names the same runner, and so does no argument at all.
	if c, _ := r.doctor(r.forge.repoURL()); c != 0 {
		t.Errorf("FAIL: doctor by URL exited %d", c)
	}
	if c, _ := r.doctor(); c != 0 {
		t.Errorf("FAIL: doctor with one runner and no argument exited %d", c)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestDoctorExitCodesFollowTheWorstLine(t *testing.T) {
	type tc struct {
		name   string
		break_ func(r *rig)
		args   []string
		code   int
		check  int // the line that is not ok, 0 for none
		last   string
	}
	cases := []tc{
		{"a warn: disk low", func(r *rig) { r.sys.diskFree = 1 << 20 }, nil, 3, 12, "warn"},
		{"a warn: nothing to run", func(r *rig) { r.env.Trunk = func(context.Context, string) (bool, error) { return false, nil } }, nil, 3, 10, "warn"},
		{"a warn: older program", func(r *rig) { r.forge.sums = strings.Repeat("0", 64) + "  quarry-runner-linux-amd64\n" }, nil, 3, 2, "warn"},
		{"FAIL beats warn", func(r *rig) {
			r.sys.diskFree = 1 << 20
			delete(r.sys.files, "/etc/sudoers.d/quarry-runner-suit")
		}, nil, 1, 5, "FAIL"},
		{"no manifest", func(r *rig) { delete(r.sys.files, "/etc/quarry-runner/suit.json") }, []string{"suit"}, 1, 1, "FAIL"},
		{"program missing", func(r *rig) { delete(r.sys.files, "/usr/local/lib/quarry-runner/quarry-runner") }, nil, 1, 2, "FAIL"},
		{"a user gone", func(r *rig) { delete(r.sys.users, "qj-suit-2") }, nil, 1, 3, "FAIL"},
		{"not in the job group", func(r *rig) { delete(r.sys.groups["qj-suit-1"], "qr-suit") }, nil, 1, 3, "FAIL"},
		{"sudo refused", func(r *rig) { r.sys.fail["as:qr-suit:sudo"] = errors.New("a password is required") }, nil, 1, 4, "FAIL"},
		{"wrong mode", func(r *rig) { r.sys.files["/etc/quarry-runner/suit.env"].mode = 0o644 }, nil, 1, 5, "FAIL"},
		{"empty env", func(r *rig) { r.sys.files["/etc/quarry-runner/suit.env"].data = []byte("QUARRY_RUNNER_TOKEN=\n") }, nil, 1, 6, "FAIL"},
		{"unit not active", func(r *rig) { r.sys.fail["systemctl is-active"] = errors.New("inactive") }, nil, 1, 7, "FAIL"},
		{"token revoked", func(r *rig) { r.forge.token = "another" }, nil, 1, 9, "FAIL"},
		{"usage", func(r *rig) {}, []string{"--nope"}, 2, 0, ""},
		{"two runners, no argument", func(r *rig) {
			r.sys.files["/etc/quarry-runner/other.json"] = &fakeFile{data: []byte("{}")}
		}, nil, 2, 0, ""},
	}
	for _, c := range cases {
		r := installed(t)
		c.break_(r)
		code, out := r.doctor(c.args...)
		if code != c.code {
			t.Errorf("FAIL: %s: exit %d, want %d\n%s", c.name, code, c.code, out)
			continue
		}
		if c.check == 0 {
			continue
		}
		lines := strings.Split(strings.TrimSpace(out), "\n")
		if len(lines) != 12 {
			t.Errorf("FAIL: %s: %d lines", c.name, len(lines))
			continue
		}
		for i, l := range lines {
			if !statusEnd.MatchString(l) {
				t.Errorf("FAIL: %s: line %d has no status: %q", c.name, i+1, l)
			}
		}
		if !strings.HasSuffix(lines[c.check-1], c.last) {
			t.Errorf("FAIL: %s: line %d is %q, want it to end %s", c.name, c.check, lines[c.check-1], c.last)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestDoctorJSONCarriesTheSameChecks(t *testing.T) {
	r := installed(t)
	r.sys.diskFree = 1 << 20
	code, out := r.doctor("--json", "suit")
	var got struct {
		Checks []struct {
			N      int    `json:"n"`
			Name   string `json:"name"`
			Status string `json:"status"`
			Detail string `json:"detail"`
		} `json:"checks"`
		Exit int `json:"exit"`
	}
	if err := json.Unmarshal([]byte(out), &got); err != nil {
		t.Fatalf("FAIL: not JSON: %v\n%s", err, out)
	}
	if code != 3 || got.Exit != 3 || len(got.Checks) != 12 {
		t.Fatalf("FAIL: exit %d/%d, %d checks", code, got.Exit, len(got.Checks))
	}
	if got.Checks[11].N != 12 || got.Checks[11].Status != "warn" || got.Checks[0].Status != "ok" {
		t.Errorf("FAIL: %+v", got.Checks)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// ---- uninstall ----

func TestUninstallRemovesOnlyWhatTheManifestListsAndRetiresTheToken(t *testing.T) {
	r := installed(t)
	// Another runner's files, a user that was here first and a stray
	// file in this runner's own home are not this install's to remove.
	r.sys.files["/etc/quarry-runner/other.json"] = &fakeFile{data: []byte("{}")}
	r.sys.files["/var/lib/quarry-runner/other"] = &fakeFile{dir: true}
	r.sys.files["/etc/sudoers.d/unrelated"] = &fakeFile{data: []byte("x")}
	r.sys.users["bystander"] = User{Name: "bystander"}
	r.sys.calls = nil
	err := Uninstall(context.Background(), r.env, []string{"suit"})
	if err != nil {
		t.Fatalf("FAIL: uninstall: %v\n%s", err, r.out)
	}
	if len(r.forge.retires) != 1 || r.forge.retires[0]["token"] != "wt-secret-1" || r.forge.retires[0]["host"] != "build-box" {
		t.Errorf("FAIL: retire calls %v", r.forge.retires)
	}
	if r.sys.indexOf("systemctl disable --now quarry-runner-suit.service") < 0 {
		t.Errorf("FAIL: the unit was not stopped and disabled: %v", r.sys.calls)
	}
	if i, j := r.sys.indexOf("systemctl disable"), r.sys.indexOf("Remove"); i < 0 || j >= 0 && j < i {
		t.Errorf("FAIL: files were removed before the unit stopped")
	}
	for _, p := range []string{
		"/etc/systemd/system/quarry-runner-suit.service", "/etc/sudoers.d/quarry-runner-suit",
		"/etc/quarry-runner/suit.env", "/etc/quarry-runner/suit.json",
		"/var/lib/quarry-runner/suit", "/var/lib/quarry-runner/suit/work",
	} {
		if r.sys.Exists(p) {
			t.Errorf("FAIL: %s is still there", p)
		}
	}
	for _, u := range []string{"qr-suit", "qj-suit-1", "qj-suit-2"} {
		if r.sys.UserExists(u) || r.sys.GroupExists(u) {
			t.Errorf("FAIL: %s is still there", u)
		}
	}
	for _, p := range []string{"/etc/quarry-runner/other.json", "/var/lib/quarry-runner/other", "/etc/sudoers.d/unrelated"} {
		if !r.sys.Exists(p) {
			t.Errorf("FAIL: %s, not in the manifest, was removed", p)
		}
	}
	if !r.sys.UserExists("bystander") {
		t.Errorf("FAIL: an unrelated user was removed")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestUninstallKeepsOtherRunnersProgram(t *testing.T) {
	r := installed(t)
	r.sys.files["/etc/quarry-runner/other.json"] = &fakeFile{data: []byte("{}")}
	if err := Uninstall(context.Background(), r.env, []string{"suit"}); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	if !r.sys.Exists("/usr/local/lib/quarry-runner/quarry-runner") {
		t.Errorf("FAIL: the program another runner uses was removed")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestUninstallKeepWorkLeavesTheWorkDirectory(t *testing.T) {
	r := installed(t)
	r.sys.files["/var/lib/quarry-runner/suit/work/ws1"] = &fakeFile{dir: true}
	if err := Uninstall(context.Background(), r.env, []string{"--keep-work", "suit"}); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	if !r.sys.Exists("/var/lib/quarry-runner/suit/work/ws1") {
		t.Errorf("FAIL: --keep-work removed the work directory")
	}
	if r.sys.Exists("/usr/local/lib/quarry-runner/quarry-runner") {
		t.Errorf("FAIL: the last runner left the program")
	}
	if r.sys.Exists("/etc/quarry-runner/suit.env") || r.sys.UserExists("qr-suit") {
		t.Errorf("FAIL: --keep-work kept more than the work directory")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestUninstallRefusesAManifestNamingSystemPaths(t *testing.T) {
	r := installed(t)
	var m map[string]any
	json.Unmarshal(r.sys.files["/etc/quarry-runner/suit.json"].data, &m)
	m["paths"] = []string{"/etc", "relative/x"}
	b, _ := json.Marshal(m)
	r.sys.files["/etc/quarry-runner/suit.json"].data = b
	r.sys.calls = nil
	if err := Uninstall(context.Background(), r.env, []string{"suit"}); err == nil {
		t.Errorf("FAIL: uninstall accepted /etc")
	}
	if r.sys.called("RemoveAll") != 0 || !r.sys.Exists("/etc/sudoers.d") {
		t.Errorf("FAIL: it removed something: %v", r.sys.calls)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// ---- a token the forge hands back is not trusted ----

func TestATokenThatCouldAddEnvLinesIsRefusedAndRetired(t *testing.T) {
	for _, bad := range []string{"tok\nQUARRY_EVIL=1", "short", "has space in it", "q\"uote12345"} {
		r := newRig(t)
		before := r.sys.snapshot()
		r.forge.token = bad
		if err := r.install("--code", "c"); err == nil {
			t.Errorf("FAIL: %q was accepted", bad)
		}
		if r.sys.snapshot() != before {
			t.Errorf("FAIL: %q left the machine changed", bad)
		}
		if env, ok := r.sys.files["/etc/quarry-runner/suit.env"]; ok {
			t.Errorf("FAIL: env file written for %q: %q", bad, env.data)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A LIVE TOKEN IS NEVER DROPPED: retired when the install cannot use
// it, kept in the env file when even that fails.
func TestAGroupMismatchRetiresTheTokenOrKeepsIt(t *testing.T) {
	r := newRig(t)
	before := r.sys.snapshot()
	r.forge.group = "elsewhere"
	if err := r.install("--code", "c"); err == nil {
		t.Fatalf("FAIL: a mismatched group was accepted")
	}
	if len(r.forge.retires) != 1 || r.forge.retires[0]["token"] != r.forge.token {
		t.Errorf("FAIL: retire calls %v", r.forge.retires)
	}
	if r.sys.snapshot() != before {
		t.Errorf("FAIL: a retired token left the install behind")
	}

	r = newRig(t)
	r.forge.group = "elsewhere"
	r.forge.retireStatus = 500
	err := r.install("--code", "c")
	if err == nil {
		t.Fatalf("FAIL: a mismatched group was accepted")
	}
	env, ok := r.sys.files["/etc/quarry-runner/suit.env"]
	if !ok || !strings.Contains(string(env.data), r.forge.token) {
		t.Errorf("FAIL: the live token was dropped")
	}
	if !strings.Contains(err.Error(), "doctor") {
		t.Errorf("FAIL: no doctor hint: %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// ---- names and manifests are checked before anything is done with them ----

func TestRunnerNamesAreChecked(t *testing.T) {
	r := installed(t)
	for _, bad := range []string{"../../tmp/x", "a/b", "x y", ".."} {
		r.sys.calls = nil
		if code, _ := r.doctor(bad); code != 2 {
			t.Errorf("FAIL: doctor %q exited %d, want 2", bad, code)
		}
		if err := Uninstall(context.Background(), r.env, []string{bad}); exitCode(err) != 2 {
			t.Errorf("FAIL: uninstall %q answered %v, want exit 2", bad, err)
		}
		if len(r.sys.calls) != 0 {
			t.Errorf("FAIL: %q reached the machine: %v", bad, r.sys.calls)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestUninstallRefusesPathsAndNamesAnInstallDoesNotMake(t *testing.T) {
	for name, edit := range map[string]func(m map[string]any){
		"a deep path elsewhere": func(m map[string]any) { m["paths"] = []string{"/var/lib/other/deep/dir"} },
		"another runner's home": func(m map[string]any) { m["paths"] = []string{"/var/lib/quarry-runner/other"} },
		"a user not ours":       func(m map[string]any) { m["users"] = []string{"root"} },
		"a group not ours":      func(m map[string]any) { m["groups"] = []string{"wheel"} },
		"a bad program":         func(m map[string]any) { m["program"] = "/bin/sh" },
	} {
		r := installed(t)
		var m map[string]any
		json.Unmarshal(r.sys.files["/etc/quarry-runner/suit.json"].data, &m)
		edit(m)
		b, _ := json.Marshal(m)
		r.sys.files["/etc/quarry-runner/suit.json"].data = b
		r.sys.calls = nil
		if err := Uninstall(context.Background(), r.env, []string{"suit"}); err == nil {
			t.Errorf("FAIL: %s: uninstall went ahead", name)
		}
		if r.sys.called("RemoveAll")+r.sys.called("DelUser")+r.sys.called("DelGroup")+r.sys.called("systemctl") != 0 {
			t.Errorf("FAIL: %s: it acted: %v", name, r.sys.calls)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// ---- uninstall does not call an unretired token retired ----

func TestUninstallFailsWhenTheTokenIsNotRetired(t *testing.T) {
	for _, status := range []int{404, 500} {
		r := installed(t)
		r.forge.retireStatus = status
		err := Uninstall(context.Background(), r.env, []string{"suit"})
		if err == nil {
			t.Errorf("FAIL: a %d on retire was taken for success", status)
			continue
		}
		if !strings.Contains(err.Error(), "wt1") {
			t.Errorf("FAIL: the token id is not in the error: %v", err)
		}
		// The rest is still removed.
		if r.sys.Exists("/etc/quarry-runner/suit.env") || r.sys.UserExists("qr-suit") {
			t.Errorf("FAIL: a failed retire left the install in place")
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// ---- doctor never runs the manifest's binary as root ----

func TestDoctorRunsZitAsTheServiceUser(t *testing.T) {
	r := newRig(t)
	if err := r.install("--code", "c", "--checkout", "zit", "--zit", "/opt/zit"); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	r.sys.calls = nil
	if code, out := r.doctor("suit"); code != 0 {
		t.Fatalf("FAIL: exit %d\n%s", code, out)
	}
	if r.sys.called("as:qr-suit:/opt/zit") != 1 {
		t.Errorf("FAIL: zit did not run as the service user: %v", r.sys.calls)
	}
	if r.sys.called("/opt/zit") != 0 {
		t.Errorf("FAIL: zit ran as the caller (root): %v", r.sys.calls)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The program is copied whole however large it is: a copy read through
// ReadFile once installed a program cut off at 64 MiB, which crashed.
func TestInstallCopiesAProgramLargerThanReadFileReads(t *testing.T) {
	r := newRig(t)
	big := bytes.Repeat([]byte("p"), 4096)
	r.sys.files[r.sys.exe].data = big
	r.sys.readMax = 1024
	// Doctor warns (exit 3) that this program is not a build the test
	// forge lists; the install itself must succeed.
	if err := r.install("--code", "qri_abc"); err != nil && exitCode(err) != 3 {
		t.Fatalf("FAIL: install: %v (code %d)\n%s", err, exitCode(err), r.out)
	}
	got := r.sys.files["/usr/local/lib/quarry-runner/quarry-runner"]
	if got == nil || !bytes.Equal(got.data, big) {
		t.Errorf("FAIL: the program copy is not the whole program")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// ---- doctor's bridge check is the forge's own token check ----

func bridgeCheck(t *testing.T, r *rig) (int, Check) {
	t.Helper()
	r.out.Reset()
	err := Doctor(context.Background(), r.env, []string{"--json", "suit"})
	var out struct {
		Checks []Check `json:"checks"`
	}
	if jerr := json.Unmarshal(r.out.Bytes(), &out); jerr != nil {
		t.Fatalf("FAIL: doctor's JSON: %v\n%s", jerr, r.out)
	}
	for _, c := range out.Checks {
		if c.Name == "bridge" {
			return exitCode(err), c
		}
	}
	t.Fatal("FAIL: no bridge check")
	return 0, Check{}
}

func TestDoctorBridgeUsesTheCheckRouteAndRegistersNoWorker(t *testing.T) {
	r := installed(t)
	if code, c := bridgeCheck(t, r); code != 0 || c.Status != statOK {
		t.Errorf("FAIL: healthy: exit %d, check %+v", code, c)
	}
	if r.forge.connects != 0 {
		t.Errorf("FAIL: %d connects were made", r.forge.connects)
	}
	r.forge.token = "another"
	if code, c := bridgeCheck(t, r); code != 1 || c.Status != statFail {
		t.Errorf("FAIL: unrecorded: exit %d, check %+v", code, c)
	}
	r = installed(t)
	r.forge.other = "elsewhere"
	if code, c := bridgeCheck(t, r); code != 1 || c.Status != statFail || !strings.Contains(c.Detail, "elsewhere") {
		t.Errorf("FAIL: other repo: exit %d, check %+v", code, c)
	}
	if r.forge.connects != 0 {
		t.Errorf("FAIL: %d connects were made", r.forge.connects)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestDoctorOnAnOlderForgeFallsBackToConnectAndNeverFailsOnTheDeadline(t *testing.T) {
	old := bridgeDeadline
	bridgeDeadline = 200 * time.Millisecond
	defer func() { bridgeDeadline = old }()
	r := installed(t)
	r.forge.oldForge = true
	if code, c := bridgeCheck(t, r); code != 0 || c.Status != statOK || r.forge.connects != 1 {
		t.Errorf("FAIL: older forge, answering: exit %d, check %+v, %d connects", code, c, r.forge.connects)
	}
	r.forge.hangConnect = true
	if code, c := bridgeCheck(t, r); code != 3 || c.Status != statWarn {
		t.Errorf("FAIL: older forge, hanging: exit %d, check %+v", code, c)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestInstallExitsZeroWhenDoctorOnlyWarns(t *testing.T) {
	old := bridgeDeadline
	bridgeDeadline = 100 * time.Millisecond
	defer func() { bridgeDeadline = old }()
	r := newRig(t)
	r.forge.oldForge, r.forge.hangConnect = true, true
	if err := r.install("--code", "qri_abc"); err != nil {
		t.Errorf("FAIL: install with only a warning: %v\n%s", err, r.out)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}