Manager: allow test code to override settings

Add some callback functionality, so that test code can inject/change
settings before they are processed.

Will be used in an actual test in the following commit.
This commit is contained in:
Sybren A. Stüvel 2022-03-25 16:15:46 +01:00
parent 98a5d48611
commit 1a79c0958c

View File

@ -174,15 +174,18 @@ func getConf() (Conf, error) {
} }
// DefaultConfig returns a copy of the default configuration. // DefaultConfig returns a copy of the default configuration.
func DefaultConfig() Conf { func DefaultConfig(override ...func(c *Conf)) Conf {
c := defaultConfig c := defaultConfig
c.Meta.Version = latestConfigVersion c.Meta.Version = latestConfigVersion
for _, overrideFunc := range override {
overrideFunc(&c)
}
c.constructVariableLookupTable(zerolog.TraceLevel) c.constructVariableLookupTable(zerolog.TraceLevel)
return c return c
} }
// loadConf parses the given file and returns its contents as a Conf object. // loadConf parses the given file and returns its contents as a Conf object.
func loadConf(filename string) (Conf, error) { func loadConf(filename string, overrides ...func(c *Conf)) (Conf, error) {
log.Info().Str("file", filename).Msg("loading configuration") log.Info().Str("file", filename).Msg("loading configuration")
yamlFile, err := os.ReadFile(filename) yamlFile, err := os.ReadFile(filename)
if err != nil { if err != nil {
@ -193,7 +196,7 @@ func loadConf(filename string) (Conf, error) {
evt = log.Warn().Err(err) evt = log.Warn().Err(err)
} }
evt.Msg("unable to load configuration, using defaults") evt.Msg("unable to load configuration, using defaults")
return DefaultConfig(), err return DefaultConfig(overrides...), err
} }
// First parse attempt, find the version. // First parse attempt, find the version.
@ -215,6 +218,10 @@ func loadConf(filename string) (Conf, error) {
return c, fmt.Errorf("unable to parse %s: %w", filename, err) return c, fmt.Errorf("unable to parse %s: %w", filename, err)
} }
for _, overrideFunc := range overrides {
overrideFunc(&c)
}
c.constructVariableLookupTable(zerolog.DebugLevel) c.constructVariableLookupTable(zerolog.DebugLevel)
c.parseURLs() c.parseURLs()
c.checkDatabase() c.checkDatabase()
@ -478,12 +485,14 @@ func (c *Conf) parseURLs() {
// GetTestConfig returns the configuration for unit tests. // GetTestConfig returns the configuration for unit tests.
// The config is loaded from `test-flamenco-manager.yaml` in the directory // The config is loaded from `test-flamenco-manager.yaml` in the directory
// containing the caller's source. // containing the caller's source.
func GetTestConfig() Conf { // The `overrides` parameter can be used to override configuration between
// loading it and processing the file's contents.
func GetTestConfig(overrides ...func(c *Conf)) Conf {
_, myFilename, _, _ := runtime.Caller(1) _, myFilename, _, _ := runtime.Caller(1)
myDir := path.Dir(myFilename) myDir := path.Dir(myFilename)
filepath := path.Join(myDir, "test-flamenco-manager.yaml") filepath := path.Join(myDir, "test-flamenco-manager.yaml")
conf, err := loadConf(filepath) conf, err := loadConf(filepath, overrides...)
if err != nil { if err != nil {
log.Fatal().Err(err).Str("file", filepath).Msg("unable to load test config") log.Fatal().Err(err).Str("file", filepath).Msg("unable to load test config")
} }