github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/transform_destroy_edge_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/terraform-plugin-sdk/internal/addrs" 8 ) 9 10 func TestDestroyEdgeTransformer_basic(t *testing.T) { 11 g := Graph{Path: addrs.RootModuleInstance} 12 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.A"}) 13 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.B"}) 14 tf := &DestroyEdgeTransformer{ 15 Config: testModule(t, "transform-destroy-edge-basic"), 16 Schemas: simpleTestSchemas(), 17 } 18 if err := tf.Transform(&g); err != nil { 19 t.Fatalf("err: %s", err) 20 } 21 22 actual := strings.TrimSpace(g.String()) 23 expected := strings.TrimSpace(testTransformDestroyEdgeBasicStr) 24 if actual != expected { 25 t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) 26 } 27 } 28 29 func TestDestroyEdgeTransformer_create(t *testing.T) { 30 g := Graph{Path: addrs.RootModuleInstance} 31 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.A"}) 32 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.B"}) 33 g.Add(&graphNodeCreatorTest{AddrString: "test_object.A"}) 34 tf := &DestroyEdgeTransformer{ 35 Config: testModule(t, "transform-destroy-edge-basic"), 36 Schemas: simpleTestSchemas(), 37 } 38 if err := tf.Transform(&g); err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 42 actual := strings.TrimSpace(g.String()) 43 expected := strings.TrimSpace(testTransformDestroyEdgeCreatorStr) 44 if actual != expected { 45 t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) 46 } 47 } 48 49 func TestDestroyEdgeTransformer_multi(t *testing.T) { 50 g := Graph{Path: addrs.RootModuleInstance} 51 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.A"}) 52 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.B"}) 53 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.C"}) 54 tf := &DestroyEdgeTransformer{ 55 Config: testModule(t, "transform-destroy-edge-multi"), 56 Schemas: simpleTestSchemas(), 57 } 58 if err := tf.Transform(&g); err != nil { 59 t.Fatalf("err: %s", err) 60 } 61 62 actual := strings.TrimSpace(g.String()) 63 expected := strings.TrimSpace(testTransformDestroyEdgeMultiStr) 64 if actual != expected { 65 t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) 66 } 67 } 68 69 func TestDestroyEdgeTransformer_selfRef(t *testing.T) { 70 g := Graph{Path: addrs.RootModuleInstance} 71 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.A"}) 72 tf := &DestroyEdgeTransformer{ 73 Config: testModule(t, "transform-destroy-edge-self-ref"), 74 Schemas: simpleTestSchemas(), 75 } 76 if err := tf.Transform(&g); err != nil { 77 t.Fatalf("err: %s", err) 78 } 79 80 actual := strings.TrimSpace(g.String()) 81 expected := strings.TrimSpace(testTransformDestroyEdgeSelfRefStr) 82 if actual != expected { 83 t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) 84 } 85 } 86 87 func TestDestroyEdgeTransformer_module(t *testing.T) { 88 g := Graph{Path: addrs.RootModuleInstance} 89 g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.b"}) 90 g.Add(&graphNodeDestroyerTest{AddrString: "test_object.a"}) 91 tf := &DestroyEdgeTransformer{ 92 Config: testModule(t, "transform-destroy-edge-module"), 93 Schemas: simpleTestSchemas(), 94 } 95 if err := tf.Transform(&g); err != nil { 96 t.Fatalf("err: %s", err) 97 } 98 99 actual := strings.TrimSpace(g.String()) 100 expected := strings.TrimSpace(testTransformDestroyEdgeModuleStr) 101 if actual != expected { 102 t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) 103 } 104 } 105 106 func TestDestroyEdgeTransformer_moduleOnly(t *testing.T) { 107 g := Graph{Path: addrs.RootModuleInstance} 108 g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.a"}) 109 g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.b"}) 110 g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.c"}) 111 tf := &DestroyEdgeTransformer{ 112 Config: testModule(t, "transform-destroy-edge-module-only"), 113 Schemas: simpleTestSchemas(), 114 } 115 if err := tf.Transform(&g); err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 actual := strings.TrimSpace(g.String()) 120 expected := strings.TrimSpace(` 121 module.child.test_object.a (destroy) 122 module.child.test_object.b (destroy) 123 module.child.test_object.c (destroy) 124 module.child.test_object.b (destroy) 125 module.child.test_object.c (destroy) 126 module.child.test_object.c (destroy) 127 `) 128 if actual != expected { 129 t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) 130 } 131 } 132 133 type graphNodeCreatorTest struct { 134 AddrString string 135 Refs []string 136 } 137 138 var ( 139 _ GraphNodeCreator = (*graphNodeCreatorTest)(nil) 140 _ GraphNodeReferencer = (*graphNodeCreatorTest)(nil) 141 ) 142 143 func (n *graphNodeCreatorTest) Name() string { 144 return n.CreateAddr().String() 145 } 146 147 func (n *graphNodeCreatorTest) mustAddr() addrs.AbsResourceInstance { 148 addr, diags := addrs.ParseAbsResourceInstanceStr(n.AddrString) 149 if diags.HasErrors() { 150 panic(diags.Err()) 151 } 152 return addr 153 } 154 155 func (n *graphNodeCreatorTest) Path() addrs.ModuleInstance { 156 return n.mustAddr().Module 157 } 158 159 func (n *graphNodeCreatorTest) CreateAddr() *addrs.AbsResourceInstance { 160 addr := n.mustAddr() 161 return &addr 162 } 163 164 func (n *graphNodeCreatorTest) References() []*addrs.Reference { 165 ret := make([]*addrs.Reference, len(n.Refs)) 166 for i, str := range n.Refs { 167 ref, diags := addrs.ParseRefStr(str) 168 if diags.HasErrors() { 169 panic(diags.Err()) 170 } 171 ret[i] = ref 172 } 173 return ret 174 } 175 176 type graphNodeDestroyerTest struct { 177 AddrString string 178 CBD bool 179 Modified bool 180 } 181 182 var _ GraphNodeDestroyer = (*graphNodeDestroyerTest)(nil) 183 184 func (n *graphNodeDestroyerTest) Name() string { 185 result := n.DestroyAddr().String() + " (destroy)" 186 if n.Modified { 187 result += " (modified)" 188 } 189 190 return result 191 } 192 193 func (n *graphNodeDestroyerTest) mustAddr() addrs.AbsResourceInstance { 194 addr, diags := addrs.ParseAbsResourceInstanceStr(n.AddrString) 195 if diags.HasErrors() { 196 panic(diags.Err()) 197 } 198 return addr 199 } 200 201 func (n *graphNodeDestroyerTest) CreateBeforeDestroy() bool { 202 return n.CBD 203 } 204 205 func (n *graphNodeDestroyerTest) ModifyCreateBeforeDestroy(v bool) error { 206 n.Modified = true 207 return nil 208 } 209 210 func (n *graphNodeDestroyerTest) DestroyAddr() *addrs.AbsResourceInstance { 211 addr := n.mustAddr() 212 return &addr 213 } 214 215 const testTransformDestroyEdgeBasicStr = ` 216 test_object.A (destroy) 217 test_object.B (destroy) 218 test_object.B (destroy) 219 ` 220 221 const testTransformDestroyEdgeCreatorStr = ` 222 test_object.A 223 test_object.A (destroy) 224 test_object.A (destroy) 225 test_object.B (destroy) 226 test_object.B (destroy) 227 ` 228 229 const testTransformDestroyEdgeMultiStr = ` 230 test_object.A (destroy) 231 test_object.B (destroy) 232 test_object.C (destroy) 233 test_object.B (destroy) 234 test_object.C (destroy) 235 test_object.C (destroy) 236 ` 237 238 const testTransformDestroyEdgeSelfRefStr = ` 239 test_object.A (destroy) 240 ` 241 242 const testTransformDestroyEdgeModuleStr = ` 243 module.child.test_object.b (destroy) 244 test_object.a (destroy) 245 test_object.a (destroy) 246 `