github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/transform_destroy_cbd_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/plans"
    10  )
    11  
    12  func cbdTestGraph(t *testing.T, mod string, changes *plans.Changes) *Graph {
    13  	module := testModule(t, mod)
    14  
    15  	applyBuilder := &ApplyGraphBuilder{
    16  		Config:     module,
    17  		Changes:    changes,
    18  		Components: simpleMockComponentFactory(),
    19  		Schemas:    simpleTestSchemas(),
    20  	}
    21  	g, err := (&BasicGraphBuilder{
    22  		Steps: cbdTestSteps(applyBuilder.Steps()),
    23  		Name:  "ApplyGraphBuilder",
    24  	}).Build(addrs.RootModuleInstance)
    25  	if err != nil {
    26  		t.Fatalf("err: %s", err)
    27  	}
    28  
    29  	return filterInstances(g)
    30  }
    31  
    32  // override the apply graph builder to halt the process after CBD
    33  func cbdTestSteps(steps []GraphTransformer) []GraphTransformer {
    34  	found := false
    35  	var i int
    36  	var t GraphTransformer
    37  	for i, t = range steps {
    38  		if _, ok := t.(*CBDEdgeTransformer); ok {
    39  			found = true
    40  			break
    41  		}
    42  	}
    43  
    44  	if !found {
    45  		panic("CBDEdgeTransformer not found")
    46  	}
    47  
    48  	return steps[:i+1]
    49  }
    50  
    51  // remove extra nodes for easier test comparisons
    52  func filterInstances(g *Graph) *Graph {
    53  	for _, v := range g.Vertices() {
    54  		if _, ok := v.(GraphNodeResourceInstance); !ok {
    55  			g.Remove(v)
    56  		}
    57  
    58  	}
    59  	return g
    60  }
    61  
    62  func TestCBDEdgeTransformer(t *testing.T) {
    63  	changes := &plans.Changes{
    64  		Resources: []*plans.ResourceInstanceChangeSrc{
    65  			{
    66  				Addr: mustResourceInstanceAddr("test_object.A"),
    67  				ChangeSrc: plans.ChangeSrc{
    68  					Action: plans.CreateThenDelete,
    69  				},
    70  			},
    71  			{
    72  				Addr: mustResourceInstanceAddr("test_object.B"),
    73  				ChangeSrc: plans.ChangeSrc{
    74  					Action: plans.Update,
    75  				},
    76  			},
    77  		},
    78  	}
    79  
    80  	g := cbdTestGraph(t, "transform-destroy-cbd-edge-basic", changes)
    81  	g = filterInstances(g)
    82  
    83  	actual := strings.TrimSpace(g.String())
    84  	expected := regexp.MustCompile(strings.TrimSpace(`
    85  (?m)test_object.A
    86  test_object.A \(destroy deposed \w+\)
    87    test_object.A
    88    test_object.B
    89  test_object.B
    90    test_object.A
    91  `))
    92  
    93  	if !expected.MatchString(actual) {
    94  		t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
    95  	}
    96  }
    97  
    98  func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) {
    99  	changes := &plans.Changes{
   100  		Resources: []*plans.ResourceInstanceChangeSrc{
   101  			{
   102  				Addr: mustResourceInstanceAddr("test_object.A"),
   103  				ChangeSrc: plans.ChangeSrc{
   104  					Action: plans.CreateThenDelete,
   105  				},
   106  			},
   107  			{
   108  				Addr: mustResourceInstanceAddr("test_object.B[0]"),
   109  				ChangeSrc: plans.ChangeSrc{
   110  					Action: plans.Update,
   111  				},
   112  			},
   113  			{
   114  				Addr: mustResourceInstanceAddr("test_object.B[1]"),
   115  				ChangeSrc: plans.ChangeSrc{
   116  					Action: plans.Update,
   117  				},
   118  			},
   119  		},
   120  	}
   121  
   122  	g := cbdTestGraph(t, "transform-cbd-destroy-edge-count", changes)
   123  
   124  	actual := strings.TrimSpace(g.String())
   125  	expected := regexp.MustCompile(strings.TrimSpace(`
   126  (?m)test_object.A
   127  test_object.A \(destroy deposed \w+\)
   128    test_object.A
   129    test_object.B\[0\]
   130    test_object.B\[1\]
   131  test_object.B\[0\]
   132    test_object.A
   133  test_object.B\[1\]
   134    test_object.A`))
   135  
   136  	if !expected.MatchString(actual) {
   137  		t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
   138  	}
   139  }
   140  
   141  func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
   142  	changes := &plans.Changes{
   143  		Resources: []*plans.ResourceInstanceChangeSrc{
   144  			{
   145  				Addr: mustResourceInstanceAddr("test_object.A[0]"),
   146  				ChangeSrc: plans.ChangeSrc{
   147  					Action: plans.CreateThenDelete,
   148  				},
   149  			},
   150  			{
   151  				Addr: mustResourceInstanceAddr("test_object.A[1]"),
   152  				ChangeSrc: plans.ChangeSrc{
   153  					Action: plans.CreateThenDelete,
   154  				},
   155  			},
   156  			{
   157  				Addr: mustResourceInstanceAddr("test_object.B[0]"),
   158  				ChangeSrc: plans.ChangeSrc{
   159  					Action: plans.Update,
   160  				},
   161  			},
   162  			{
   163  				Addr: mustResourceInstanceAddr("test_object.B[1]"),
   164  				ChangeSrc: plans.ChangeSrc{
   165  					Action: plans.Update,
   166  				},
   167  			},
   168  		},
   169  	}
   170  
   171  	g := cbdTestGraph(t, "transform-cbd-destroy-edge-both-count", changes)
   172  
   173  	actual := strings.TrimSpace(g.String())
   174  	expected := regexp.MustCompile(strings.TrimSpace(`
   175  test_object.A \(destroy deposed \w+\)
   176    test_object.A\[0\]
   177    test_object.A\[1\]
   178    test_object.B\[0\]
   179    test_object.B\[1\]
   180  test_object.A \(destroy deposed \w+\)
   181    test_object.A\[0\]
   182    test_object.A\[1\]
   183    test_object.B\[0\]
   184    test_object.B\[1\]
   185  test_object.A\[0\]
   186  test_object.A\[1\]
   187  test_object.B\[0\]
   188    test_object.A\[0\]
   189    test_object.A\[1\]
   190  test_object.B\[1\]
   191    test_object.A\[0\]
   192    test_object.A\[1\]
   193  `))
   194  
   195  	if !expected.MatchString(actual) {
   196  		t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
   197  	}
   198  }