e2e/ci_dedupe_test.go
103 lines · 4702 bytes
1 symbol in this file
1package e2e
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10// A fast-forward lands a commit already built on its branch, and it is
11// built again for the target: a job sees GITBAY_REF, so the branch's
12// result does not say what the job does on main (#335). That holds for
13// a branch build that passed, one still queued, and one that failed.
14func TestFastForwardMergeBuildsOnTarget(t *testing.T) {
15 t.Parallel()
16 inst := startInstance(t)
17 inst.runner = buildRunner(t)
18 aliceKey := inst.newKey(t, "alice")
19 runnerKey := inst.newKey(t, "ci")
20 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
21 inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
22 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
23 t.Fatalf("repo create: %s", errOut)
24 }
25 mainBuilds := func() int {
26 out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app", "--ref", "main")
27 return strings.Count(out, "\n")
28 }
29 work := t.TempDir()
30 env := inst.gitEnv(aliceKey)
31 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
32 dir := filepath.Join(work, "w")
33 os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
34 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte("jobs:\n unit:\n steps:\n - test -f f.txt || test ! -f fail.txt\n"), 0o644)
35 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
36 mustGit(t, dir, env, "add", ".")
37 mustGit(t, dir, env, "commit", "-q", "-m", "base")
38 mustGit(t, dir, env, "push", "-q", "origin", "main")
39 inst.runnerOnce(t, runnerKey) // main's own build
40
41 // A branch whose build passes.
42 mustGit(t, dir, env, "checkout", "-q", "-b", "good")
43 os.WriteFile(filepath.Join(dir, "f.txt"), []byte("x\n"), 0o644)
44 mustGit(t, dir, env, "add", ".")
45 mustGit(t, dir, env, "commit", "-q", "-m", "good change")
46 mustGit(t, dir, env, "push", "-q", "origin", "good")
47 inst.runnerOnce(t, runnerKey)
48 if list := inst.buildList(t, aliceKey); !strings.Contains(list, "unit\tsuccess") {
49 t.Fatalf("branch build did not pass:\n%s", list)
50 }
51 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app", "--source", "good", "--target", "main", "--title", "good"); code != 0 {
52 t.Fatalf("mr create: %s", errOut)
53 }
54 before := mainBuilds()
55 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1", "--strategy", "ff"); code != 0 {
56 t.Fatalf("mr merge: %s", errOut)
57 }
58 if after := mainBuilds(); after != before+1 {
59 t.Fatalf("fast-forward of a passed commit queued %d builds on main, want 1:\n%s", after-before, inst.buildList(t, aliceKey))
60 }
61 inst.runnerOnce(t, runnerKey)
62
63 // A branch merged before its build has run: main gets its own.
64 mustGit(t, dir, env, "checkout", "-q", "-b", "early")
65 os.WriteFile(filepath.Join(dir, "g.txt"), []byte("y\n"), 0o644)
66 mustGit(t, dir, env, "add", ".")
67 mustGit(t, dir, env, "commit", "-q", "-m", "early change")
68 mustGit(t, dir, env, "push", "-q", "origin", "early")
69 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app", "--source", "early", "--target", "main", "--title", "early"); code != 0 {
70 t.Fatalf("mr create: %s", errOut)
71 }
72 before = mainBuilds()
73 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "2", "--strategy", "ff"); code != 0 {
74 t.Fatalf("mr merge early: %s", errOut)
75 }
76 if after := mainBuilds(); after != before+1 {
77 t.Fatalf("a merge before the branch build ran queued %d builds on main, want 1:\n%s", after-before, inst.buildList(t, aliceKey))
78 }
79 inst.runnerOnce(t, runnerKey) // the branch's
80 inst.runnerOnce(t, runnerKey) // main's
81
82 // A branch whose build fails.
83 mustGit(t, dir, env, "checkout", "-q", "-b", "bad")
84 os.WriteFile(filepath.Join(dir, "fail.txt"), []byte("x\n"), 0o644)
85 os.Remove(filepath.Join(dir, "f.txt"))
86 mustGit(t, dir, env, "add", "-A")
87 mustGit(t, dir, env, "commit", "-q", "-m", "bad change")
88 mustGit(t, dir, env, "push", "-q", "origin", "bad")
89 inst.runnerOnce(t, runnerKey)
90 if list := inst.buildList(t, aliceKey); !strings.Contains(list, "unit\tfailure") {
91 t.Fatalf("bad branch build did not fail:\n%s", list)
92 }
93 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app", "--source", "bad", "--target", "main", "--title", "bad"); code != 0 {
94 t.Fatalf("mr create: %s", errOut)
95 }
96 before = mainBuilds()
97 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "3", "--strategy", "ff"); code != 0 {
98 t.Fatalf("mr merge bad: %s", errOut)
99 }
100 if after := mainBuilds(); after != before+1 {
101 t.Fatalf("a failed commit queued %d builds on main, want 1:\n%s", after-before, inst.buildList(t, aliceKey))
102 }
103}