e2e/ci_dedupe_test.go

00033f022fed6fe0b36e1d7a1e2b3f1df6139ccd
gitbay/e2e/ci_dedupe_test.go history · blame · raw

100 lines · 4967 bytes

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