config: validate module names are valid

This commit is contained in:
Mitchell Hashimoto 2014-10-08 16:03:22 -07:00
parent 9ed89dbabd
commit 67d9188a29
3 changed files with 19 additions and 2 deletions

View File

@ -241,9 +241,9 @@ func (c *Config) Validate() error {
} }
} }
// If we haven't seen this module before, check that the
// source has no interpolations.
if _, ok := modules[m.Id()]; !ok { if _, ok := modules[m.Id()]; !ok {
// If we haven't seen this module before, check that the
// source has no interpolations.
rc, err := NewRawConfig(map[string]interface{}{ rc, err := NewRawConfig(map[string]interface{}{
"root": m.Source, "root": m.Source,
}) })
@ -256,6 +256,13 @@ func (c *Config) Validate() error {
"%s: module source cannot contain interpolations", "%s: module source cannot contain interpolations",
m.Id())) m.Id()))
} }
// Check that the name matches our regexp
if !NameRegexp.Match([]byte(m.Name)) {
errs = append(errs, fmt.Errorf(
"%s: module name cannot contain special characters",
m.Id()))
}
} }
modules[m.Id()] = m modules[m.Id()] = m

View File

@ -123,6 +123,13 @@ func TestConfigValidate_dupResource(t *testing.T) {
} }
} }
func TestConfigValidate_moduleNameBad(t *testing.T) {
c := testConfig(t, "validate-module-name-bad")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_moduleSourceVar(t *testing.T) { func TestConfigValidate_moduleSourceVar(t *testing.T) {
c := testConfig(t, "validate-module-source-var") c := testConfig(t, "validate-module-source-var")
if err := c.Validate(); err == nil { if err := c.Validate(); err == nil {

View File

@ -0,0 +1,3 @@
module "foo bar" {
source = "foo"
}