internal/control/build_test.go

00033f022fed6fe0b36e1d7a1e2b3f1df6139ccd
gitbay/internal/control/build_test.go history · blame · raw

91 lines · 2860 bytes

 1package control
 2
 3import (
 4	"os"
 5	"os/exec"
 6	"path/filepath"
 7	"strings"
 8	"testing"
 9	"time"
10
11	"gitbay.org/gitbay/internal/store"
12)
13
14// A DiffFiles failure must not turn into a silent skip: when the old sha
15// on record cannot be diffed against, every job runs regardless of what
16// it names in paths, the same as when there is no diff base at all.
17func TestQueueBranchBuildsFailsOpenOnDiffFailure(t *testing.T) {
18	st, err := store.Open(":memory:")
19	if err != nil {
20		t.Fatal(err)
21	}
22	defer st.Close()
23	if err := st.MigrateUp(); err != nil {
24		t.Fatal(err)
25	}
26	uid, err := st.CreateUser("alice", false)
27	if err != nil {
28		t.Fatal(err)
29	}
30	repoID, err := st.CreateRepo("user", uid, "app", "public")
31	if err != nil {
32		t.Fatal(err)
33	}
34	repo, err := st.RepoByID(repoID)
35	if err != nil {
36		t.Fatal(err)
37	}
38
39	root := t.TempDir()
40	env := append(os.Environ(),
41		"GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null",
42		"GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.test",
43		"GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.test")
44	git := func(dir string, args ...string) string {
45		t.Helper()
46		cmd := exec.Command("git", args...)
47		cmd.Dir = dir
48		cmd.Env = env
49		out, err := cmd.CombinedOutput()
50		if err != nil {
51			t.Fatalf("git %v: %v\n%s", args, err, out)
52		}
53		return string(out)
54	}
55
56	// A job whose paths would exclude a docs-only change, so the test
57	// proves something: without fail-open, the diff failure would leave
58	// the filter unevaluated and this build would never queue.
59	src := filepath.Join(root, "src")
60	os.MkdirAll(filepath.Join(src, ".gitbay"), 0o755)
61	os.MkdirAll(filepath.Join(src, "docs"), 0o755)
62	os.WriteFile(filepath.Join(src, ".gitbay", "ci.yml"), []byte(
63		"jobs:\n  unit:\n    paths:\n      - src/**\n    steps:\n      - echo hi\n"), 0o644)
64	os.WriteFile(filepath.Join(src, "docs", "x.md"), []byte("# x\n"), 0o644)
65	git(root, "init", "-q", "-b", "main", "src")
66	git(src, "add", ".")
67	git(src, "commit", "-q", "-m", "base")
68	os.WriteFile(filepath.Join(src, "docs", "x.md"), []byte("# x changed\n"), 0o644)
69	git(src, "add", ".")
70	git(src, "commit", "-q", "-m", "docs only")
71	newSHA := strings.TrimSpace(git(src, "rev-parse", "HEAD"))
72
73	dir := RepoDir(root, repo.OwnerName, repo.Name)
74	os.MkdirAll(filepath.Dir(dir), 0o755)
75	git(root, "clone", "-q", "--bare", src, dir)
76
77	// Well-formed but names no object in this repo: git diff itself
78	// fails, rather than the empty/all-zero short-circuit HasDiffBase
79	// already covers.
80	old := strings.Repeat("1", 40)
81
82	QueueBranchBuilds(st, root, "https://x.test", repo, uid, "main", old, newSHA, time.Now())
83
84	builds, err := st.ListBuilds(repo.ID, 10)
85	if err != nil || len(builds) != 1 {
86		t.Fatalf("builds after queue: %v %v", builds, err)
87	}
88	if builds[0].Job != "unit" || builds[0].Status != "pending" || builds[0].SHA != newSHA {
89		t.Fatalf("queued build wrong: %+v", builds[0])
90	}
91}