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
134 lines
5.2 KiB
Go
134 lines
5.2 KiB
Go
package config_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
)
|
|
|
|
// TestRegisterServiceIsTheSingleDeclaration pins criterion 3 of
|
|
// docs/prds/service-registry.md: a service is declared by one call carrying the
|
|
// five fields it actually has, and every later layer reads that declaration
|
|
// rather than restating the name.
|
|
func TestRegisterServiceIsTheSingleDeclaration(t *testing.T) {
|
|
config.RegisterService(config.ServiceBoth, "widget", "127.0.0.1:7001", "127.0.0.1:7002", "WIDGET_TOKEN")
|
|
|
|
spec, ok := config.LookupService("widget")
|
|
if !ok {
|
|
t.Fatal("LookupService(widget) = false, want the service just registered")
|
|
}
|
|
if spec.Context != config.ServiceBoth {
|
|
t.Errorf("Context = %q, want %q", spec.Context, config.ServiceBoth)
|
|
}
|
|
if spec.Target != "127.0.0.1:7001" {
|
|
t.Errorf("Target = %q, want the registered default", spec.Target)
|
|
}
|
|
if spec.Listen != "127.0.0.1:7002" {
|
|
t.Errorf("Listen = %q, want the registered default", spec.Listen)
|
|
}
|
|
if spec.TokenEnv != "WIDGET_TOKEN" {
|
|
t.Errorf("TokenEnv = %q, want the registered env var", spec.TokenEnv)
|
|
}
|
|
}
|
|
|
|
// TestBuiltInServicesAreRegistered pins that the two services the daemon ships
|
|
// are declared through the same entry point as any other, with the asymmetry
|
|
// the old struct form left unstated: state has no default target, so the
|
|
// operator must supply one, while gateway does.
|
|
func TestBuiltInServicesAreRegistered(t *testing.T) {
|
|
state, ok := config.LookupService("state")
|
|
if !ok {
|
|
t.Fatal("LookupService(state) = false, want the State Store registered")
|
|
}
|
|
if state.Target != "" {
|
|
t.Errorf("state Target default = %q, want empty: services.state.target is operator-supplied", state.Target)
|
|
}
|
|
if state.Listen == "" {
|
|
t.Error("state Listen default is empty, want an address: an empty listen reaches net.Listen as any free port")
|
|
}
|
|
if state.TokenEnv != "STATE_STORE_TOKEN" {
|
|
t.Errorf("state TokenEnv = %q, want STATE_STORE_TOKEN", state.TokenEnv)
|
|
}
|
|
|
|
gateway, ok := config.LookupService("gateway")
|
|
if !ok {
|
|
t.Fatal("LookupService(gateway) = false, want the Gateway registered")
|
|
}
|
|
if gateway.Target == "" {
|
|
t.Error("gateway Target default is empty, want an address: the gateway target is defaulted, not operator-supplied")
|
|
}
|
|
if gateway.TokenEnv != "GATEWAY_TOKEN" {
|
|
t.Errorf("gateway TokenEnv = %q, want GATEWAY_TOKEN", gateway.TokenEnv)
|
|
}
|
|
}
|
|
|
|
// TestUnregisteredServiceIsNotFound keeps the registry the authority on which
|
|
// names exist, which is what lets the loader report a typo'd [services.gatway]
|
|
// as an unknown key once Services is a map that would otherwise swallow it.
|
|
func TestUnregisteredServiceIsNotFound(t *testing.T) {
|
|
if _, ok := config.LookupService("gatway"); ok {
|
|
t.Error("LookupService(gatway) = true, want false for an unregistered name")
|
|
}
|
|
}
|
|
|
|
// TestServicesGetReturnsTheNamedConnection pins lookup by name, which replaced
|
|
// per-service struct field access, so adding a service needs no new field.
|
|
func TestServicesGetReturnsTheNamedConnection(t *testing.T) {
|
|
services := config.Services{
|
|
"state": {Target: "10.0.0.1:9090", Listen: "0.0.0.0:9090", TargetToken: "explicit"},
|
|
}
|
|
got := services.Get("state")
|
|
if got.Target != "10.0.0.1:9090" {
|
|
t.Errorf("Get(state).Target = %q, want the configured target", got.Target)
|
|
}
|
|
if services.Get("absent") != (config.ServiceConnection{}) {
|
|
t.Error("Get(absent) returned a non-zero connection, want the zero value")
|
|
}
|
|
}
|
|
|
|
// TestResolvedTokenPrefersTheExplicitKey pins the precedence the two
|
|
// hand-copied resolvers used: the config key wins, and the registered env var
|
|
// is only a fallback.
|
|
func TestResolvedTokenPrefersTheExplicitKey(t *testing.T) {
|
|
config.RegisterService(config.ServiceClient, "widget-explicit", "", "127.0.0.1:7003", "WIDGET_EXPLICIT_TOKEN")
|
|
services := config.Services{"widget-explicit": {TargetToken: "from-config"}}
|
|
|
|
getenv := func(string) string { return "from-env" }
|
|
if got := services.ResolvedToken("widget-explicit", getenv); got != "from-config" {
|
|
t.Errorf("ResolvedToken = %q, want the explicit target_token to win", got)
|
|
}
|
|
}
|
|
|
|
// TestResolvedTokenFallsBackToTheRegisteredEnvVar is the half that the old
|
|
// stateStoreResolvedToken and gatewayResolvedToken duplicated, differing only
|
|
// in the env var name they hard-coded.
|
|
func TestResolvedTokenFallsBackToTheRegisteredEnvVar(t *testing.T) {
|
|
config.RegisterService(config.ServiceClient, "widget-env", "", "127.0.0.1:7004", "WIDGET_ENV_TOKEN")
|
|
services := config.Services{"widget-env": {}}
|
|
|
|
var asked string
|
|
getenv := func(name string) string {
|
|
asked = name
|
|
return "from-env"
|
|
}
|
|
if got := services.ResolvedToken("widget-env", getenv); got != "from-env" {
|
|
t.Errorf("ResolvedToken = %q, want the env fallback", got)
|
|
}
|
|
if asked != "WIDGET_ENV_TOKEN" {
|
|
t.Errorf("read env var %q, want the name from the registration", asked)
|
|
}
|
|
}
|
|
|
|
// TestResolvedTokenOfUnregisteredServiceHasNoEnvFallback keeps an unknown name
|
|
// from reaching os.Getenv("") and returning something surprising.
|
|
func TestResolvedTokenOfUnregisteredServiceHasNoEnvFallback(t *testing.T) {
|
|
services := config.Services{"gatway": {}}
|
|
getenv := func(string) string {
|
|
t.Error("getenv called for an unregistered service, want no env fallback")
|
|
return "leaked"
|
|
}
|
|
if got := services.ResolvedToken("gatway", getenv); got != "" {
|
|
t.Errorf("ResolvedToken = %q, want empty", got)
|
|
}
|
|
}
|