Skip to content
quarry Artifact [37cb3310cd]
Login

Artifact [37cb3310cd]

Artifact 37cb3310cdf313c4a15f83425ed93fccc87f8728010d94064869eb29c09ad6c1:


package install

import (
	"bytes"
	"context"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"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
}

// 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) 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}, 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
}

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/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("POST /r/suit/ci/bridge/v1/workers/connect", func(w http.ResponseWriter, r *http.Request) {
		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" }