From 44233441c25561946e95a918de1ac79999559531 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Feb 2017 10:53:12 -0800 Subject: [PATCH] terraform: CBD edge should ignore "index" --- .../transform-destroy-edge-splat/main.tf | 5 ++ terraform/transform_destroy_cbd.go | 25 +++++- terraform/transform_destroy_cbd_test.go | 87 +++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 terraform/test-fixtures/transform-destroy-edge-splat/main.tf diff --git a/terraform/test-fixtures/transform-destroy-edge-splat/main.tf b/terraform/test-fixtures/transform-destroy-edge-splat/main.tf new file mode 100644 index 000000000..3ed06ae1b --- /dev/null +++ b/terraform/test-fixtures/transform-destroy-edge-splat/main.tf @@ -0,0 +1,5 @@ +resource "test" "A" {} +resource "test" "B" { + count = 2 + value = "${test.A.*.value}" +} diff --git a/terraform/transform_destroy_cbd.go b/terraform/transform_destroy_cbd.go index e1a525b88..edfb460bf 100644 --- a/terraform/transform_destroy_cbd.go +++ b/terraform/transform_destroy_cbd.go @@ -89,9 +89,20 @@ func (t *CBDEdgeTransformer) Transform(g *Graph) error { g.Connect(&DestroyEdge{S: de.Target(), T: de.Source()}) } + // If the address has an index, we strip that. Our depMap creation + // graph doesn't expand counts so we don't currently get _exact_ + // dependencies. One day when we limit dependencies more exactly + // this will have to change. We have a test case covering this + // (depNonCBDCountBoth) so it'll be caught. + addr := dn.DestroyAddr() + if addr.Index >= 0 { + addr = addr.Copy() // Copy so that we don't modify any pointers + addr.Index = -1 + } + // Add this to the list of nodes that we need to fix up // the edges for (step 2 above in the docs). - key := dn.DestroyAddr().String() + key := addr.String() destroyMap[key] = append(destroyMap[key], v) } @@ -134,9 +145,19 @@ func (t *CBDEdgeTransformer) Transform(g *Graph) error { // Get the address addr := rn.CreateAddr() - key := addr.String() + + // If the address has an index, we strip that. Our depMap creation + // graph doesn't expand counts so we don't currently get _exact_ + // dependencies. One day when we limit dependencies more exactly + // this will have to change. We have a test case covering this + // (depNonCBDCount) so it'll be caught. + if addr.Index >= 0 { + addr = addr.Copy() // Copy so that we don't modify any pointers + addr.Index = -1 + } // If there is nothing this resource should depend on, ignore it + key := addr.String() dns, ok := depMap[key] if !ok { continue diff --git a/terraform/transform_destroy_cbd_test.go b/terraform/transform_destroy_cbd_test.go index bb8fc24f8..0f79a1987 100644 --- a/terraform/transform_destroy_cbd_test.go +++ b/terraform/transform_destroy_cbd_test.go @@ -68,6 +68,93 @@ func TestCBDEdgeTransformer_depNonCBD(t *testing.T) { } } +func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) { + g := Graph{Path: RootModulePath} + g.Add(&graphNodeCreatorTest{AddrString: "test.A"}) + g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"}) + g.Add(&graphNodeCreatorTest{AddrString: "test.B[1]"}) + g.Add(&graphNodeDestroyerTest{AddrString: "test.A", CBD: true}) + + module := testModule(t, "transform-destroy-edge-splat") + + { + tf := &DestroyEdgeTransformer{ + Module: module, + } + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + { + tf := &CBDEdgeTransformer{Module: module} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(` +test.A +test.A (destroy) + test.A + test.B[0] + test.B[1] +test.B[0] +test.B[1] + `) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} + +func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) { + g := Graph{Path: RootModulePath} + g.Add(&graphNodeCreatorTest{AddrString: "test.A[0]"}) + g.Add(&graphNodeCreatorTest{AddrString: "test.A[1]"}) + g.Add(&graphNodeCreatorTest{AddrString: "test.B[0]"}) + g.Add(&graphNodeCreatorTest{AddrString: "test.B[1]"}) + g.Add(&graphNodeDestroyerTest{AddrString: "test.A[0]", CBD: true}) + g.Add(&graphNodeDestroyerTest{AddrString: "test.A[1]", CBD: true}) + + module := testModule(t, "transform-destroy-edge-splat") + + { + tf := &DestroyEdgeTransformer{ + Module: module, + } + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + { + tf := &CBDEdgeTransformer{Module: module} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(` +test.A[0] +test.A[0] (destroy) + test.A[0] + test.B[0] + test.B[1] +test.A[1] +test.A[1] (destroy) + test.A[1] + test.B[0] + test.B[1] +test.B[0] +test.B[1] + `) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} + const testTransformCBDEdgeBasicStr = ` test.A test.A (destroy)