From ede34b6fd0b4a2b8dd17eb62f269dd4191c8dd81 Mon Sep 17 00:00:00 2001 From: Luis Alfonso Higuera Gamboa Date: Fri, 21 Jun 2019 17:09:33 -0600 Subject: [PATCH] Use environment variables when error is strict ** 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 --- config_parse.go | 2 +- config_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/config_parse.go b/config_parse.go index a3f20b6b7..f5f3bcd30 100644 --- a/config_parse.go +++ b/config_parse.go @@ -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 } } diff --git a/config_test.go b/config_test.go index c08287ed6..19274b636 100644 --- a/config_test.go +++ b/config_test.go @@ -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 @@ -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