From 79a9dfce4a4184336783f9e758819058755597d5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 24 Jul 2014 20:16:15 -0700 Subject: [PATCH] terraform: add test for diff var changing --- terraform/context_test.go | 49 ++++++++++++++++++++ terraform/terraform_test.go | 16 +++++++ terraform/test-fixtures/plan-diffvar/main.tf | 7 +++ 3 files changed, 72 insertions(+) create mode 100644 terraform/test-fixtures/plan-diffvar/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 7af65afec..9e1981158 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1464,6 +1464,55 @@ func TestContextPlan_destroy(t *testing.T) { } } +func TestContextPlan_diffVar(t *testing.T) { + c := testConfig(t, "plan-diffvar") + p := testProvider("aws") + s := &State{ + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + ID: "bar", + Attributes: map[string]string{ + "num": "2", + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Config: c, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + p.DiffFn = func( + s *ResourceState, c *ResourceConfig) (*ResourceDiff, error) { + if s.ID != "bar" { + return testDiffFn(s, c) + } + + return &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "num": &ResourceAttrDiff{ + Old: "2", + New: "3", + }, + }, + }, nil + } + + plan, err := ctx.Plan(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanDiffVarStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContextPlan_hook(t *testing.T) { c := testConfig(t, "plan-good") h := new(MockHook) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 05be1c85a..ecd0b232d 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -378,6 +378,22 @@ aws_instance.two: ID = baz ` +const testTerraformPlanDiffVarStr = ` +DIFF: + +CREATE: aws_instance.bar + num: "" => "3" + type: "" => "aws_instance" +UPDATE: aws_instance.foo + num: "2" => "3" + +STATE: + +aws_instance.foo: + ID = bar + num = 2 +` + const testTerraformPlanEmptyStr = ` DIFF: diff --git a/terraform/test-fixtures/plan-diffvar/main.tf b/terraform/test-fixtures/plan-diffvar/main.tf new file mode 100644 index 000000000..1dbc7c981 --- /dev/null +++ b/terraform/test-fixtures/plan-diffvar/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" { + num = "2" +} + +resource "aws_instance" "bar" { + num = "${aws_instance.foo.num}" +}