Skip to content

Commit

Permalink
Use environment variables when error is strict
Browse files Browse the repository at this point in the history
** Summary **

Fix minor bug in readConfig function when cheking the error of
unmarshalSemiStrictly.

** Motivation **

This will allow us to use a configuration that has an unmarshalling
strict error, i.e. when the configuration have unknown fields, and
the validate-config and validate-config-strict command-line flags
are both false.

** Test plan **

Automated testing.

** Rollout/monitoring/revert plan **

None
  • Loading branch information
lahiguera committed Jul 1, 2019
1 parent 9d9345e commit ede34b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func readConfig(r io.Reader) (Config, error) {
}
unmarshalErr := unmarshalSemiStrictly(bts, &c)
if unmarshalErr != nil {
if _, ok := err.(*UnknownConfigKeys); !ok {
if _, ok := unmarshalErr.(*UnknownConfigKeys); !ok {
return c, unmarshalErr
}
}
Expand Down
29 changes: 29 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ func TestReadBadConfig(t *testing.T) {
assert.Equal(t, c, Config{}, "Parsing invalid config file should return zero struct")
}

func TestReadBadConfig_EnvVarsAreNotRead(t *testing.T) {
os.Setenv("VENEUR_HOSTNAME", "cux")
defer os.Unsetenv("VENEUR_HOSTNAME")

const exampleConfig = `--- api_hostname: :bad`
r := strings.NewReader(exampleConfig)
c, err := readConfig(r)

assert.NotNil(t, err, "Should have encountered parsing error when reading invalid config file")
assert.Equal(t, c, Config{}, "Parsing invalid config file should return zero struct")
}

func TestReadUnknownKeysConfig(t *testing.T) {
const config = `---
no_such_key: 1
Expand All @@ -51,6 +63,23 @@ hostname: foobar
assert.Equal(t, "foobar", c.Hostname)
}

func TestReadUnknownKeysConfig_EnvVarsAreParsed(t *testing.T) {
os.Setenv("VENEUR_HOSTNAME", "cux")
defer os.Unsetenv("VENEUR_HOSTNAME")

const config = `---
no_such_key: 1
hostname: foobar
`
r := strings.NewReader(config)
c, err := readConfig(r)
assert.Error(t, err)
_, ok := err.(*UnknownConfigKeys)
t.Log(err)
assert.True(t, ok, "Returned error should indicate a strictness error")
assert.Equal(t, "cux", c.Hostname)
}

func TestReadUnknownKeysProxyConfig(t *testing.T) {
const config = `---
no_such_key: 1
Expand Down

0 comments on commit ede34b6

Please sign in to comment.