github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/builtin/providers/test/resource_diff_suppress_test.go (about) 1 package test 2 3 import ( 4 "errors" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/addrs" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestResourceDiffSuppress_create(t *testing.T) { 15 resource.UnitTest(t, resource.TestCase{ 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckResourceDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: strings.TrimSpace(` 21 resource "test_resource_diff_suppress" "foo" { 22 val_to_upper = "foo" 23 } 24 `), 25 }, 26 }, 27 }) 28 } 29 func TestResourceDiffSuppress_update(t *testing.T) { 30 resource.UnitTest(t, resource.TestCase{ 31 Providers: testAccProviders, 32 CheckDestroy: testAccCheckResourceDestroy, 33 Steps: []resource.TestStep{ 34 resource.TestStep{ 35 Config: strings.TrimSpace(` 36 resource "test_resource_diff_suppress" "foo" { 37 val_to_upper = "foo" 38 } 39 `), 40 }, 41 resource.TestStep{ 42 Config: strings.TrimSpace(` 43 resource "test_resource_diff_suppress" "foo" { 44 val_to_upper = "bar" 45 optional = "more" 46 } 47 `), 48 }, 49 }, 50 }) 51 } 52 53 func TestResourceDiffSuppress_updateIgnoreChanges(t *testing.T) { 54 // None of these steps should replace the instance 55 id := "" 56 checkFunc := func(s *terraform.State) error { 57 root := s.ModuleByPath(addrs.RootModuleInstance) 58 res := root.Resources["test_resource_diff_suppress.foo"] 59 if id != "" && res.Primary.ID != id { 60 return errors.New("expected no resource replacement") 61 } 62 id = res.Primary.ID 63 return nil 64 } 65 66 resource.UnitTest(t, resource.TestCase{ 67 Providers: testAccProviders, 68 CheckDestroy: testAccCheckResourceDestroy, 69 Steps: []resource.TestStep{ 70 resource.TestStep{ 71 Config: strings.TrimSpace(` 72 resource "test_resource_diff_suppress" "foo" { 73 val_to_upper = "foo" 74 75 network = "foo" 76 subnetwork = "foo" 77 78 node_pool { 79 name = "default-pool" 80 } 81 lifecycle { 82 ignore_changes = ["node_pool"] 83 } 84 } 85 `), 86 Check: checkFunc, 87 }, 88 resource.TestStep{ 89 Config: strings.TrimSpace(` 90 resource "test_resource_diff_suppress" "foo" { 91 val_to_upper = "foo" 92 93 network = "ignored" 94 subnetwork = "ignored" 95 96 node_pool { 97 name = "default-pool" 98 } 99 lifecycle { 100 ignore_changes = ["node_pool"] 101 } 102 } 103 `), 104 Check: checkFunc, 105 }, 106 resource.TestStep{ 107 Config: strings.TrimSpace(` 108 resource "test_resource_diff_suppress" "foo" { 109 val_to_upper = "foo" 110 111 network = "ignored" 112 subnetwork = "ignored" 113 114 node_pool { 115 name = "ignored" 116 } 117 lifecycle { 118 ignore_changes = ["node_pool"] 119 } 120 } 121 `), 122 Check: checkFunc, 123 }, 124 }, 125 }) 126 }