feat(configuration): translate [scheduling] into scheduling.EngineConfig
The engine needs a fully-resolved configuration, not zero values it has to
second-guess. Add the translator the wiring slice then hands to
internal/app/archied:
- TranslateScheduling resolves unset tunables from the domain's own
Default* constants rather than a second copy here, so the two cannot
drift.
- A negative tunable is reported as a startup error wrapping
ErrInvalidInput, matching validateCapture and validatePollInterval.
Clamping was rejected: it runs something other than what the operator
wrote, which is how a misconfiguration stays invisible.
- Events and Clock stay nil. This package owns no bus and no clock --
attaching them is composition -- and the engine documents a nil sink as
a valid configuration: it still runs, it is just unobservable.
applySchedulingDefaults and validateScheduling keep defaulting and
validation separate, the split applyDefaults' own doc explains: the load
path applies both, Validate applies only the latter.
TDD: the translator tests were written first and failed on assertions
(Interval = 0s, want 1m0s; negative values accepted silently), not on
compilation.
This commit is contained in:
1 parent
57d8d48279
commit
4d838b9fb1
4 files changed
+366
No files matched your search
@@ -75,6 +75,7 @@ func (l *Loader) applyDefaults(cfg *config.Config) {
|
||||
applyContainerDefaults(cfg)
|
||||
applyToolPolicyDefaults(cfg)
|
||||
applyCaptureDefaults(cfg)
|
||||
applySchedulingDefaults(cfg)
|
||||
applyNATSDefaults(cfg)
|
||||
applyMemoryDefaults(cfg)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
)
|
||||
|
||||
// DefaultScheduling returns the defaults an absent [scheduling] section
|
||||
// resolves to, taken from the engine's own documented constants rather than
|
||||
// restated here -- one answer for "unset", owned by the domain that acts on
|
||||
// it. [applySchedulingDefaults] fills a decoded config with them.
|
||||
func DefaultScheduling() config.SchedulingConfig {
|
||||
return config.SchedulingConfig{
|
||||
Interval: config.Duration(scheduling.DefaultInterval),
|
||||
MaxParallel: scheduling.DefaultMaxParallel,
|
||||
JobTimeout: config.Duration(scheduling.DefaultJobTimeout),
|
||||
EventsSink: config.EventsSinkNone,
|
||||
}
|
||||
}
|
||||
|
||||
// applySchedulingDefaults resolves an absent [scheduling] section. Every field
|
||||
// defaults independently, so a section naming only max_parallel still gets a
|
||||
// cadence and a run timeout, and "nothing configured" is one resolved state
|
||||
// rather than two that happen to mean the same thing.
|
||||
//
|
||||
// A negative value is left alone for [validateScheduling] to reject.
|
||||
func applySchedulingDefaults(cfg *config.Config) {
|
||||
d := DefaultScheduling()
|
||||
if cfg.Scheduling.Interval == 0 {
|
||||
cfg.Scheduling.Interval = d.Interval
|
||||
}
|
||||
if cfg.Scheduling.MaxParallel == 0 {
|
||||
cfg.Scheduling.MaxParallel = d.MaxParallel
|
||||
}
|
||||
if cfg.Scheduling.JobTimeout == 0 {
|
||||
cfg.Scheduling.JobTimeout = d.JobTimeout
|
||||
}
|
||||
if cfg.Scheduling.EventsSink == "" {
|
||||
cfg.Scheduling.EventsSink = d.EventsSink
|
||||
}
|
||||
}
|
||||
|
||||
// validateScheduling rejects unusable engine tunables. applySchedulingDefaults
|
||||
// fills every zero value, so this only fires on an explicitly negative one --
|
||||
// the engine's own contract documents "negative is an error" for a reason:
|
||||
// a negative interval cannot be a tick cadence, and clamping it would run
|
||||
// something other than what the operator wrote, which is how a
|
||||
// misconfiguration stays invisible. It also rejects an unknown events_sink
|
||||
// spelling, so a typo cannot silently leave a deployment unobservable.
|
||||
func validateScheduling(cfg *config.Config) error {
|
||||
switch {
|
||||
case cfg.Scheduling.Interval < 0:
|
||||
return fmt.Errorf("%w: scheduling.interval must not be negative, got %s", ErrInvalidInput, cfg.Scheduling.Interval.Std())
|
||||
case cfg.Scheduling.MaxParallel < 0:
|
||||
return fmt.Errorf("%w: scheduling.max_parallel must not be negative, got %d", ErrInvalidInput, cfg.Scheduling.MaxParallel)
|
||||
case cfg.Scheduling.JobTimeout < 0:
|
||||
return fmt.Errorf("%w: scheduling.job_timeout must not be negative, got %s", ErrInvalidInput, cfg.Scheduling.JobTimeout.Std())
|
||||
}
|
||||
// Empty is valid input here (applySchedulingDefaults resolves it to
|
||||
// "none" before this runs on the real load path); it is not "unknown".
|
||||
if sink := cfg.Scheduling.EventsSink; sink != "" && !oneOf(sink, eventsSinkModes) {
|
||||
return fmt.Errorf("%w: scheduling.events_sink %q (want %s)", ErrInvalidInput, sink, list(eventsSinkModes))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TranslateScheduling turns the operator's [scheduling] input into the
|
||||
// engine's own configuration type, fully resolved: an unset tunable becomes
|
||||
// the engine's documented default, so the engine receives what the daemon
|
||||
// means rather than zero values it has to second-guess.
|
||||
//
|
||||
// Clock and Events are deliberately left nil. This package owns no clock (the
|
||||
// engine's system clock is the deployment's answer, not an operator's) and no
|
||||
// bus: attaching the sink is composition, and a nil Events is a configuration
|
||||
// the engine documents as valid -- it still runs, it is just unobservable.
|
||||
//
|
||||
// A negative tunable, or an unknown events_sink, is reported rather than
|
||||
// repaired, so a bad file stops startup instead of running something the
|
||||
// operator did not ask for.
|
||||
func TranslateScheduling(cfg config.SchedulingConfig) (scheduling.EngineConfig, error) {
|
||||
holder := config.Config{Scheduling: cfg}
|
||||
if err := validateScheduling(&holder); err != nil {
|
||||
return scheduling.EngineConfig{}, err
|
||||
}
|
||||
// Resolve through the same applier the load path uses, so a hand-built
|
||||
// config and a decoded file cannot resolve differently.
|
||||
applySchedulingDefaults(&holder)
|
||||
|
||||
return scheduling.EngineConfig{
|
||||
Interval: holder.Scheduling.Interval.Std(),
|
||||
MaxParallel: holder.Scheduling.MaxParallel,
|
||||
JobTimeout: holder.Scheduling.JobTimeout.Std(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
)
|
||||
|
||||
// writeSchedulingConfig writes a config file whose only content beyond the
|
||||
// fields every check requires is the given [scheduling] fragment.
|
||||
func writeSchedulingConfig(t *testing.T, section string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
if err := os.WriteFile(path, []byte("bot_user = \"widget\"\n"+section), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// TestTranslateSchedulingResolvesZeroValuesToEngineDefaults pins the
|
||||
// translator to the engine's own documented constants: the domain has one
|
||||
// answer for "unset", and a second copy of those numbers here would be free
|
||||
// to drift from it.
|
||||
func TestTranslateSchedulingResolvesZeroValuesToEngineDefaults(t *testing.T) {
|
||||
eng, err := TranslateScheduling(config.SchedulingConfig{})
|
||||
if err != nil {
|
||||
t.Fatalf("TranslateScheduling(zero value) = %v, want nil", err)
|
||||
}
|
||||
if got := eng.Interval; got != scheduling.DefaultInterval {
|
||||
t.Errorf("Interval = %v, want %v", got, scheduling.DefaultInterval)
|
||||
}
|
||||
if got := eng.MaxParallel; got != scheduling.DefaultMaxParallel {
|
||||
t.Errorf("MaxParallel = %d, want %d", got, scheduling.DefaultMaxParallel)
|
||||
}
|
||||
if got := eng.JobTimeout; got != scheduling.DefaultJobTimeout {
|
||||
t.Errorf("JobTimeout = %v, want %v", got, scheduling.DefaultJobTimeout)
|
||||
}
|
||||
// Events stays nil: this package owns no bus, and the engine documents a
|
||||
// nil sink as a valid configuration -- it still runs, it is just
|
||||
// unobservable. Attaching one is the composition slice's job.
|
||||
if eng.Events != nil {
|
||||
t.Errorf("Events = %v, want nil for an absent events_sink", eng.Events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslateSchedulingKeepsConfiguredValues(t *testing.T) {
|
||||
eng, err := TranslateScheduling(config.SchedulingConfig{
|
||||
Interval: config.Duration(30 * time.Second),
|
||||
MaxParallel: 2,
|
||||
JobTimeout: config.Duration(5 * time.Minute),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("TranslateScheduling(configured) = %v, want nil", err)
|
||||
}
|
||||
if got := eng.Interval; got != 30*time.Second {
|
||||
t.Errorf("Interval = %v, want unchanged 30s", got)
|
||||
}
|
||||
if got := eng.MaxParallel; got != 2 {
|
||||
t.Errorf("MaxParallel = %d, want unchanged 2", got)
|
||||
}
|
||||
if got := eng.JobTimeout; got != 5*time.Minute {
|
||||
t.Errorf("JobTimeout = %v, want unchanged 5m", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTranslateSchedulingRejectsNegativeValues: the engine's own contract
|
||||
// documents "negative is an error". A clamp would run something other than
|
||||
// what the operator wrote, which is how a misconfiguration stays invisible,
|
||||
// so the translator reports it and lets startup fail.
|
||||
func TestTranslateSchedulingRejectsNegativeValues(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg config.SchedulingConfig
|
||||
}{
|
||||
{name: "interval", cfg: config.SchedulingConfig{Interval: config.Duration(-time.Second)}},
|
||||
{name: "max_parallel", cfg: config.SchedulingConfig{MaxParallel: -1}},
|
||||
{name: "job_timeout", cfg: config.SchedulingConfig{JobTimeout: config.Duration(-time.Minute)}},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := TranslateScheduling(tc.cfg)
|
||||
if err == nil {
|
||||
t.Fatalf("TranslateScheduling(negative %s) = nil error, want a startup error", tc.name)
|
||||
}
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("TranslateScheduling(negative %s) = %v, want it to wrap ErrInvalidInput", tc.name, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadSchedulingBlockDecodesAllFourFields pins the four toml keys: the
|
||||
// translator is only reachable from operator input through them.
|
||||
func TestLoadSchedulingBlockDecodesAllFourFields(t *testing.T) {
|
||||
path := writeSchedulingConfig(t, `
|
||||
[scheduling]
|
||||
interval = "30s"
|
||||
max_parallel = 2
|
||||
job_timeout = "5m"
|
||||
events_sink = "bus"
|
||||
`)
|
||||
cfg, err := loadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("loadFile() = %v, want nil", err)
|
||||
}
|
||||
if got := cfg.Scheduling.Interval.Std(); got != 30*time.Second {
|
||||
t.Errorf("Scheduling.Interval = %v, want 30s", got)
|
||||
}
|
||||
if got := cfg.Scheduling.MaxParallel; got != 2 {
|
||||
t.Errorf("Scheduling.MaxParallel = %d, want 2", got)
|
||||
}
|
||||
if got := cfg.Scheduling.JobTimeout.Std(); got != 5*time.Minute {
|
||||
t.Errorf("Scheduling.JobTimeout = %v, want 5m", got)
|
||||
}
|
||||
if got := cfg.Scheduling.EventsSink; got != config.EventsSinkBus {
|
||||
t.Errorf("Scheduling.EventsSink = %q, want %q", got, config.EventsSinkBus)
|
||||
}
|
||||
|
||||
eng, err := TranslateScheduling(cfg.Scheduling)
|
||||
if err != nil {
|
||||
t.Fatalf("TranslateScheduling(loaded) = %v, want nil", err)
|
||||
}
|
||||
if eng.Interval != 30*time.Second || eng.MaxParallel != 2 || eng.JobTimeout != 5*time.Minute {
|
||||
t.Errorf("loaded engine config = %+v, want {30s 2 5m}", eng)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadAbsentSchedulingSectionResolvesEngineDefaults covers the operator
|
||||
// who never writes the section at all -- the common case. Both the loaded
|
||||
// section and the translated engine config must be fully resolved, so
|
||||
// "nothing configured" and "configured with the defaults" are one state.
|
||||
func TestLoadAbsentSchedulingSectionResolvesEngineDefaults(t *testing.T) {
|
||||
cfg, err := loadFile(writeSchedulingConfig(t, ""))
|
||||
if err != nil {
|
||||
t.Fatalf("loadFile() = %v, want nil", err)
|
||||
}
|
||||
if got := cfg.Scheduling.Interval.Std(); got != scheduling.DefaultInterval {
|
||||
t.Errorf("Scheduling.Interval = %v, want %v", got, scheduling.DefaultInterval)
|
||||
}
|
||||
if got := cfg.Scheduling.MaxParallel; got != scheduling.DefaultMaxParallel {
|
||||
t.Errorf("Scheduling.MaxParallel = %d, want %d", got, scheduling.DefaultMaxParallel)
|
||||
}
|
||||
if got := cfg.Scheduling.JobTimeout.Std(); got != scheduling.DefaultJobTimeout {
|
||||
t.Errorf("Scheduling.JobTimeout = %v, want %v", got, scheduling.DefaultJobTimeout)
|
||||
}
|
||||
|
||||
eng, err := TranslateScheduling(cfg.Scheduling)
|
||||
if err != nil {
|
||||
t.Fatalf("TranslateScheduling(loaded) = %v, want nil", err)
|
||||
}
|
||||
if eng.Interval != scheduling.DefaultInterval {
|
||||
t.Errorf("Interval = %v, want %v", eng.Interval, scheduling.DefaultInterval)
|
||||
}
|
||||
if eng.MaxParallel != scheduling.DefaultMaxParallel {
|
||||
t.Errorf("MaxParallel = %d, want %d", eng.MaxParallel, scheduling.DefaultMaxParallel)
|
||||
}
|
||||
if eng.JobTimeout != scheduling.DefaultJobTimeout {
|
||||
t.Errorf("JobTimeout = %v, want %v", eng.JobTimeout, scheduling.DefaultJobTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadRejectsNegativeSchedulingValues puts the negative-value rule on the
|
||||
// load path, not just in the translator: a file edit that breaks a running
|
||||
// deployment (a SIGHUP reload) must be refused with the operator's mistake
|
||||
// named, rather than applied as a negative cadence.
|
||||
func TestLoadRejectsNegativeSchedulingValues(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
section string
|
||||
}{
|
||||
{name: "interval", section: "[scheduling]\ninterval = \"-1m\"\n"},
|
||||
{name: "max_parallel", section: "[scheduling]\nmax_parallel = -1\n"},
|
||||
{name: "job_timeout", section: "[scheduling]\njob_timeout = \"-1h\"\n"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := loadFile(writeSchedulingConfig(t, tc.section))
|
||||
if err == nil {
|
||||
t.Fatalf("loadFile(negative %s) = nil, want a load failure", tc.name)
|
||||
}
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("loadFile(negative %s) = %v, want it to wrap ErrInvalidInput", tc.name, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadAcceptsEveryDocumentedEventsSinkSpelling pins the on/off
|
||||
// vocabulary: "bus" publishes on the event stream, and "none" means no sink.
|
||||
// An absent key resolves to "none" rather than staying empty, the same shape
|
||||
// applyMemoryDefaults and applyNATSDefaults use -- a resolved config has one
|
||||
// spelling for "not publishing", not two that happen to mean it. "off" is
|
||||
// deliberately not a third spelling: a near-miss must be rejected, not
|
||||
// guessed at.
|
||||
func TestLoadAcceptsEveryDocumentedEventsSinkSpelling(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
section string
|
||||
want string
|
||||
}{
|
||||
{name: "absent", section: "", want: config.EventsSinkNone},
|
||||
{name: "none", section: "[scheduling]\nevents_sink = \"none\"\n", want: config.EventsSinkNone},
|
||||
{name: "bus", section: "[scheduling]\nevents_sink = \"bus\"\n", want: config.EventsSinkBus},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg, err := loadFile(writeSchedulingConfig(t, tc.section))
|
||||
if err != nil {
|
||||
t.Fatalf("loadFile(events_sink %s) = %v, want nil", tc.name, err)
|
||||
}
|
||||
if got := cfg.Scheduling.EventsSink; got != tc.want {
|
||||
t.Errorf("Scheduling.EventsSink = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadRejectsUnknownEventsSink: a typo must not silently leave the
|
||||
// engine unobservable, which is exactly what an unrecognised value would do
|
||||
// if it defaulted to off.
|
||||
func TestLoadRejectsUnknownEventsSink(t *testing.T) {
|
||||
_, err := loadFile(writeSchedulingConfig(t, "[scheduling]\nevents_sink = \"kafka\"\n"))
|
||||
if err == nil {
|
||||
t.Fatal("loadFile(unknown events_sink) = nil, want a load failure")
|
||||
}
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("loadFile(unknown events_sink) = %v, want it to wrap ErrInvalidInput", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestExampleConfigSchedulingSectionIsDocumentationOnly checks the shipped
|
||||
// template the way an operator's first run will: whatever the [scheduling]
|
||||
// block documents, the file must load and resolve to the documented state,
|
||||
// and the block must stay dormant (no live section) the same way [[repos]]
|
||||
// does -- a live example is a default an operator never chose.
|
||||
func TestExampleConfigSchedulingSectionIsDocumentationOnly(t *testing.T) {
|
||||
cfg, err := loadFile(filepath.Join("..", "..", "..", "config.example.toml"))
|
||||
if err != nil {
|
||||
t.Fatalf("loadFile(config.example.toml) = %v, want nil", err)
|
||||
}
|
||||
eng, err := TranslateScheduling(cfg.Scheduling)
|
||||
if err != nil {
|
||||
t.Fatalf("TranslateScheduling(example) = %v, want nil", err)
|
||||
}
|
||||
if eng.Interval != scheduling.DefaultInterval {
|
||||
t.Errorf("Interval = %v, want the documented default %v", eng.Interval, scheduling.DefaultInterval)
|
||||
}
|
||||
if eng.MaxParallel != scheduling.DefaultMaxParallel {
|
||||
t.Errorf("MaxParallel = %d, want the documented default %d", eng.MaxParallel, scheduling.DefaultMaxParallel)
|
||||
}
|
||||
if eng.JobTimeout != scheduling.DefaultJobTimeout {
|
||||
t.Errorf("JobTimeout = %v, want the documented default %v", eng.JobTimeout, scheduling.DefaultJobTimeout)
|
||||
}
|
||||
if eng.Events != nil {
|
||||
t.Errorf("Events = %v, want nil: the example section is commented out, so no sink is configured", eng.Events)
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,10 @@ var (
|
||||
natsModes = []string{config.NATSModeEmbedded, config.NATSModeExternal}
|
||||
forgeIntakes = []string{config.ForgeIntakePoll, config.ForgeIntakeWebhook, config.ForgeIntakeBoth}
|
||||
memoryEngines = []string{memoryEngineBuiltin}
|
||||
// eventsSinkModes are the explicit [scheduling].events_sink spellings.
|
||||
// The empty string also means "no sink" but is resolved by
|
||||
// applySchedulingDefaults before validation, so it is not listed here.
|
||||
eventsSinkModes = []string{config.EventsSinkBus, config.EventsSinkNone}
|
||||
)
|
||||
|
||||
// Validate runs the same checks Loader applies before accepting a config,
|
||||
@@ -89,6 +93,9 @@ func validate(cfg *config.Config) error {
|
||||
if err := validateImage(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateScheduling(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateCapture(cfg)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user