create a downstream failure from a computed value

These are the largest source of the old "diffs didn't match after apply"
errors. It's almost always an upstream dependency that caused the final
error.
This commit is contained in:
James Bardin 2019-04-03 17:36:08 -04:00
parent 51ddc554f5
commit f52a6630f5
2 changed files with 62 additions and 5 deletions

View File

@ -19,7 +19,7 @@ func testResource() *schema.Resource {
},
CustomizeDiff: func(d *schema.ResourceDiff, _ interface{}) error {
if d.HasChange("required") {
if d.HasChange("optional") {
d.SetNewComputed("planned_computed")
}
return nil
@ -176,7 +176,7 @@ func testResourceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("computed_list", []string{"listval1", "listval2"})
d.Set("computed_set", []string{"setval1", "setval2"})
d.Set("planned_computed", d.Get("required"))
d.Set("planned_computed", d.Get("optional"))
// if there is no "set" value, erroneously set it to an empty set. This
// might change a null value to an empty set, but we should be able to

View File

@ -851,21 +851,23 @@ resource "test_resource" "foo" {
required_map = {
key = "value"
}
optional = "hi"
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource.foo", "planned_computed", "ok",
"test_resource.foo", "planned_computed", "hi",
),
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "changed"
required = "ok"
required_map = {
key = "value"
}
optional = "changed"
}
`),
Check: resource.ComposeTestCheckFunc(
@ -916,3 +918,58 @@ func TestDiffApply_map(t *testing.T) {
t.Fatalf("expected:%#v got:%#v", expect, newAttrs)
}
}
func TestResource_dependsComputed(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
variable "change" {
default = false
}
resource "test_resource" "foo" {
required = "ok"
required_map = {
key = "value"
}
optional = var.change ? "after" : ""
}
resource "test_resource" "bar" {
count = var.change ? 1 : 0
required = test_resource.foo.planned_computed
required_map = {
key = "value"
}
}
`),
},
resource.TestStep{
Config: strings.TrimSpace(`
variable "change" {
default = true
}
resource "test_resource" "foo" {
required = "ok"
required_map = {
key = "value"
}
optional = var.change ? "after" : ""
}
resource "test_resource" "bar" {
count = var.change ? 1 : 0
required = test_resource.foo.planned_computed
required_map = {
key = "value"
}
}
`),
},
},
})
}