2016-09-20 12:16:49 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
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}
|
2018-05-09 19:05:18 -05:00
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.A"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.B"})
|
2016-09-20 12:16:49 -05:00
|
|
|
tf := &DestroyEdgeTransformer{
|
2018-05-31 14:39:45 -05:00
|
|
|
Config: testModule(t, "transform-destroy-edge-basic"),
|
|
|
|
Schemas: simpleTestSchemas(),
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
|
|
|
if err := tf.Transform(&g); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeBasicStr)
|
|
|
|
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:16:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
func TestDestroyEdgeTransformer_create(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
2018-05-09 19:05:18 -05:00
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.A"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.B"})
|
|
|
|
g.Add(&graphNodeCreatorTest{AddrString: "test_object.A"})
|
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())
|
|
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeCreatorStr)
|
|
|
|
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}
|
2018-05-09 19:05:18 -05:00
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.A"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.B"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.C"})
|
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}
|
2018-05-09 19:05:18 -05:00
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "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}
|
2018-05-09 19:05:18 -05:00
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.b"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test_object.a"})
|
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}
|
2018-05-09 19:05:18 -05:00
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.a"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.b"})
|
|
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.test_object.c"})
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(`
|
2018-05-09 19:05:18 -05:00
|
|
|
module.child.test_object.a (destroy)
|
|
|
|
module.child.test_object.b (destroy)
|
|
|
|
module.child.test_object.c (destroy)
|
|
|
|
module.child.test_object.b (destroy)
|
|
|
|
module.child.test_object.c (destroy)
|
|
|
|
module.child.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
type graphNodeCreatorTest struct {
|
|
|
|
AddrString string
|
terraform: apply resource must depend on destroy deps
Fixes #10440
This updates the behavior of "apply" resources to depend on the
destroy versions of their dependencies.
We make an exception to this behavior when the "apply" resource is CBD.
This is odd and not 100% correct, but it mimics the behavior of the
legacy graphs and avoids us having to do major core work to support the
100% correct solution.
I'll explain this in examples...
Given the following configuration:
resource "null_resource" "a" {
count = "${var.count}"
}
resource "null_resource" "b" {
triggers { key = "${join(",", null_resource.a.*.id)}" }
}
Assume we've successfully created this configuration with count = 2.
When going from count = 2 to count = 1, `null_resource.b` should wait
for `null_resource.a.1` to destroy.
If it doesn't, then it is a race: depending when we interpolate the
`triggers.key` attribute of `null_resource.b`, we may get 1 value or 2.
If `null_resource.a.1` is destroyed, we'll get 1. Otherwise, we'll get
2. This was the root cause of #10440
In the legacy graphs, `null_resource.b` would depend on the destruction
of any `null_resource.a` (orphans, tainted, anything!). This would
ensure proper ordering. We mimic that behavior here.
The difference is CBD. If `null_resource.b` has CBD enabled, then the
ordering **in the legacy graph** becomes:
1. null_resource.b (create)
2. null_resource.b (destroy)
3. null_resource.a (destroy)
In this case, the update would always have 2 values for `triggers.key`,
even though we were destroying a resource later! This scenario required
two `terraform apply` operations.
This is what the CBD check is for in this PR. We do this to mimic the
behavior of the legacy graph.
The correct solution to do one day is to allow splat references
(`null_resource.a.*.id`) to happen in parallel and only read up to to
the `count` amount in the state. This requires some fairly significant
work close to the 0.8 release date, so we can defer this to later and
adopt the 0.7.x behavior for now.
2016-12-04 01:44:09 -06:00
|
|
|
Refs []string
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
|
2018-05-09 19:05:18 -05:00
|
|
|
var (
|
|
|
|
_ GraphNodeCreator = (*graphNodeCreatorTest)(nil)
|
|
|
|
_ GraphNodeReferencer = (*graphNodeCreatorTest)(nil)
|
|
|
|
)
|
2016-09-21 12:55:07 -05:00
|
|
|
|
2018-05-09 19:05:18 -05:00
|
|
|
func (n *graphNodeCreatorTest) Name() string {
|
|
|
|
return n.CreateAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeCreatorTest) mustAddr() addrs.AbsResourceInstance {
|
|
|
|
addr, diags := addrs.ParseAbsResourceInstanceStr(n.AddrString)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Err())
|
|
|
|
}
|
2016-09-21 12:55:07 -05:00
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2018-05-09 19:05:18 -05:00
|
|
|
func (n *graphNodeCreatorTest) Path() addrs.ModuleInstance {
|
|
|
|
return n.mustAddr().Module
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeCreatorTest) CreateAddr() *addrs.AbsResourceInstance {
|
|
|
|
addr := n.mustAddr()
|
|
|
|
return &addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeCreatorTest) References() []*addrs.Reference {
|
|
|
|
ret := make([]*addrs.Reference, len(n.Refs))
|
|
|
|
for i, str := range n.Refs {
|
|
|
|
ref, diags := addrs.ParseRefStr(str)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Err())
|
|
|
|
}
|
|
|
|
ret[i] = ref
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
terraform: apply resource must depend on destroy deps
Fixes #10440
This updates the behavior of "apply" resources to depend on the
destroy versions of their dependencies.
We make an exception to this behavior when the "apply" resource is CBD.
This is odd and not 100% correct, but it mimics the behavior of the
legacy graphs and avoids us having to do major core work to support the
100% correct solution.
I'll explain this in examples...
Given the following configuration:
resource "null_resource" "a" {
count = "${var.count}"
}
resource "null_resource" "b" {
triggers { key = "${join(",", null_resource.a.*.id)}" }
}
Assume we've successfully created this configuration with count = 2.
When going from count = 2 to count = 1, `null_resource.b` should wait
for `null_resource.a.1` to destroy.
If it doesn't, then it is a race: depending when we interpolate the
`triggers.key` attribute of `null_resource.b`, we may get 1 value or 2.
If `null_resource.a.1` is destroyed, we'll get 1. Otherwise, we'll get
2. This was the root cause of #10440
In the legacy graphs, `null_resource.b` would depend on the destruction
of any `null_resource.a` (orphans, tainted, anything!). This would
ensure proper ordering. We mimic that behavior here.
The difference is CBD. If `null_resource.b` has CBD enabled, then the
ordering **in the legacy graph** becomes:
1. null_resource.b (create)
2. null_resource.b (destroy)
3. null_resource.a (destroy)
In this case, the update would always have 2 values for `triggers.key`,
even though we were destroying a resource later! This scenario required
two `terraform apply` operations.
This is what the CBD check is for in this PR. We do this to mimic the
behavior of the legacy graph.
The correct solution to do one day is to allow splat references
(`null_resource.a.*.id`) to happen in parallel and only read up to to
the `count` amount in the state. This requires some fairly significant
work close to the 0.8 release date, so we can defer this to later and
adopt the 0.7.x behavior for now.
2016-12-04 01:44:09 -06:00
|
|
|
|
2016-09-20 12:16:49 -05:00
|
|
|
type graphNodeDestroyerTest struct {
|
|
|
|
AddrString string
|
2016-09-21 12:55:07 -05:00
|
|
|
CBD bool
|
2016-12-02 08:46:42 -06:00
|
|
|
Modified bool
|
|
|
|
}
|
|
|
|
|
2018-05-09 19:05:18 -05:00
|
|
|
var _ GraphNodeDestroyer = (*graphNodeDestroyerTest)(nil)
|
|
|
|
|
2016-12-02 08:46:42 -06:00
|
|
|
func (n *graphNodeDestroyerTest) Name() string {
|
|
|
|
result := n.DestroyAddr().String() + " (destroy)"
|
|
|
|
if n.Modified {
|
|
|
|
result += " (modified)"
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
|
|
|
|
2018-05-09 19:05:18 -05:00
|
|
|
func (n *graphNodeDestroyerTest) mustAddr() addrs.AbsResourceInstance {
|
|
|
|
addr, diags := addrs.ParseAbsResourceInstanceStr(n.AddrString)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Err())
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeDestroyerTest) CreateBeforeDestroy() bool {
|
|
|
|
return n.CBD
|
|
|
|
}
|
2016-12-02 08:46:42 -06:00
|
|
|
|
|
|
|
func (n *graphNodeDestroyerTest) ModifyCreateBeforeDestroy(v bool) error {
|
|
|
|
n.Modified = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-09 19:05:18 -05:00
|
|
|
func (n *graphNodeDestroyerTest) DestroyAddr() *addrs.AbsResourceInstance {
|
|
|
|
addr := n.mustAddr()
|
|
|
|
return &addr
|
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
|
|
|
`
|