| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "gitlab.com/Timfa/bluegit/internal/readcache" |
| 13 | "gitlab.com/Timfa/bluegit/internal/store" |
| 14 | ) |
| 15 | |
| 16 | func cacheServer(t *testing.T) (*Server, string, func(path string) (int, string), *http.Client, string, string) { |
| 17 | t.Helper() |
| 18 | srv, ts, st := mergeFixServer(t) |
| 19 | srv.StateDir = t.TempDir() |
| 20 | root := t.TempDir() |
| 21 | if err := srv.opSetCache(root, 64); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | tim := loginClient(t, ts) |
| 25 | token := mintToken(t, tim, ts) |
| 26 | work := pushPair(t, ts, token, "cached", "hallo\n", "verder\n") |
| 27 | git(t, work, "checkout", "master") |
| 28 | writeFile(t, filepath.Join(work, "README.md"), "# Eerste versie\n") |
| 29 | git(t, work, "add", "README.md") |
| 30 | git(t, work, "commit", "-m", "readme") |
| 31 | git(t, work, "push", "origin", "master") |
| 32 | _ = st |
| 33 | get := func(path string) (int, string) { |
| 34 | res, err := tim.Get(ts.URL + path) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | return res.StatusCode, read(t, res) |
| 39 | } |
| 40 | return srv, root, get, tim, ts.URL, work |
| 41 | } |
| 42 | |
| 43 | func repoOf(t *testing.T, srv *Server, name string) store.Repo { |
| 44 | t.Helper() |
| 45 | repos, err := srv.Store.AllRepos(context.Background()) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | for _, r := range repos { |
| 50 | if r.Name == name { |
| 51 | return r |
| 52 | } |
| 53 | } |
| 54 | t.Fatalf("repo %s ontbreekt", name) |
| 55 | return store.Repo{} |
| 56 | } |
| 57 | |
| 58 | // waitWarm wacht tot de achtergrond-vulling na een push klaar is. |
| 59 | func waitWarm(srv *Server) { |
| 60 | time.Sleep(300 * time.Millisecond) |
| 61 | } |
| 62 | |
| 63 | func TestReadCacheServesOverviewWithoutRepoDisk(t *testing.T) { |
| 64 | srv, root, get, _, _, work := cacheServer(t) |
| 65 | waitWarm(srv) |
| 66 | pages := []string{"/tim/cached", "/tim/cached/-/branches", "/tim/cached/-/tags"} |
| 67 | for _, p := range pages { |
| 68 | if code, _ := get(p); code != 200 { |
| 69 | t.Fatalf("%s: %d", p, code) |
| 70 | } |
| 71 | } |
| 72 | if srv.ReadCache().Usage().Entries == 0 { |
| 73 | t.Fatal("niets gecachet") |
| 74 | } |
| 75 | // HDD 'weg': de repository-map tijdelijk verplaatsen. |
| 76 | repo := repoOf(t, srv, "cached") |
| 77 | dir := srv.gdir(repo) |
| 78 | if err := os.Rename(dir, dir+".weg"); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | _, body := get("/tim/cached") |
| 82 | if !strings.Contains(body, "Eerste versie") { |
| 83 | t.Fatal("README niet uit de cache") |
| 84 | } |
| 85 | if _, body := get("/tim/cached/-/branches"); !strings.Contains(body, "topic") { |
| 86 | t.Fatal("branches niet uit de cache") |
| 87 | } |
| 88 | if err := os.Rename(dir+".weg", dir); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | |
| 92 | // write-through: na een push nooit de oude README |
| 93 | writeFile(t, filepath.Join(work, "README.md"), "# Tweede versie\n") |
| 94 | git(t, work, "add", "README.md") |
| 95 | git(t, work, "commit", "-m", "readme 2") |
| 96 | git(t, work, "push", "origin", "master") |
| 97 | git(t, work, "push", "origin", "master:nieuwe-branch") |
| 98 | _, body = get("/tim/cached") |
| 99 | if !strings.Contains(body, "Tweede versie") || strings.Contains(body, "Eerste versie") { |
| 100 | t.Fatal("verouderde README na push") |
| 101 | } |
| 102 | if _, body := get("/tim/cached/-/branches"); !strings.Contains(body, "nieuwe-branch") { |
| 103 | t.Fatal("nieuwe branch ontbreekt") |
| 104 | } |
| 105 | |
| 106 | // corrupte entries zijn een miss, geen fout |
| 107 | _ = filepath.Walk(filepath.Join(root, "cache"), func(p string, info os.FileInfo, err error) error { |
| 108 | if err == nil && !info.IsDir() { |
| 109 | _ = os.WriteFile(p, []byte("kapot"), 0o600) |
| 110 | } |
| 111 | return nil |
| 112 | }) |
| 113 | if code, body := get("/tim/cached"); code != 200 || !strings.Contains(body, "Tweede versie") { |
| 114 | t.Fatalf("corrupte cache brak de pagina: %d", code) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestReadCacheRespectsVisibility(t *testing.T) { |
| 119 | srv, _, get, _, base, _ := cacheServer(t) |
| 120 | waitWarm(srv) |
| 121 | get("/tim/cached") |
| 122 | repo := repoOf(t, srv, "cached") |
| 123 | anon := &http.Client{} |
| 124 | res, _ := anon.Get(base + "/tim/cached") |
| 125 | res.Body.Close() |
| 126 | if res.StatusCode != 200 { |
| 127 | t.Fatalf("publiek anoniem: %d", res.StatusCode) |
| 128 | } |
| 129 | before := srv.ReadCache().Usage().Entries |
| 130 | tim, _ := srv.Store.UserByUsername(context.Background(), "tim") |
| 131 | if err := srv.opUpdateRepo(context.Background(), tim, repo, repo.Description, "private"); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | if after := srv.ReadCache().Usage().Entries; after >= before { |
| 135 | t.Fatalf("cache niet gewist bij zichtbaarheid: %d -> %d", before, after) |
| 136 | } |
| 137 | get("/tim/cached") // eigenaar warmt opnieuw |
| 138 | for _, p := range []string{"/tim/cached", "/tim/cached/-/branches", "/tim/cached/-/tags"} { |
| 139 | res, _ := anon.Get(base + p) |
| 140 | body := read(t, res) |
| 141 | if res.StatusCode == 200 || strings.Contains(body, "Eerste versie") { |
| 142 | t.Fatalf("%s lekt naar anoniem: %d", p, res.StatusCode) |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestReadCacheClearKeepsData(t *testing.T) { |
| 148 | srv, root, get, _, _, _ := cacheServer(t) |
| 149 | get("/tim/cached") |
| 150 | keep := filepath.Join(root, "data", "bewaar.txt") |
| 151 | _ = os.MkdirAll(filepath.Dir(keep), 0o750) |
| 152 | _ = os.WriteFile(keep, []byte("x"), 0o600) |
| 153 | srv.ReadCache().Clear() |
| 154 | if srv.ReadCache().Usage().Entries != 0 { |
| 155 | t.Fatal("niet leeg") |
| 156 | } |
| 157 | if _, err := os.Stat(keep); err != nil { |
| 158 | t.Fatal("data/ geraakt") |
| 159 | } |
| 160 | repo := repoOf(t, srv, "cached") |
| 161 | srv.ReadCache().PutJSON(repo.ID, false, "x", 1) |
| 162 | tim, _ := srv.Store.UserByUsername(context.Background(), "tim") |
| 163 | if err := srv.opDeleteRepo(context.Background(), tim, repo, "cached", false); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if srv.ReadCache().Usage().Entries != 0 { |
| 167 | t.Fatal("cache van verwijderde repo bleef staan") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func TestSetCacheValidates(t *testing.T) { |
| 172 | srv, _, _ := mergeFixServer(t) |
| 173 | srv.StateDir = t.TempDir() |
| 174 | if srv.opSetCache("relatief", 0) == nil { |
| 175 | t.Fatal("relatief pad geaccepteerd") |
| 176 | } |
| 177 | if srv.opSetCache(filepath.Join(srv.StoragePath(), "cache"), 0) == nil { |
| 178 | t.Fatal("pad in opslag geaccepteerd") |
| 179 | } |
| 180 | root := t.TempDir() |
| 181 | if err := srv.opSetCache(root, 0); err != nil || srv.ReadCache() == nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | // blijft bewaard over een herstart |
| 185 | srv2 := &Server{StateDir: srv.StateDir} |
| 186 | srv2.InitReadCache() |
| 187 | if srv2.ReadCache() == nil || srv2.CacheConfig().Path != root { |
| 188 | t.Fatal("instelling niet bewaard") |
| 189 | } |
| 190 | if err := srv.opSetCache("", 0); err != nil || srv.ReadCache() != nil { |
| 191 | t.Fatal("uitzetten mislukt") |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // TestReadCacheColdWarm meet een overzichtspagina koud en warm. |
| 196 | func TestReadCacheColdWarm(t *testing.T) { |
| 197 | srv, _, get, _, _, _ := cacheServer(t) |
| 198 | waitWarm(srv) |
| 199 | const n = 20 |
| 200 | measure := func() time.Duration { |
| 201 | start := time.Now() |
| 202 | for i := 0; i < n; i++ { |
| 203 | get("/tim/cached") |
| 204 | } |
| 205 | return time.Since(start) / n |
| 206 | } |
| 207 | keep := srv.ReadCache() |
| 208 | srv.SetReadCache(nil) |
| 209 | cold := measure() |
| 210 | srv.SetReadCache(keep) |
| 211 | get("/tim/cached") |
| 212 | warm := measure() |
| 213 | t.Logf("overzicht: zonder cache %v, warm %v per pagina", cold, warm) |
| 214 | if warm > cold*2 { |
| 215 | t.Fatalf("warm (%v) veel trager dan koud (%v)", warm, cold) |
| 216 | } |
| 217 | _ = readcache.DefaultMax |
| 218 | } |