Wave 1 of the Postgres migration (archie-core-7r0x.9). This is the shared shape every port lane builds on; nothing here switches a live path to Postgres yet. - config: add `database_url` beside `db_path` (config.go), mark it bootstrap-only in denied_keys, expose it in the webui StorageView/config schema, and add it to config.example.toml plus all deployments/*.toml. - docker-compose: add a postgres:18 service; `task dev` (dev:postgres) brings it up and waits for it before the State Store boots. - archied: `openStateStore` now opens and migrates the process-scoped Postgres pool via `openStateStorePool`. The State Store fails closed (D1.2) when database_url is absent or invalid rather than fall back to SQLite; serving against Postgres later requires an import-completion record (D1.3), which is a later lane's gate, not this one. - composition seam: task-store construction (state_store_task.go) and EDA-store construction (state_store_eda.go) are extracted from openStateStore, with the pool open in state_store_pool.go. This is a behaviour-preserving refactor only — no store, interface, error, ordering or logging change — so L1 and L2 build against separate files instead of sharing state_store.go. Tests: fail-closed pool behaviour is pinned (absent/invalid URL); the State Store boot/restart tests now run against a throwaway Postgres 18 container (pgtest TestMain). Scoped test command and module-wide build pass; see lane report.
92 lines
3.3 KiB
Go
92 lines
3.3 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
|
)
|
|
|
|
// startupGrace is how long RunStateStore is given to either refuse the config
|
|
// or settle into serving before the test cancels it. It bounds "did startup
|
|
// reject us" (fast, deterministic) rather than "is the machine fast enough"
|
|
// (I/O-bound, which is what made the previous 200ms budget a coin flip).
|
|
const startupGrace = 2 * time.Second
|
|
|
|
// shutdownGrace bounds how long a cancelled RunStateStore may take to return.
|
|
const shutdownGrace = 5 * time.Second
|
|
|
|
func TestExternalServicesDoNotRequireBridgeNetwork(t *testing.T) {
|
|
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case strings.HasSuffix(r.URL.Path, "/_ping"):
|
|
w.Header().Set("API-Version", "1.55")
|
|
_, _ = w.Write([]byte("OK"))
|
|
case strings.HasSuffix(r.URL.Path, "/containers/json"):
|
|
writeDockerResponse(t, w, []any{})
|
|
default:
|
|
http.Error(w, "bridge discovery must not be required", http.StatusBadRequest)
|
|
}
|
|
}))
|
|
defer api.Close()
|
|
t.Setenv("DOCKER_HOST", api.URL)
|
|
t.Setenv("DOCKER_API_VERSION", "1.55")
|
|
t.Setenv("DOCKER_TLS_VERIFY", "")
|
|
cfg := config.Config{}
|
|
cfg.NATS.Mode = config.NATSModeExternal
|
|
cfg.Containers.Network = "host"
|
|
cfg.Services = config.Services{config.ServiceNameState: {Target: "state.example.test:9090"}}
|
|
pool, _, cleanup := startContainers(t.Context(), cfg, slog.New(slog.DiscardHandler))
|
|
defer cleanup()
|
|
if pool == nil {
|
|
t.Fatal("external services on host networking must construct a worker pool")
|
|
}
|
|
}
|
|
|
|
func TestRunStateStoreAcceptsEnvironmentToken(t *testing.T) {
|
|
t.Setenv("STATE_STORE_TOKEN", "environment-token")
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.toml")
|
|
data := fmt.Sprintf("bot_user = 'archie'\ndb_path = %q\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n[[repos]]\nowner = 'acme'\nname = 'widget'\n", filepath.Join(dir, "state.db"), pgtest.URL(t))
|
|
if err := os.WriteFile(path, []byte(data), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Cancellation is the assertion: RunStateStore must start (the env token
|
|
// satisfies the non-loopback listener) and then return promptly when the
|
|
// context ends. It must NOT be raced against a wall-clock budget -- SQLite
|
|
// schema init is I/O, so a test that gives startup a few hundred
|
|
// milliseconds is a coin flip, not a check. Cancel first, then assert.
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
|
|
errCh := make(chan error, 1)
|
|
go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "0.0.0.0:0"}) }()
|
|
|
|
select {
|
|
case err := <-errCh:
|
|
// Returning before cancellation means startup refused the config, which
|
|
// is the failure this test exists to catch.
|
|
t.Fatalf("RunStateStore returned before cancellation, want it to serve: %v", err)
|
|
case <-time.After(startupGrace):
|
|
}
|
|
cancel()
|
|
select {
|
|
case err := <-errCh:
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("RunStateStore returned %v, want nil or context.Canceled", err)
|
|
}
|
|
case <-time.After(shutdownGrace):
|
|
t.Fatal("RunStateStore did not return within the shutdown grace after cancellation")
|
|
}
|
|
}
|