e2e/ci_merge_test.go

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

74 lines · 2818 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	t.Parallel()
16	inst := startInstance(t)
17	aliceKey := inst.newKey(t, "alice")
18	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub",
19		"--email", "alice@example.test", "--verified")
20
21	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
22		t.Fatalf("repo create: %s", errOut)
23	}
24	work := t.TempDir()
25	env := inst.gitEnv(aliceKey)
26	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
27	dir := filepath.Join(work, "w")
28	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
29	os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
30		"jobs:\n  unit:\n    steps:\n      - echo hello\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
36	// A feature branch, pushed and merged rather than pushed to main.
37	mustGit(t, dir, env, "checkout", "-q", "-b", "feature")
38	os.WriteFile(filepath.Join(dir, "f.txt"), []byte("x\n"), 0o644)
39	mustGit(t, dir, env, "add", ".")
40	mustGit(t, dir, env, "commit", "-q", "-m", "a change")
41	mustGit(t, dir, env, "push", "-q", "origin", "feature")
42
43	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app",
44		"--source", "feature", "--target", "main", "--title", "'a change'"); code != 0 {
45		t.Fatalf("mr create: %s", errOut)
46	}
47	before := strings.Count(inst.buildList(t, aliceKey), "\n")
48
49	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1",
50		"--strategy", "merge"); code != 0 {
51		t.Fatalf("mr merge: %s", errOut)
52	}
53	merged := strings.TrimSpace(mustGit(t, dir, env, "ls-remote", "origin", "refs/heads/main"))
54	sha := strings.Fields(merged)[0]
55
56	// The merge queued the branch's jobs at the merge commit.
57	out := inst.buildList(t, aliceKey)
58	if strings.Count(out, "\n") <= before {
59		t.Fatalf("merge queued no build:\n%s", out)
60	}
61	if !strings.Contains(out, "unit\tpending") {
62		t.Fatalf("merge did not queue the unit job:\n%s", out)
63	}
64	status, _, _ := inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha)
65	if !strings.Contains(status, "ci/unit") || !strings.Contains(status, "pending") {
66		t.Fatalf("no pending status on the merge commit %s:\n%s", sha, status)
67	}
68}
69
70func (i *instance) buildList(t *testing.T, key string) string {
71	t.Helper()
72	out, _, _ := i.ssh(t, key, "", "build", "list", "alice/app")
73	return out
74}