Commit 6a868d3112
Verified · cmc
e2e/runnerattach_test.go added +124
| @@ -0,0 +1,124 @@ | ||
| 1 | package e2e | |
| 2 | ||
| 3 | import ( | |
| 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. | |
| 16 | func 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 | ||
| 78 | // Attach with the printed key. | |
| 79 | if _, errOut, code := inst.ssh(t, aliceKey, string(pub), "repo", "runner", "add", "alice/app"); code != 0 { | |
| 80 | t.Fatalf("repo runner add: %s", errOut) | |
| 81 | } | |
| 82 | out, _, _ := inst.ssh(t, aliceKey, "", "repo", "runner", "list", "alice/app", "--json") | |
| 83 | if !strings.Contains(out, `"username":"alice"`) || strings.Contains(out, `"last_seen":"20`) { | |
| 84 | t.Fatalf("list after attach: %s", out) | |
| 85 | } | |
| 86 | ||
| 87 | // The runner builds it. | |
| 88 | run() | |
| 89 | if out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); !strings.Contains(out, "unit\tsuccess") { | |
| 90 | t.Fatalf("build not built by the attached runner: %s", out) | |
| 91 | } | |
| 92 | if out, _, _ = inst.ssh(t, aliceKey, "", "repo", "runner", "list", "alice/app", "--json"); !strings.Contains(out, `"last_seen":"20`) { | |
| 93 | t.Fatalf("no heartbeat after a poll: %s", out) | |
| 94 | } | |
| 95 | ||
| 96 | // bob forks and opens a merge request: an untrusted build in the target. | |
| 97 | if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "fork", "alice/app"); code != 0 { | |
| 98 | t.Fatalf("fork: %s", errOut) | |
| 99 | } | |
| 100 | bwork := t.TempDir() | |
| 101 | benv := inst.gitEnv(bobKey) | |
| 102 | mustGit(t, bwork, benv, "clone", inst.sshURL("bob/app"), "w") | |
| 103 | bdir := filepath.Join(bwork, "w") | |
| 104 | mustGit(t, bdir, benv, "checkout", "-q", "-b", "feat") | |
| 105 | os.WriteFile(filepath.Join(bdir, "f.txt"), []byte("y\n"), 0o644) | |
| 106 | mustGit(t, bdir, benv, "add", ".") | |
| 107 | mustGit(t, bdir, benv, "commit", "-q", "-m", "change") | |
| 108 | mustGit(t, bdir, benv, "push", "-q", "origin", "feat") | |
| 109 | if _, _, code := inst.ssh(t, bobKey, "", "build", "cancel", "bob/app", "1"); code != 0 { | |
| 110 | t.Fatal("cancel bob's own build") | |
| 111 | } | |
| 112 | if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app", | |
| 113 | "--source", "bob/app:feat", "--target", "main", "--title", "change"); code != 0 { | |
| 114 | t.Fatalf("mr create: %s", errOut) | |
| 115 | } | |
| 116 | run() | |
| 117 | if out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); strings.Count(out, "pending") != 1 { | |
| 118 | t.Fatalf("fork head was claimed without -untrusted:\n%s", out) | |
| 119 | } | |
| 120 | run("-untrusted") | |
| 121 | if out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app"); strings.Contains(out, "pending") { | |
| 122 | t.Fatalf("fork head not built with -untrusted:\n%s", out) | |
| 123 | } | |
| 124 | } | |