github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/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  func TestDestroyEdgeTransformer_moduleOnly(t *testing.T) {
   100  	g := Graph{Path: RootModulePath}
   101  	g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.a"})
   102  	g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"})
   103  	g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.c"})
   104  	tf := &DestroyEdgeTransformer{
   105  		Module: testModule(t, "transform-destroy-edge-module-only"),
   106  	}
   107  	if err := tf.Transform(&g); err != nil {
   108  		t.Fatalf("err: %s", err)
   109  	}
   110  
   111  	actual := strings.TrimSpace(g.String())
   112  	expected := strings.TrimSpace(`
   113  module.child.aws_instance.a (destroy)
   114    module.child.aws_instance.b (destroy)
   115    module.child.aws_instance.c (destroy)
   116  module.child.aws_instance.b (destroy)
   117    module.child.aws_instance.c (destroy)
   118  module.child.aws_instance.c (destroy)
   119  `)
   120  	if actual != expected {
   121  		t.Fatalf("bad:\n\n%s", actual)
   122  	}
   123  }
   124  
   125  type graphNodeCreatorTest struct {
   126  	AddrString string
   127  	Refs       []string
   128  }
   129  
   130  func (n *graphNodeCreatorTest) Name() string { return n.CreateAddr().String() }
   131  func (n *graphNodeCreatorTest) CreateAddr() *ResourceAddress {
   132  	addr, err := ParseResourceAddress(n.AddrString)
   133  	if err != nil {
   134  		panic(err)
   135  	}
   136  
   137  	return addr
   138  }
   139  
   140  func (n *graphNodeCreatorTest) References() []string { return n.Refs }
   141  
   142  type graphNodeDestroyerTest struct {
   143  	AddrString string
   144  	CBD        bool
   145  	Modified   bool
   146  }
   147  
   148  func (n *graphNodeDestroyerTest) Name() string {
   149  	result := n.DestroyAddr().String() + " (destroy)"
   150  	if n.Modified {
   151  		result += " (modified)"
   152  	}
   153  
   154  	return result
   155  }
   156  
   157  func (n *graphNodeDestroyerTest) CreateBeforeDestroy() bool { return n.CBD }
   158  
   159  func (n *graphNodeDestroyerTest) ModifyCreateBeforeDestroy(v bool) error {
   160  	n.Modified = true
   161  	return nil
   162  }
   163  
   164  func (n *graphNodeDestroyerTest) DestroyAddr() *ResourceAddress {
   165  	addr, err := ParseResourceAddress(n.AddrString)
   166  	if err != nil {
   167  		panic(err)
   168  	}
   169  
   170  	return addr
   171  }
   172  
   173  const testTransformDestroyEdgeBasicStr = `
   174  test.A (destroy)
   175    test.B (destroy)
   176  test.B (destroy)
   177  `
   178  
   179  const testTransformDestroyEdgeCreatorStr = `
   180  test.A
   181    test.A (destroy)
   182  test.A (destroy)
   183    test.B (destroy)
   184  test.B (destroy)
   185  `
   186  
   187  const testTransformDestroyEdgeMultiStr = `
   188  test.A (destroy)
   189    test.B (destroy)
   190    test.C (destroy)
   191  test.B (destroy)
   192    test.C (destroy)
   193  test.C (destroy)
   194  `
   195  
   196  const testTransformDestroyEdgeSelfRefStr = `
   197  test.A (destroy)
   198  `
   199  
   200  const testTransformDestroyEdgeModuleStr = `
   201  aws_instance.a (destroy)
   202  module.child.aws_instance.b (destroy)
   203    aws_instance.a (destroy)
   204  `