container-runtime-policies seeded and validated config.ContainerConfig directly,
the last resource in the registry whose document was the internal config struct
(archie-core-1171). encoding/json fell back to that struct's Go field names --
Image, MaxConcurrency, LegacyEnabled, Profiles -- the only Go-cased keys the Web
UI still rendered, and case-insensitive matching let a lowercase misspelling
("maxconcurrency") decode as the field while "max_concurrency" was read back
half-applied.
Define the document here instead: containerRuntimePolicies with explicit
snake_case tags and agentProfile at the profile nesting level, the same shape
092194cd gave the channel document. The two durations stay config.Duration,
which already writes the string the file accepts; this leak was the keys only.
The mapping to config.ContainerConfig is one function the validator and the
runtime layering share.
Reading a legacy document is deliberate, so it stays strict about everything
else: DecodeRenamingLegacyKeys renames the seven Go-cased keys an existing store
holds (and the four at the profile level), drops LegacyEnabled -- the removed
containers.enabled switch, which had no runtime consumer and no current
counterpart -- and keeps DisallowUnknownFields, because swallowing an unknown
key decodes as unset and an unset image is a broken worker. A misspelling is
refused.
normalizeContainerPolicies, through the Definition.Normalize hook the channel
document already uses, rewrites a legacy document into the current shape on
write, so a stored document converges the next time it is saved. The layering
keeps its profile semantics -- a document that omits profiles (one stored before
they existed) inherits the file's, one that carries them replaces the file's --
and now checks both spellings of the key.
docs/development/control-plane.md's 'adding a database-owned field' still
instructed the untagged practice this class of issue removes; it now names the
wire-shape rule, the legacy-key tolerance and the Normalize hook instead.
204 lines
7.9 KiB
Go
204 lines
7.9 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,
|
|
},
|
|
{
|
|
// A zero interval is what applyDefaults replaces before validation
|
|
// runs, so the file layer only ever sees it as a value to reject. The
|
|
// resource has no defaulting behind it: a stored "0s" would become
|
|
// cfg.PollInterval = 0 and stop archied starting.
|
|
name: "zero poll interval",
|
|
kind: SchedulingPolicyKind,
|
|
value: map[string]any{"poll_interval": "0s", "max_retries": 0, "dispatch": map[string]any{"trigger": "assignee"}},
|
|
mutate: func(cfg *config.Config) { cfg.PollInterval = 0 },
|
|
wantErr: true,
|
|
},
|
|
{
|
|
// The pairing rule the file layer's validateDispatch enforces on the
|
|
// effective document (the GH#445 guard): a label-requiring trigger
|
|
// with no label matches every open issue. The resource carries the
|
|
// label precisely so the write path can judge the pairing, so a
|
|
// stored policy without one is refused here and by boot alike.
|
|
name: "label trigger without a label",
|
|
kind: SchedulingPolicyKind,
|
|
value: map[string]any{"poll_interval": "1m0s", "max_retries": 0, "dispatch": map[string]any{"trigger": "label"}},
|
|
mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "label" },
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "label trigger with a label",
|
|
kind: SchedulingPolicyKind,
|
|
value: map[string]any{"poll_interval": "1m0s", "max_retries": 0, "label": "archie:labelled", "dispatch": map[string]any{"trigger": "label"}},
|
|
mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "label"; cfg.Label = "archie:labelled" },
|
|
wantErr: false,
|
|
},
|
|
{
|
|
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,
|
|
},
|
|
{
|
|
// Both layers have to call a duplicate list invalid. When only the
|
|
// resource did, a config the file layer blessed left the daemon
|
|
// unbootable on a database-named error.
|
|
name: "duplicate repository",
|
|
kind: RepositoryPoliciesKind,
|
|
value: []config.Repo{{Owner: "acme", Name: "app"}, {Owner: "acme", Name: "app"}},
|
|
mutate: func(cfg *config.Config) {
|
|
cfg.Repos = []config.Repo{{Owner: "acme", Name: "app"}, {Owner: "acme", Name: "app"}}
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid container runtime policies",
|
|
kind: ContainerRuntimePoliciesKind,
|
|
value: containerRuntimePolicies{Image: "archie-agent:test", PullPolicy: "missing"},
|
|
mutate: func(cfg *config.Config) {},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "container profile with an empty tool name",
|
|
kind: ContainerRuntimePoliciesKind,
|
|
value: containerRuntimePolicies{Image: "archie-agent:test", Profiles: map[string]agentProfile{
|
|
"net": {Tools: []string{""}},
|
|
}},
|
|
mutate: func(cfg *config.Config) {
|
|
cfg.Containers.Profiles = map[string]config.AgentProfile{"net": {Tools: []string{""}}}
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "container runtime policies without an image",
|
|
kind: ContainerRuntimePoliciesKind,
|
|
value: containerRuntimePolicies{PullPolicy: "missing"},
|
|
mutate: func(cfg *config.Config) { cfg.Containers.Image = "" },
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "negative container volume ttl",
|
|
kind: ContainerRuntimePoliciesKind,
|
|
value: containerRuntimePolicies{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"}},
|
|
}
|
|
}
|