internal/control/buildfollow_test.go
190 lines · 5796 bytes
1package control
2
3import (
4 "bytes"
5 "strings"
6 "sync"
7 "testing"
8 "time"
9
10 "gitbay.org/gitbay/internal/protocol"
11 "gitbay.org/gitbay/internal/store"
12)
13
14// syncBuffer is a bytes.Buffer guarded by a mutex, safe for a test to poll
15// while the follow goroutine is still writing to it.
16type syncBuffer struct {
17 mu sync.Mutex
18 buf bytes.Buffer
19}
20
21func (b *syncBuffer) Write(p []byte) (int, error) {
22 b.mu.Lock()
23 defer b.mu.Unlock()
24 return b.buf.Write(p)
25}
26
27func (b *syncBuffer) String() string {
28 b.mu.Lock()
29 defer b.mu.Unlock()
30 return b.buf.String()
31}
32
33// follow starts build log --follow on build 1 of repo and returns the
34// buffers and a channel carrying the exit code.
35func follow(t *testing.T, st *store.Store, uid int64, repo store.Repo, done <-chan struct{}) (*syncBuffer, *syncBuffer, chan int) {
36 t.Helper()
37 u, err := st.UserByID(uid)
38 if err != nil {
39 t.Fatal(err)
40 }
41 var out, errOut syncBuffer
42 c := &Ctx{User: u, Scope: "full", Store: st, Stdin: strings.NewReader(""),
43 Stdout: &out, Stderr: &errOut, Done: done}
44 res := make(chan int, 1)
45 go func() { res <- Dispatch(c, []string{"build", "log", repo.Path(), "1", "--follow"}) }()
46 return &out, &errOut, res
47}
48
49func waitExit(t *testing.T, res chan int) int {
50 t.Helper()
51 select {
52 case code := <-res:
53 return code
54 case <-time.After(10 * time.Second):
55 t.Fatal("follow did not end")
56 return -1
57 }
58}
59
60func shortFollowTimers(t *testing.T) {
61 settle, poll, queued := followSettle, followPoll, followQueued
62 followSettle, followPoll = 200*time.Millisecond, 50*time.Millisecond
63 t.Cleanup(func() { followSettle, followPoll, followQueued = settle, poll, queued })
64}
65
66// The follow prints the stored log, then what arrives, and ends with the
67// outcome on stderr once the build finishes.
68func TestBuildLogFollow(t *testing.T) {
69 shortFollowTimers(t)
70 st, repo, uid := newQueueTestRepo(t)
71 id, err := st.CreateBuild(repo.ID, "unit", "abc", "main", `["true"]`, "", "", true)
72 if err != nil {
73 t.Fatal(err)
74 }
75 st.AppendBuildLog(id, []byte("queued\n"))
76 out, errOut, res := follow(t, st, uid, repo, nil)
77
78 if _, ok, err := st.ClaimBuild([]int64{repo.ID}, false); err != nil || !ok {
79 t.Fatalf("claim: %v %v", ok, err)
80 }
81 st.AppendBuildLog(id, []byte("step one\n"))
82 st.AppendBuildLog(id, []byte("step two\n"))
83 if err := st.FinishBuild(id, "success"); err != nil {
84 t.Fatal(err)
85 }
86 if code := waitExit(t, res); code != protocol.ExitOK {
87 t.Fatalf("exit %d: %s", code, errOut)
88 }
89 if got := out.String(); got != "queued\nstep one\nstep two\n" {
90 t.Errorf("stdout %q", got)
91 }
92 if got := strings.TrimSpace(errOut.String()); got != "build 1 success" {
93 t.Errorf("stderr %q", got)
94 }
95}
96
97// A cancel ends the follow, and the line the cancel appends after the
98// status change still arrives.
99func TestBuildLogFollowCancel(t *testing.T) {
100 shortFollowTimers(t)
101 st, repo, uid := newQueueTestRepo(t)
102 id, err := st.CreateBuild(repo.ID, "unit", "abc", "main", `["true"]`, "", "", true)
103 if err != nil {
104 t.Fatal(err)
105 }
106 out, errOut, res := follow(t, st, uid, repo, nil)
107 if err := st.CancelBuild(id); err != nil {
108 t.Fatal(err)
109 }
110 st.AppendBuildLog(id, []byte("cancelled by alice before a runner claimed it\n"))
111 if code := waitExit(t, res); code != protocol.ExitOK {
112 t.Fatalf("exit %d: %s", code, errOut)
113 }
114 if !strings.Contains(out.String(), "cancelled by alice") {
115 t.Errorf("the cancel line did not arrive: %q", out)
116 }
117 if got := strings.TrimSpace(errOut.String()); got != "build 1 cancelled" {
118 t.Errorf("stderr %q", got)
119 }
120}
121
122// Closing Done ends a follow of a build that is still running, even while
123// it is blocked waiting for the next change: the build gets a line, the
124// follower is confirmed to have read it (so it is back in its wait), then
125// Done closes.
126func TestBuildLogFollowDone(t *testing.T) {
127 shortFollowTimers(t)
128 st, repo, uid := newQueueTestRepo(t)
129 id, err := st.CreateBuild(repo.ID, "unit", "abc", "main", `["true"]`, "", "", true)
130 if err != nil {
131 t.Fatal(err)
132 }
133 st.AppendBuildLog(id, []byte("step one\n"))
134 done := make(chan struct{})
135 out, _, res := follow(t, st, uid, repo, done)
136
137 deadline := time.After(2 * time.Second)
138 for !strings.Contains(out.String(), "step one") {
139 select {
140 case <-deadline:
141 t.Fatal("follow never read the appended line")
142 case <-time.After(10 * time.Millisecond):
143 }
144 }
145 close(done)
146 if code := waitExit(t, res); code != protocol.ExitFailure {
147 t.Fatalf("exit %d, want %d", code, protocol.ExitFailure)
148 }
149}
150
151// A build that stays pending ends its own follow: nothing reaps a queued
152// build, so the follow must give up on its own.
153func TestBuildLogFollowQueued(t *testing.T) {
154 shortFollowTimers(t)
155 followQueued = 150 * time.Millisecond
156 st, repo, uid := newQueueTestRepo(t)
157 if _, err := st.CreateBuild(repo.ID, "unit", "abc", "main", `["true"]`, "", "", true); err != nil {
158 t.Fatal(err)
159 }
160 _, errOut, res := follow(t, st, uid, repo, nil)
161 if code := waitExit(t, res); code != protocol.ExitFailure {
162 t.Fatalf("exit %d, want %d: %s", code, protocol.ExitFailure, errOut)
163 }
164 if !strings.Contains(errOut.String(), "still queued") {
165 t.Errorf("stderr %q", errOut)
166 }
167}
168
169// An account holding maxFollows is refused another.
170func TestBuildLogFollowCap(t *testing.T) {
171 st, repo, uid := newQueueTestRepo(t)
172 if _, err := st.CreateBuild(repo.ID, "unit", "abc", "main", `["true"]`, "", "", true); err != nil {
173 t.Fatal(err)
174 }
175 followMu.Lock()
176 follows[uid] = maxFollows
177 followMu.Unlock()
178 t.Cleanup(func() {
179 followMu.Lock()
180 delete(follows, uid)
181 followMu.Unlock()
182 })
183 _, errOut, res := follow(t, st, uid, repo, nil)
184 if code := waitExit(t, res); code != protocol.ExitDenied {
185 t.Fatalf("exit %d, want %d", code, protocol.ExitDenied)
186 }
187 if !strings.Contains(errOut.String(), "8 follows are already open") {
188 t.Errorf("stderr %q", errOut)
189 }
190}