BlueGit

timfa bluegit

Files

bluegit / internal / backupserver / integration / compat_test.go

compat_test.go

compat_test.go 6.1 KiB Download Raw
1//go:build integration
2
3package integration
4
5import (
6 "net/http"
7 "strings"
8 "testing"
9 "time"
10
11 "git.timfalken.com/timfa/bluegit-backup/internal/protocol"
12 "git.timfalken.com/timfa/bluegit-backup/internal/server"
13)
14
15// setBackupProtocol bootst een andere versie van de backup server na (die
16// draait in dit testproces). Terugzetten gebeurt aan het eind van de test.
17func setBackupProtocol(t *testing.T, proto, min string) (restore func()) {
18 oldP, oldM := protocol.ProtocolVersion, protocol.MinBlueGitProtocol
19 protocol.ProtocolVersion, protocol.MinBlueGitProtocol = proto, min
20 restore = func() { protocol.ProtocolVersion, protocol.MinBlueGitProtocol = oldP, oldM }
21 t.Cleanup(restore)
22 return restore
23}
24
25// bannerPages zijn pagina's van verschillende soorten; de banner hoort op elk.
26var bannerPages = []string{"/", "/users/tim", "/tim/alpha", "/admin", "/admin/backup", "/-/info", "/help"}
27
28func requireBanner(t *testing.T, g *BlueGit, hc *http.Client, name string, want ...string) {
29 t.Helper()
30 for _, p := range bannerPages {
31 html := g.Page(hc, p)
32 if !strings.Contains(html, `class="compat-banner"`) || !strings.Contains(html, name) || !strings.Contains(html, `href="/admin/backup"`) {
33 t.Fatalf("%s: geen banner voor %q:\n%s", p, name, tail(html, 30))
34 }
35 for _, w := range want {
36 if !strings.Contains(html, w) {
37 t.Fatalf("%s: banner mist %q", p, w)
38 }
39 }
40 }
41}
42
43// Feature: een backup server die een nieuwer protocol vraagt dan BlueGit
44// spreekt ("BlueGit te oud"). De backup server weigert te synchroniseren en
45// terug te zetten en meldt het in zijn UI en API; BlueGit toont beheerders op
46// elke pagina een banner. Na de update (hier: terug naar protocol 1) is
47// alles weer normaal.
48func TestCompatBlueGitTooOld(t *testing.T) {
49 g := startBlueGit(t)
50 g.Project("Alpha", "README.md", "v1")
51 b := startBackup(t, nil)
52 id, key := g.AddServer("nas")
53 b.Connect(g, key)
54 b.Sync()
55 arch := b.Archive()
56 if st := g.ServerStatus(id); st.Compat != "ok" || st.Protocol.Backup != 1 || st.Protocol.BlueGit != 1 {
57 t.Fatalf("begin: %+v", st)
58 }
59
60 // De backup server wordt "geüpdatet" naar protocol 2 en vraagt BlueGit ≥ 2.
61 restore := setBackupProtocol(t, "2", "2")
62 code, raw := b.Admin("POST", "/sync", nil)
63 if code != http.StatusConflict || !strings.Contains(string(raw), "BlueGit is verouderd") {
64 t.Fatalf("sync bij te oude BlueGit: %d %s", code, raw)
65 }
66 st := b.Status()
67 if st.Compat != "bluegit_too_old" || st.BlueGitProtocol != 1 || !strings.Contains(st.CompatMessage, "Update BlueGit") {
68 t.Fatalf("status: %+v", st)
69 }
70 if html := openUI(t, b).Page(); !strings.Contains(html, `class="compat-banner"`) || !strings.Contains(html, "Update BlueGit") {
71 t.Fatalf("backup-UI zonder waarschuwing:\n%s", tail(html, 40))
72 }
73 // BlueGit hoort het protocol in het antwoord van de backup server.
74 if st := g.ServerStatus(id); st.Compat != "bluegit_too_old" || st.Protocol.Backup != 2 || st.Protocol.BackupMin != 2 {
75 t.Fatalf("BlueGit: %+v", st)
76 }
77 // Terugzetten weigert de backup server; BlueGit blijft ongemoeid.
78 g.Commit("alpha", "na.txt", "x", "create")
79 if code, raw := g.Action(id, "rollback", map[string]any{"archive": arch, "confirm": arch}); code < 400 || !strings.Contains(string(raw), "verouderd") {
80 t.Fatalf("rollback bij te oude BlueGit: %d %s", code, raw)
81 }
82 if code, _ := g.File("alpha", "na.txt"); code != 200 {
83 t.Fatal("BlueGit is toch teruggezet")
84 }
85 requireBanner(t, g, g.Session(), "nas", "BlueGit is out of date", "Update BlueGit")
86
87 // "BlueGit geüpdatet" (hier: de backup server weer op protocol 1).
88 restore()
89 b.Sync()
90 if st := b.Status(); st.Compat != "ok" || st.CompatMessage != "" {
91 t.Fatalf("na update: %+v", st)
92 }
93 Eventually(t, 10*time.Second, "BlueGit ziet weer protocol 1", func() bool { return g.ServerStatus(id).Compat == "ok" })
94 if strings.Contains(g.Page(g.Session(), "/"), "compat-banner") {
95 t.Fatal("banner blijft na de update")
96 }
97}
98
99// Feature: BlueGit vraagt een nieuwer protocol dan de backup server spreekt
100// ("backup te oud"). Nagebootst met een test-build van BlueGit met protocol
101// 99 (-ldflags -X, scripts/build-bluegit.sh). Koppelen lukt, zodat het
102// zichtbaar wordt; BlueGit weigert gegevens (426), de backup server weigert
103// te synchroniseren, beheerders zien de banner en andere gebruikers niet.
104func TestCompatBackupTooOld(t *testing.T) {
105 g := startBlueGitBin(t, bluegitFutureBin(t))
106 g.Project("Alpha", "README.md", "v1")
107 b := startBackup(t, nil)
108 id, key := g.AddServer("offsite")
109 code, raw := b.Admin("POST", "/connect", server.ConnectRequest{URL: g.URL, Key: key})
110 if code != 200 || !strings.Contains(string(raw), "backup_too_old") || !strings.Contains(string(raw), "Update de backup server") {
111 t.Fatalf("koppelen: %d %s", code, raw)
112 }
113 code, raw = b.Admin("POST", "/sync", nil)
114 if code != http.StatusConflict || !strings.Contains(string(raw), "backup server is verouderd") {
115 t.Fatalf("sync: %d %s", code, raw)
116 }
117 if st := b.Status(); st.Compat != "backup_too_old" || st.BlueGitProtocol != FutureProtocol || st.BlueGitMinProtocol != FutureProtocol {
118 t.Fatalf("status: %+v", st)
119 }
120 // Een oude backup server zonder protocolkoppen krijgt van BlueGit 426.
121 req, _ := http.NewRequest("GET", g.URL+"/api/v1/backup/manifest", nil)
122 req.Header.Set("Authorization", "Bearer "+key)
123 res, err := http.DefaultClient.Do(req)
124 if err != nil {
125 t.Fatal(err)
126 }
127 res.Body.Close()
128 if res.StatusCode != http.StatusUpgradeRequired {
129 t.Fatalf("manifest zonder koppen: %d", res.StatusCode)
130 }
131 if st := g.ServerStatus(id); st.Compat != "backup_too_old" || st.Protocol.BlueGit != FutureProtocol {
132 t.Fatalf("BlueGit: %+v", st)
133 }
134 requireBanner(t, g, g.Session(), "offsite", "The backup server is out of date", "Update the backup server")
135
136 // Een gebruiker die geen beheerder is, ziet de banner niet.
137 g.RestartAs("bob", 2)
138 bob := g.Session()
139 for _, p := range []string{"/", "/users/bob", "/-/info", "/help"} {
140 if html := g.Page(bob, p); strings.Contains(html, "compat-banner") || strings.Contains(html, "offsite") {
141 t.Fatalf("%s: niet-beheerder ziet de banner", p)
142 }
143 }
144 if html := g.Page(bob, "/"); !strings.Contains(html, "bob") {
145 t.Fatalf("niet als bob aangemeld:\n%s", tail(html, 20))
146 }
147}