Files
archie/internal/app/archied/control_plane_validation_test.go
T
sam 7f7eb573a1 fix(controlplane): a kind with no stored value leaves the file's value in force
The fix-closure review found the previous pass moved the failure instead of
ending it. A seed the resource validator refuses leaves the kind ABSENT, and
RuntimeConfig treated an absent kind as fatal, so archied and the Gateway
refused to start with "control-plane resource not found" -- a database-named
error that editing config.toml cannot clear. Reproduced before the fix: every
kind in the new table failed that way.

Two changes, one on each side of the seam:

- Client.RuntimeConfig (and RuntimeChatConfig) now treat a kind with no stored
  value as "the file document's value stays in effect", which is what the
  skip-and-report comment already claimed. The version map records no version
  for such a kind, so nothing is reported as applied. A value the file layer
  also refuses still fails closed, at configuration.Validate, with a message
  naming the file's own field -- and the file is the fix.

- Which repository lists are valid can no longer differ between the layers.
  configuration.ValidateRepositories is now the single rule set (owner and name
  present, no duplicates, compilable test glob) and the repository-policies
  resource validator delegates to it. The file layer had no duplicate rule
  while the resource layer did, so a duplicate [[repos]] entry passed the loader
  and then vanished into a skipped seed.

Also, from the same review: the scheduling validator's "0s" carve-out is gone,
so a zero poll interval is refused at write time as configuration.Validate
refuses it (there is no defaulting behind a stored value); applyOverlayFile's
doc now says which overlay forms fold (a conf.d extra does not, and nothing
reads cfg.Extra); and plugin-settings' no-op validator is a named function with
the reason it accepts everything.
2026-09-21 22:12:32 +10:00

254 lines
9.6 KiB
Go

