github.com/rhenning/terraform@v0.8.0-beta2/terraform/transform_destroy_edge_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestDestroyEdgeTransformer_basic(t *testing.T) { 9 g := Graph{Path: RootModulePath} 10 g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) 11 g.Add(&graphNodeDestroyerTest{AddrString: "test.B"}) 12 tf := &DestroyEdgeTransformer{ 13 Module: testModule(t, "transform-destroy-edge-basic"), 14 } 15 if err := tf.Transform(&g); err != nil { 16 t.Fatalf("err: %s", err) 17 } 18 19 actual := strings.TrimSpace(g.String()) 20 expected := strings.TrimSpace(testTransformDestroyEdgeBasicStr) 21 if actual != expected { 22 t.Fatalf("bad:\n\n%s", actual) 23 } 24 } 25 26 func TestDestroyEdgeTransformer_create(t *testing.T) { 27 g := Graph{Path: RootModulePath} 28 g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) 29 g.Add(&graphNodeDestroyerTest{AddrString: "test.B"}) 30 g.Add(&graphNodeCreatorTest{AddrString: "test.A"}) 31 tf := &DestroyEdgeTransformer{ 32 Module: testModule(t, "transform-destroy-edge-basic"), 33 } 34 if err := tf.Transform(&g); err != nil { 35 t.Fatalf("err: %s", err) 36 } 37 38 actual := strings.TrimSpace(g.String()) 39 expected := strings.TrimSpace(testTransformDestroyEdgeCreatorStr) 40 if actual != expected { 41 t.Fatalf("bad:\n\n%s", actual) 42 } 43 } 44 45 func TestDestroyEdgeTransformer_multi(t *testing.T) { 46 g := Graph{Path: RootModulePath} 47 g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) 48 g.Add(&graphNodeDestroyerTest{AddrString: "test.B"}) 49 g.Add(&graphNodeDestroyerTest{AddrString: "test.C"}) 50 tf := &DestroyEdgeTransformer{ 51 Module: testModule(t, "transform-destroy-edge-multi"), 52 } 53 if err := tf.Transform(&g); err != nil { 54 t.Fatalf("err: %s", err) 55 } 56 57 actual := strings.TrimSpace(g.String()) 58 expected := strings.TrimSpace(testTransformDestroyEdgeMultiStr) 59 if actual != expected { 60 t.Fatalf("bad:\n\n%s", actual) 61 } 62 } 63 64 func TestDestroyEdgeTransformer_selfRef(t *testing.T) { 65 g := Graph{Path: RootModulePath} 66 g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) 67 tf := &DestroyEdgeTransformer{ 68 Module: testModule(t, "transform-destroy-edge-self-ref"), 69 } 70 if err := tf.Transform(&g); err != nil { 71 t.Fatalf("err: %s", err) 72 } 73 74 actual := strings.TrimSpace(g.String()) 75 expected := strings.TrimSpace(testTransformDestroyEdgeSelfRefStr) 76 if actual != expected { 77 t.Fatalf("bad:\n\n%s", actual) 78 } 79 } 80 81 func TestDestroyEdgeTransformer_module(t *testing.T) { 82 g := Graph{Path: RootModulePath} 83 g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"}) 84 g.Add(&graphNodeDestroyerTest{AddrString: "aws_instance.a"}) 85 tf := &DestroyEdgeTransformer{ 86 Module: testModule(t, "transform-destroy-edge-module"), 87 } 88 if err := tf.Transform(&g); err != nil { 89 t.Fatalf("err: %s", err) 90 } 91 92 actual := strings.TrimSpace(g.String()) 93 expected := strings.TrimSpace(testTransformDestroyEdgeModuleStr) 94 if actual != expected { 95 t.Fatalf("bad:\n\n%s", actual) 96 } 97 } 98 99 type graphNodeCreatorTest struct { 100 AddrString string 101 } 102 103 func (n *graphNodeCreatorTest) Name() string { return n.CreateAddr().String() } 104 func (n *graphNodeCreatorTest) CreateAddr() *ResourceAddress { 105 addr, err := ParseResourceAddress(n.AddrString) 106 if err != nil { 107 panic(err) 108 } 109 110 return addr 111 } 112 113 type graphNodeDestroyerTest struct { 114 AddrString string 115 CBD bool 116 } 117 118 func (n *graphNodeDestroyerTest) Name() string { return n.DestroyAddr().String() + " (destroy)" } 119 func (n *graphNodeDestroyerTest) CreateBeforeDestroy() bool { return n.CBD } 120 func (n *graphNodeDestroyerTest) DestroyAddr() *ResourceAddress { 121 addr, err := ParseResourceAddress(n.AddrString) 122 if err != nil { 123 panic(err) 124 } 125 126 return addr 127 } 128 129 const testTransformDestroyEdgeBasicStr = ` 130 test.A (destroy) 131 test.B (destroy) 132 test.B (destroy) 133 ` 134 135 const testTransformDestroyEdgeCreatorStr = ` 136 test.A 137 test.A (destroy) 138 test.A (destroy) 139 test.B (destroy) 140 test.B (destroy) 141 ` 142 143 const testTransformDestroyEdgeMultiStr = ` 144 test.A (destroy) 145 test.B (destroy) 146 test.C (destroy) 147 test.B (destroy) 148 test.C (destroy) 149 test.C (destroy) 150 ` 151 152 const testTransformDestroyEdgeSelfRefStr = ` 153 test.A (destroy) 154 ` 155 156 const testTransformDestroyEdgeModuleStr = ` 157 aws_instance.a (destroy) 158 module.child.aws_instance.b (destroy) 159 aws_instance.a (destroy) 160 `