feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
@@ -0,0 +1,168 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/google/uuid"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/dr"
|
||||
"pageswiki/internal/server"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestPostgreSQLMigrationAndTLSStartup(t *testing.T) {
|
||||
dsn := os.Getenv("PAGESWIKI_TEST_POSTGRES_TLS_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("set a disposable PostgreSQL verify-full DSN")
|
||||
}
|
||||
ctx := context.Background()
|
||||
admin, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: dsn})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema := "pageswiki_cli_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
if _, err = admin.Exec(`CREATE SCHEMA "` + schema + `"`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _, _ = admin.Exec(`DROP SCHEMA "` + schema + `" CASCADE`); admin.Close() })
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q := u.Query()
|
||||
q.Set("search_path", schema)
|
||||
u.RawQuery = q.Encode()
|
||||
t.Setenv("PAGESWIKI_POSTGRES_DSN", u.String())
|
||||
t.Setenv("PAGESWIKI_AUTH_KEY", "")
|
||||
dir := t.TempDir()
|
||||
database, err := db.Open(filepath.Join(dir, "pages.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := store.NewWithDocumentRoot(database, filepath.Join(dir, "documents"))
|
||||
if err = s.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := s.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner, err := s.BootstrapOwner(domain.UUID, "owner", "synthetic cli password", "Owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := s.CreateAuthSession(domain.UUID, owner.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = database.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
if err = runPostgreSQLMigration([]string{"--data-dir", dir, "--apply"}, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PAGESWIKI_DB_BACKEND", "postgresql")
|
||||
t.Chdir("../..")
|
||||
for range 2 {
|
||||
database, native, err := openApplicationStore(ctx, dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := native.PrincipalForSession(domain.UUID, token); err != nil || got.UUID != owner.UUID {
|
||||
database.Close()
|
||||
t.Fatal("session lost", err)
|
||||
}
|
||||
srv, err := server.NewChecked(native)
|
||||
if err != nil {
|
||||
database.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
closeCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
err = srv.Close(closeCtx)
|
||||
cancel()
|
||||
database.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLMigrationPreflight(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
database, err := db.Open(filepath.Join(dir, "pages.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := store.NewWithDocumentRoot(database, filepath.Join(dir, "documents"))
|
||||
if err = s.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = database.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// No target is needed for the schema-only preflight.
|
||||
t.Setenv("PAGESWIKI_POSTGRES_DSN", "")
|
||||
var out bytes.Buffer
|
||||
if err = runPostgreSQLMigration([]string{"--data-dir", dir}, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var result struct {
|
||||
Mode string
|
||||
Copied bool
|
||||
Tables []string
|
||||
}
|
||||
if err = json.Unmarshal(out.Bytes(), &result); err != nil || result.Mode != "preflight" || result.Copied || len(result.Tables) < 130 {
|
||||
t.Fatal(result, err)
|
||||
}
|
||||
lock, err := dr.Lock(dir, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer lock.Close()
|
||||
if err = runPostgreSQLMigration([]string{"--data-dir", dir}, &out); err == nil {
|
||||
t.Fatal("active instance maintenance accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLBackendRefusesFallback(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, backend := range []string{"unsupported", "postgresql"} {
|
||||
t.Setenv("PAGESWIKI_DB_BACKEND", backend)
|
||||
t.Setenv("PAGESWIKI_POSTGRES_DSN", "")
|
||||
if _, _, err := openApplicationStore(context.Background(), dir); err == nil {
|
||||
t.Fatal("invalid configuration accepted")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "pages.db")); !os.IsNotExist(err) {
|
||||
t.Fatal("SQLite fallback created")
|
||||
}
|
||||
if err := runSchema([]string{"status", "--data-dir", dir}, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("legacy inspection accepted")
|
||||
}
|
||||
if err := runDR([]string{"init", "--data-dir", dir}, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("legacy DR accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLMigrationArguments(t *testing.T) {
|
||||
for _, args := range [][]string{{"--timeout", "0"}, {"unexpected"}, {"--data-dir", filepath.Join(t.TempDir(), "missing")}} {
|
||||
if err := runPostgreSQLMigration(args, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("invalid arguments accepted", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user