BlueGit

timfa bluegit

Files

bluegit / internal / server / pipeline_retry_test.go

pipeline_retry_test.go

pipeline_retry_test.go 10.2 KiB Download Raw
1package server
2
3import (
4 "context"
5 "encoding/json"
6 "io"
7 "net/http"
8 "net/http/cookiejar"
9 "net/http/httptest"
10 "net/url"
11 "os/exec"
12 "path/filepath"
13 "strconv"
14 "strings"
15 "testing"
16
17 "gitlab.com/Timfa/bluegit/internal/access"
18 "gitlab.com/Timfa/bluegit/internal/auth"
19 "gitlab.com/Timfa/bluegit/internal/config"
20 "gitlab.com/Timfa/bluegit/internal/gitrepo"
21 "gitlab.com/Timfa/bluegit/internal/store"
22)
23
24func TestPipelineRetryButtonAndAPI(t *testing.T) {
25 if _, err := exec.LookPath("git"); err != nil {
26 t.Skip("git ontbreekt")
27 }
28 storage := t.TempDir()
29 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
30 if err != nil {
31 t.Fatal(err)
32 }
33 t.Cleanup(func() { _ = st.Close() })
34 cfg := config.Config{
35 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
36 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
37 Git: config.Git{Binary: "git", DefaultBranch: "master"},
38 }
39 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
40 ts := httptest.NewServer(srv.Handler())
41 t.Cleanup(ts.Close)
42 jar, _ := cookiejar.New(nil)
43 tim := &http.Client{Jar: jar}
44 page := read(t, mustGet(t, tim, ts.URL+"/login"))
45 if _, err := tim.PostForm(ts.URL+"/new/repo", url.Values{
46 "csrf": {csrfOf(t, page)}, "name": {"opnieuw"}, "visibility": {"public"},
47 }); err != nil {
48 t.Fatal(err)
49 }
50 ctx := context.Background()
51 repo, err := st.RepoByPath(ctx, "tim", "opnieuw")
52 if err != nil {
53 t.Fatal(err)
54 }
55 ro, err := st.UpsertUser(ctx, 2, "ro", "", false)
56 if err != nil {
57 t.Fatal(err)
58 }
59 if err := st.SetMember(ctx, "repo", repo.ID, "ro", access.RoleReadonly); err != nil {
60 t.Fatal(err)
61 }
62 roToken, err := st.CreateToken(ctx, ro.ID, "ro")
63 if err != nil {
64 t.Fatal(err)
65 }
66 owner, err := st.UserByUsername(ctx, "tim")
67 if err != nil {
68 t.Fatal(err)
69 }
70 ownerToken, err := st.CreateToken(ctx, owner.ID, "tim")
71 if err != nil {
72 t.Fatal(err)
73 }
74
75 dir := gitrepo.Dir(storage, "tim", "opnieuw")
76 work := t.TempDir()
77 git(t, work, "init", "-b", "master")
78 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
79 git(t, work, "config", "user.name", "tim")
80 writeFile(t, filepath.Join(work, "README.md"), "hallo\n")
81 git(t, work, "add", "README.md")
82 git(t, work, "commit", "-m", "leesmij")
83 git(t, work, "remote", "add", "origin", dir)
84 git(t, work, "push", "origin", "master")
85 sha := strings.TrimSpace(mustGit(t, dir, "rev-parse", "refs/heads/master"))
86 git(t, dir, "branch", "topic", sha)
87
88 mr, err := st.CreateMergeRequest(ctx, repo.ID, owner.ID, "Opnieuw", "", "topic", "master")
89 if err != nil {
90 t.Fatal(err)
91 }
92 okID := makePipe(t, st, repo.ID, sha, "groen", []store.NewJob{
93 {Name: "lint", Stage: "test", Script: "exit 0"},
94 })
95 finishPipe(t, st, okID, "success")
96 id := makePipe(t, st, repo.ID, sha, "rood", []store.NewJob{
97 {Name: "lint", Stage: "test", Script: "exit 0"},
98 {Name: "unit", Stage: "test", Script: "exit 1"},
99 {Name: "pack", Stage: "deploy", Script: "exit 0"},
100 {Name: "pages", Stage: "deploy", Script: "exit 0", When: "manual"},
101 {Name: "aur", Stage: "deploy", Script: "echo aur", NeedsSet: true},
102 {Name: "note", Stage: "deploy", Script: "exit 0"},
103 })
104 jobs, err := st.JobsByPipeline(ctx, id)
105 if err != nil {
106 t.Fatal(err)
107 }
108 for _, j := range jobs {
109 switch j.Name {
110 case "lint", "aur":
111 if err := st.FinishJob(ctx, j.ID, "success", "ok\n"); err != nil {
112 t.Fatal(err)
113 }
114 case "unit":
115 if err := st.FinishJob(ctx, j.ID, "failed", "nee\n"); err != nil {
116 t.Fatal(err)
117 }
118 case "note":
119 if err := st.FinishJob(ctx, j.ID, "canceled", "stop\n"); err != nil {
120 t.Fatal(err)
121 }
122 case "pack", "pages":
123 if err := st.SkipJob(ctx, j.ID, "skipped\n"); err != nil {
124 t.Fatal(err)
125 }
126 }
127 }
128 if _, err := st.RefreshPipelineStatus(ctx, id); err != nil {
129 t.Fatal(err)
130 }
131 if err := st.AttachPipelineMR(ctx, id, mr.ID); err != nil {
132 t.Fatal(err)
133 }
134
135 list := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/pipelines"))
136 detail := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)))
137 home := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw"))
138 mrPage := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/merge-requests/1"))
139 for _, body := range []string{list, detail, home, mrPage} {
140 if !strings.Contains(body, `class="rowmenu piperetry"`) || !strings.Contains(body, "Retry entire pipeline") {
141 t.Fatalf("schrijver mist de knop:\n%s", body)
142 }
143 }
144 failedForm := `data-retry-failed data-pipe="` + strconv.FormatInt(id, 10) + `"`
145 okForm := `data-retry-failed data-pipe="` + strconv.FormatInt(okID, 10) + `" hidden`
146 if !strings.Contains(list, failedForm) || strings.Contains(list, failedForm+" hidden") || !strings.Contains(list, okForm) {
147 t.Fatalf("mislukte optie hoort alleen bij de rode pipeline:\n%s", list)
148 }
149 if !strings.Contains(detail, "Retry failed jobs") || strings.Contains(detail, failedForm+" hidden") {
150 t.Fatalf("detail mist de mislukte optie:\n%s", detail)
151 }
152 if !strings.Contains(home, "Retry failed jobs") || !strings.Contains(mrPage, "Retry failed jobs") {
153 t.Fatal("overzicht of merge request mist de mislukte optie")
154 }
155
156 anon := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/pipelines"))
157 anonHome := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw"))
158 anonMR := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/merge-requests/1"))
159 anonDetail := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)))
160 for _, body := range []string{anon, anonHome, anonMR, anonDetail} {
161 if strings.Contains(body, "piperetry") || strings.Contains(body, "Retry entire pipeline") {
162 t.Fatalf("lezer ziet de knop:\n%s", body)
163 }
164 }
165
166 code, body := retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", roToken, `{"mode":"failed"}`)
167 if code != 403 {
168 t.Fatalf("readonly api %d %s", code, body)
169 }
170 code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"nope"}`)
171 if code != 400 {
172 t.Fatalf("mode %d %s", code, body)
173 }
174 res, err := http.PostForm(ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)+"/retry", url.Values{"mode": {"all"}})
175 if err != nil {
176 t.Fatal(err)
177 }
178 if res.StatusCode != http.StatusForbidden {
179 t.Fatalf("anonieme post %d", res.StatusCode)
180 }
181 res.Body.Close()
182
183 csrf := csrfOf(t, detail)
184 done := read(t, mustPost(t, tim, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)+"/retry", url.Values{
185 "csrf": {csrf}, "mode": {"failed"}, "back": {"/tim/opnieuw/-/pipelines/" + strconv.FormatInt(id, 10)},
186 }))
187 if !strings.Contains(done, "Pipeline retried") || !strings.Contains(done, "Running") {
188 t.Fatalf("na mislukte jobs:\n%s", done)
189 }
190 if !strings.Contains(done, failedForm+" hidden") {
191 t.Fatalf("optie blijft staan zonder mislukking:\n%s", done)
192 }
193 got := map[string]string{}
194 jobs, err = st.JobsByPipeline(ctx, id)
195 if err != nil {
196 t.Fatal(err)
197 }
198 for _, j := range jobs {
199 got[j.Name] = j.Status
200 }
201 if got["lint"] != "success" || got["aur"] != "success" || got["unit"] != "queued" || got["pack"] != "queued" || got["pages"] != "manual" || got["note"] != "canceled" {
202 t.Fatalf("jobs na retry: %v", got)
203 }
204 p, err := st.PipelineByID(ctx, repo.ID, id)
205 if err != nil || p.Status != "running" {
206 t.Fatalf("pipelinestatus %s %v", p.Status, err)
207 }
208
209 code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"failed"}`)
210 if code != 409 {
211 t.Fatalf("tweede failed %d %s", code, body)
212 }
213 code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{}`)
214 if code != 409 {
215 t.Fatalf("leeg object default failed %d %s", code, body)
216 }
217 code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, "")
218 if code != 409 {
219 t.Fatalf("lege body default failed %d %s", code, body)
220 }
221 // Zonder gefaalde job weigert ook "all" (zelfde regel als de UI).
222 for _, pid := range []int64{id, okID} {
223 code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(pid, 10)+"/retry", ownerToken, `{"mode":"all"}`)
224 if code != 409 || !strings.Contains(body, "no failed jobs") {
225 t.Fatalf("all zonder mislukking %d: %d %s", pid, code, body)
226 }
227 }
228 if !strings.Contains(read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(okID, 10))), okForm) {
229 t.Fatal("geslaagde pipeline toont retry")
230 }
231 jobs, _ = st.JobsByPipeline(ctx, id)
232 for _, j := range jobs {
233 if j.Name == "unit" {
234 if err := st.FinishJob(ctx, j.ID, "failed", "runner_system_failure: weg\n"); err != nil {
235 t.Fatal(err)
236 }
237 }
238 }
239 code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"all"}`)
240 if code != 201 {
241 t.Fatalf("all %d %s", code, body)
242 }
243 var created map[string]any
244 if err := json.Unmarshal([]byte(body), &created); err != nil {
245 t.Fatal(err)
246 }
247 newID, _ := created["id"].(float64)
248 if int64(newID) == id || created["sha"] != sha || created["source"] != "push" && created["source"] != "merge_request_event" {
249 t.Fatalf("antwoord %s", body)
250 }
251 clone, err := st.PipelineByID(ctx, repo.ID, int64(newID))
252 if err != nil {
253 t.Fatal(err)
254 }
255 if clone.CommitSHA != sha || clone.Ref != "refs/heads/master" || clone.Source != "push" || clone.MRID != mr.ID || clone.Status != "queued" {
256 t.Fatalf("kloon %+v", clone)
257 }
258 old, err := st.PipelineByID(ctx, repo.ID, id)
259 if err != nil || old.Status != "running" {
260 t.Fatalf("oude pipeline %s %v", old.Status, err)
261 }
262}
263
264func retryAPI(t *testing.T, ts *httptest.Server, method, path, token, body string) (int, string) {
265 t.Helper()
266 req, err := http.NewRequest(method, ts.URL+path, strings.NewReader(body))
267 if err != nil {
268 t.Fatal(err)
269 }
270 req.Header.Set("Content-Type", "application/json")
271 req.Header.Set("Authorization", "Bearer "+token)
272 res, err := http.DefaultClient.Do(req)
273 if err != nil {
274 t.Fatal(err)
275 }
276 defer res.Body.Close()
277 raw, err := io.ReadAll(res.Body)
278 if err != nil {
279 t.Fatal(err)
280 }
281 return res.StatusCode, string(raw)
282}