2016-09-20 12:16:49 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2020-07-16 18:11:08 -05:00
|
|
|
"fmt"
|
2016-09-20 12:16:49 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2019-12-03 14:00:48 -06:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2016-09-20 12:16:49 -05:00
|
|
|
)
|
|
|
|
|
2016-11-08 11:35:57 -06:00
|
|
|
func TestDestroyEdgeTransformer_basic(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2019-12-03 14:00:48 -06:00
|
|
|
g.Add(testDestroyNode("test_object.A"))
|
|
|
|
g.Add(testDestroyNode("test_object.B"))
|
|
|
|
|
|
|
|
state := states.NewState()
|
|
|
|
root := state.EnsureModule(addrs.RootModuleInstance)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.A").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"A"}`),
|
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-12-03 14:00:48 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B","test_string":"x"}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")},
|
2019-12-03 14:00:48 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-12-03 14:00:48 -06:00
|
|
|
)
|
|
|
|
if err := (&AttachStateTransformer{State: state}).Transform(&g); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
tf := &DestroyEdgeTransformer{
|
2018-05-31 14:39:45 -05:00
|
|
|
Config: testModule(t, "transform-destroy-edge-basic"),
|
|
|
|
Schemas: simpleTestSchemas(),
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
2019-12-03 14:00:48 -06:00
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeBasicStr)
|
2016-09-21 12:55:07 -05:00
|
|
|
if actual != expected {
|
2018-05-09 19:05:18 -05:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 12:18:31 -05:00
|
|
|
func TestDestroyEdgeTransformer_multi(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2019-12-03 14:00:48 -06:00
|
|
|
g.Add(testDestroyNode("test_object.A"))
|
|
|
|
g.Add(testDestroyNode("test_object.B"))
|
|
|
|
g.Add(testDestroyNode("test_object.C"))
|
|
|
|
|
|
|
|
state := states.NewState()
|
|
|
|
root := state.EnsureModule(addrs.RootModuleInstance)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.A").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"A"}`),
|
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-12-03 14:00:48 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B","test_string":"x"}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")},
|
2019-12-03 14:00:48 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-12-03 14:00:48 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.C").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"C","test_string":"x"}`),
|
2020-03-23 14:26:18 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{
|
2020-09-24 10:57:43 -05:00
|
|
|
mustConfigResourceAddr("test_object.A"),
|
|
|
|
mustConfigResourceAddr("test_object.B"),
|
2019-12-03 14:00:48 -06:00
|
|
|
},
|
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-12-03 14:00:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := (&AttachStateTransformer{State: state}).Transform(&g); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-20 12:18:31 -05:00
|
|
|
tf := &DestroyEdgeTransformer{
|
2018-05-31 14:39:45 -05:00
|
|
|
Config: testModule(t, "transform-destroy-edge-multi"),
|
|
|
|
Schemas: simpleTestSchemas(),
|
2016-09-20 12:18:31 -05:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeMultiStr)
|
|
|
|
if actual != expected {
|
2018-05-09 19:05:18 -05:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-09-20 12:18:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:27:33 -06:00
|
|
|
func TestDestroyEdgeTransformer_selfRef(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2019-12-03 14:00:48 -06:00
|
|
|
g.Add(testDestroyNode("test_object.A"))
|
2016-11-08 14:27:33 -06:00
|
|
|
tf := &DestroyEdgeTransformer{
|
2018-05-31 14:39:45 -05:00
|
|
|
Config: testModule(t, "transform-destroy-edge-self-ref"),
|
|
|
|
Schemas: simpleTestSchemas(),
|
2016-11-08 14:27:33 -06:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeSelfRefStr)
|
|
|
|
if actual != expected {
|
2018-05-09 19:05:18 -05:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-11-08 14:27:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:29:19 -06:00
|
|
|
func TestDestroyEdgeTransformer_module(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2019-12-03 14:00:48 -06:00
|
|
|
g.Add(testDestroyNode("module.child.test_object.b"))
|
|
|
|
g.Add(testDestroyNode("test_object.a"))
|
|
|
|
state := states.NewState()
|
|
|
|
root := state.EnsureModule(addrs.RootModuleInstance)
|
|
|
|
child := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.a").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"a"}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("module.child.test_object.b")},
|
2019-12-03 14:00:48 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-12-03 14:00:48 -06:00
|
|
|
)
|
|
|
|
child.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.b").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"b","test_string":"x"}`),
|
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-12-03 14:00:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := (&AttachStateTransformer{State: state}).Transform(&g); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:29:19 -06:00
|
|
|
tf := &DestroyEdgeTransformer{
|
2018-05-31 14:39:45 -05:00
|
|
|
Config: testModule(t, "transform-destroy-edge-module"),
|
|
|
|
Schemas: simpleTestSchemas(),
|
2016-11-11 16:29:19 -06:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeModuleStr)
|
|
|
|
if actual != expected {
|
2018-05-09 19:05:18 -05:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-11-11 16:29:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:12:03 -06:00
|
|
|
func TestDestroyEdgeTransformer_moduleOnly(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2019-12-03 14:00:48 -06:00
|
|
|
|
|
|
|
state := states.NewState()
|
2020-07-16 18:11:08 -05:00
|
|
|
for moduleIdx := 0; moduleIdx < 2; moduleIdx++ {
|
|
|
|
g.Add(testDestroyNode(fmt.Sprintf("module.child[%d].test_object.a", moduleIdx)))
|
|
|
|
g.Add(testDestroyNode(fmt.Sprintf("module.child[%d].test_object.b", moduleIdx)))
|
|
|
|
g.Add(testDestroyNode(fmt.Sprintf("module.child[%d].test_object.c", moduleIdx)))
|
|
|
|
|
|
|
|
child := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.IntKey(moduleIdx)))
|
|
|
|
child.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.a").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"a"}`),
|
2019-12-03 14:00:48 -06:00
|
|
|
},
|
2020-07-16 18:11:08 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
|
|
|
)
|
|
|
|
child.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.b").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"b","test_string":"x"}`),
|
|
|
|
Dependencies: []addrs.ConfigResource{
|
2020-09-24 10:57:43 -05:00
|
|
|
mustConfigResourceAddr("module.child.test_object.a"),
|
2020-07-16 18:11:08 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
|
|
|
)
|
|
|
|
child.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.c").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"c","test_string":"x"}`),
|
|
|
|
Dependencies: []addrs.ConfigResource{
|
2020-09-24 10:57:43 -05:00
|
|
|
mustConfigResourceAddr("module.child.test_object.a"),
|
|
|
|
mustConfigResourceAddr("module.child.test_object.b"),
|
2020-07-16 18:11:08 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
|
|
|
)
|
|
|
|
}
|
2019-12-03 14:00:48 -06:00
|
|
|
|
|
|
|
if err := (&AttachStateTransformer{State: state}).Transform(&g); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:12:03 -06:00
|
|
|
tf := &DestroyEdgeTransformer{
|
2018-05-31 14:39:45 -05:00
|
|
|
Config: testModule(t, "transform-destroy-edge-module-only"),
|
|
|
|
Schemas: simpleTestSchemas(),
|
2017-02-07 21:12:03 -06:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 18:11:08 -05:00
|
|
|
// The analyses done in the destroy edge transformer are between
|
|
|
|
// not-yet-expanded objects, which is conservative and so it will generate
|
|
|
|
// edges that aren't strictly necessary. As a special case we filter out
|
|
|
|
// any edges that are between resources instances that are in different
|
|
|
|
// instances of the same module, because those edges are never needed
|
|
|
|
// (one instance of a module cannot depend on another instance of the
|
|
|
|
// same module) and including them can, in complex cases, cause cycles due
|
|
|
|
// to unnecessary interactions between destroyed and created module
|
|
|
|
// instances in the same plan.
|
|
|
|
//
|
|
|
|
// Therefore below we expect to see the dependencies within each instance
|
|
|
|
// of module.child reflected, but we should not see any dependencies
|
|
|
|
// _between_ instances of module.child.
|
|
|
|
|
2017-02-07 21:12:03 -06:00
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(`
|
2020-06-25 14:23:00 -05:00
|
|
|
module.child[0].test_object.a (destroy)
|
|
|
|
module.child[0].test_object.b (destroy)
|
|
|
|
module.child[0].test_object.c (destroy)
|
|
|
|
module.child[0].test_object.b (destroy)
|
|
|
|
module.child[0].test_object.c (destroy)
|
|
|
|
module.child[0].test_object.c (destroy)
|
2020-07-16 18:11:08 -05:00
|
|
|
module.child[1].test_object.a (destroy)
|
|
|
|
module.child[1].test_object.b (destroy)
|
|
|
|
module.child[1].test_object.c (destroy)
|
|
|
|
module.child[1].test_object.b (destroy)
|
|
|
|
module.child[1].test_object.c (destroy)
|
|
|
|
module.child[1].test_object.c (destroy)
|
2017-02-07 21:12:03 -06:00
|
|
|
`)
|
|
|
|
if actual != expected {
|
2018-05-09 19:05:18 -05:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2017-02-07 21:12:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:00:48 -06:00
|
|
|
func testDestroyNode(addrString string) GraphNodeDestroyer {
|
|
|
|
instAddr := mustResourceInstanceAddr(addrString)
|
2016-09-21 12:55:07 -05:00
|
|
|
|
2020-03-23 07:56:55 -05:00
|
|
|
inst := NewNodeAbstractResourceInstance(instAddr)
|
2016-12-02 08:46:42 -06:00
|
|
|
|
2019-12-03 14:00:48 -06:00
|
|
|
return &NodeDestroyResourceInstance{NodeAbstractResourceInstance: inst}
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const testTransformDestroyEdgeBasicStr = `
|
2018-05-09 19:05:18 -05:00
|
|
|
test_object.A (destroy)
|
|
|
|
test_object.B (destroy)
|
|
|
|
test_object.B (destroy)
|
2016-09-20 12:16:49 -05:00
|
|
|
`
|
2016-09-20 12:18:31 -05:00
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
const testTransformDestroyEdgeCreatorStr = `
|
2018-05-09 19:05:18 -05:00
|
|
|
test_object.A
|
|
|
|
test_object.A (destroy)
|
|
|
|
test_object.A (destroy)
|
|
|
|
test_object.B (destroy)
|
|
|
|
test_object.B (destroy)
|
2016-09-21 12:55:07 -05:00
|
|
|
`
|
|
|
|
|
2016-09-20 12:18:31 -05:00
|
|
|
const testTransformDestroyEdgeMultiStr = `
|
2018-05-09 19:05:18 -05:00
|
|
|
test_object.A (destroy)
|
|
|
|
test_object.B (destroy)
|
|
|
|
test_object.C (destroy)
|
|
|
|
test_object.B (destroy)
|
|
|
|
test_object.C (destroy)
|
|
|
|
test_object.C (destroy)
|
2016-09-20 12:18:31 -05:00
|
|
|
`
|
2016-11-08 14:27:33 -06:00
|
|
|
|
|
|
|
const testTransformDestroyEdgeSelfRefStr = `
|
2018-05-09 19:05:18 -05:00
|
|
|
test_object.A (destroy)
|
2016-11-08 14:27:33 -06:00
|
|
|
`
|
2016-11-11 16:29:19 -06:00
|
|
|
|
|
|
|
const testTransformDestroyEdgeModuleStr = `
|
2018-05-09 19:05:18 -05:00
|
|
|
module.child.test_object.b (destroy)
|
|
|
|
test_object.a (destroy)
|
|
|
|
test_object.a (destroy)
|
2016-11-11 16:29:19 -06:00
|
|
|
`
|