package install
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"path"
"sort"
"strings"
"sync"
"testing"
)
type fakeFile struct {
data []byte
owner, group string
mode fs.FileMode
dir bool
link bool
}
// fakeSys is a machine in memory: files, users, groups, and a log of
// every change made to it.
type fakeSys struct {
uid int
files map[string]*fakeFile
users map[string]User
groups map[string]map[string]bool
tools map[string]bool
calls []string
fail map[string]error // a change whose log line starts with the key fails
diskFree uint64
exe string
// readMax, when set, makes ReadFile refuse a larger file as the
// host's does.
readMax int
}
func newFakeSys() *fakeSys {
f := &fakeSys{
uid: 0,
files: map[string]*fakeFile{},
users: map[string]User{},
groups: map[string]map[string]bool{},
tools: map[string]bool{"useradd": true, "sudo": true, "visudo": true, "systemctl": true},
fail: map[string]error{},
diskFree: 50 << 30,
exe: "/opt/dl/quarry-runner",
}
f.files["/opt/dl/quarry-runner"] = &fakeFile{data: []byte("PROGRAM-BYTES"), owner: "alice", group: "alice", mode: 0o755}
for _, d := range []string{"/", "/etc", "/etc/sudoers.d", "/var", "/var/lib", "/usr", "/usr/local", "/usr/local/lib", "/etc/systemd", "/etc/systemd/system", "/run", "/run/systemd", "/run/systemd/system"} {
f.files[d] = &fakeFile{dir: true, owner: "root", group: "root", mode: 0o755}
}
return f
}
func (f *fakeSys) rec(format string, a ...any) error {
line := fmt.Sprintf(format, a...)
f.calls = append(f.calls, line)
for k, err := range f.fail {
if strings.HasPrefix(line, k) {
return err
}
}
return nil
}
func (f *fakeSys) Geteuid() int { return f.uid }
func (f *fakeSys) Hostname() (string, error) { return "build-box", nil }
func (f *fakeSys) UserHome() (string, error) { return "/Users/dev", nil }
func (f *fakeSys) Executable() (string, error) { return f.exe, nil }
func (f *fakeSys) LookPath(n string) (string, error) {
if f.tools[n] {
return "/usr/sbin/" + n, nil
}
return "", errors.New("not found")
}
func (f *fakeSys) Run(ctx context.Context, name string, args ...string) ([]byte, error) {
line := name + " " + strings.Join(args, " ")
if err := f.rec("%s", line); err != nil {
return nil, err
}
switch {
case name == "visudo":
if _, ok := f.files[args[len(args)-1]]; !ok {
return nil, errors.New("no such file")
}
case name == "systemctl" && args[0] == "is-enabled":
return []byte("enabled\n"), nil
case name == "systemctl" && args[0] == "is-active":
return []byte("active\n"), nil
}
return nil, nil
}
func (f *fakeSys) RunAs(ctx context.Context, who, name string, args ...string) ([]byte, error) {
return f.Run(ctx, "as:"+who+":"+name, args...)
}
func (f *fakeSys) UserExists(n string) bool { _, ok := f.users[n]; return ok }
func (f *fakeSys) GroupExists(n string) bool { _, ok := f.groups[n]; return ok }
func (f *fakeSys) InGroup(u, g string) bool { return f.groups[g][u] }
func (f *fakeSys) AddGroup(n string) error {
if err := f.rec("AddGroup %s", n); err != nil {
return err
}
f.groups[n] = map[string]bool{}
return nil
}
func (f *fakeSys) AddUser(u User) error {
if err := f.rec("AddUser %s", u.Name); err != nil {
return err
}
f.users[u.Name] = u
f.groups[u.Group][u.Name] = true
return nil
}
func (f *fakeSys) AddToGroup(u, g string) error {
if err := f.rec("AddToGroup %s %s", u, g); err != nil {
return err
}
f.groups[g][u] = true
return nil
}
func (f *fakeSys) DelUser(n string) error {
if err := f.rec("DelUser %s", n); err != nil {
return err
}
delete(f.users, n)
for _, m := range f.groups {
delete(m, n)
}
return nil
}
func (f *fakeSys) DelGroup(n string) error {
if err := f.rec("DelGroup %s", n); err != nil {
return err
}
delete(f.groups, n)
return nil
}
func (f *fakeSys) Mkdir(p, owner, group string, mode fs.FileMode) error {
if err := f.rec("Mkdir %s %s:%s %04o", p, owner, group, mode); err != nil {
return err
}
if _, ok := f.files[path.Dir(p)]; !ok {
return errors.New("no parent")
}
f.files[p] = &fakeFile{dir: true, owner: owner, group: group, mode: mode}
return nil
}
func (f *fakeSys) WriteFile(p string, data []byte, owner, group string, mode fs.FileMode) error {
if err := f.rec("WriteFile %s %s:%s %04o", p, owner, group, mode); err != nil {
return err
}
if _, ok := f.files[path.Dir(p)]; !ok {
return errors.New("no parent")
}
f.files[p] = &fakeFile{data: append([]byte(nil), data...), owner: owner, group: group, mode: mode}
return nil
}
func (f *fakeSys) CopyFile(src, dst, owner, group string, mode fs.FileMode) error {
s, ok := f.files[src]
if !ok {
return errors.New("no source")
}
return f.WriteFile(dst, s.data, owner, group, mode)
}
func (f *fakeSys) Rename(a, b string) error {
if err := f.rec("Rename %s %s", a, b); err != nil {
return err
}
x, ok := f.files[a]
if !ok {
return errors.New("no such file")
}
f.files[b] = x
delete(f.files, a)
return nil
}
func (f *fakeSys) ReadFile(p string) ([]byte, error) {
x, ok := f.files[p]
if !ok || x.dir {
return nil, fs.ErrNotExist
}
if f.readMax > 0 && len(x.data) > f.readMax {
return nil, errors.New("larger than ReadFile reads")
}
return x.data, nil
}
func (f *fakeSys) Sum256(p string) (string, error) {
x, ok := f.files[p]
if !ok || x.dir {
return "", fs.ErrNotExist
}
sum := sha256.Sum256(x.data)
return hex.EncodeToString(sum[:]), nil
}
func (f *fakeSys) ReadDir(p string) ([]string, error) {
var out []string
for k := range f.files {
if k != p && path.Dir(k) == p {
out = append(out, path.Base(k))
}
}
sort.Strings(out)
return out, nil
}
func (f *fakeSys) Stat(p string) (Info, error) {
x, ok := f.files[p]
if !ok {
return Info{}, fs.ErrNotExist
}
return Info{Owner: x.owner, Group: x.group, Mode: x.mode, IsDir: x.dir, IsLink: x.link}, nil
}
func (f *fakeSys) Exists(p string) bool { _, ok := f.files[p]; return ok }
func (f *fakeSys) Remove(p string) error {
if err := f.rec("Remove %s", p); err != nil {
return err
}
for k := range f.files {
if k != p && path.Dir(k) == p {
return errors.New("directory not empty")
}
}
delete(f.files, p)
return nil
}
func (f *fakeSys) RemoveAll(p string) error {
if err := f.rec("RemoveAll %s", p); err != nil {
return err
}
for k := range f.files {
if k == p || strings.HasPrefix(k, p+"/") {
delete(f.files, k)
}
}
return nil
}
func (f *fakeSys) DiskFree(string) (uint64, error) { return f.diskFree, nil }
// snapshot is the machine's state, for "nothing changed" assertions.
func (f *fakeSys) snapshot() string {
var lines []string
for p, x := range f.files {
lines = append(lines, fmt.Sprintf("F %s %s:%s %04o %v %x", p, x.owner, x.group, x.mode, x.dir, x.data))
}
for n := range f.users {
lines = append(lines, "U "+n)
}
for g, m := range f.groups {
var ms []string
for u := range m {
ms = append(ms, u)
}
sort.Strings(ms)
lines = append(lines, "G "+g+" "+strings.Join(ms, ","))
}
sort.Strings(lines)
return strings.Join(lines, "\n")
}
func (f *fakeSys) called(prefix string) int {
n := 0
for _, c := range f.calls {
if strings.HasPrefix(c, prefix) {
n++
}
}
return n
}
func (f *fakeSys) indexOf(prefix string) int {
for i, c := range f.calls {
if strings.HasPrefix(c, prefix) {
return i
}
}
return -1
}
// testForge answers the routes install and doctor use, and counts them.
type testForge struct {
srv *httptest.Server
mu sync.Mutex
redeems []map[string]any
retires []map[string]any
token string
sums string
redeemErr int
// group overrides the worker group redeem names; retireStatus the
// answer to a retire.
group string
retireStatus int
// A runner request: starts are the bodies posted, startStatus the
// forge's answer to one (0 is accepted), answer what a collect says
// once the key is held ("approve", "deny" or "wait"), and polls how
// many collects have come.
starts []map[string]any
startStatus int
answer string
polls int
pickups []string
// other, when set, is the repository a bridge connect says the token
// belongs to.
other string
// oldForge leaves the check route unanswered (404); hangConnect holds
// a connect until its caller gives up; connects counts the connects.
oldForge bool
hangConnect bool
connects int
}
func newTestForge(t *testing.T, program string) *testForge {
t.Helper()
tf := &testForge{token: "wt-secret-1"}
h := sha256.Sum256([]byte(program))
tf.sums = hex.EncodeToString(h[:]) + " quarry-runner-linux-amd64\n"
mux := http.NewServeMux()
mux.HandleFunc("GET /api/v1", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("{}")) })
mux.HandleFunc("GET /runner/SHA256SUMS", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(tf.sums)) })
mux.HandleFunc("POST /api/v1/r/suit/ci/runner-codes/redeem", func(w http.ResponseWriter, r *http.Request) {
var body map[string]any
decode(r, &body)
tf.mu.Lock()
tf.redeems = append(tf.redeems, body)
tf.mu.Unlock()
if tf.redeemErr != 0 {
http.Error(w, "no such install code, or it has expired or been used", tf.redeemErr)
return
}
g := tf.group
if g == "" {
g = "suit"
}
out, _ := json.Marshal(map[string]any{"value": tf.token, "token": map[string]string{"id": "wt1"}, "group": g})
w.Write(out)
})
mux.HandleFunc("POST /api/v1/setup/start", func(w http.ResponseWriter, r *http.Request) {
var body map[string]any
decode(r, &body)
tf.mu.Lock()
tf.starts = append(tf.starts, body)
tf.mu.Unlock()
if tf.startStatus != 0 {
http.Error(w, "no such repository", tf.startStatus)
return
}
w.Write([]byte(`{"code":"ABCD-EFGH","link":"https://forge.test/me/requests","expires":"2026-11-18T10:10:00Z"}`))
})
mux.HandleFunc("POST /api/v1/pickups/collect", func(w http.ResponseWriter, r *http.Request) {
tf.mu.Lock()
tf.polls++
tf.mu.Unlock()
auth := r.Header.Get("Authorization")
if !strings.HasPrefix(auth, "Pickup ") || r.Header.Get("Quarry-Pickup-Kind") != "runner" {
http.Error(w, "no", http.StatusNotFound)
return
}
tf.mu.Lock()
tf.pickups = append(tf.pickups, strings.TrimPrefix(auth, "Pickup "))
tf.mu.Unlock()
switch tf.answer {
case "deny":
w.WriteHeader(http.StatusGone)
w.Write([]byte(`{"state":"denied","error":"the request was denied"}`))
case "expire":
w.WriteHeader(http.StatusGone)
w.Write([]byte(`{"state":"expired","error":"nobody answered in time"}`))
case "wait":
w.WriteHeader(http.StatusAccepted)
w.Write([]byte(`{"state":"pending"}`))
case "stranger":
w.Write([]byte(`{"request":"q1","op":"ci.worker.token.new","agent":"someone","repo":"other","result":{"token":{"id":"wt9"}},"secret":{"value":"` + tf.token + `"}}`))
default:
if tf.polls < 2 {
w.WriteHeader(http.StatusAccepted)
w.Write([]byte(`{"state":"pending"}`))
return
}
w.Write([]byte(`{"request":"q1","op":"ci.worker.token.new","agent":"runner at 203.0.113.5","repo":"suit","result":{"token":{"id":"wt1"}},"secret":{"value":"` + tf.token + `"}}`))
}
})
mux.HandleFunc("POST /api/v1/r/suit/ci/worker-tokens/retire", func(w http.ResponseWriter, r *http.Request) {
var body map[string]any
decode(r, &body)
tf.mu.Lock()
tf.retires = append(tf.retires, body)
tf.mu.Unlock()
if tf.retireStatus != 0 {
http.Error(w, "no such worker token", tf.retireStatus)
return
}
w.Write([]byte(`{"tokens":[]}`))
})
mux.HandleFunc("GET /r/suit/ci/bridge/check", func(w http.ResponseWriter, r *http.Request) {
if tf.oldForge {
http.NotFound(w, r)
return
}
if r.Header.Get("Authorization") != "Bearer "+tf.token {
http.Error(w, "no", http.StatusUnauthorized)
return
}
if tf.other != "" {
w.Header().Set("Quarry-Bridge-Reason", "other-repo")
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, `{"reason":"other-repo","repository":%q}`, tf.other)
return
}
w.Write([]byte(`{"repository":"suit"}`))
})
mux.HandleFunc("POST /r/suit/ci/bridge/v1/workers/connect", func(w http.ResponseWriter, r *http.Request) {
tf.mu.Lock()
tf.connects++
tf.mu.Unlock()
if tf.hangConnect {
// The body is read so the server notices the caller leaving.
io.Copy(io.Discard, r.Body)
<-r.Context().Done()
return
}
if tf.other != "" {
w.Header().Set("Quarry-Bridge-Reason", "other-repo")
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, `{"reason":"other-repo","repository":%q}`, tf.other)
return
}
if r.Header.Get("Authorization") != "Bearer "+tf.token {
http.Error(w, "no", http.StatusUnauthorized)
return
}
w.WriteHeader(200)
})
tf.srv = httptest.NewServer(mux)
t.Cleanup(tf.srv.Close)
return tf
}
func decode(r *http.Request, into any) {
var b bytes.Buffer
b.ReadFrom(r.Body)
jsonUnmarshal(b.Bytes(), into)
}
func (tf *testForge) repoURL() string { return tf.srv.URL + "/r/dmestas/suit" }