Files
pages/internal/server/dr_recovery_test.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

139 lines
4.0 KiB
Go

package server
import (
"context"
"crypto/sha256"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"pageswiki/internal/db"
"pageswiki/internal/dr"
"pageswiki/internal/store"
)
// Full startup against a real schema and synthetic data, not a mock connection.
// No production data, imported calendars or outbound requests are involved.
func TestDRRecoveryReadOnlyServer(t *testing.T) {
t.Chdir("../..")
root := t.TempDir()
database, err := db.Open(filepath.Join(root, "pages.db"))
if err != nil {
t.Fatal(err)
}
if err = db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.NewWithDocumentRoot(database, filepath.Join(root, "documents"))
if err = st.EnsureConfigTable(); err != nil {
t.Fatal(err)
}
domain, err := st.GetDefaultDomain()
if err != nil {
t.Fatal(err)
}
owner, err := st.BootstrapOwner(domain.UUID, "dr-fixture", "synthetic long test password", "DR Fixture", "")
if err != nil {
t.Fatal(err)
}
token, err := st.CreateAuthSession(domain.UUID, owner.UUID)
if err != nil {
t.Fatal(err)
}
if err = st.SetConfigInDomain(domain.UUID, "authorization_mode", "enforcing"); err != nil {
t.Fatal(err)
}
object, err := st.CreatePageAtPath("/dr-fixture")
if err != nil {
t.Fatal(err)
}
rev, err := st.SaveRevisionForLanguage(object, "<h1>DR fixture</h1><p>Canonical body.</p>", "test", "und")
if err != nil {
t.Fatal(err)
}
if err = database.Close(); err != nil {
t.Fatal(err)
}
if _, err = dr.Init(root); err != nil {
t.Fatal(err)
}
snap := filepath.Join(t.TempDir(), "snapshot")
_, digest, err := dr.Snapshot(context.Background(), root, snap)
if err != nil {
t.Fatal(err)
}
standby := filepath.Join(t.TempDir(), "standby")
node, err := dr.Restore(context.Background(), snap, digest, standby)
if err != nil {
t.Fatal(err)
}
path := filepath.Join(standby, "pages.db")
before, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
ro, err := db.OpenReadOnly(path)
if err != nil {
t.Fatal(err)
}
defer ro.Close()
readonly, err := store.NewReadOnlyWithDocumentRoot(ro, filepath.Join(standby, "documents"))
if err != nil {
t.Fatal(err)
}
readonly.StandbyReadUntil = node.ReadUntil()
s := New(readonly)
if s.previewRoot != "" || s.importRoot != "" || s.schedulerRunning.Load() != 0 {
t.Fatal("started standby worker/cache writers")
}
request := func(method, path, auth string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, "http://localhost"+path, nil)
if auth != "" {
r.Header.Set("Authorization", "Bearer "+auth)
}
w := httptest.NewRecorder()
s.handler().ServeHTTP(w, r)
return w
}
for _, path := range []string{"/v/raw/rev:" + rev.UUID, "/dr-fixture"} {
w := request("GET", path, token)
if w.Code != 200 || !strings.Contains(w.Body.String(), "Canonical body.") {
t.Fatalf("read %s: %d %s", path, w.Code, w.Body.String())
}
if w.Header().Get("Cache-Control") != "private, no-store" {
t.Fatalf("stale authorization cache permitted: %s", w.Header().Get("Cache-Control"))
}
}
if w := request("GET", "/v/raw/rev:"+rev.UUID, ""); w.Code == 200 {
t.Fatal("read-only bypassed authorization")
}
if w := request("PUT", "/_/api/v1/objects/"+object, token); w.Code != 503 {
t.Fatal("standby write not rejected")
}
if _, err = readonly.SaveRevisionForLanguage(object, "must never persist", "test", "und"); err == nil {
t.Fatal("store allowed write")
}
if _, err = store.NewReadOnlyWithDocumentRoot(ro, filepath.Join(t.TempDir(), "documents")); err == nil {
t.Fatal("regenerated missing auth key")
}
readonly.StandbyReadUntil = time.Now().Add(-time.Second)
if w := request("GET", "/dr-fixture", token); w.Code != 503 {
t.Fatal("stale snapshot served")
}
after, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if sha256.Sum256(before) != sha256.Sum256(after) {
t.Fatal("standby changed database bytes")
}
for _, name := range []string{"previews", "imports", "pages.db-wal", "pages.db-shm"} {
if _, err = os.Stat(filepath.Join(standby, name)); !os.IsNotExist(err) {
t.Fatalf("created standby artifact %s", name)
}
}
}