e2e/buildorphan_test.go
73 lines · 3029 bytes
1 symbol in this file
1package e2e
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10// Signed commits mean only fast-forward merges are allowed, so a branch
11// whose target advances gets rebased and force-pushed — orphaning whatever
12// was queued for the old head. That build must not run and fail at clone
13// looking like a real failure; it is cancelled when a runner claims it, and
14// the runner gets the real build behind it instead, in the same poll.
15func TestBuildOrphanedByForcePushCancelledAtClaim(t *testing.T) {
16 t.Parallel()
17 inst := startInstance(t)
18 inst.runner = buildRunner(t)
19 aliceKey := inst.newKey(t, "alice")
20 runnerKey := inst.newKey(t, "ci")
21 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
22 inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
23 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
24 t.Fatal("repo create failed")
25 }
26 work := t.TempDir()
27 env := inst.gitEnv(aliceKey)
28 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
29 dir := filepath.Join(work, "w")
30 os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
31 os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte("jobs:\n unit:\n steps:\n - echo fine\n"), 0o644)
32 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
33 mustGit(t, dir, env, "add", ".")
34 mustGit(t, dir, env, "commit", "-q", "-m", "base")
35 mustGit(t, dir, env, "push", "-q", "origin", "main")
36 orphanSHA := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
37
38 // Amend and force-push: build 1 (queued for orphanSHA) is left behind,
39 // unreachable once main moves; build 2 queues for the amended commit.
40 mustGit(t, dir, env, "commit", "-q", "--amend", "-m", "amended")
41 mustGit(t, dir, env, "push", "-q", "--force", "origin", "main")
42 realSHA := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
43 if orphanSHA == realSHA {
44 t.Fatal("amend produced the same sha; test setup is broken")
45 }
46
47 out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
48 if !strings.Contains(out, "1\tunit\tpending") || !strings.Contains(out, "2\tunit\tpending") {
49 t.Fatalf("expected both builds queued and pending:\n%s", out)
50 }
51
52 // One claim: the orphaned build is cancelled and the real one runs,
53 // without a second poll.
54 inst.runnerOnce(t, runnerKey)
55
56 out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
57 if !strings.Contains(out, "1\tunit\tcancelled") {
58 t.Fatalf("orphaned build not cancelled:\n%s", out)
59 }
60 if !strings.Contains(out, "2\tunit\tsuccess") {
61 t.Fatalf("real build behind it did not run:\n%s", out)
62 }
63
64 log, _, _ := inst.ssh(t, aliceKey, "", "build", "log", "alice/app", "1")
65 if !strings.Contains(log, "not reachable") {
66 t.Fatalf("cancelled build's log does not explain why:\n%s", log)
67 }
68
69 st, _, _ := inst.ssh(t, aliceKey, "", "status", "list", "alice/app", orphanSHA)
70 if !strings.Contains(st, "ci/unit") || strings.Contains(st, "pending") {
71 t.Fatalf("orphaned commit's status left pending:\n%s", st)
72 }
73}