e2e/buildorphan_test.go

v1.31.0
gitbay/e2e/buildorphan_test.go history · blame · raw

72 lines · 3015 bytes

 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	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")
21	inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
22	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
23		t.Fatal("repo create failed")
24	}
25	work := t.TempDir()
26	env := inst.gitEnv(aliceKey)
27	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
28	dir := filepath.Join(work, "w")
29	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
30	os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte("jobs:\n  unit:\n    steps:\n      - echo fine\n"), 0o644)
31	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
32	mustGit(t, dir, env, "add", ".")
33	mustGit(t, dir, env, "commit", "-q", "-m", "base")
34	mustGit(t, dir, env, "push", "-q", "origin", "main")
35	orphanSHA := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
36
37	// Amend and force-push: build 1 (queued for orphanSHA) is left behind,
38	// unreachable once main moves; build 2 queues for the amended commit.
39	mustGit(t, dir, env, "commit", "-q", "--amend", "-m", "amended")
40	mustGit(t, dir, env, "push", "-q", "--force", "origin", "main")
41	realSHA := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
42	if orphanSHA == realSHA {
43		t.Fatal("amend produced the same sha; test setup is broken")
44	}
45
46	out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
47	if !strings.Contains(out, "1\tunit\tpending") || !strings.Contains(out, "2\tunit\tpending") {
48		t.Fatalf("expected both builds queued and pending:\n%s", out)
49	}
50
51	// One claim: the orphaned build is cancelled and the real one runs,
52	// without a second poll.
53	inst.runnerOnce(t, runnerKey)
54
55	out, _, _ = inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
56	if !strings.Contains(out, "1\tunit\tcancelled") {
57		t.Fatalf("orphaned build not cancelled:\n%s", out)
58	}
59	if !strings.Contains(out, "2\tunit\tsuccess") {
60		t.Fatalf("real build behind it did not run:\n%s", out)
61	}
62
63	log, _, _ := inst.ssh(t, aliceKey, "", "build", "log", "alice/app", "1")
64	if !strings.Contains(log, "not reachable") {
65		t.Fatalf("cancelled build's log does not explain why:\n%s", log)
66	}
67
68	st, _, _ := inst.ssh(t, aliceKey, "", "status", "list", "alice/app", orphanSHA)
69	if !strings.Contains(st, "ci/unit") || strings.Contains(st, "pending") {
70		t.Fatalf("orphaned commit's status left pending:\n%s", st)
71	}
72}