terraform: tests that computed placeholders get inserted proprely

This commit is contained in:
Mitchell Hashimoto 2014-06-05 20:17:03 -07:00
parent 796dfd54e9
commit 967d4a61c1
4 changed files with 58 additions and 6 deletions

View File

@ -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 = "<computed>"
}
buf.WriteString(fmt.Sprintf(
" %s: %#v => %#v\n",
attr,
attrDiff.Old,
attrDiff.New,
v,
))
}
}

View File

@ -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
}

View File

@ -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: "" => "<computed>"
aws_instance.foo
num: "" => "2"
id: "" => "<computed>"
`

View File

@ -0,0 +1,8 @@
resource "aws_instance" "foo" {
num = "2"
compute = "id"
}
resource "aws_instance" "bar" {
foo = "${aws_instance.foo.id}"
}