From 967d4a61c1adbd855a268ee3fa2e5ae3dc45cdfc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 5 Jun 2014 20:17:03 -0700 Subject: [PATCH] terraform: tests that computed placeholders get inserted proprely --- terraform/diff_test.go | 7 ++- terraform/terraform.go | 5 +-- terraform/terraform_test.go | 44 ++++++++++++++++++- terraform/test-fixtures/diff-computed/main.tf | 8 ++++ 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 terraform/test-fixtures/diff-computed/main.tf diff --git a/terraform/diff_test.go b/terraform/diff_test.go index 5150731d9..efc16e29a 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -19,11 +19,16 @@ func testDiffStr(d *Diff) string { r := d.Resources[n] buf.WriteString(fmt.Sprintf("%s\n", n)) for attr, attrDiff := range r { + v := attrDiff.New + if attrDiff.NewComputed { + v = "" + } + buf.WriteString(fmt.Sprintf( " %s: %#v => %#v\n", attr, attrDiff.Old, - attrDiff.New, + v, )) } } diff --git a/terraform/terraform.go b/terraform/terraform.go index 63b439c99..82238b2a6 100644 --- a/terraform/terraform.go +++ b/terraform/terraform.go @@ -106,9 +106,6 @@ func (t *Terraform) diffWalkFn( // Initialize the result diff so we can write to it result.init() - // This is the value that will be used for computed properties - computedId := "computed" - // Initialize the variables for application vars := make(map[string]string) for k, v := range t.variables { @@ -167,7 +164,7 @@ func (t *Terraform) diffWalkFn( result.Resources[r.Id()] = diff.Attributes // Determine the new state and update variables - rs = rs.MergeDiff(diff.Attributes, computedId) + rs = rs.MergeDiff(diff.Attributes, ComputedPlaceholder) for ak, av := range rs.Attributes { vars[fmt.Sprintf("%s.%s", r.Id(), ak)] = av } diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 6af9a1726..44173cfdf 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -202,6 +202,25 @@ func TestTerraformDiff(t *testing.T) { } } +func TestTerraformDiff_computed(t *testing.T) { + tf := testTerraform(t, "diff-computed") + + diff, err := tf.Diff(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + if len(diff.Resources) < 2 { + t.Fatalf("bad: %#v", diff.Resources) + } + + actual := strings.TrimSpace(testDiffStr(diff)) + expected := strings.TrimSpace(testTerraformDiffComputedStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func testConfig(t *testing.T, name string) *config.Config { c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf")) if err != nil { @@ -230,10 +249,25 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory { continue } - diff.Attributes[k] = &ResourceAttrDiff{ + if k == "compute" { + diff.Attributes[v.(string)] = &ResourceAttrDiff{ + Old: "", + New: "", + NewComputed: true, + } + continue + } + + attrDiff := &ResourceAttrDiff{ Old: "", New: v.(string), } + + if strings.Contains(attrDiff.New, ComputedPlaceholder) { + attrDiff.NewComputed = true + } + + diff.Attributes[k] = attrDiff } return diff, nil @@ -326,3 +360,11 @@ aws_instance.bar aws_instance.foo num: "" => "2" ` + +const testTerraformDiffComputedStr = ` +aws_instance.bar + foo: "" => "" +aws_instance.foo + num: "" => "2" + id: "" => "" +` diff --git a/terraform/test-fixtures/diff-computed/main.tf b/terraform/test-fixtures/diff-computed/main.tf new file mode 100644 index 000000000..6f886cb62 --- /dev/null +++ b/terraform/test-fixtures/diff-computed/main.tf @@ -0,0 +1,8 @@ +resource "aws_instance" "foo" { + num = "2" + compute = "id" +} + +resource "aws_instance" "bar" { + foo = "${aws_instance.foo.id}" +}