e2e/runnerattach_test.go

v1.27.0
gitbay/e2e/runnerattach_test.go history · blame · raw

128 lines · 5258 bytes

  1package e2e
  2
  3import (
  4	"fmt"
  5	"os"
  6	"os/exec"
  7	"path/filepath"
  8	"strings"
  9	"testing"
 10)
 11
 12// The whole flow a user goes through: init on their machine, attach the
 13// printed key to their repository, start the runner from the config init
 14// wrote. The runner builds their push and leaves a fork's merge request
 15// head alone until started with -untrusted.
 16func TestAttachedRunnerBuildsOwnRepo(t *testing.T) {
 17	inst := startInstance(t)
 18	inst.runner = buildRunner(t)
 19	aliceKey := inst.newKey(t, "alice")
 20	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
 21	bobKey := inst.newKey(t, "bob")
 22	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
 23	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
 24		t.Fatalf("repo create: %s", errOut)
 25	}
 26
 27	// init on "alice's laptop".
 28	xdg := t.TempDir()
 29	initCmd := exec.Command(inst.runner, "init", "-remote", "git@127.0.0.1")
 30	initCmd.Env = append(os.Environ(), "XDG_CONFIG_HOME="+xdg)
 31	initOut, err := initCmd.CombinedOutput()
 32	if err != nil {
 33		t.Fatalf("init: %v\n%s", err, initOut)
 34	}
 35	cdir := filepath.Join(xdg, "gitbay-runner")
 36	pub, err := os.ReadFile(filepath.Join(cdir, "id_ed25519.pub"))
 37	if err != nil {
 38		t.Fatal(err)
 39	}
 40	if !strings.Contains(string(initOut), strings.TrimSpace(string(pub))) {
 41		t.Fatalf("init did not print the key:\n%s", initOut)
 42	}
 43
 44	// The unattached key claims nothing, even with a build queued.
 45	work := t.TempDir()
 46	env := inst.gitEnv(aliceKey)
 47	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
 48	dir := filepath.Join(work, "w")
 49	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
 50	os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte("jobs:\n  unit:\n    steps:\n      - echo built\n"), 0o644)
 51	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 52	mustGit(t, dir, env, "add", ".")
 53	mustGit(t, dir, env, "commit", "-q", "-m", "ci")
 54	mustGit(t, dir, env, "push", "-q", "origin", "main")
 55
 56	run := func(extra ...string) string {
 57		t.Helper()
 58		// No -i in ssh-opts: the identity from the config is what
 59		// authenticates, which is the point.
 60		opts := fmt.Sprintf("-p %d -o StrictHostKeyChecking=no -o UserKnownHostsFile=%s -o BatchMode=yes",
 61			inst.port, filepath.Join(inst.sshDir, "known_hosts"))
 62		args := append([]string{"-config", filepath.Join(cdir, "config.toml"), "-once",
 63			"-ssh-opts", opts,
 64			"-clone-base", fmt.Sprintf("ssh://git@127.0.0.1:%d", inst.port),
 65			"-workdir", t.TempDir()}, extra...)
 66		cmd := exec.Command(inst.runner, args...)
 67		cmd.Env = append(os.Environ(), "XDG_CONFIG_HOME="+xdg, "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null")
 68		out, err := cmd.CombinedOutput()
 69		if err != nil {
 70			t.Fatalf("runner: %v\n%s", err, out)
 71		}
 72		return string(out)
 73	}
 74	if out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); !strings.Contains(out, "unit\tpending") {
 75		t.Fatalf("build not pending before attach: %s", out)
 76	}
 77	run()
 78	if out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); !strings.Contains(out, "unit\tpending") {
 79		t.Fatalf("unattached runner claimed the build: %s", out)
 80	}
 81
 82	// Attach with the printed key.
 83	if _, errOut, code := inst.ssh(t, aliceKey, string(pub), "repo", "runner", "add", "alice/app"); code != 0 {
 84		t.Fatalf("repo runner add: %s", errOut)
 85	}
 86	out, _, _ := inst.ssh(t, aliceKey, "", "repo", "runner", "list", "alice/app", "--json")
 87	if !strings.Contains(out, `"username":"alice"`) || strings.Contains(out, `"last_seen":"20`) {
 88		t.Fatalf("list after attach: %s", out)
 89	}
 90
 91	// The runner builds it.
 92	run()
 93	if out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); !strings.Contains(out, "unit\tsuccess") {
 94		t.Fatalf("build not built by the attached runner: %s", out)
 95	}
 96	if out, _, _ = inst.ssh(t, aliceKey, "", "repo", "runner", "list", "alice/app", "--json"); !strings.Contains(out, `"last_seen":"20`) {
 97		t.Fatalf("no heartbeat after a poll: %s", out)
 98	}
 99
100	// bob forks and opens a merge request: an untrusted build in the target.
101	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "fork", "alice/app"); code != 0 {
102		t.Fatalf("fork: %s", errOut)
103	}
104	bwork := t.TempDir()
105	benv := inst.gitEnv(bobKey)
106	mustGit(t, bwork, benv, "clone", inst.sshURL("bob/app"), "w")
107	bdir := filepath.Join(bwork, "w")
108	mustGit(t, bdir, benv, "checkout", "-q", "-b", "feat")
109	os.WriteFile(filepath.Join(bdir, "f.txt"), []byte("y\n"), 0o644)
110	mustGit(t, bdir, benv, "add", ".")
111	mustGit(t, bdir, benv, "commit", "-q", "-m", "change")
112	mustGit(t, bdir, benv, "push", "-q", "origin", "feat")
113	if _, _, code := inst.ssh(t, bobKey, "", "build", "cancel", "bob/app", "1"); code != 0 {
114		t.Fatal("cancel bob's own build")
115	}
116	if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app",
117		"--source", "bob/app:feat", "--target", "main", "--title", "change"); code != 0 {
118		t.Fatalf("mr create: %s", errOut)
119	}
120	run()
121	if out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); strings.Count(out, "pending") != 1 {
122		t.Fatalf("fork head was claimed without -untrusted:\n%s", out)
123	}
124	run("-untrusted")
125	if out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); strings.Contains(out, "pending") {
126		t.Fatalf("fork head not built with -untrusted:\n%s", out)
127	}
128}