config/module: validate that parameters into modules valid

This commit is contained in:
Mitchell Hashimoto 2014-09-15 10:32:41 -07:00
parent 2419bf79f2
commit 12e7c75211
6 changed files with 54 additions and 6 deletions

View File

@ -0,0 +1,5 @@
module "child" {
source = "./child"
memory = "foo"
}

View File

@ -1 +1 @@
# Good
variable "memory" {}

View File

@ -1,3 +1,4 @@
module "child" {
source = "./child"
memory = "1G"
}

View File

@ -211,16 +211,20 @@ func (t *Tree) Validate() error {
return fmt.Errorf("tree must be loaded before calling Validate")
}
// If something goes wrong, here is our error template
newErr := &ValidateError{Name: []string{t.Name()}}
// Validate our configuration first.
if err := t.config.Validate(); err != nil {
return &ValidateError{
Name: []string{t.Name()},
Err: err,
}
newErr.Err = err
return newErr
}
// Get the child trees
children := t.Children()
// Validate all our children
for _, c := range t.Children() {
for _, c := range children {
err := c.Validate()
if err == nil {
continue
@ -237,6 +241,32 @@ func (t *Tree) Validate() error {
return verr
}
// Go over all the modules and verify that any parameters are valid
// variables into the module in question.
for _, m := range t.config.Modules {
tree, ok := children[m.Name]
if !ok {
// This should never happen because Load watches us
panic("module not found in children: " + m.Name)
}
// Build the variables that the module defines
varMap := make(map[string]struct{})
for _, v := range tree.config.Variables {
varMap[v.Name] = struct{}{}
}
// Compare to the keys in our raw config for the module
for k, _ := range m.RawConfig.Raw {
if _, ok := varMap[k]; !ok {
newErr.Err = fmt.Errorf(
"module %s: %s is not a valid parameter",
m.Name, k)
return newErr
}
}
}
return nil
}

View File

@ -92,6 +92,18 @@ func TestTreeValidate_badChild(t *testing.T) {
}
}
func TestTreeValidate_badChildVar(t *testing.T) {
tree := NewTree(testConfig(t, "validate-bad-var"))
if err := tree.Load(testStorage(t), GetModeGet); err != nil {
t.Fatalf("err: %s", err)
}
if err := tree.Validate(); err == nil {
t.Fatal("should error")
}
}
func TestTreeValidate_badRoot(t *testing.T) {
tree := NewTree(testConfig(t, "validate-root-bad"))