Files
archie/internal/app/controlplane/validators_agree_test.go
T
sam 9a45a35c27 fix(config): judge a database-owned setting at the layer that owns it
Review of the two previous commits found the split was incomplete in both
directions: the checks moved off the file load path were still enforced
anywhere the value could enter the system, and the fold reached only one of the
three overlay paths.

A stale database-owned TOML value could still stop archie-state-store. The
process seeds every control-plane resource from the same file config on a fresh
database, and the seed runs the resource's own validator; three of the five
values the loader now accepts aborted the boot there (verified by booting
RunStateStore before the fix). A seed that does not validate is now SKIPPED and
reported instead of fatal: the resource stays absent, so the file's value is the
one in effect and editing config.toml is still the fix -- the daemon and the
Gateway are where it fails closed, at boot.runtimeConfig.

The resource validators were weaker than configuration.Validate for exactly the
fields the split reclassified, so a value the file path used to refuse at load
became storable in the database, and then un-fixable by editing config.toml.
The trigger enum and the test-glob rule now come from the configuration package
rather than being restated, the container image is required there as it is in the
file, and a table test ties the two layers together value by value.

The directory form of -config-overlay kept the archie-core-e2e2 failure: it
decoded each overlay file straight into the populated config, so a map-valued
entry it only partly addressed still cleared the fields it did not name. Both
the overlay layer's main file and its feature files now fold, which is what
Loader.Dir's documented parity with Loader.Overlay has always claimed.

The overlay file's key report is derived from the apply's own view of the file as
well as the file decode's, because TOML matches keys case-insensitively and the
fold's yaml decode does not: a key only the file decode matched was consumed by
the report and dropped by the apply, a silent no-op that worked in the base file.

Tests: the state-store boot is asserted against the real process, the directory
overlay and the case-mismatched key each have one, and ImportConfig pins that a
refused seed is not stored.
2026-09-21 22:12:32 +10:00

151 lines
5.5 KiB
Go

package controlplane
import (
"encoding/json"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
)
// TestResourceValidatorsRejectWhatEffectiveValidationRejects ties the two rule
// sets that guard the same fields. A control-plane resource is seeded from the
// file document and then replaces it wholesale (Client.RuntimeConfig), so a
// value the file path accepts must not be storable through the resource, and a
// value the resource accepts must not stop the daemon's validation of the
// effective document. Guards that live only on one side are how a stale TOML
// value becomes a database value the operator cannot fix by editing config.toml.
//
// Each case mutates the same field the resource carries, so the two halves
// cannot drift apart without failing here.
func TestResourceValidatorsRejectWhatEffectiveValidationRejects(t *testing.T) {
tests := []struct {
name string
kind string
value any
mutate func(cfg *config.Config)
wantErr bool
}{
{
name: "valid scheduling policy",
kind: SchedulingPolicyKind,
value: map[string]any{"poll_interval": "1m0s", "max_retries": 0, "dispatch": map[string]any{"trigger": "assignee"}},
mutate: func(cfg *config.Config) {},
wantErr: false,
},
{
name: "unrecognised dispatch trigger",
kind: SchedulingPolicyKind,
value: map[string]any{"poll_interval": "1m0s", "max_retries": 0, "dispatch": map[string]any{"trigger": "labels"}},
mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "labels" },
wantErr: true,
},
{
name: "negative poll interval",
kind: SchedulingPolicyKind,
value: map[string]any{"poll_interval": "-5s", "max_retries": 0, "dispatch": map[string]any{"trigger": "assignee"}},
mutate: func(cfg *config.Config) { cfg.PollInterval = config.Duration(-5 * time.Second) },
wantErr: true,
},
{
name: "valid repository policies",
kind: RepositoryPoliciesKind,
value: []config.Repo{{Owner: "acme", Name: "app", TestGlob: "**/*_test.go"}},
mutate: func(cfg *config.Config) {},
wantErr: false,
},
{
name: "malformed test glob",
kind: RepositoryPoliciesKind,
value: []config.Repo{{Owner: "acme", Name: "app", TestGlob: "["}},
mutate: func(cfg *config.Config) { cfg.Repos = []config.Repo{{Owner: "acme", Name: "app", TestGlob: "["}} },
wantErr: true,
},
{
name: "valid container runtime policies",
kind: ContainerRuntimePoliciesKind,
value: config.ContainerConfig{Image: "archie-agent:test", PullPolicy: "missing"},
mutate: func(cfg *config.Config) {},
wantErr: false,
},
{
name: "container runtime policies without an image",
kind: ContainerRuntimePoliciesKind,
value: config.ContainerConfig{PullPolicy: "missing"},
mutate: func(cfg *config.Config) { cfg.Containers.Image = "" },
wantErr: true,
},
{
name: "negative container volume ttl",
kind: ContainerRuntimePoliciesKind,
value: config.ContainerConfig{Image: "archie-agent:test", VolumeTTL: config.Duration(-time.Minute)},
mutate: func(cfg *config.Config) { cfg.Containers.VolumeTTL = config.Duration(-time.Minute) },
wantErr: true,
},
{
name: "provider base URL carrying userinfo",
kind: ProviderSettingsKind,
value: map[string]any{"acme": map[string]any{"class": "openai", "base_url": "https://user:pass@example.com"}},
mutate: func(cfg *config.Config) {
cfg.Providers = map[string]config.Provider{"acme": {Class: "openai", BaseURL: "https://user:pass@example.com"}}
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := validConfigForValidation()
tt.mutate(&cfg)
configErr := configuration.Validate(&cfg)
encoded, err := json.Marshal(tt.value)
if err != nil {
t.Fatal(err)
}
resourceErr := decodeResource(t, tt.kind, encoded)
if !tt.wantErr {
if configErr != nil {
t.Errorf("configuration.Validate = %v, want nil for a valid value", configErr)
}
if resourceErr != nil {
t.Errorf("%s Decode = %v, want nil for a valid value", tt.kind, resourceErr)
}
return
}
if configErr == nil {
t.Errorf("configuration.Validate accepted the value, want the effective document to reject it")
}
if resourceErr == nil {
t.Errorf("%s accepted the value, want the resource validator to reject what the effective document rejects", tt.kind)
}
})
}
}
// decodeResource runs the resource validator exactly as ImportConfig does when
// it seeds a kind from the file config (registry.go), against the production
// server the composition roots build (testServer).
func decodeResource(t *testing.T, kind string, encoded []byte) error {
t.Helper()
_, err := testServer(t, nil).definitions[kind].Decode(encoded)
return err
}
// validConfigForValidation is a document every check passes, so each case above
// fails only for the value it changes -- same fixture the configuration package
// uses, restated here because Validate documents that it does not apply
// defaults.
func validConfigForValidation() config.Config {
return config.Config{
BotUser: "archie-bot",
PollInterval: config.Duration(60 * time.Second),
Dispatch: config.Dispatch{Trigger: "assignee"},
Forge: config.Forge{Type: "none"},
Containers: config.ContainerConfig{Image: "ghcr.io/samcharles93/archie-agent:latest"},
Services: config.Services{config.ServiceNameGateway: {Target: "127.0.0.1:8585"}},
}
}