e2e/ci_test.go
270 lines · 11799 bytes
1package e2e
2
3import (
4 "fmt"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12func buildRunner(t *testing.T) string {
13 t.Helper()
14 bin := filepath.Join(t.TempDir(), "gitbay-runner")
15 cmd := exec.Command("go", "build", "-o", bin, "gitbay.org/gitbay/cmd/gitbay-runner")
16 cmd.Dir = ".."
17 if out, err := cmd.CombinedOutput(); err != nil {
18 t.Fatalf("build gitbay-runner: %v\n%s", err, out)
19 }
20 return bin
21}
22
23// runnerOnce processes at most one pending build with the given key.
24func (i *instance) runnerOnce(t *testing.T, key string) string {
25 t.Helper()
26 opts := fmt.Sprintf("-p %d -i %s -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=%s -o BatchMode=yes",
27 i.port, key, filepath.Join(i.sshDir, "known_hosts"))
28 cmd := exec.Command(i.runner, "-once",
29 "-remote", "git@127.0.0.1",
30 "-ssh-opts", opts,
31 "-clone-base", fmt.Sprintf("ssh://git@127.0.0.1:%d", i.port),
32 "-workdir", t.TempDir())
33 cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null")
34 out, err := cmd.CombinedOutput()
35 if err != nil {
36 t.Fatalf("runner: %v\n%s", err, out)
37 }
38 return string(out)
39}
40
41// A failed build mails the repo owner with the log tail; green builds
42// stay silent.
43func TestBuildFailureMail(t *testing.T) {
44 smtp := startFakeSMTP(t)
45 inst := startInstanceWith(t, fmt.Sprintf(
46 "[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n", smtp.addr))
47 inst.runner = buildRunner(t)
48 aliceKey := inst.newKey(t, "alice")
49 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub",
50 "--email", "alice@example.test", "--verified")
51 runnerKey := inst.newKey(t, "ci")
52 inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
53
54 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
55 t.Fatalf("repo create: %s", errOut)
56 }
57 work := t.TempDir()
58 env := inst.gitEnv(aliceKey)
59 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
60 dir := filepath.Join(work, "w")
61 os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
62 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
63 "jobs:\n ok:\n steps:\n - echo fine\n broken:\n steps:\n - echo the dataset went stale && false\n"), 0o644)
64 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
65 mustGit(t, dir, env, "add", ".")
66 mustGit(t, dir, env, "commit", "-q", "-m", "base")
67 mustGit(t, dir, env, "push", "-q", "origin", "main")
68
69 inst.runnerOnce(t, runnerKey) // broken (sorts first)
70 inst.runnerOnce(t, runnerKey) // ok
71 mail := smtp.waitFor(t, "alice@example.test", "failed")
72 if !strings.Contains(mail, "broken") || !strings.Contains(mail, "the dataset went stale") ||
73 !strings.Contains(mail, "/alice/app/builds/") {
74 t.Fatalf("failure mail missing detail:\n%s", mail)
75 }
76 // Only the failure mailed: no message mentions the green job.
77 for _, m := range smtp.mailTo("alice@example.test") {
78 if strings.Contains(m, "build") && strings.Contains(m, " ok ") && strings.Contains(m, "failed") == false {
79 t.Fatalf("green build mailed:\n%s", m)
80 }
81 }
82}
83
84func TestCI(t *testing.T) {
85 inst := startInstance(t)
86 inst.runner = buildRunner(t)
87 aliceKey := inst.newKey(t, "alice")
88 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
89 runnerKey := inst.newKey(t, "ci")
90 inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
91
92 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
93 t.Fatalf("repo create: %s", errOut)
94 }
95 work := t.TempDir()
96 env := inst.gitEnv(aliceKey)
97 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
98 dir := filepath.Join(work, "w")
99 os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
100 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
101 "jobs:\n ok:\n steps:\n - echo hello from $GITBAY_JOB\n broken:\n steps:\n - \"false\"\n"), 0o644)
102 os.WriteFile(filepath.Join(dir, "f.txt"), []byte("x\n"), 0o644)
103 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
104 mustGit(t, dir, env, "add", ".")
105 mustGit(t, dir, env, "commit", "-q", "-m", "base")
106 mustGit(t, dir, env, "push", "-q", "origin", "main")
107 sha := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
108
109 // The push queued one pending build per job, with pending statuses.
110 out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
111 if !strings.Contains(out, "broken\tpending") || !strings.Contains(out, "ok\tpending") {
112 t.Fatalf("builds not queued:\n%s", out)
113 }
114 out, _, _ = inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha)
115 if !strings.Contains(out, "ci/ok") || !strings.Contains(out, "pending") {
116 t.Fatalf("pending statuses missing:\n%s", out)
117 }
118
119 // Non-admins cannot claim jobs.
120 if _, _, code := inst.ssh(t, aliceKey, "", "runner", "next"); code != 4 {
121 t.Fatalf("non-admin claimed a build: exit %d", code)
122 }
123
124 // The runner processes both jobs ("broken" sorts first).
125 inst.runnerOnce(t, runnerKey)
126 inst.runnerOnce(t, runnerKey)
127
128 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
129 if !strings.Contains(out, "ok\tsuccess") || !strings.Contains(out, "broken\tfailure") {
130 t.Fatalf("build outcomes wrong:\n%s", out)
131 }
132 // Logs captured the step output and the failure.
133 var okN, brokenN string
134 for _, l := range strings.Split(strings.TrimSpace(out), "\n") {
135 f := strings.Split(l, "\t")
136 if f[1] == "ok" {
137 okN = f[0]
138 } else {
139 brokenN = f[0]
140 }
141 }
142 out, _, _ = inst.ssh(t, aliceKey, "", "build", "log", "alice/app", okN)
143 if !strings.Contains(out, "hello from ok") {
144 t.Fatalf("ok log:\n%s", out)
145 }
146 out, _, _ = inst.ssh(t, aliceKey, "", "build", "log", "alice/app", brokenN)
147 if !strings.Contains(out, "step failed") {
148 t.Fatalf("broken log:\n%s", out)
149 }
150 // Statuses resolved, with target URLs pointing at the build pages.
151 out, _, _ = inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha, "--json")
152 if !strings.Contains(out, `"ci/ok","state":"success"`) && !strings.Contains(out, `"state":"success"`) {
153 t.Fatalf("status not success:\n%s", out)
154 }
155 if !strings.Contains(out, "/alice/app/builds/") {
156 t.Fatalf("status target url missing:\n%s", out)
157 }
158
159 // Web: list page and log page.
160 status, body := inst.get(t, "/alice/app/builds")
161 if status != 200 || !strings.Contains(body, "ok") || !strings.Contains(body, "failure") {
162 t.Fatalf("builds page: %d\n%s", status, body)
163 }
164 if _, body = inst.get(t, "/alice/app/builds/"+okN); !strings.Contains(body, "hello from ok") {
165 t.Fatalf("build log page:\n%s", body)
166 }
167
168 // --- secrets: stdin in, names-only out, injected into the build env ---
169 if _, errOut, code := inst.ssh(t, aliceKey, "hunter2\n", "repo", "secret", "set", "alice/app", "MY_TOKEN"); code != 0 {
170 t.Fatalf("secret set: %s", errOut)
171 }
172 if _, _, code := inst.ssh(t, aliceKey, "x\n", "repo", "secret", "set", "alice/app", "bad-name"); code != 2 {
173 t.Fatal("bad secret name accepted")
174 }
175 out, _, _ = inst.ssh(t, aliceKey, "", "repo", "secret", "list", "alice/app")
176 if !strings.Contains(out, "MY_TOKEN") || strings.Contains(out, "hunter2") {
177 t.Fatalf("secret list leaked or missed: %s", out)
178 }
179
180 // --- schedules and manual trigger ---
181 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
182 "jobs:\n usesecret:\n steps:\n - echo token=$MY_TOKEN\n nightly:\n schedule: \"0 6 * * 1\"\n steps:\n - echo scheduled ran\n"), 0o644)
183 mustGit(t, dir, env, "add", ".")
184 mustGit(t, dir, env, "commit", "-q", "-m", "secrets and schedule")
185 mustGit(t, dir, env, "push", "-q", "origin", "main")
186
187 // The push queued only the unscheduled job.
188 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
189 if !strings.Contains(out, "usesecret\tpending") || strings.Contains(out, "nightly") {
190 t.Fatalf("scheduled job queued on push:\n%s", out)
191 }
192 inst.runnerOnce(t, runnerKey)
193 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
194 usecretN := strings.Split(out, "\t")[0]
195 out, _, _ = inst.ssh(t, aliceKey, "", "build", "log", "alice/app", usecretN)
196 if !strings.Contains(out, "token=hunter2") {
197 t.Fatalf("secret not injected:\n%s", out)
198 }
199 // The scheduled job runs on demand via trigger.
200 if _, errOut, code := inst.ssh(t, aliceKey, "", "build", "trigger", "alice/app", "nightly"); code != 0 {
201 t.Fatalf("trigger: %s", errOut)
202 }
203 if _, _, code := inst.ssh(t, aliceKey, "", "build", "trigger", "alice/app", "nosuch"); code != 3 {
204 t.Fatal("triggered a job that does not exist")
205 }
206 inst.runnerOnce(t, runnerKey)
207 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
208 if !strings.Contains(out, "nightly\tsuccess") {
209 t.Fatalf("triggered build did not run:\n%s", out)
210 }
211 // Removing the secret stops injection.
212 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "secret", "remove", "alice/app", "MY_TOKEN"); code != 0 {
213 t.Fatal("secret remove failed")
214 }
215
216 // --- tag-triggered jobs ---
217 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
218 "jobs:\n test:\n steps:\n - echo branch build\n publish:\n tags: \"v*\"\n steps:\n - echo publishing $GITBAY_REF\n"), 0o644)
219 mustGit(t, dir, env, "add", ".")
220 mustGit(t, dir, env, "commit", "-q", "-m", "tag job")
221 mustGit(t, dir, env, "push", "-q", "origin", "main")
222 // The branch push queued only the branch job.
223 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
224 if strings.Contains(out, "publish") {
225 t.Fatalf("tag job queued on branch push:\n%s", out)
226 }
227 // An annotated tag queues the tag job, with the peeled commit as sha.
228 mustGit(t, dir, env, "tag", "-a", "-m", "rel", "v1.0.0")
229 mustGit(t, dir, env, "push", "-q", "origin", "v1.0.0")
230 headSHA := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
231 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
232 if !strings.Contains(out, "publish\tpending\t"+headSHA[:10]) || !strings.Contains(out, "v1.0.0") {
233 t.Fatalf("tag build missing or unpeeled:\n%s", out)
234 }
235 // A non-matching tag queues nothing.
236 mustGit(t, dir, env, "tag", "nightly-1")
237 mustGit(t, dir, env, "push", "-q", "origin", "nightly-1")
238 out2, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
239 if strings.Count(out2, "publish") != strings.Count(out, "publish") {
240 t.Fatalf("non-matching tag queued a build:\n%s", out2)
241 }
242 inst.runnerOnce(t, runnerKey) // branch "test" job
243 inst.runnerOnce(t, runnerKey) // tag "publish" job
244 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
245 if !strings.Contains(out, "publish\tsuccess") {
246 t.Fatalf("tag build did not run:\n%s", out)
247 }
248 // schedule and tags together are refused.
249 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
250 "jobs:\n both:\n schedule: \"0 6 * * *\"\n tags: \"v*\"\n steps: [echo x]\n"), 0o644)
251 mustGit(t, dir, env, "add", ".")
252 mustGit(t, dir, env, "commit", "-q", "-m", "both triggers")
253 mustGit(t, dir, env, "push", "-q", "origin", "main")
254 shaBoth := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
255 out, _, _ = inst.ssh(t, aliceKey, "", "status", "list", "alice/app", shaBoth)
256 if !strings.Contains(out, "ci/config") || !strings.Contains(out, "failure") {
257 t.Fatalf("mutually exclusive triggers not refused:\n%s", out)
258 }
259
260 // A broken ci.yml surfaces as a failed ci/config status.
261 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte("jobs: {bad name: {steps: [x]}}\n"), 0o644)
262 mustGit(t, dir, env, "add", ".")
263 mustGit(t, dir, env, "commit", "-q", "-m", "break config")
264 mustGit(t, dir, env, "push", "-q", "origin", "main")
265 sha2 := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
266 out, _, _ = inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha2)
267 if !strings.Contains(out, "ci/config") || !strings.Contains(out, "failure") {
268 t.Fatalf("config failure status missing:\n%s", out)
269 }
270}