github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/terraform/transform_destroy_cbd_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestCBDEdgeTransformer(t *testing.T) {
     9  	g := Graph{Path: RootModulePath}
    10  	g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
    11  	g.Add(&graphNodeCreatorTest{AddrString: "test.B"})
    12  	g.Add(&graphNodeDestroyerTest{AddrString: "test.A", CBD: true})
    13  
    14  	module := testModule(t, "transform-destroy-edge-basic")
    15  
    16  	{
    17  		tf := &DestroyEdgeTransformer{
    18  			Module: module,
    19  		}
    20  		if err := tf.Transform(&g); err != nil {
    21  			t.Fatalf("err: %s", err)
    22  		}
    23  	}
    24  
    25  	{
    26  		tf := &CBDEdgeTransformer{Module: module}
    27  		if err := tf.Transform(&g); err != nil {
    28  			t.Fatalf("err: %s", err)
    29  		}
    30  	}
    31  
    32  	actual := strings.TrimSpace(g.String())
    33  	expected := strings.TrimSpace(testTransformCBDEdgeBasicStr)
    34  	if actual != expected {
    35  		t.Fatalf("bad:\n\n%s", actual)
    36  	}
    37  }
    38  
    39  func TestCBDEdgeTransformer_depNonCBD(t *testing.T) {
    40  	g := Graph{Path: RootModulePath}
    41  	g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
    42  	g.Add(&graphNodeCreatorTest{AddrString: "test.B"})
    43  	g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
    44  	g.Add(&graphNodeDestroyerTest{AddrString: "test.B", CBD: true})
    45  
    46  	module := testModule(t, "transform-destroy-edge-basic")
    47  
    48  	{
    49  		tf := &DestroyEdgeTransformer{
    50  			Module: module,
    51  		}
    52  		if err := tf.Transform(&g); err != nil {
    53  			t.Fatalf("err: %s", err)
    54  		}
    55  	}
    56  
    57  	{
    58  		tf := &CBDEdgeTransformer{Module: module}
    59  		if err := tf.Transform(&g); err != nil {
    60  			t.Fatalf("err: %s", err)
    61  		}
    62  	}
    63  
    64  	actual := strings.TrimSpace(g.String())
    65  	expected := strings.TrimSpace(testTransformCBDEdgeDepNonCBDStr)
    66  	if actual != expected {
    67  		t.Fatalf("bad:\n\n%s", actual)
    68  	}
    69  }
    70  
    71  func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) {
    72  	g := Graph{Path: RootModulePath}
    73  	g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
    74  	g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"})
    75  	g.Add(&graphNodeCreatorTest{AddrString: "test.B[1]"})
    76  	g.Add(&graphNodeDestroyerTest{AddrString: "test.A", CBD: true})
    77  
    78  	module := testModule(t, "transform-destroy-edge-splat")
    79  
    80  	{
    81  		tf := &DestroyEdgeTransformer{
    82  			Module: module,
    83  		}
    84  		if err := tf.Transform(&g); err != nil {
    85  			t.Fatalf("err: %s", err)
    86  		}
    87  	}
    88  
    89  	{
    90  		tf := &CBDEdgeTransformer{Module: module}
    91  		if err := tf.Transform(&g); err != nil {
    92  			t.Fatalf("err: %s", err)
    93  		}
    94  	}
    95  
    96  	actual := strings.TrimSpace(g.String())
    97  	expected := strings.TrimSpace(`
    98  test.A
    99  test.A (destroy)
   100    test.A
   101    test.B[0]
   102    test.B[1]
   103  test.B[0]
   104  test.B[1]
   105  	`)
   106  	if actual != expected {
   107  		t.Fatalf("bad:\n\n%s", actual)
   108  	}
   109  }
   110  
   111  func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
   112  	g := Graph{Path: RootModulePath}
   113  	g.Add(&graphNodeCreatorTest{AddrString: "test.A[0]"})
   114  	g.Add(&graphNodeCreatorTest{AddrString: "test.A[1]"})
   115  	g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"})
   116  	g.Add(&graphNodeCreatorTest{AddrString: "test.B[1]"})
   117  	g.Add(&graphNodeDestroyerTest{AddrString: "test.A[0]", CBD: true})
   118  	g.Add(&graphNodeDestroyerTest{AddrString: "test.A[1]", CBD: true})
   119  
   120  	module := testModule(t, "transform-destroy-edge-splat")
   121  
   122  	{
   123  		tf := &DestroyEdgeTransformer{
   124  			Module: module,
   125  		}
   126  		if err := tf.Transform(&g); err != nil {
   127  			t.Fatalf("err: %s", err)
   128  		}
   129  	}
   130  
   131  	{
   132  		tf := &CBDEdgeTransformer{Module: module}
   133  		if err := tf.Transform(&g); err != nil {
   134  			t.Fatalf("err: %s", err)
   135  		}
   136  	}
   137  
   138  	actual := strings.TrimSpace(g.String())
   139  	expected := strings.TrimSpace(`
   140  test.A[0]
   141  test.A[0] (destroy)
   142    test.A[0]
   143    test.B[0]
   144    test.B[1]
   145  test.A[1]
   146  test.A[1] (destroy)
   147    test.A[1]
   148    test.B[0]
   149    test.B[1]
   150  test.B[0]
   151  test.B[1]
   152  	`)
   153  	if actual != expected {
   154  		t.Fatalf("bad:\n\n%s", actual)
   155  	}
   156  }
   157  
   158  const testTransformCBDEdgeBasicStr = `
   159  test.A
   160  test.A (destroy)
   161    test.A
   162    test.B
   163  test.B
   164  `
   165  
   166  const testTransformCBDEdgeDepNonCBDStr = `
   167  test.A
   168  test.A (destroy) (modified)
   169    test.A
   170    test.B
   171    test.B (destroy)
   172  test.B
   173  test.B (destroy)
   174    test.B
   175  `