Files
archie/internal/app/archied/state_store_regression_test.go
T
sam 6bdbd5e31e feat(config): key services by name through a registry
Services was a struct with one field per service, so each service's name was
stated three times: the field, the loader's defaulting branch, and every
consumer's field access. Adding one meant editing all three, and nothing
stopped them disagreeing. The drift was already there: stateStoreResolvedToken
and gatewayResolvedToken were identical apart from the env var each hard-coded.

Services becomes map[string]ServiceConnection, and RegisterService declares a
service once with the five fields it has. Everything that differs between
services is now an argument rather than a branch elsewhere: an empty registered
target means the operator must supply one, which is how services.state.target
stays required while the gateway's is defaulted, and the registered token env
var replaces the two copied resolvers with Services.ResolvedToken.

Defaulting iterates the registry, so a new service needs no loader edit.
Consumers read Services.Get(name).

Keeping a typo'd section reported took explicit work. [services.gatway] used to
land in toml.MetaData.Undecoded() because the struct had no such field; a map
decodes any section, so it would have loaded silently and left the daemon
dialing the default. unregisteredServiceKeys asks the registry which names are
real, and the test for it fails with an empty UnknownKeys when that check is
removed.

The TOML shape is unchanged, so no deployment profile, config.example.toml or
install.sh edit is needed.

Refs: archie-core-u23g
2026-09-18 23:32:36 +10:00

91 lines
3.2 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"
)
// 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\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n[[repos]]\nowner = 'acme'\nname = 'widget'\n", filepath.Join(dir, "state.db"))
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")
}
}