e2e/ci_dedupe_test.go
101 lines · 4981 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 the exact commit that was already built on its
11// branch; that commit is not built again. A commit whose build failed is.
12func TestFastForwardMergeSkipsBuiltCommit(t *testing.T) {
13 t.Parallel()
14 inst := startInstance(t)
15 inst.runner = buildRunner(t)
16 aliceKey := inst.newKey(t, "alice")
17 runnerKey := inst.newKey(t, "ci")
18 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
19 inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
20 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
21 t.Fatalf("repo create: %s", errOut)
22 }
23 work := t.TempDir()
24 env := inst.gitEnv(aliceKey)
25 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
26 dir := filepath.Join(work, "w")
27 os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
28 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)
29 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
30 mustGit(t, dir, env, "add", ".")
31 mustGit(t, dir, env, "commit", "-q", "-m", "base")
32 mustGit(t, dir, env, "push", "-q", "origin", "main")
33 inst.runnerOnce(t, runnerKey) // main's own build
34
35 // A branch whose build passes.
36 mustGit(t, dir, env, "checkout", "-q", "-b", "good")
37 os.WriteFile(filepath.Join(dir, "f.txt"), []byte("x\n"), 0o644)
38 mustGit(t, dir, env, "add", ".")
39 mustGit(t, dir, env, "commit", "-q", "-m", "good change")
40 mustGit(t, dir, env, "push", "-q", "origin", "good")
41 inst.runnerOnce(t, runnerKey)
42 list := inst.buildList(t, aliceKey)
43 if !strings.Contains(list, "good\tunit\tsuccess") && !strings.Contains(list, "unit\tsuccess") {
44 t.Fatalf("branch build did not pass:\n%s", list)
45 }
46 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app", "--source", "good", "--target", "main", "--title", "good"); code != 0 {
47 t.Fatalf("mr create: %s", errOut)
48 }
49 before := strings.Count(inst.buildList(t, aliceKey), "\n")
50 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1", "--strategy", "ff"); code != 0 {
51 t.Fatalf("mr merge: %s", errOut)
52 }
53 if after := strings.Count(inst.buildList(t, aliceKey), "\n"); after != before {
54 t.Fatalf("fast-forward queued a build for an already-built commit:\n%s", inst.buildList(t, aliceKey))
55 }
56 sha := strings.Fields(strings.TrimSpace(mustGit(t, dir, env, "ls-remote", "origin", "refs/heads/main")))[0]
57 if status, _, _ := inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha); !strings.Contains(status, "success") || strings.Contains(status, "pending") {
58 t.Fatalf("statuses on the merged commit changed:\n%s", status)
59 }
60
61 // A branch merged before its build has run: the queued build will
62 // say, so the merge queues nothing either.
63 mustGit(t, dir, env, "checkout", "-q", "-b", "early")
64 os.WriteFile(filepath.Join(dir, "g.txt"), []byte("y\n"), 0o644)
65 mustGit(t, dir, env, "add", ".")
66 mustGit(t, dir, env, "commit", "-q", "-m", "early change")
67 mustGit(t, dir, env, "push", "-q", "origin", "early")
68 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app", "--source", "early", "--target", "main", "--title", "early"); code != 0 {
69 t.Fatalf("mr create: %s", errOut)
70 }
71 before = strings.Count(inst.buildList(t, aliceKey), "\n")
72 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "2", "--strategy", "ff"); code != 0 {
73 t.Fatalf("mr merge early: %s", errOut)
74 }
75 if after := strings.Count(inst.buildList(t, aliceKey), "\n"); after != before {
76 t.Fatalf("a merge before the branch build ran queued a second build:\n%s", inst.buildList(t, aliceKey))
77 }
78 inst.runnerOnce(t, runnerKey) // the branch's queued build, now the only one
79
80 // A branch whose build fails is built again when it lands.
81 mustGit(t, dir, env, "checkout", "-q", "-b", "bad")
82 os.WriteFile(filepath.Join(dir, "fail.txt"), []byte("x\n"), 0o644)
83 os.Remove(filepath.Join(dir, "f.txt"))
84 mustGit(t, dir, env, "add", "-A")
85 mustGit(t, dir, env, "commit", "-q", "-m", "bad change")
86 mustGit(t, dir, env, "push", "-q", "origin", "bad")
87 inst.runnerOnce(t, runnerKey)
88 if list := inst.buildList(t, aliceKey); !strings.Contains(list, "unit\tfailure") {
89 t.Fatalf("bad branch build did not fail:\n%s", list)
90 }
91 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app", "--source", "bad", "--target", "main", "--title", "bad"); code != 0 {
92 t.Fatalf("mr create: %s", errOut)
93 }
94 before = strings.Count(inst.buildList(t, aliceKey), "\n")
95 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "3", "--strategy", "ff"); code != 0 {
96 t.Fatalf("mr merge bad: %s", errOut)
97 }
98 if after := strings.Count(inst.buildList(t, aliceKey), "\n"); after != before+1 {
99 t.Fatalf("a failed commit was not rebuilt on landing:\n%s", inst.buildList(t, aliceKey))
100 }
101}