From 87a9701f91ec974b1e53504f4385f85f7e30d75f Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 20 Jan 2016 11:58:40 -0600 Subject: [PATCH] config: validation error when output is missing value field Also lists out invalid keys in errmsg when they are present Closes #4398 --- config/config.go | 21 ++++++++++++------- config/config_test.go | 7 +++++++ .../validate-child-good/child/main.tf | 2 +- .../validate-output-missing-equals/main.tf | 3 +++ 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 config/test-fixtures/validate-output-missing-equals/main.tf diff --git a/config/config.go b/config/config.go index d31777f6e..d0177c955 100644 --- a/config/config.go +++ b/config/config.go @@ -500,16 +500,23 @@ func (c *Config) Validate() error { // Check that all outputs are valid for _, o := range c.Outputs { - invalid := false - for k, _ := range o.RawConfig.Raw { - if k != "value" { - invalid = true - break + var invalidKeys []string + valueKeyFound := false + for k := range o.RawConfig.Raw { + if k == "value" { + valueKeyFound = true + } else { + invalidKeys = append(invalidKeys, k) } } - if invalid { + if len(invalidKeys) > 0 { errs = append(errs, fmt.Errorf( - "%s: output should only have 'value' field", o.Name)) + "%s: output has invalid keys: %s", + o.Name, strings.Join(invalidKeys, ", "))) + } + if !valueKeyFound { + errs = append(errs, fmt.Errorf( + "%s: output is missing required 'value' key", o.Name)) } for _, v := range o.RawConfig.Variables { diff --git a/config/config_test.go b/config/config_test.go index 2c9791877..a2a15d4bc 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -196,6 +196,13 @@ func TestConfigValidate_outputBadField(t *testing.T) { } } +func TestConfigValidate_outputMissingEquals(t *testing.T) { + c := testConfig(t, "validate-output-missing-equals") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_pathVar(t *testing.T) { c := testConfig(t, "validate-path-var") if err := c.Validate(); err != nil { diff --git a/config/module/test-fixtures/validate-child-good/child/main.tf b/config/module/test-fixtures/validate-child-good/child/main.tf index 2cfd2a80f..09f869aca 100644 --- a/config/module/test-fixtures/validate-child-good/child/main.tf +++ b/config/module/test-fixtures/validate-child-good/child/main.tf @@ -1,3 +1,3 @@ variable "memory" {} -output "result" {} +output "result" { value = "foo" } diff --git a/config/test-fixtures/validate-output-missing-equals/main.tf b/config/test-fixtures/validate-output-missing-equals/main.tf new file mode 100644 index 000000000..8d949a230 --- /dev/null +++ b/config/test-fixtures/validate-output-missing-equals/main.tf @@ -0,0 +1,3 @@ +output "whoops" { + value "wheresmyequals" +}