From 8daa459e57c552ec382d6377a3539c738f9f07e3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 23 Jun 2015 22:02:56 -0700 Subject: [PATCH] terraform: tests to check behavior of computed provider configs --- terraform/context_test.go | 71 +++++++++++++++++++ .../apply-provider-computed/main.tf | 9 +++ .../validate-computed-var/main.tf | 9 +++ 3 files changed, 89 insertions(+) create mode 100644 terraform/test-fixtures/apply-provider-computed/main.tf create mode 100644 terraform/test-fixtures/validate-computed-var/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 6275fd8d7..06916cd2f 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -2447,6 +2447,34 @@ func TestContext2Validate_badVar(t *testing.T) { } } +func TestContext2Validate_computedVar(t *testing.T) { + p := testProvider("aws") + m := testModule(t, "validate-computed-var") + c := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + "test": testProviderFuncFixed(testProvider("test")), + }, + }) + + p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { + if !c.IsComputed("value") { + return nil, []error{fmt.Errorf("value isn't computed")} + } + + return nil, c.CheckSet([]string{"value"}) + } + + w, e := c.Validate() + if len(w) > 0 { + t.Fatalf("bad: %#v", w) + } + if len(e) > 0 { + t.Fatalf("bad: %#v", e) + } +} + func TestContext2Validate_countNegative(t *testing.T) { p := testProvider("aws") m := testModule(t, "validate-count-negative") @@ -4583,6 +4611,49 @@ func TestContext2Apply_outputOrphan(t *testing.T) { } } +func TestContext2Apply_providerComputedVar(t *testing.T) { + m := testModule(t, "apply-provider-computed") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + pTest := testProvider("test") + pTest.ApplyFn = testApplyFn + pTest.DiffFn = testDiffFn + + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + "test": testProviderFuncFixed(pTest), + }, + }) + + p.ConfigureFn = func(c *ResourceConfig) error { + if c.IsComputed("value") { + return fmt.Errorf("value is computed") + } + + v, ok := c.Get("value") + if !ok { + return fmt.Errorf("value is not found") + } + if v != "yes" { + return fmt.Errorf("value is not 'yes': %v", v) + } + + return nil + } + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + if _, err := ctx.Apply(); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestContext2Apply_Provisioner_compute(t *testing.T) { m := testModule(t, "apply-provisioner-compute") p := testProvider("aws") diff --git a/terraform/test-fixtures/apply-provider-computed/main.tf b/terraform/test-fixtures/apply-provider-computed/main.tf new file mode 100644 index 000000000..4a39bb546 --- /dev/null +++ b/terraform/test-fixtures/apply-provider-computed/main.tf @@ -0,0 +1,9 @@ +provider "aws" { + value = "${test_instance.foo.value}" +} + +resource "aws_instance" "bar" {} + +resource "test_instance" "foo" { + value = "yes" +} diff --git a/terraform/test-fixtures/validate-computed-var/main.tf b/terraform/test-fixtures/validate-computed-var/main.tf new file mode 100644 index 000000000..4a39bb546 --- /dev/null +++ b/terraform/test-fixtures/validate-computed-var/main.tf @@ -0,0 +1,9 @@ +provider "aws" { + value = "${test_instance.foo.value}" +} + +resource "aws_instance" "bar" {} + +resource "test_instance" "foo" { + value = "yes" +}