| 1 | //go:build integration |
| 2 | |
| 3 | // Integratietests: elke test start een echte BlueGit (BLUEGIT_BIN), maakt |
| 4 | // daar een beheerder, token en Backupserver-Key aan, en test de backup |
| 5 | // server ertegen. Zonder BLUEGIT_BIN falen ze; ze slaan niet over, zodat de |
| 6 | // release-jobs nooit op een overgeslagen test kunnen leunen. |
| 7 | package integration |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "context" |
| 12 | "encoding/json" |
| 13 | "fmt" |
| 14 | "io" |
| 15 | "log/slog" |
| 16 | "net" |
| 17 | "net/http" |
| 18 | "net/http/cookiejar" |
| 19 | "net/url" |
| 20 | "os" |
| 21 | "os/exec" |
| 22 | "path/filepath" |
| 23 | "strings" |
| 24 | "sync" |
| 25 | "testing" |
| 26 | "time" |
| 27 | |
| 28 | "git.timfalken.com/timfa/bluegit-backup/internal/config" |
| 29 | "git.timfalken.com/timfa/bluegit-backup/internal/server" |
| 30 | ) |
| 31 | |
| 32 | func bluegitBin(t *testing.T) string { |
| 33 | t.Helper() |
| 34 | bin := os.Getenv("BLUEGIT_BIN") |
| 35 | if bin == "" { |
| 36 | t.Fatal("BLUEGIT_BIN ontbreekt: bouw BlueGit met scripts/build-bluegit.sh") |
| 37 | } |
| 38 | if _, err := os.Stat(bin); err != nil { |
| 39 | t.Fatalf("BLUEGIT_BIN: %v", err) |
| 40 | } |
| 41 | if _, err := exec.LookPath("git"); err != nil { |
| 42 | t.Fatal("git ontbreekt") |
| 43 | } |
| 44 | return bin |
| 45 | } |
| 46 | |
| 47 | func freePort(t *testing.T) int { |
| 48 | t.Helper() |
| 49 | ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | defer ln.Close() |
| 54 | return ln.Addr().(*net.TCPAddr).Port |
| 55 | } |
| 56 | |
| 57 | // BlueGit is één lokaal opgespinde BlueGit. |
| 58 | type BlueGit struct { |
| 59 | t *testing.T |
| 60 | Bin string |
| 61 | URL string |
| 62 | Config string |
| 63 | State string |
| 64 | Storage string |
| 65 | Token string |
| 66 | cmd *exec.Cmd |
| 67 | log *syncBuf |
| 68 | } |
| 69 | |
| 70 | type syncBuf struct { |
| 71 | mu sync.Mutex |
| 72 | b bytes.Buffer |
| 73 | } |
| 74 | |
| 75 | func (s *syncBuf) Write(p []byte) (int, error) { |
| 76 | s.mu.Lock() |
| 77 | defer s.mu.Unlock() |
| 78 | return s.b.Write(p) |
| 79 | } |
| 80 | |
| 81 | func (s *syncBuf) String() string { |
| 82 | s.mu.Lock() |
| 83 | defer s.mu.Unlock() |
| 84 | return s.b.String() |
| 85 | } |
| 86 | |
| 87 | func startBlueGit(t *testing.T) *BlueGit { |
| 88 | t.Helper() |
| 89 | bin := bluegitBin(t) |
| 90 | dir := t.TempDir() |
| 91 | port := freePort(t) |
| 92 | g := &BlueGit{t: t, Bin: bin, URL: fmt.Sprintf("http://127.0.0.1:%d", port), |
| 93 | State: filepath.Join(dir, "state"), Storage: filepath.Join(dir, "storage"), Config: filepath.Join(dir, "bluegit.yml"), log: &syncBuf{}} |
| 94 | _ = os.MkdirAll(g.Storage, 0o750) |
| 95 | cfg := fmt.Sprintf(`base_url: %s |
| 96 | listen: 127.0.0.1:%d |
| 97 | state_path: %s |
| 98 | storage_path: %s |
| 99 | auth: |
| 100 | mode: dev |
| 101 | dev_user: tim |
| 102 | dev_auth_id: 1 |
| 103 | user_agent: bluegit-backup-it |
| 104 | git: |
| 105 | binary: git |
| 106 | default_branch: master |
| 107 | ssh: |
| 108 | enabled: false |
| 109 | ci: |
| 110 | enabled: false |
| 111 | `, g.URL, port, g.State, g.Storage) |
| 112 | if err := os.WriteFile(g.Config, []byte(cfg), 0o600); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | g.cmd = exec.Command(bin, "serve", "--config", g.Config) |
| 116 | g.cmd.Stdout, g.cmd.Stderr = g.log, g.log |
| 117 | if err := g.cmd.Start(); err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | t.Cleanup(func() { |
| 121 | _ = g.cmd.Process.Kill() |
| 122 | _, _ = g.cmd.Process.Wait() |
| 123 | if t.Failed() { |
| 124 | t.Logf("BlueGit-log %s:\n%s", g.URL, tail(g.log.String(), 60)) |
| 125 | } |
| 126 | }) |
| 127 | g.WaitUp(20 * time.Second) |
| 128 | // Aanmelden maakt de eerste gebruiker (beheerder) aan. |
| 129 | jar, _ := cookiejar.New(nil) |
| 130 | hc := &http.Client{Jar: jar} |
| 131 | res, err := hc.Get(g.URL + "/login") |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | res.Body.Close() |
| 136 | out, err := exec.Command(bin, "token", "create", "--config", g.Config, "--user", "tim", "--name", "it").CombinedOutput() |
| 137 | if err != nil { |
| 138 | t.Fatalf("token: %v %s", err, out) |
| 139 | } |
| 140 | g.Token = strings.TrimSpace(string(out)) |
| 141 | return g |
| 142 | } |
| 143 | |
| 144 | func tail(s string, n int) string { |
| 145 | lines := strings.Split(s, "\n") |
| 146 | if len(lines) > n { |
| 147 | lines = lines[len(lines)-n:] |
| 148 | } |
| 149 | return strings.Join(lines, "\n") |
| 150 | } |
| 151 | |
| 152 | // WaitUp wacht op /healthz. |
| 153 | func (g *BlueGit) WaitUp(d time.Duration) { |
| 154 | g.t.Helper() |
| 155 | deadline := time.Now().Add(d) |
| 156 | for time.Now().Before(deadline) { |
| 157 | res, err := http.Get(g.URL + "/healthz") |
| 158 | if err == nil { |
| 159 | res.Body.Close() |
| 160 | if res.StatusCode == 200 { |
| 161 | return |
| 162 | } |
| 163 | } |
| 164 | time.Sleep(100 * time.Millisecond) |
| 165 | } |
| 166 | g.t.Fatalf("BlueGit %s kwam niet op:\n%s", g.URL, tail(g.log.String(), 40)) |
| 167 | } |
| 168 | |
| 169 | // API doet een verzoek met het token. path begint na /api/v1. |
| 170 | func (g *BlueGit) API(method, path string, in any) (int, []byte) { |
| 171 | g.t.Helper() |
| 172 | return g.APIWith(g.Token, method, path, in) |
| 173 | } |
| 174 | |
| 175 | // APIWith gebruikt een ander token. |
| 176 | func (g *BlueGit) APIWith(tok, method, path string, in any) (int, []byte) { |
| 177 | g.t.Helper() |
| 178 | var body io.Reader |
| 179 | if in != nil { |
| 180 | b, _ := json.Marshal(in) |
| 181 | body = bytes.NewReader(b) |
| 182 | } |
| 183 | req, _ := http.NewRequest(method, g.URL+"/api/v1"+path, body) |
| 184 | req.Header.Set("Authorization", "Bearer "+tok) |
| 185 | if in != nil { |
| 186 | req.Header.Set("Content-Type", "application/json") |
| 187 | } |
| 188 | res, err := http.DefaultClient.Do(req) |
| 189 | if err != nil { |
| 190 | g.t.Fatalf("%s %s: %v", method, path, err) |
| 191 | } |
| 192 | defer res.Body.Close() |
| 193 | b, _ := io.ReadAll(res.Body) |
| 194 | return res.StatusCode, b |
| 195 | } |
| 196 | |
| 197 | // MustAPI faalt bij een fout. |
| 198 | func (g *BlueGit) MustAPI(method, path string, in any) []byte { |
| 199 | g.t.Helper() |
| 200 | code, b := g.API(method, path, in) |
| 201 | if code >= 300 { |
| 202 | g.t.Fatalf("%s %s: %d %s", method, path, code, b) |
| 203 | } |
| 204 | return b |
| 205 | } |
| 206 | |
| 207 | // NewKey maakt een Backupserver-Key via de beheer-API. |
| 208 | func (g *BlueGit) NewKey() string { |
| 209 | g.t.Helper() |
| 210 | var out struct{ Key string } |
| 211 | _ = json.Unmarshal(g.MustAPI("POST", "/admin/backup/key", nil), &out) |
| 212 | if !strings.HasPrefix(out.Key, "bgbk_") { |
| 213 | g.t.Fatalf("key %q", out.Key) |
| 214 | } |
| 215 | return out.Key |
| 216 | } |
| 217 | |
| 218 | // Project maakt een project met één commit. |
| 219 | func (g *BlueGit) Project(name, file, content string) { |
| 220 | g.t.Helper() |
| 221 | g.MustAPI("POST", "/projects", map[string]any{"name": name, "visibility": "private"}) |
| 222 | g.Commit(strings.ToLower(name), file, content, "create") |
| 223 | } |
| 224 | |
| 225 | // Commit schrijft één bestand op master. |
| 226 | func (g *BlueGit) Commit(repo, file, content, action string) { |
| 227 | g.t.Helper() |
| 228 | g.MustAPI("POST", "/projects/tim/"+repo+"/repository/commits", map[string]any{ |
| 229 | "branch": "master", "commit_message": "zet " + file, |
| 230 | "actions": []map[string]string{{"action": action, "file_path": file, "content": content}}, |
| 231 | }) |
| 232 | } |
| 233 | |
| 234 | // File leest een bestand van master, of geeft de HTTP-code. |
| 235 | func (g *BlueGit) File(repo, file string) (int, string) { |
| 236 | g.t.Helper() |
| 237 | req, _ := http.NewRequest("GET", g.URL+"/api/v1/projects/tim/"+repo+"/repository/files/"+file+"?ref=master", nil) |
| 238 | req.Header.Set("Authorization", "Bearer "+g.Token) |
| 239 | res, err := http.DefaultClient.Do(req) |
| 240 | if err != nil { |
| 241 | g.t.Fatal(err) |
| 242 | } |
| 243 | defer res.Body.Close() |
| 244 | b, _ := io.ReadAll(res.Body) |
| 245 | return res.StatusCode, string(b) |
| 246 | } |
| 247 | |
| 248 | // Issue maakt een issue. |
| 249 | func (g *BlueGit) Issue(repo, title string) { |
| 250 | g.t.Helper() |
| 251 | g.MustAPI("POST", "/repos/tim/"+repo+"/issues", map[string]any{"title": title}) |
| 252 | } |
| 253 | |
| 254 | // IssueTitles geeft alle issuetitels. |
| 255 | func (g *BlueGit) IssueTitles(repo string) []string { |
| 256 | g.t.Helper() |
| 257 | code, b := g.API("GET", "/repos/tim/"+repo+"/issues?state=all", nil) |
| 258 | if code != 200 { |
| 259 | g.t.Fatalf("issues: %d %s", code, b) |
| 260 | } |
| 261 | var list struct { |
| 262 | Issues []struct{ Title string } `json:"issues"` |
| 263 | } |
| 264 | _ = json.Unmarshal(b, &list) |
| 265 | var out []string |
| 266 | for _, i := range list.Issues { |
| 267 | out = append(out, i.Title) |
| 268 | } |
| 269 | return out |
| 270 | } |
| 271 | |
| 272 | // ProjectCode is de HTTP-code van GET project. |
| 273 | func (g *BlueGit) ProjectCode(repo string) int { |
| 274 | g.t.Helper() |
| 275 | code, _ := g.API("GET", "/projects/tim/"+repo, nil) |
| 276 | return code |
| 277 | } |
| 278 | |
| 279 | // BackupStatus is GET /admin/backup in BlueGit. |
| 280 | type BackupStatus struct { |
| 281 | InstanceID string `json:"instance_id"` |
| 282 | HasKey bool `json:"has_key"` |
| 283 | Connected bool `json:"connected"` |
| 284 | ServerURL string `json:"server_url"` |
| 285 | Archives []struct { |
| 286 | Name string `json:"name"` |
| 287 | Size int64 `json:"size"` |
| 288 | } `json:"archives"` |
| 289 | RemoteError string `json:"remote_error"` |
| 290 | } |
| 291 | |
| 292 | func (g *BlueGit) BackupStatus() BackupStatus { |
| 293 | g.t.Helper() |
| 294 | var st BackupStatus |
| 295 | _ = json.Unmarshal(g.MustAPI("GET", "/admin/backup", nil), &st) |
| 296 | return st |
| 297 | } |
| 298 | |
| 299 | // Session geeft een HTTP-client met sessiecookie (dev-login). |
| 300 | func (g *BlueGit) Session() *http.Client { |
| 301 | g.t.Helper() |
| 302 | jar, _ := cookiejar.New(nil) |
| 303 | hc := &http.Client{Jar: jar} |
| 304 | res, err := hc.Get(g.URL + "/login") |
| 305 | if err != nil { |
| 306 | g.t.Fatal(err) |
| 307 | } |
| 308 | res.Body.Close() |
| 309 | return hc |
| 310 | } |
| 311 | |
| 312 | // Git draait git met het token als wachtwoord. |
| 313 | func (g *BlueGit) Git(dir string, args ...string) string { |
| 314 | g.t.Helper() |
| 315 | cmd := exec.Command("git", args...) |
| 316 | cmd.Dir = dir |
| 317 | cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0", "GIT_AUTHOR_NAME=it", "GIT_AUTHOR_EMAIL=it@example.invalid", "GIT_COMMITTER_NAME=it", "GIT_COMMITTER_EMAIL=it@example.invalid") |
| 318 | out, err := cmd.CombinedOutput() |
| 319 | if err != nil { |
| 320 | g.t.Fatalf("git %v: %v\n%s", args, err, out) |
| 321 | } |
| 322 | return string(out) |
| 323 | } |
| 324 | |
| 325 | // RemoteURL is de clone-URL met token. |
| 326 | func (g *BlueGit) RemoteURL(repo string) string { |
| 327 | u, _ := url.Parse(g.URL) |
| 328 | u.User = url.UserPassword("tim", g.Token) |
| 329 | u.Path = "/tim/" + repo + ".git" |
| 330 | return u.String() |
| 331 | } |
| 332 | |
| 333 | // Backup is één backup server in dit proces. |
| 334 | type Backup struct { |
| 335 | t *testing.T |
| 336 | Srv *server.Server |
| 337 | URL string |
| 338 | Dir string |
| 339 | Cfg config.Config |
| 340 | } |
| 341 | |
| 342 | func startBackup(t *testing.T, mod func(*config.Config)) *Backup { |
| 343 | t.Helper() |
| 344 | port := freePort(t) |
| 345 | b := &Backup{t: t, URL: fmt.Sprintf("http://127.0.0.1:%d", port), Dir: t.TempDir()} |
| 346 | cfg := config.Config{Listen: fmt.Sprintf("127.0.0.1:%d", port), PublicURL: b.URL, DataDir: b.Dir} |
| 347 | // Snel genoeg voor tests; de standaardwaarden staan in config.Defaults. |
| 348 | cfg.MinSyncInterval.Duration = 50 * time.Millisecond |
| 349 | cfg.ArchiveInterval.Duration = time.Hour |
| 350 | cfg.FullSyncInterval.Duration = time.Hour |
| 351 | cfg.RateLimitKiB = -1 |
| 352 | cfg.MinFreeMB = 1 |
| 353 | if mod != nil { |
| 354 | mod(&cfg) |
| 355 | } |
| 356 | cfg.Defaults() |
| 357 | b.Cfg = cfg |
| 358 | srv, err := server.New(cfg, slog.New(slog.NewTextHandler(&testWriter{t}, &slog.HandlerOptions{Level: slog.LevelWarn}))) |
| 359 | if err != nil { |
| 360 | t.Fatal(err) |
| 361 | } |
| 362 | b.Srv = srv |
| 363 | ln, err := net.Listen("tcp", cfg.Listen) |
| 364 | if err != nil { |
| 365 | t.Fatal(err) |
| 366 | } |
| 367 | hs := &http.Server{Handler: srv.Handler()} |
| 368 | ctx, cancel := context.WithCancel(context.Background()) |
| 369 | go func() { _ = hs.Serve(ln) }() |
| 370 | srv.Run(ctx) |
| 371 | t.Cleanup(func() { |
| 372 | cancel() |
| 373 | _ = hs.Close() |
| 374 | }) |
| 375 | return b |
| 376 | } |
| 377 | |
| 378 | type testWriter struct{ t *testing.T } |
| 379 | |
| 380 | func (w *testWriter) Write(p []byte) (int, error) { |
| 381 | w.t.Log(strings.TrimRight(string(p), "\n")) |
| 382 | return len(p), nil |
| 383 | } |
| 384 | |
| 385 | // Admin doet een verzoek op de lokale beheer-API. |
| 386 | func (b *Backup) Admin(method, path string, in any) (int, []byte) { |
| 387 | b.t.Helper() |
| 388 | var body io.Reader |
| 389 | if in != nil { |
| 390 | raw, _ := json.Marshal(in) |
| 391 | body = bytes.NewReader(raw) |
| 392 | } |
| 393 | req, _ := http.NewRequest(method, b.URL+"/api/v1/admin"+path, body) |
| 394 | req.Header.Set("Authorization", "Bearer "+b.Srv.AdminToken()) |
| 395 | res, err := http.DefaultClient.Do(req) |
| 396 | if err != nil { |
| 397 | b.t.Fatal(err) |
| 398 | } |
| 399 | defer res.Body.Close() |
| 400 | raw, _ := io.ReadAll(res.Body) |
| 401 | return res.StatusCode, raw |
| 402 | } |
| 403 | |
| 404 | // Connect koppelt aan g zonder conflictkeuze; faalt bij een fout. |
| 405 | func (b *Backup) Connect(g *BlueGit, key string) { |
| 406 | b.t.Helper() |
| 407 | code, raw := b.Admin("POST", "/connect", server.ConnectRequest{URL: g.URL, Key: key}) |
| 408 | if code != 200 { |
| 409 | b.t.Fatalf("connect: %d %s", code, raw) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Sync synchroniseert nu. |
| 414 | func (b *Backup) Sync() { |
| 415 | b.t.Helper() |
| 416 | code, raw := b.Admin("POST", "/sync", nil) |
| 417 | if code != 200 { |
| 418 | b.t.Fatalf("sync: %d %s", code, raw) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Archive maakt nu een archief en geeft de naam. |
| 423 | func (b *Backup) Archive() string { |
| 424 | b.t.Helper() |
| 425 | code, raw := b.Admin("POST", "/archive", nil) |
| 426 | if code != 201 { |
| 427 | b.t.Fatalf("archive: %d %s", code, raw) |
| 428 | } |
| 429 | var a struct{ Name string } |
| 430 | _ = json.Unmarshal(raw, &a) |
| 431 | return a.Name |
| 432 | } |
| 433 | |
| 434 | // Mirror is het pad van een logisch bestand in de kopie. |
| 435 | func (b *Backup) Mirror(logical string) string { |
| 436 | return filepath.Join(b.Dir, "mirror", filepath.FromSlash(logical)) |
| 437 | } |
| 438 | |
| 439 | // Archives geeft de archiefnamen, nieuwste eerst. |
| 440 | func (b *Backup) Archives() []string { |
| 441 | b.t.Helper() |
| 442 | _, raw := b.Admin("GET", "/archives", nil) |
| 443 | var out struct { |
| 444 | Archives []struct{ Name string } |
| 445 | } |
| 446 | _ = json.Unmarshal(raw, &out) |
| 447 | var names []string |
| 448 | for _, a := range out.Archives { |
| 449 | names = append(names, a.Name) |
| 450 | } |
| 451 | return names |
| 452 | } |
| 453 | |
| 454 | // Status leest de status. |
| 455 | func (b *Backup) Status() server.Status { |
| 456 | b.t.Helper() |
| 457 | _, raw := b.Admin("GET", "/status", nil) |
| 458 | var st server.Status |
| 459 | _ = json.Unmarshal(raw, &st) |
| 460 | return st |
| 461 | } |
| 462 | |
| 463 | // Eventually herhaalt f tot hij true geeft. |
| 464 | func Eventually(t *testing.T, d time.Duration, what string, f func() bool) { |
| 465 | t.Helper() |
| 466 | deadline := time.Now().Add(d) |
| 467 | for time.Now().Before(deadline) { |
| 468 | if f() { |
| 469 | return |
| 470 | } |
| 471 | time.Sleep(50 * time.Millisecond) |
| 472 | } |
| 473 | t.Fatalf("na %v nog steeds niet: %s", d, what) |
| 474 | } |
| 475 | |
| 476 | // gitLog geeft de onderwerpen van een bare repo in de kopie. |
| 477 | func gitLog(t *testing.T, gitDir string) string { |
| 478 | t.Helper() |
| 479 | out, err := exec.Command("git", "--git-dir", gitDir, "log", "--format=%s", "master").CombinedOutput() |
| 480 | if err != nil { |
| 481 | return "FOUT: " + string(out) |
| 482 | } |
| 483 | return string(out) |
| 484 | } |