Skip to content

Commit

Permalink
verify DiffSuppresFunc behavior
Browse files Browse the repository at this point in the history
Terraform used to provide empty diffs to the provider when calculating
`ignore_changes`, which would cause some DiffSuppressFunc to fail, as
can be seen in #18209.

Verify that this is no longer the case in 0.12
  • Loading branch information
jbardin committed Nov 16, 2018
1 parent 5a7e5f2 commit 2d2283e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
29 changes: 25 additions & 4 deletions builtin/providers/test/resource_diff_suppress.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package test

import (
"fmt"
"math/rand"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)

func testResourceDiffSuppress() *schema.Resource {
diffSuppress := func(k, old, new string, d *schema.ResourceData) bool {
if strings.Contains(new, "replace") {
if old == "" || strings.Contains(new, "replace") {
return false
}

return true
}

Expand Down Expand Up @@ -55,14 +56,34 @@ func testResourceDiffSuppress() *schema.Resource {
ForceNew: true,
DiffSuppressFunc: diffSuppress,
},

"node_pool": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
},
},
},
}
}

func testResourceDiffSuppressCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId("testId")
d.Set("network", "modified")
d.Set("subnetwork", "modified")

return testResourceRead(d, meta)
id := fmt.Sprintf("%x", rand.Int63())
d.SetId(id)
return nil
}

func testResourceDiffSuppressRead(d *schema.ResourceData, meta interface{}) error {
Expand Down
79 changes: 79 additions & 0 deletions builtin/providers/test/resource_diff_suppress_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package test

import (
"errors"
"strings"
"testing"

"github.com/hashicorp/terraform/addrs"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestResourceDiffSuppress_create(t *testing.T) {
Expand Down Expand Up @@ -45,3 +49,78 @@ resource "test_resource_diff_suppress" "foo" {
},
})
}

func TestResourceDiffSuppress_updateIgnoreChanges(t *testing.T) {
// None of these steps should replace the instance
id := ""
checkFunc := func(s *terraform.State) error {
root := s.ModuleByPath(addrs.RootModuleInstance)
res := root.Resources["test_resource_diff_suppress.foo"]
if id != "" && res.Primary.ID != id {
return errors.New("expected no resource replacement")
}
id = res.Primary.ID
return nil
}

resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_diff_suppress" "foo" {
val_to_upper = "foo"
network = "foo"
subnetwork = "foo"
node_pool {
name = "default-pool"
}
lifecycle {
ignore_changes = ["node_pool"]
}
}
`),
Check: checkFunc,
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_diff_suppress" "foo" {
val_to_upper = "foo"
network = "ignored"
subnetwork = "ignored"
node_pool {
name = "default-pool"
}
lifecycle {
ignore_changes = ["node_pool"]
}
}
`),
Check: checkFunc,
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_diff_suppress" "foo" {
val_to_upper = "foo"
network = "ignored"
subnetwork = "ignored"
node_pool {
name = "ignored"
}
lifecycle {
ignore_changes = ["node_pool"]
}
}
`),
Check: checkFunc,
},
},
})
}

0 comments on commit 2d2283e

Please sign in to comment.