config: validation error when output is missing value field

Also lists out invalid keys in errmsg when they are present

Closes #4398
This commit is contained in:
Paul Hinze 2016-01-20 11:58:40 -06:00
parent 2dff310b8b
commit 87a9701f91
4 changed files with 25 additions and 8 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -1,3 +1,3 @@
variable "memory" {}
output "result" {}
output "result" { value = "foo" }

View File

@ -0,0 +1,3 @@
output "whoops" {
value "wheresmyequals"
}