github.com/opsidian/terraform@v0.7.8-0.20161104123224-27c39cdfba5b/terraform/transform_destroy_edge_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestDestroyEdgeTransformer(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 type graphNodeCreatorTest struct { 65 AddrString string 66 } 67 68 func (n *graphNodeCreatorTest) Name() string { return n.CreateAddr().String() } 69 func (n *graphNodeCreatorTest) CreateAddr() *ResourceAddress { 70 addr, err := ParseResourceAddress(n.AddrString) 71 if err != nil { 72 panic(err) 73 } 74 75 return addr 76 } 77 78 type graphNodeDestroyerTest struct { 79 AddrString string 80 CBD bool 81 } 82 83 func (n *graphNodeDestroyerTest) Name() string { return n.DestroyAddr().String() + " (destroy)" } 84 func (n *graphNodeDestroyerTest) CreateBeforeDestroy() bool { return n.CBD } 85 func (n *graphNodeDestroyerTest) DestroyAddr() *ResourceAddress { 86 addr, err := ParseResourceAddress(n.AddrString) 87 if err != nil { 88 panic(err) 89 } 90 91 return addr 92 } 93 94 const testTransformDestroyEdgeBasicStr = ` 95 test.A (destroy) 96 test.B (destroy) 97 test.B (destroy) 98 ` 99 100 const testTransformDestroyEdgeCreatorStr = ` 101 test.A 102 test.A (destroy) 103 test.A (destroy) 104 test.B (destroy) 105 test.B (destroy) 106 ` 107 108 const testTransformDestroyEdgeMultiStr = ` 109 test.A (destroy) 110 test.B (destroy) 111 test.B (destroy) 112 test.C (destroy) 113 test.C (destroy) 114 `