2016-09-20 12:16:49 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-11-08 11:35:57 -06:00
|
|
|
func TestDestroyEdgeTransformer_basic(t *testing.T) {
|
2016-09-20 12:16:49 -05:00
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
|
|
|
|
tf := &DestroyEdgeTransformer{
|
|
|
|
Module: testModule(t, "transform-destroy-edge-basic"),
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeBasicStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
func TestDestroyEdgeTransformer_create(t *testing.T) {
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
|
|
|
|
g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
|
|
|
|
tf := &DestroyEdgeTransformer{
|
|
|
|
Module: testModule(t, "transform-destroy-edge-basic"),
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeCreatorStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 12:18:31 -05:00
|
|
|
func TestDestroyEdgeTransformer_multi(t *testing.T) {
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.C"})
|
|
|
|
tf := &DestroyEdgeTransformer{
|
|
|
|
Module: testModule(t, "transform-destroy-edge-multi"),
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeMultiStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:27:33 -06:00
|
|
|
func TestDestroyEdgeTransformer_selfRef(t *testing.T) {
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
|
|
tf := &DestroyEdgeTransformer{
|
|
|
|
Module: testModule(t, "transform-destroy-edge-self-ref"),
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeSelfRefStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:29:19 -06:00
|
|
|
func TestDestroyEdgeTransformer_module(t *testing.T) {
|
|
|
|
g := Graph{Path: RootModulePath}
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "aws_instance.a"})
|
|
|
|
tf := &DestroyEdgeTransformer{
|
|
|
|
Module: testModule(t, "transform-destroy-edge-module"),
|
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeModuleStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
type graphNodeCreatorTest struct {
|
|
|
|
AddrString string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeCreatorTest) Name() string { return n.CreateAddr().String() }
|
|
|
|
func (n *graphNodeCreatorTest) CreateAddr() *ResourceAddress {
|
|
|
|
addr, err := ParseResourceAddress(n.AddrString)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2016-09-20 12:16:49 -05:00
|
|
|
type graphNodeDestroyerTest struct {
|
|
|
|
AddrString string
|
2016-09-21 12:55:07 -05:00
|
|
|
CBD bool
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
func (n *graphNodeDestroyerTest) Name() string { return n.DestroyAddr().String() + " (destroy)" }
|
|
|
|
func (n *graphNodeDestroyerTest) CreateBeforeDestroy() bool { return n.CBD }
|
2016-09-20 12:16:49 -05:00
|
|
|
func (n *graphNodeDestroyerTest) DestroyAddr() *ResourceAddress {
|
|
|
|
addr, err := ParseResourceAddress(n.AddrString)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
const testTransformDestroyEdgeBasicStr = `
|
|
|
|
test.A (destroy)
|
|
|
|
test.B (destroy)
|
|
|
|
test.B (destroy)
|
|
|
|
`
|
2016-09-20 12:18:31 -05:00
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
const testTransformDestroyEdgeCreatorStr = `
|
|
|
|
test.A
|
|
|
|
test.A (destroy)
|
|
|
|
test.A (destroy)
|
|
|
|
test.B (destroy)
|
|
|
|
test.B (destroy)
|
|
|
|
`
|
|
|
|
|
2016-09-20 12:18:31 -05:00
|
|
|
const testTransformDestroyEdgeMultiStr = `
|
|
|
|
test.A (destroy)
|
|
|
|
test.B (destroy)
|
2016-11-11 16:29:19 -06:00
|
|
|
test.C (destroy)
|
2016-09-20 12:18:31 -05:00
|
|
|
test.B (destroy)
|
|
|
|
test.C (destroy)
|
|
|
|
test.C (destroy)
|
|
|
|
`
|
2016-11-08 14:27:33 -06:00
|
|
|
|
|
|
|
const testTransformDestroyEdgeSelfRefStr = `
|
|
|
|
test.A (destroy)
|
|
|
|
`
|
2016-11-11 16:29:19 -06:00
|
|
|
|
|
|
|
const testTransformDestroyEdgeModuleStr = `
|
|
|
|
aws_instance.a (destroy)
|
|
|
|
module.child.aws_instance.b (destroy)
|
|
|
|
aws_instance.a (destroy)
|
|
|
|
`
|