package archied
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
)
// staleDatabaseOwnedSettings are TOML fragments, each carrying one value a
// control-plane resource is seeded from and replaces wholesale. They are the
// values docs/prds/runtime-control-plane.md,
// "Bootstrap, migration, and recovery" says must not block a process: "after
// migration, settings in TOML are ignored and cannot block State Store
// startup".
var staleDatabaseOwnedSettings = []struct {
name string
body string
}{
{
name: "provider base_url carrying userinfo",
body: "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n",
},
{
name: "negative poll_interval",
body: "poll_interval = \"-5s\"\n",
},
{
name: "unrecognised dispatch.trigger",
body: "[dispatch]\ntrigger = \"labels\"\n",
},
{
name: "negative containers.volume_ttl",
body: "[containers]\nvolume_ttl = \"-1m\"\n",
},
{
name: "malformed repos test_glob",
body: "[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n",
},
}
// TestLoadConfigAcceptsStaleDatabaseOwnedValues is the first of the two layers
// archie-state-store boots through. loadConfig resolves the file and is where
// the loader used to reject these values; it must not, because the setting
// belongs to the control plane. The second layer is the boot itself, covered by
// TestStateStoreBootsWithStaleDatabaseOwnedValues -- passing only this one is
// not the item's claim.
func TestLoadConfigAcceptsStaleDatabaseOwnedValues(t *testing.T) {
for _, tt := range staleDatabaseOwnedSettings {
t.Run(tt.name, func(t *testing.T) {
path := t.TempDir() + "/config.toml"
writeConfig(t, path, "bot_user = \"widget\"\n"+tt.body)
b := &boot{log: slog.New(slog.DiscardHandler)}
if err := b.loadConfig(t.Context(), path, ""); err != nil {
t.Fatalf("loadConfig: %v (a stale database-owned value must not fail boot)", err)
}
})
}
}
// staleBootGrace is how long one boot in
// TestStateStoreBootsWithStaleDatabaseOwnedValues may run before the test cancels
// it: short, because the assertion does not depend on it -- a refusal arrives as
// an error either before the cancel (first select) or after it (second select,
// where a non-Canceled error fails the test).
//
// staleShutdownGrace bounds the shutdown, and is deliberately not the 5s the
// single-boot regression test uses: this table runs five boots back to back on a
// host that may be busy, and the select returns the moment the process does, so
// the bound costs nothing when shutdown is quick and only decides how long a
// failure takes to report.
const (
staleBootGrace = 250 * time.Millisecond
staleShutdownGrace = 30 * time.Second
)
// TestStateStoreBootsWithStaleDatabaseOwnedValues is the process-level half of
// the item, and the layer that actually decides it: archie-state-store seeds
// every control-plane resource from the same file config on a fresh database
// (state_store.go, control.ImportConfig), and that seed is what runs the
// resource's own validator. A seed that does not validate is skipped and
// reported, not fatal -- the kind stays absent, the file's value is the one in
// effect, and the operator's fix in config.toml is what clears it.
func TestStateStoreBootsWithStaleDatabaseOwnedValues(t *testing.T) {
for _, tt := range staleDatabaseOwnedSettings {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
body := fmt.Sprintf(
"bot_user = 'archie'\ndb_path = %q\n%s[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n",
filepath.Join(dir, "state.db"), tt.body,
)
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
// Loopback, so no token is required. Cancellation is the assertion:
// a boot that refuses the config returns before it, and one that
// serves returns only when the context ends. Cancel first, then
// assert -- racing startup against a wall clock is a coin flip.
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
errCh := make(chan error, 1)
go func() {
errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "127.0.0.1:0"})
}()
select {
case err := <-errCh:
t.Fatalf("RunStateStore returned before cancellation, want it to serve: %v", err)
case <-time.After(staleBootGrace):
}
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(staleShutdownGrace):
t.Fatal("RunStateStore did not return within the shutdown grace after cancellation")
}
})
}
}
// TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource is the other
// end of a seed the control plane refused: the kind stays ABSENT rather than
// being stored (ImportConfig skips it), and boot must then fall back to the
// file document's value. Treating an absent kind as fatal is how a config the
// file layer blesses left the daemon unbootable with a database-named error,
// and left editing config.toml unable to fix it.
func TestRuntimeConfigKeepsTheFileValueWhenAKindHasNoStoredResource(t *testing.T) {
tests := []struct {
name string
kind string
check func(t *testing.T, cfg config.Config)
}{
{
name: "repository policies",
kind: controlplane.RepositoryPoliciesKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if len(cfg.Repos) != 1 || cfg.Repos[0].Name != "from-file" {
t.Errorf("Repos = %+v, want the file's from-file", cfg.Repos)
}
},
},
{
name: "scheduling policy",
kind: controlplane.SchedulingPolicyKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if cfg.PollInterval != config.Duration(time.Minute) {
t.Errorf("PollInterval = %v, want the file's 1m", cfg.PollInterval)
}
},
},
{
name: "container runtime policies",
kind: controlplane.ContainerRuntimePoliciesKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if cfg.Containers.Image != "archie:from-file" {
t.Errorf("Containers.Image = %q, want the file's archie:from-file", cfg.Containers.Image)
}
},
},
{
name: "providers",
kind: controlplane.ProviderSettingsKind,
check: func(t *testing.T, cfg config.Config) {
t.Helper()
if got := cfg.Providers["file"].Class; got != "openai" {
t.Errorf("Providers[file].Class = %q, want the file's openai", got)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resources := databaseOwnedResources()
delete(resources, tt.kind)
b := newReloadBoot(t, &controlPlaneStub{values: resources})
if err := b.loadRuntimeConfig(t.Context()); err != nil {
t.Fatalf("loadRuntimeConfig: %v (a kind with no stored value must leave the file's value in effect)", err)
}
tt.check(t, b.cfgHolder.Get())
})
}
}
// TestDuplicateRepositoriesFailOnTheFilesOwnMessage is the end of the path the
// fix-closure review found. A duplicate [[repos]] entry is refused by the
// repository-policies validator, so the kind is never seeded and stays absent --
// and boot must then fail on the FILE's copy of the value, naming the duplicate,
// with the file as the fix. It used to fail on "control-plane resource not
// found": a database-named error that editing config.toml could not clear.
func TestDuplicateRepositoriesFailOnTheFilesOwnMessage(t *testing.T) {
resources := databaseOwnedResources()
delete(resources, controlplane.RepositoryPoliciesKind)
b := newReloadBoot(t, &controlPlaneStub{values: resources})
b.cfg.Repos = []config.Repo{{Owner: "acme", Name: "app"}, {Owner: "acme", Name: "app"}}
err := b.loadRuntimeConfig(t.Context())
if err == nil {
t.Fatal("loadRuntimeConfig accepted a duplicate repository list")
}
if !errors.Is(err, configuration.ErrInvalidInput) {
t.Errorf("loadRuntimeConfig = %v, want it to wrap configuration.ErrInvalidInput", err)
}
if !strings.Contains(err.Error(), "duplicates") {
t.Errorf("loadRuntimeConfig = %v, want the duplicate named the way configuration.Validate names it", err)
}
if strings.Contains(err.Error(), "not found") {
t.Errorf("loadRuntimeConfig = %v, want no absent-resource error: the file's value is the one in effect", err)
}
}
// TestRuntimeConfigFailsClosedOnAnInvalidDatabaseValue is the other half of the
// same change: the checks that no longer run at load run on the effective
// document, at the one place database resources are layered in. A stored value
// that will not validate must stop archied starting rather than be published as
// the running configuration -- a state nothing can recover from in band.
func TestRuntimeConfigFailsClosedOnAnInvalidDatabaseValue(t *testing.T) {
resources := databaseOwnedResources()
resources[controlplane.ProviderSettingsKind] = map[string]any{
"main": map[string]any{"class": "openai", "base_url": "https://token@example.com/v1"},
}
b := newReloadBoot(t, &controlPlaneStub{values: resources})
err := b.loadRuntimeConfig(t.Context())
if err == nil {
t.Fatal("loadRuntimeConfig accepted a database value that does not validate")
}
if !errors.Is(err, configuration.ErrInvalidInput) {
t.Errorf("loadRuntimeConfig = %v, want it to wrap configuration.ErrInvalidInput", err)
}
if got := b.cfgHolder.Get().Providers["main"].BaseURL; got == "https://token@example.com/v1" {
t.Error("the invalid database value was published as the running configuration")
}
}