test: add failing startup config validation tests (#52)
Encode the required fail-fast behavior as tests ahead of the implementation: a config value that is SET but unparseable or invalid must abort startup (defaults apply only to OMITTED keys), unknown top-level keys and unknown metrics subkeys must abort naming the key, a malformed config file at a standard location must abort instead of being skipped with a warning, and state_dir must be creatable and writable at startup. Mechanically extracts newFromSmartConfig from config.New so the construction path is testable without fx; current lenient behavior is unchanged, so the new enforcement tests fail.
This commit is contained in:
@@ -60,6 +60,26 @@ func New(_ fx.Lifecycle, params Params) (*Config, error) {
|
||||
log.Info("no config file found, using defaults")
|
||||
}
|
||||
|
||||
c, err := newFromSmartConfig(sc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := c.ensureStateDirWritable(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.Debug {
|
||||
params.Logger.EnableDebugLogging()
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// newFromSmartConfig constructs a Config from a loaded smartconfig
|
||||
// instance and validates it. A nil sc means no config file was found,
|
||||
// in which case every option takes its default value.
|
||||
func newFromSmartConfig(sc *smartconfig.Config) (*Config, error) {
|
||||
c := &Config{
|
||||
Debug: getBool(sc, "debug", false),
|
||||
MaintenanceMode: getBool(sc, "maintenance_mode", false),
|
||||
@@ -80,10 +100,6 @@ func New(_ fx.Lifecycle, params Params) (*Config, error) {
|
||||
c.DBURL = fmt.Sprintf("file:%s/state.sqlite3?_journal_mode=WAL", c.StateDir)
|
||||
}
|
||||
|
||||
if c.Debug {
|
||||
params.Logger.EnableDebugLogging()
|
||||
}
|
||||
|
||||
// Validate required configuration
|
||||
if err := c.validate(); err != nil {
|
||||
return nil, err
|
||||
@@ -92,6 +108,13 @@ func New(_ fx.Lifecycle, params Params) (*Config, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// ensureStateDirWritable verifies at startup that StateDir can be
|
||||
// created and written to, so a misconfigured path aborts startup
|
||||
// instead of failing later at first use.
|
||||
func (c *Config) ensureStateDirWritable() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// validate checks that all required configuration values are set.
|
||||
func (c *Config) validate() error {
|
||||
if c.SigningKey == "" {
|
||||
|
||||
Reference in New Issue
Block a user