| 1 | //go:build integration |
| 2 | |
| 3 | package integration |
| 4 | |
| 5 | import ( |
| 6 | "crypto/rand" |
| 7 | "encoding/json" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "gitlab.com/Timfa/bluegit/internal/backupserver/config" |
| 19 | "gitlab.com/Timfa/bluegit/internal/backupserver/server" |
| 20 | ) |
| 21 | |
| 22 | // Feature: Backupserver-Key in BlueGit genereren en de backup server koppelen. |
| 23 | func TestKeyGenerationAndConnect(t *testing.T) { |
| 24 | g := startBlueGit(t) |
| 25 | b := startBackup(t, nil) |
| 26 | |
| 27 | // Zonder key niets; een verkeerde key wordt geweigerd. |
| 28 | if st := g.BackupStatus(); st.HasKey || st.Connected { |
| 29 | t.Fatalf("vers BlueGit heeft al een key: %+v", st) |
| 30 | } |
| 31 | code, raw := b.Admin("POST", "/connect", server.ConnectRequest{URL: g.URL, Key: "bgbk_verkeerd"}) |
| 32 | if code != 401 { |
| 33 | t.Fatalf("verkeerde key: %d %s", code, raw) |
| 34 | } |
| 35 | key := g.NewKey() |
| 36 | b.Connect(g, key) |
| 37 | |
| 38 | st := g.BackupStatus() |
| 39 | if !st.HasKey || !st.Connected || st.ServerURL != b.URL || st.RemoteError != "" { |
| 40 | t.Fatalf("BlueGit ziet de koppeling niet: %+v", st) |
| 41 | } |
| 42 | bs := b.Status() |
| 43 | if !bs.Connected || bs.InstanceID != st.InstanceID || bs.BlueGitURL != g.URL { |
| 44 | t.Fatalf("backup status: %+v", bs) |
| 45 | } |
| 46 | // De key staat niet leesbaar in BlueGit. |
| 47 | raw2, _ := os.ReadFile(filepath.Join(g.State, "backup.json")) |
| 48 | if strings.Contains(string(raw2), key) { |
| 49 | t.Fatal("BlueGit bewaart de key leesbaar") |
| 50 | } |
| 51 | // Een nieuwe key maakt de oude ongeldig: de backup server kan niet meer syncen. |
| 52 | g.RotateKey() |
| 53 | code, raw = b.Admin("POST", "/sync", nil) |
| 54 | if code == 200 { |
| 55 | t.Fatalf("sync met ingetrokken key gelukt: %s", raw) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Feature: alle gegevens en configuratie ophalen. |
| 60 | func TestFullSyncCopiesAllData(t *testing.T) { |
| 61 | g := startBlueGit(t) |
| 62 | g.Project("Alpha", "README.md", "hallo alpha") |
| 63 | g.Issue("alpha", "eerste issue") |
| 64 | b := startBackup(t, nil) |
| 65 | b.Connect(g, g.NewKey()) |
| 66 | b.Sync() |
| 67 | |
| 68 | for _, p := range []string{"state/bluegit.db", "state/secret.key", "data/repos/tim/alpha.git/HEAD"} { |
| 69 | if _, err := os.Stat(b.Mirror(p)); err != nil { |
| 70 | t.Fatalf("kopie mist %s: %v", p, err) |
| 71 | } |
| 72 | } |
| 73 | if log := gitLog(t, b.Mirror("data/repos/tim/alpha.git")); !strings.Contains(log, "zet README.md") { |
| 74 | t.Fatalf("kopie van de repo is niet bruikbaar: %s", log) |
| 75 | } |
| 76 | db, _ := os.ReadFile(b.Mirror("state/bluegit.db")) |
| 77 | if !strings.HasPrefix(string(db), "SQLite format 3") || !strings.Contains(string(db), "eerste issue") { |
| 78 | t.Fatal("databasekopie mist het issue") |
| 79 | } |
| 80 | key1, _ := os.ReadFile(filepath.Join(g.State, "secret.key")) |
| 81 | key2, _ := os.ReadFile(b.Mirror("state/secret.key")) |
| 82 | if string(key1) != string(key2) { |
| 83 | t.Fatal("secret.key wijkt af") |
| 84 | } |
| 85 | // Installatiegebonden bestanden gaan niet mee. |
| 86 | for _, p := range []string{"state/backup.json", "state/instance_id", "state/storage_path"} { |
| 87 | if _, err := os.Stat(b.Mirror(p)); err == nil { |
| 88 | t.Fatalf("%s hoort niet in de kopie", p) |
| 89 | } |
| 90 | } |
| 91 | // Een tweede sync zonder wijzigingen haalt (bijna) niets op. |
| 92 | code, raw := b.Admin("POST", "/sync", nil) |
| 93 | var st struct{ Downloaded int } |
| 94 | _ = json.Unmarshal(raw, &st) |
| 95 | if code != 200 || st.Downloaded > 1 { |
| 96 | t.Fatalf("tweede sync haalde %d bestanden op: %s", st.Downloaded, raw) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Feature: BlueGit meldt elke wijziging; de backup server haalt hem op. |
| 101 | func TestNotificationTriggersSync(t *testing.T) { |
| 102 | g := startBlueGit(t) |
| 103 | g.Project("Alpha", "README.md", "v1") |
| 104 | b := startBackup(t, nil) // full sync pas na een uur: alleen meldingen tellen |
| 105 | b.Connect(g, g.NewKey()) |
| 106 | Eventually(t, 15*time.Second, "eerste sync na koppelen", func() bool { |
| 107 | return strings.Contains(gitLog(t, b.Mirror("data/repos/tim/alpha.git")), "zet README.md") |
| 108 | }) |
| 109 | before := b.Status().Notifies |
| 110 | |
| 111 | // Wijziging via de API. |
| 112 | g.Commit("alpha", "CHANGELOG.md", "nieuw", "create") |
| 113 | Eventually(t, 15*time.Second, "commit via API in de kopie", func() bool { |
| 114 | return strings.Contains(gitLog(t, b.Mirror("data/repos/tim/alpha.git")), "zet CHANGELOG.md") |
| 115 | }) |
| 116 | // Wijziging via git push over HTTP. |
| 117 | work := t.TempDir() |
| 118 | g.Git(work, "clone", "-q", g.RemoteURL("alpha"), "w") |
| 119 | repo := filepath.Join(work, "w") |
| 120 | _ = os.WriteFile(filepath.Join(repo, "pushed.txt"), []byte("via git"), 0o644) |
| 121 | g.Git(repo, "add", ".") |
| 122 | g.Git(repo, "commit", "-qm", "gepusht met git") |
| 123 | g.Git(repo, "push", "-q", "origin", "master") |
| 124 | Eventually(t, 15*time.Second, "git push in de kopie", func() bool { |
| 125 | return strings.Contains(gitLog(t, b.Mirror("data/repos/tim/alpha.git")), "gepusht met git") |
| 126 | }) |
| 127 | // Een issue staat alleen in de database. |
| 128 | g.Issue("alpha", "issue-na-melding") |
| 129 | Eventually(t, 15*time.Second, "issue in de databasekopie", func() bool { |
| 130 | db, _ := os.ReadFile(b.Mirror("state/bluegit.db")) |
| 131 | return strings.Contains(string(db), "issue-na-melding") |
| 132 | }) |
| 133 | if b.Status().Notifies <= before { |
| 134 | t.Fatal("geen meldingen geteld") |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Feature: downloaden op lage snelheid. |
| 139 | func TestDownloadIsThrottled(t *testing.T) { |
| 140 | g := startBlueGit(t) |
| 141 | g.MustAPI("POST", "/projects", map[string]any{"name": "Big", "visibility": "private"}) |
| 142 | work := t.TempDir() |
| 143 | g.Git(work, "init", "-q", "-b", "master", "w") |
| 144 | repo := filepath.Join(work, "w") |
| 145 | blob := make([]byte, 1536<<10) // 1,5 MiB, niet te comprimeren |
| 146 | _, _ = rand.Read(blob) |
| 147 | _ = os.WriteFile(filepath.Join(repo, "random.bin"), blob, 0o644) |
| 148 | g.Git(repo, "add", ".") |
| 149 | g.Git(repo, "commit", "-qm", "groot bestand") |
| 150 | g.Git(repo, "push", "-q", g.RemoteURL("big"), "master") |
| 151 | |
| 152 | b := startBackup(t, func(c *config.Config) { c.RateLimitKiB = 512 }) |
| 153 | start := time.Now() |
| 154 | b.Connect(g, g.NewKey()) |
| 155 | b.Sync() |
| 156 | took := time.Since(start) |
| 157 | // 1,5 MiB tegen 512 KiB/s is minstens ~2,7 s (min de eerste burst). |
| 158 | if took < 2500*time.Millisecond { |
| 159 | t.Fatalf("sync duurde %v; de limiet van 512 KiB/s werkt niet", took) |
| 160 | } |
| 161 | if b.Status().RateLimitKiB != 512 { |
| 162 | t.Fatal("limiet niet in de status") |
| 163 | } |
| 164 | if !strings.Contains(gitLog(t, b.Mirror("data/repos/tim/big.git")), "groot bestand") { |
| 165 | t.Fatal("groot bestand niet gekopieerd") |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Feature: op een interval archiveren; archieflijst in BlueGit. |
| 170 | func TestArchiveIntervalAndListInBlueGit(t *testing.T) { |
| 171 | g := startBlueGit(t) |
| 172 | g.Project("Alpha", "README.md", "v1") |
| 173 | b := startBackup(t, func(c *config.Config) { c.ArchiveInterval.Duration = 2 * time.Second }) |
| 174 | if b.Status().Interval != "2s" { |
| 175 | t.Fatalf("interval: %s", b.Status().Interval) |
| 176 | } |
| 177 | b.Connect(g, g.NewKey()) |
| 178 | Eventually(t, 20*time.Second, "twee archieven door het interval", func() bool { return len(b.Archives()) >= 2 }) |
| 179 | |
| 180 | names := b.Archives() |
| 181 | st := g.BackupStatus() |
| 182 | if len(st.Archives) < 2 || st.Archives[0].Name != names[0] || st.Archives[0].Size == 0 { |
| 183 | t.Fatalf("BlueGit toont de archieven niet: %+v vs %v", st.Archives, names) |
| 184 | } |
| 185 | // De beheerpagina toont de lijst met de grote waarschuwing. |
| 186 | res, err := g.Session().Get(g.URL + "/admin/backup/s/" + g.Server) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | page, _ := io.ReadAll(res.Body) |
| 191 | res.Body.Close() |
| 192 | for _, want := range []string{names[0], `class="backup-danger"`, `name="understood"`, `name="confirm"`} { |
| 193 | if !strings.Contains(string(page), want) { |
| 194 | t.Fatalf("beheerpagina mist %q", want) |
| 195 | } |
| 196 | } |
| 197 | // Het archief is een geldige tar.gz met de logische paden. |
| 198 | out, err := exec.Command("tar", "-tzf", filepath.Join(b.Dir, "archives", names[0])).CombinedOutput() |
| 199 | if err != nil || !strings.Contains(string(out), "state/bluegit.db") || !strings.Contains(string(out), "data/repos/tim/alpha.git/HEAD") { |
| 200 | t.Fatalf("archief: %v %s", err, out) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Feature: archieven ouder dan x dagen, of de oudste bij een volle schijf, verdwijnen. |
| 205 | func TestRetentionAndDiskFull(t *testing.T) { |
| 206 | g := startBlueGit(t) |
| 207 | g.Project("Alpha", "README.md", "v1") |
| 208 | b := startBackup(t, func(c *config.Config) { c.RetentionDays = 7 }) |
| 209 | b.Connect(g, g.NewKey()) |
| 210 | b.Sync() |
| 211 | var names []string |
| 212 | for i := 0; i < 4; i++ { |
| 213 | names = append(names, b.Archive()) |
| 214 | time.Sleep(1100 * time.Millisecond) // namen hebben secondes |
| 215 | } |
| 216 | // De twee oudste zijn 10 en 8 dagen oud, de derde 6. |
| 217 | ages := []int{10, 8, 6} |
| 218 | for i, d := range ages { |
| 219 | old := time.Now().Add(-time.Duration(d) * 24 * time.Hour) |
| 220 | _ = os.Chtimes(filepath.Join(b.Dir, "archives", names[i]), old, old) |
| 221 | } |
| 222 | code, raw := b.Admin("POST", "/prune", nil) |
| 223 | if code != 200 || !strings.Contains(string(raw), names[0]) || !strings.Contains(string(raw), names[1]) || strings.Contains(string(raw), names[2]) { |
| 224 | t.Fatalf("prune op leeftijd: %d %s", code, raw) |
| 225 | } |
| 226 | if got := b.Archives(); len(got) != 2 { |
| 227 | t.Fatalf("na leeftijd: %v", got) |
| 228 | } |
| 229 | // Schijf vol: de oudste gaat weg, het nieuwste blijft altijd. |
| 230 | b.Srv.SetFree(func(string) (uint64, error) { return 0, nil }) |
| 231 | code, raw = b.Admin("POST", "/prune", nil) |
| 232 | if code != 200 || !strings.Contains(string(raw), names[2]) { |
| 233 | t.Fatalf("prune schijf vol: %d %s", code, raw) |
| 234 | } |
| 235 | if got := b.Archives(); len(got) != 1 || got[0] != names[3] { |
| 236 | t.Fatalf("na schijf vol: %v, wil alleen %s", got, names[3]) |
| 237 | } |
| 238 | // Ook een nieuw archief maakt eerst ruimte: het oude gaat weg, het nieuwe komt. |
| 239 | b.Srv.SetFree(func(string) (uint64, error) { |
| 240 | if len(b.Archives()) > 0 { |
| 241 | return 0, nil |
| 242 | } |
| 243 | return 10 << 30, nil |
| 244 | }) |
| 245 | time.Sleep(1100 * time.Millisecond) |
| 246 | newest := b.Archive() |
| 247 | if got := b.Archives(); len(got) != 1 || got[0] != newest { |
| 248 | t.Fatalf("na archief bij volle schijf: %v", got) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Feature: rollback vanuit BlueGit, met bevestiging; kopie en BlueGit gaan terug. |
| 253 | func TestRollbackFromBlueGit(t *testing.T) { |
| 254 | g := startBlueGit(t) |
| 255 | g.Project("Alpha", "README.md", "versie 1") |
| 256 | g.Issue("alpha", "voor het archief") |
| 257 | b := startBackup(t, nil) |
| 258 | b.Connect(g, g.NewKey()) |
| 259 | b.Sync() |
| 260 | archiveName := b.Archive() |
| 261 | |
| 262 | // Na het archief: alles verandert. |
| 263 | g.Commit("alpha", "README.md", "versie 2", "update") |
| 264 | g.Issue("alpha", "na het archief") |
| 265 | g.Project("Beta", "b.txt", "nieuw project") |
| 266 | b.Sync() |
| 267 | if _, s := g.File("alpha", "README.md"); s != "versie 2" { |
| 268 | t.Fatalf("voorbereiding: %q", s) |
| 269 | } |
| 270 | |
| 271 | // Zonder juiste bevestiging gebeurt er niets. |
| 272 | code, raw := g.Action(g.Server, "rollback", map[string]any{"archive": archiveName, "confirm": "ja"}) |
| 273 | if code != 400 { |
| 274 | t.Fatalf("rollback zonder bevestiging: %d %s", code, raw) |
| 275 | } |
| 276 | // Via het formulier zonder vinkje ook niet. |
| 277 | if code := postRollbackForm(t, g, archiveName, false); code/100 != 3 { |
| 278 | t.Fatalf("formulier zonder vinkje: %d", code) |
| 279 | } |
| 280 | time.Sleep(300 * time.Millisecond) |
| 281 | if b.Status().Rollback.State != "idle" { |
| 282 | t.Fatalf("rollback gestart zonder bevestiging: %+v", b.Status().Rollback) |
| 283 | } |
| 284 | |
| 285 | // Echte rollback via de UI. |
| 286 | if code := postRollbackForm(t, g, archiveName, true); code/100 != 3 { |
| 287 | t.Fatalf("formulier: %d", code) |
| 288 | } |
| 289 | Eventually(t, 60*time.Second, "rollback klaar", func() bool { |
| 290 | st := b.Status().Rollback |
| 291 | if st.State == "failed" { |
| 292 | t.Fatalf("rollback mislukt: %s", st.Error) |
| 293 | } |
| 294 | return st.State == "done" |
| 295 | }) |
| 296 | g.WaitUp(30 * time.Second) |
| 297 | |
| 298 | if code, s := g.File("alpha", "README.md"); code != 200 || s != "versie 1" { |
| 299 | t.Fatalf("README na rollback: %d %q", code, s) |
| 300 | } |
| 301 | if code := g.ProjectCode("beta"); code != 404 { |
| 302 | t.Fatalf("project beta bestaat na rollback nog: %d", code) |
| 303 | } |
| 304 | titles := strings.Join(g.IssueTitles("alpha"), "|") |
| 305 | if !strings.Contains(titles, "voor het archief") || strings.Contains(titles, "na het archief") { |
| 306 | t.Fatalf("issues na rollback: %s", titles) |
| 307 | } |
| 308 | // De koppeling overleeft de rollback: BlueGit ziet de server nog, en |
| 309 | // de kopie loopt weer gelijk met BlueGit. |
| 310 | if st := g.BackupStatus(); !st.Connected || st.RemoteError != "" { |
| 311 | t.Fatalf("koppeling na rollback: %+v", st) |
| 312 | } |
| 313 | b.Sync() |
| 314 | if log := gitLog(t, b.Mirror("data/repos/tim/alpha.git")); strings.Count(log, "zet README.md") != 1 { |
| 315 | t.Fatalf("kopie na rollback: %s", log) |
| 316 | } |
| 317 | if _, err := os.Stat(b.Mirror("data/repos/tim/beta.git/HEAD")); err == nil { |
| 318 | t.Fatal("beta staat nog in de kopie") |
| 319 | } |
| 320 | // Na de rollback werkt BlueGit gewoon verder en blijft melden. |
| 321 | g.Commit("alpha", "after.txt", "na rollback", "create") |
| 322 | Eventually(t, 15*time.Second, "melding na rollback", func() bool { |
| 323 | return strings.Contains(gitLog(t, b.Mirror("data/repos/tim/alpha.git")), "zet after.txt") |
| 324 | }) |
| 325 | } |
| 326 | |
| 327 | func postRollbackForm(t *testing.T, g *BlueGit, archiveName string, understood bool) int { |
| 328 | t.Helper() |
| 329 | hc := g.Session() |
| 330 | hc.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } |
| 331 | res, err := hc.Get(g.URL + "/admin/backup/s/" + g.Server) |
| 332 | if err != nil { |
| 333 | t.Fatal(err) |
| 334 | } |
| 335 | page, _ := io.ReadAll(res.Body) |
| 336 | res.Body.Close() |
| 337 | csrf := between(string(page), `name="csrf" value="`, `"`) |
| 338 | if csrf == "" { |
| 339 | t.Fatal("geen csrf op de beheerpagina") |
| 340 | } |
| 341 | form := url.Values{"csrf": {csrf}, "archive": {archiveName}, "confirm": {archiveName}} |
| 342 | if understood { |
| 343 | form.Set("understood", "1") |
| 344 | } |
| 345 | res, err = hc.PostForm(g.URL+"/admin/backup/s/"+g.Server+"/act/rollback", form) |
| 346 | if err != nil { |
| 347 | t.Fatal(err) |
| 348 | } |
| 349 | res.Body.Close() |
| 350 | return res.StatusCode |
| 351 | } |
| 352 | |
| 353 | func between(s, a, b string) string { |
| 354 | i := strings.Index(s, a) |
| 355 | if i < 0 { |
| 356 | return "" |
| 357 | } |
| 358 | s = s[i+len(a):] |
| 359 | j := strings.Index(s, b) |
| 360 | if j < 0 { |
| 361 | return "" |
| 362 | } |
| 363 | return s[:j] |
| 364 | } |
| 365 | |
| 366 | // Feature: één BlueGit per backup server; keuze "oude backups verwijderen". |
| 367 | func TestNewInstanceWipe(t *testing.T) { |
| 368 | a := startBlueGit(t) |
| 369 | a.Project("Alpha", "a.txt", "van A") |
| 370 | b := startBackup(t, nil) |
| 371 | b.Connect(a, a.NewKey()) |
| 372 | b.Sync() |
| 373 | b.Archive() |
| 374 | |
| 375 | other := startBlueGit(t) |
| 376 | other.Project("Gamma", "g.txt", "van B") |
| 377 | keyB := other.NewKey() |
| 378 | code, raw := b.Admin("POST", "/connect", server.ConnectRequest{URL: other.URL, Key: keyB}) |
| 379 | if code != 409 { |
| 380 | t.Fatalf("tweede BlueGit zonder keuze: %d %s", code, raw) |
| 381 | } |
| 382 | var cf server.ConflictError |
| 383 | _ = json.Unmarshal(raw, &cf) |
| 384 | if !cf.Conflict || cf.PreviousURL != a.URL || cf.Archives != 1 || strings.Join(cf.Choices, ",") != "wipe,restore" { |
| 385 | t.Fatalf("conflict: %+v", cf) |
| 386 | } |
| 387 | // Er is nog niets veranderd. |
| 388 | if b.Status().BlueGitURL != a.URL || len(b.Archives()) != 1 { |
| 389 | t.Fatal("conflict veranderde de koppeling") |
| 390 | } |
| 391 | code, raw = b.Admin("POST", "/connect", server.ConnectRequest{URL: other.URL, Key: keyB, OnConflict: "wipe"}) |
| 392 | if code != 200 { |
| 393 | t.Fatalf("wipe: %d %s", code, raw) |
| 394 | } |
| 395 | if len(b.Archives()) != 0 { |
| 396 | t.Fatal("oude archieven staan er nog") |
| 397 | } |
| 398 | b.Sync() |
| 399 | if _, err := os.Stat(b.Mirror("data/repos/tim/alpha.git/HEAD")); err == nil { |
| 400 | t.Fatal("A staat nog in de kopie") |
| 401 | } |
| 402 | if !strings.Contains(gitLog(t, b.Mirror("data/repos/tim/gamma.git")), "zet g.txt") { |
| 403 | t.Fatal("B staat niet in de kopie") |
| 404 | } |
| 405 | if st := other.BackupStatus(); !st.Connected || st.ServerURL != b.URL { |
| 406 | t.Fatalf("B ziet de koppeling niet: %+v", st) |
| 407 | } |
| 408 | // A is los: zijn meldingen worden geweigerd (andere instantie). |
| 409 | if b.Status().InstanceID == a.BackupStatus().InstanceID { |
| 410 | t.Fatal("backup server hoort nog bij A") |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Feature: keuze "nieuwe instantie terugzetten naar de gegevens in de backup server". |
| 415 | func TestNewInstanceRestore(t *testing.T) { |
| 416 | a := startBlueGit(t) |
| 417 | a.Project("Alpha", "a.txt", "van A") |
| 418 | a.Issue("alpha", "issue van A") |
| 419 | b := startBackup(t, nil) |
| 420 | b.Connect(a, a.NewKey()) |
| 421 | b.Sync() |
| 422 | |
| 423 | fresh := startBlueGit(t) |
| 424 | fresh.Project("Gamma", "g.txt", "van B") |
| 425 | keyB := fresh.NewKey() |
| 426 | freshID := fresh.BackupStatus().InstanceID |
| 427 | code, raw := b.Admin("POST", "/connect", server.ConnectRequest{URL: fresh.URL, Key: keyB, OnConflict: "restore"}) |
| 428 | if code != 200 || !strings.Contains(string(raw), `"restored":true`) { |
| 429 | t.Fatalf("restore: %d %s", code, raw) |
| 430 | } |
| 431 | fresh.WaitUp(30 * time.Second) |
| 432 | // De nieuwe BlueGit heeft nu de database van A, dus ook A's token. |
| 433 | fresh.Token = a.Token |
| 434 | if code, s := fresh.File("alpha", "a.txt"); code != 200 || s != "van A" { |
| 435 | t.Fatalf("A's project op de nieuwe BlueGit: %d %q", code, s) |
| 436 | } |
| 437 | if code := fresh.ProjectCode("gamma"); code != 404 { |
| 438 | t.Fatalf("B's eigen project bestaat nog: %d", code) |
| 439 | } |
| 440 | if titles := strings.Join(fresh.IssueTitles("alpha"), "|"); !strings.Contains(titles, "issue van A") { |
| 441 | t.Fatalf("issues: %s", titles) |
| 442 | } |
| 443 | // De koppeling hoort bij de installatie, niet bij de gegevens. |
| 444 | st := fresh.BackupStatus() |
| 445 | if !st.Connected || st.InstanceID != freshID || st.ServerURL != b.URL { |
| 446 | t.Fatalf("koppeling na restore: %+v", st) |
| 447 | } |
| 448 | if b.Status().InstanceID != freshID { |
| 449 | t.Fatal("backup server hoort niet bij de nieuwe instantie") |
| 450 | } |
| 451 | b.Sync() |
| 452 | if !strings.Contains(gitLog(t, b.Mirror("data/repos/tim/alpha.git")), "zet a.txt") { |
| 453 | t.Fatal("kopie na restore") |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Feature: de CLI vraagt de keuze bij een nieuwe verbinding. |
| 458 | func TestCLIConnectAsksOnConflict(t *testing.T) { |
| 459 | cli := filepath.Join(t.TempDir(), "bluegit-backup") |
| 460 | if out, err := exec.Command("go", "build", "-o", cli, "../../../cmd/bluegit-backup").CombinedOutput(); err != nil { |
| 461 | t.Fatalf("build: %v %s", err, out) |
| 462 | } |
| 463 | a := startBlueGit(t) |
| 464 | a.Project("Alpha", "a.txt", "A") |
| 465 | b := startBackup(t, nil) |
| 466 | cfgPath := filepath.Join(t.TempDir(), "config.yml") |
| 467 | _ = os.WriteFile(cfgPath, []byte("listen: "+b.Cfg.Listen+"\npublic_url: "+b.URL+"\ndata_dir: "+b.Dir+"\n"), 0o600) |
| 468 | run := func(stdin string, args ...string) (string, error) { |
| 469 | cmd := exec.Command(cli, append([]string{"connect", "--config", cfgPath}, args...)...) |
| 470 | cmd.Stdin = strings.NewReader(stdin) |
| 471 | out, err := cmd.CombinedOutput() |
| 472 | return string(out), err |
| 473 | } |
| 474 | keyA := a.NewKey() |
| 475 | cmd := exec.Command(cli, "connect", "--config", cfgPath, "--url", a.URL) |
| 476 | cmd.Env = append(os.Environ(), "BLUEGIT_BACKUP_KEY="+keyA) |
| 477 | if out, err := cmd.CombinedOutput(); err != nil { |
| 478 | t.Fatalf("eerste connect: %v %s", err, out) |
| 479 | } |
| 480 | b.Sync() |
| 481 | |
| 482 | other := startBlueGit(t) |
| 483 | keyB := other.NewKey() |
| 484 | out, err := run("\n", "--url", other.URL, "--key", keyB) |
| 485 | if err == nil || !strings.Contains(out, "Oude backups verwijderen") || !strings.Contains(out, "terugzetten") || !strings.Contains(out, "afgebroken") { |
| 486 | t.Fatalf("afbreken: %v %s", err, out) |
| 487 | } |
| 488 | if b.Status().BlueGitURL != a.URL { |
| 489 | t.Fatal("afbreken veranderde de koppeling") |
| 490 | } |
| 491 | out, err = run("1\n", "--url", other.URL, "--key", keyB) |
| 492 | if err != nil || b.Status().BlueGitURL != other.URL { |
| 493 | t.Fatalf("keuze 1: %v %s", err, out) |
| 494 | } |
| 495 | if out, err := exec.Command(cli, "status", "--config", cfgPath).CombinedOutput(); err != nil || !strings.Contains(string(out), other.URL) { |
| 496 | t.Fatalf("status: %v %s", err, out) |
| 497 | } |
| 498 | } |