From b8a0a0221748abd63efa05612913bfa12e3e098b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 21 Jul 2014 08:34:44 -0700 Subject: [PATCH] config: TODO tests for validation --- config/config.go | 3 +++ config/config_test.go | 18 ++++++++++++++++++ config/loader_libucl.go | 6 ++++-- .../validate-var-default-bad-type/main.tf | 3 +++ .../test-fixtures/validate-var-default/main.tf | 9 +++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 config/test-fixtures/validate-var-default-bad-type/main.tf create mode 100644 config/test-fixtures/validate-var-default/main.tf diff --git a/config/config.go b/config/config.go index 2083c7fc7..656f33a62 100644 --- a/config/config.go +++ b/config/config.go @@ -131,6 +131,9 @@ func (c *Config) Validate() error { varMap[v.Name] = v } + // TODO(mitchellh): Validate that variable defaults are only a string + // or mapping of strings. + // Check for references to user variables that do not actually // exist and record those errors. for source, vs := range vars { diff --git a/config/config_test.go b/config/config_test.go index 4c66466b1..f4a2ce0d7 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -64,6 +64,24 @@ func TestConfigValidate_unknownVar(t *testing.T) { } } +func TestConfigValidate_varDefault(t *testing.T) { + c := testConfig(t, "validate-var-default") + if err := c.Validate(); err != nil { + t.Fatalf("should be valid: %s", err) + } +} + +func TestConfigValidate_varDefaultBadType(t *testing.T) { + t.Skip() + + // TODO(mitchellh): FIX + + c := testConfig(t, "validate-var-default-bad-type") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestNewResourceVariable(t *testing.T) { v, err := NewResourceVariable("foo.bar.baz") if err != nil { diff --git a/config/loader_libucl.go b/config/loader_libucl.go index 398c2f5fd..176c588bd 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -51,11 +51,13 @@ func (t *libuclConfigurable) Config() (*Config, error) { if len(rawConfig.Variable) > 0 { config.Variables = make([]*Variable, 0, len(rawConfig.Variable)) for k, v := range rawConfig.Variable { - config.Variables = append(config.Variables, &Variable{ + newVar := &Variable{ Name: k, Default: v.Default, Description: v.Description, - }) + } + + config.Variables = append(config.Variables, newVar) } } diff --git a/config/test-fixtures/validate-var-default-bad-type/main.tf b/config/test-fixtures/validate-var-default-bad-type/main.tf new file mode 100644 index 000000000..c7975db38 --- /dev/null +++ b/config/test-fixtures/validate-var-default-bad-type/main.tf @@ -0,0 +1,3 @@ +variable "foo" { + default = ["foo", "bar"] +} diff --git a/config/test-fixtures/validate-var-default/main.tf b/config/test-fixtures/validate-var-default/main.tf new file mode 100644 index 000000000..75a6bc9e0 --- /dev/null +++ b/config/test-fixtures/validate-var-default/main.tf @@ -0,0 +1,9 @@ +variable "foo" { + default = "bar" +} + +variable "foo" { + default = { + "foo" = "bar" + } +}