BlueGit

timfa bluegit

Files

bluegit / internal / server / update_gate_test.go

update_gate_test.go

update_gate_test.go 3.3 KiB Download Raw
1package server
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "strings"
8 "testing"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/store"
12 "gitlab.com/Timfa/bluegit/internal/update"
13)
14
15func postUpdate(t *testing.T, url, token, body string) (int, string) {
16 t.Helper()
17 req, _ := http.NewRequest(http.MethodPost, url+"/api/v1/admin/self-update", strings.NewReader(body))
18 req.Header.Set("Authorization", "Bearer "+token)
19 req.Header.Set("Content-Type", "application/json")
20 res, err := http.DefaultClient.Do(req)
21 if err != nil {
22 t.Fatal(err)
23 }
24 return res.StatusCode, read(t, res)
25}
26
27func TestUpdateGateHoldsNewPipelines(t *testing.T) {
28 const tok = "sleutel-voor-de-test"
29 t.Setenv(update.TokenEnv, tok)
30 srv, ts, dir := newUpdateServer(t)
31 srv.UpdateStarter = func(context.Context, string) error { return nil }
32
33 before := store.Pipeline{ID: 1, Status: "running", CreatedAt: time.Now().Add(-time.Minute)}
34 if on, _ := srv.UpdateGate(); on || srv.HoldPipeline(before) {
35 t.Fatal("poort dicht zonder update")
36 }
37 time.Sleep(1100 * time.Millisecond) // RFC3339 heeft secondes
38 if code, body := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusAccepted {
39 t.Fatalf("POST: %d %s", code, body)
40 }
41 on, since := srv.UpdateGate()
42 if !on {
43 t.Fatal("geen update in afwachting")
44 }
45 // de pipeline die de update aanvroeg (ouder) loopt door: geen deadlock
46 if srv.HoldPipeline(before) {
47 t.Fatal("bestaande pipeline vastgehouden")
48 }
49 queued := store.Pipeline{ID: 2, Status: "queued", CreatedAt: since.Add(time.Second)}
50 if !srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "waiting_for_update" {
51 t.Fatal("nieuwe pipeline niet vastgehouden")
52 }
53 if got := srv.pipeJSON(store.Repo{OwnerPath: "tim", Name: "x"}, queued)["status"]; got != "waiting_for_update" {
54 t.Fatalf("API-status %v", got)
55 }
56
57 // tweede POST zonder force: 409; met force: vlag voor de updater
58 if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master"}`); code != http.StatusConflict {
59 t.Fatalf("dubbele update: %d", code)
60 }
61 if code, _ := postUpdate(t, ts.URL, tok, `{"ref":"master","force":true}`); code != http.StatusAccepted {
62 t.Fatalf("force: %d", code)
63 }
64 req, _ := http.NewRequest(http.MethodGet, ts.URL+"/api/v1/admin/self-update", nil)
65 req.Header.Set("Authorization", "Bearer "+tok)
66 res, _ := http.DefaultClient.Do(req)
67 var st map[string]any
68 _ = json.NewDecoder(res.Body).Decode(&st)
69 res.Body.Close()
70 if st["force"] != true || st["status"] != "waiting" {
71 t.Fatalf("status: %v", st)
72 }
73
74 // update klaar: poort open, wachtende pipelines starten
75 _ = update.WriteStatus(dir, update.Status{Time: time.Now().UTC().Format(time.RFC3339), SHA: "x", Result: "ok"})
76 if srv.HoldPipeline(queued) || srv.pipeStatusFor(queued) != "queued" {
77 t.Fatal("poort bleef dicht na update")
78 }
79}
80
81func TestUpdateGateSafetyValve(t *testing.T) {
82 t.Setenv(update.TokenEnv, "x")
83 srv, _, dir := newUpdateServer(t)
84 srv.Cfg.CI.UpdateMaxWaitMin = 1
85 old := time.Now().Add(-(time.Minute + updateRunning + time.Second))
86 _ = update.WriteStatus(dir, update.Status{Time: old.UTC().Format(time.RFC3339), SHA: "x", Result: update.ResultWaiting})
87 if on, _ := srv.UpdateGate(); on {
88 t.Fatal("poort bleef dicht na de maximale wachttijd")
89 }
90 p := store.Pipeline{Status: "queued", CreatedAt: time.Now()}
91 if srv.HoldPipeline(p) {
92 t.Fatal("pipeline vastgehouden na time-out")
93 }
94}