From f4faf2274bf4e333eefadee2b0207c57b5e26051 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 16 Aug 2016 13:48:12 -0700 Subject: [PATCH] config: count can't be a SimpleVariable --- config/config.go | 7 ++++++- config/config_test.go | 7 +++++++ .../validate-count-var-unknown/main.tf | 3 +++ terraform/context_validate_test.go | 19 +++++++++++++++++++ .../test-fixtures/validate-bad-count/main.tf | 3 +++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 config/test-fixtures/validate-count-var-unknown/main.tf create mode 100644 terraform/test-fixtures/validate-bad-count/main.tf diff --git a/config/config.go b/config/config.go index fb4b6bf71..54ecd4920 100644 --- a/config/config.go +++ b/config/config.go @@ -468,10 +468,15 @@ func (c *Config) Validate() error { "%s: resource count can't reference resource variable: %s", n, v.FullKey())) + case *SimpleVariable: + errs = append(errs, fmt.Errorf( + "%s: resource count can't reference variable: %s", + n, + v.FullKey())) case *UserVariable: // Good default: - panic("Unknown type in count var: " + n) + panic(fmt.Sprintf("Unknown type in count var in %s: %T", n, v)) } } diff --git a/config/config_test.go b/config/config_test.go index 66f712f67..41690733d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -216,6 +216,13 @@ func TestConfigValidate_countVarInvalid(t *testing.T) { } } +func TestConfigValidate_countVarUnknown(t *testing.T) { + c := testConfig(t, "validate-count-var-unknown") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_dependsOnVar(t *testing.T) { c := testConfig(t, "validate-depends-on-var") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-count-var-unknown/main.tf b/config/test-fixtures/validate-count-var-unknown/main.tf new file mode 100644 index 000000000..7ac5ed417 --- /dev/null +++ b/config/test-fixtures/validate-count-var-unknown/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "foo" { + count = "${list}" +} diff --git a/terraform/context_validate_test.go b/terraform/context_validate_test.go index 3c88fef5d..5d80f0333 100644 --- a/terraform/context_validate_test.go +++ b/terraform/context_validate_test.go @@ -6,6 +6,25 @@ import ( "testing" ) +func TestContext2Validate_badCount(t *testing.T) { + p := testProvider("aws") + m := testModule(t, "validate-bad-count") + c := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + w, e := c.Validate() + if len(w) > 0 { + t.Fatalf("bad: %#v", w) + } + if len(e) == 0 { + t.Fatalf("bad: %#v", e) + } +} + func TestContext2Validate_badVar(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-bad-var") diff --git a/terraform/test-fixtures/validate-bad-count/main.tf b/terraform/test-fixtures/validate-bad-count/main.tf new file mode 100644 index 000000000..a582e5ee3 --- /dev/null +++ b/terraform/test-fixtures/validate-bad-count/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "foo" { + count = "${list}" +}