e2e/ci_merge_test.go

v1.33.0
gitbay/e2e/ci_merge_test.go history · blame · raw

73 lines · 2804 bytes

 1package e2e
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// A merge moves the target ref without reaching post-receive, so the
11// ref-update work has to happen in the merge path. Before it did, merging
12// an MR into a repository with a CI config queued nothing, while pushing
13// the identical commit ran the whole config.
14func TestMergeQueuesBuildsAndRecordsPush(t *testing.T) {
15	inst := startInstance(t)
16	aliceKey := inst.newKey(t, "alice")
17	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub",
18		"--email", "alice@example.test", "--verified")
19
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(
29		"jobs:\n  unit:\n    steps:\n      - echo hello\n"), 0o644)
30	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
31	mustGit(t, dir, env, "add", ".")
32	mustGit(t, dir, env, "commit", "-q", "-m", "base")
33	mustGit(t, dir, env, "push", "-q", "origin", "main")
34
35	// A feature branch, pushed and merged rather than pushed to main.
36	mustGit(t, dir, env, "checkout", "-q", "-b", "feature")
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", "a change")
40	mustGit(t, dir, env, "push", "-q", "origin", "feature")
41
42	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app",
43		"--source", "feature", "--target", "main", "--title", "'a change'"); code != 0 {
44		t.Fatalf("mr create: %s", errOut)
45	}
46	before := strings.Count(inst.buildList(t, aliceKey), "\n")
47
48	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1",
49		"--strategy", "merge"); code != 0 {
50		t.Fatalf("mr merge: %s", errOut)
51	}
52	merged := strings.TrimSpace(mustGit(t, dir, env, "ls-remote", "origin", "refs/heads/main"))
53	sha := strings.Fields(merged)[0]
54
55	// The merge queued the branch's jobs at the merge commit.
56	out := inst.buildList(t, aliceKey)
57	if strings.Count(out, "\n") <= before {
58		t.Fatalf("merge queued no build:\n%s", out)
59	}
60	if !strings.Contains(out, "unit\tpending") {
61		t.Fatalf("merge did not queue the unit job:\n%s", out)
62	}
63	status, _, _ := inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha)
64	if !strings.Contains(status, "ci/unit") || !strings.Contains(status, "pending") {
65		t.Fatalf("no pending status on the merge commit %s:\n%s", sha, status)
66	}
67}
68
69func (i *instance) buildList(t *testing.T, key string) string {
70	t.Helper()
71	out, _, _ := i.ssh(t, key, "", "build", "list", "alice/app")
72	return out
73}