mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
verify DiffSuppresFunc behavior
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
This commit is contained in:
parent
17ecda53b5
commit
a681124301
@ -1,23 +1,36 @@
|
||||
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 old == "" || strings.Contains(new, "replace") {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
return &schema.Resource{
|
||||
Create: testResourceDiffSuppressCreate,
|
||||
Read: testResourceDiffSuppressRead,
|
||||
Update: testResourceDiffSuppressUpdate,
|
||||
Delete: testResourceDiffSuppressDelete,
|
||||
Update: testResourceDiffSuppressUpdate,
|
||||
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"optional": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"val_to_upper": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
@ -29,18 +42,48 @@ func testResourceDiffSuppress() *schema.Resource {
|
||||
return strings.ToUpper(old) == strings.ToUpper(new)
|
||||
},
|
||||
},
|
||||
"optional": {
|
||||
Type: schema.TypeString,
|
||||
"network": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Default: "default",
|
||||
ForceNew: true,
|
||||
DiffSuppressFunc: diffSuppress,
|
||||
},
|
||||
"subnetwork": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
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 {
|
||||
|
@ -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) {
|
||||
@ -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,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user