| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "os/exec" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 15 | ) |
| 16 | |
| 17 | func TestGitMaintenanceAPI(t *testing.T) { |
| 18 | ts, client, srv := graphTestRepo(t) |
| 19 | page := read(t, mustGet(t, client, ts.URL+"/settings")) |
| 20 | page = read(t, mustPost(t, client, ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"maint"}})) |
| 21 | token := between(page, `<p class="code">`, "</p>") |
| 22 | if token == "" { |
| 23 | t.Fatal("geen API-token") |
| 24 | } |
| 25 | |
| 26 | res := getBearer(t, ts.URL+"/api/v1/admin/git-maintenance", token) |
| 27 | defer res.Body.Close() |
| 28 | if res.StatusCode != 200 { |
| 29 | b, _ := io.ReadAll(res.Body) |
| 30 | t.Fatalf("GET status %d %s", res.StatusCode, b) |
| 31 | } |
| 32 | var st map[string]any |
| 33 | if err := json.NewDecoder(res.Body).Decode(&st); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if st["enabled"] != true { |
| 37 | t.Fatalf("onderhoud zou standaard aan moeten staan: %v", st) |
| 38 | } |
| 39 | if iv, _ := st["interval"].(string); !strings.Contains(iv, "h") { |
| 40 | t.Fatalf("interval ontbreekt: %v", st) |
| 41 | } |
| 42 | |
| 43 | req, err := http.NewRequest(http.MethodPost, ts.URL+"/api/v1/admin/git-maintenance", strings.NewReader(`{"repo":"tim/grafiek"}`)) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | req.Header.Set("Authorization", "Bearer "+token) |
| 48 | req.Header.Set("Content-Type", "application/json") |
| 49 | post, err := http.DefaultClient.Do(req) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | defer post.Body.Close() |
| 54 | body, _ := io.ReadAll(post.Body) |
| 55 | if post.StatusCode != 200 { |
| 56 | t.Fatalf("POST %d %s", post.StatusCode, body) |
| 57 | } |
| 58 | var out map[string]any |
| 59 | if err := json.Unmarshal(body, &out); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if out["ok"] != float64(1) || out["skipped"] != float64(0) { |
| 63 | t.Fatalf("handmatige run op tim/grafiek mislukte: %s", body) |
| 64 | } |
| 65 | dir := gitrepo.Dir(srv.StoragePath(), "tim", "grafiek") |
| 66 | cmd := exec.Command("git", "-C", dir, "rev-parse", "refs/heads/master") |
| 67 | if b, err := cmd.CombinedOutput(); err != nil { |
| 68 | t.Fatalf("master-ref verdween na onderhoud: %s %v", b, err) |
| 69 | } |
| 70 | cmd = exec.Command("git", "-C", dir, "cat-file", "-t", "v1") |
| 71 | if b, err := cmd.CombinedOutput(); err != nil { |
| 72 | t.Fatalf("tag v1 verdween na onderhoud: %s %v", b, err) |
| 73 | } |
| 74 | |
| 75 | bad, err := http.Get(ts.URL + "/api/v1/admin/git-maintenance") |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | bad.Body.Close() |
| 80 | if bad.StatusCode != http.StatusUnauthorized { |
| 81 | t.Fatalf("zonder token: %d, wil 401", bad.StatusCode) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestGitMaintenanceSweeperRunsOnce(t *testing.T) { |
| 86 | ts, _, srv := graphTestRepo(t) |
| 87 | _ = ts |
| 88 | oldDelay := gitMaintFirstDelay |
| 89 | gitMaintFirstDelay = 20 * time.Millisecond |
| 90 | t.Cleanup(func() { gitMaintFirstDelay = oldDelay }) |
| 91 | srv.Cfg.Git.MaintenanceInterval = "30ms" |
| 92 | |
| 93 | ctx, cancel := context.WithCancel(context.Background()) |
| 94 | t.Cleanup(cancel) |
| 95 | go srv.GitMaintenanceSweeper(ctx) |
| 96 | |
| 97 | deadline := time.Now().Add(3 * time.Second) |
| 98 | for time.Now().Before(deadline) { |
| 99 | st := srv.gitMaintStatus() |
| 100 | ok, _ := st["ok"].(int) |
| 101 | if ok >= 1 { |
| 102 | return |
| 103 | } |
| 104 | time.Sleep(20 * time.Millisecond) |
| 105 | } |
| 106 | t.Fatalf("sweeper draaide niet: %+v", srv.gitMaintStatus()) |
| 107 | } |
| 108 | |
| 109 | func TestGitMaintenanceSkipsMissingRepo(t *testing.T) { |
| 110 | _, _, srv := graphTestRepo(t) |
| 111 | rep, started := srv.RunGitMaintenance(context.Background(), "tim/bestaat-niet") |
| 112 | if !started { |
| 113 | t.Fatal("ronde startte niet") |
| 114 | } |
| 115 | if rep.Error == "" && rep.Skipped == 0 && rep.Repos == 0 { |
| 116 | t.Fatalf("ontbrekende repo zou als fout gelogd moeten worden: %+v", rep) |
| 117 | } |
| 118 | if rep.Error == "" { |
| 119 | t.Fatalf("verwacht een foutmelding: %+v", rep) |
| 120 | } |
| 121 | } |