2016-09-21 12:55:07 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2019-09-26 11:36:44 -05:00
|
|
|
"regexp"
|
2016-09-21 12:55:07 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2019-09-26 11:36:44 -05:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
2019-11-19 16:41:45 -06:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2016-09-21 12:55:07 -05:00
|
|
|
)
|
|
|
|
|
2019-11-19 16:41:45 -06:00
|
|
|
func cbdTestGraph(t *testing.T, mod string, changes *plans.Changes, state *states.State) *Graph {
|
2019-09-26 11:36:44 -05:00
|
|
|
module := testModule(t, mod)
|
2016-09-21 18:37:41 -05:00
|
|
|
|
2019-09-26 11:36:44 -05:00
|
|
|
applyBuilder := &ApplyGraphBuilder{
|
|
|
|
Config: module,
|
|
|
|
Changes: changes,
|
|
|
|
Components: simpleMockComponentFactory(),
|
|
|
|
Schemas: simpleTestSchemas(),
|
2019-11-19 16:41:45 -06:00
|
|
|
State: state,
|
2019-09-26 11:36:44 -05:00
|
|
|
}
|
|
|
|
g, err := (&BasicGraphBuilder{
|
|
|
|
Steps: cbdTestSteps(applyBuilder.Steps()),
|
|
|
|
Name: "ApplyGraphBuilder",
|
|
|
|
}).Build(addrs.RootModuleInstance)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
|
2019-09-26 11:36:44 -05:00
|
|
|
return filterInstances(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
// override the apply graph builder to halt the process after CBD
|
|
|
|
func cbdTestSteps(steps []GraphTransformer) []GraphTransformer {
|
|
|
|
found := false
|
|
|
|
var i int
|
|
|
|
var t GraphTransformer
|
|
|
|
for i, t = range steps {
|
|
|
|
if _, ok := t.(*CBDEdgeTransformer); ok {
|
|
|
|
found = true
|
|
|
|
break
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:36:44 -05:00
|
|
|
if !found {
|
|
|
|
panic("CBDEdgeTransformer not found")
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
2019-09-26 11:36:44 -05:00
|
|
|
|
|
|
|
return steps[:i+1]
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
|
2019-09-26 11:36:44 -05:00
|
|
|
// remove extra nodes for easier test comparisons
|
|
|
|
func filterInstances(g *Graph) *Graph {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if _, ok := v.(GraphNodeResourceInstance); !ok {
|
|
|
|
g.Remove(v)
|
2016-12-02 08:46:42 -06:00
|
|
|
}
|
2019-09-26 11:36:44 -05:00
|
|
|
|
2016-12-02 08:46:42 -06:00
|
|
|
}
|
2019-09-26 11:36:44 -05:00
|
|
|
return g
|
|
|
|
}
|
2016-12-02 08:46:42 -06:00
|
|
|
|
2019-09-26 11:36:44 -05:00
|
|
|
func TestCBDEdgeTransformer(t *testing.T) {
|
|
|
|
changes := &plans.Changes{
|
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.A"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.CreateThenDelete,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.B"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Update,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-12-02 08:46:42 -06:00
|
|
|
}
|
|
|
|
|
2019-11-19 16:41:45 -06:00
|
|
|
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-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")},
|
2019-11-19 16:41:45 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
g := cbdTestGraph(t, "transform-destroy-cbd-edge-basic", changes, state)
|
2019-09-26 11:36:44 -05:00
|
|
|
g = filterInstances(g)
|
|
|
|
|
2016-12-02 08:46:42 -06:00
|
|
|
actual := strings.TrimSpace(g.String())
|
2019-09-26 11:36:44 -05:00
|
|
|
expected := regexp.MustCompile(strings.TrimSpace(`
|
|
|
|
(?m)test_object.A
|
|
|
|
test_object.A \(destroy deposed \w+\)
|
|
|
|
test_object.A
|
|
|
|
test_object.B
|
|
|
|
test_object.B
|
|
|
|
test_object.A
|
|
|
|
`))
|
|
|
|
|
|
|
|
if !expected.MatchString(actual) {
|
2018-05-09 19:05:18 -05:00
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
2016-12-02 08:46:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 09:56:28 -06:00
|
|
|
func TestCBDEdgeTransformerMulti(t *testing.T) {
|
|
|
|
changes := &plans.Changes{
|
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.A"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.CreateThenDelete,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.B"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.CreateThenDelete,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.C"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Update,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:41:45 -06:00
|
|
|
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-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B"}`),
|
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.C").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"C","test_list":["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-11-19 16:41:45 -06:00
|
|
|
},
|
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
g := cbdTestGraph(t, "transform-destroy-cbd-edge-multi", changes, state)
|
2019-11-20 09:56:28 -06:00
|
|
|
g = filterInstances(g)
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := regexp.MustCompile(strings.TrimSpace(`
|
|
|
|
(?m)test_object.A
|
|
|
|
test_object.A \(destroy deposed \w+\)
|
|
|
|
test_object.A
|
|
|
|
test_object.C
|
|
|
|
test_object.B
|
|
|
|
test_object.B \(destroy deposed \w+\)
|
|
|
|
test_object.B
|
|
|
|
test_object.C
|
|
|
|
test_object.C
|
|
|
|
test_object.A
|
|
|
|
test_object.B
|
|
|
|
`))
|
|
|
|
|
|
|
|
if !expected.MatchString(actual) {
|
|
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 12:53:12 -06:00
|
|
|
func TestCBDEdgeTransformer_depNonCBDCount(t *testing.T) {
|
2019-09-26 11:36:44 -05:00
|
|
|
changes := &plans.Changes{
|
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.A"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.CreateThenDelete,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.B[0]"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Update,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.B[1]"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Update,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-07 12:53:12 -06:00
|
|
|
}
|
|
|
|
|
2019-11-19 16:41:45 -06:00
|
|
|
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-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B[0]").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")},
|
2019-11-19 16:41:45 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B[1]").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")},
|
2019-11-19 16:41:45 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
g := cbdTestGraph(t, "transform-cbd-destroy-edge-count", changes, state)
|
2017-02-07 12:53:12 -06:00
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
2019-09-26 11:36:44 -05:00
|
|
|
expected := regexp.MustCompile(strings.TrimSpace(`
|
|
|
|
(?m)test_object.A
|
|
|
|
test_object.A \(destroy deposed \w+\)
|
2018-05-09 19:05:18 -05:00
|
|
|
test_object.A
|
2019-09-26 11:36:44 -05:00
|
|
|
test_object.B\[0\]
|
|
|
|
test_object.B\[1\]
|
|
|
|
test_object.B\[0\]
|
|
|
|
test_object.A
|
|
|
|
test_object.B\[1\]
|
|
|
|
test_object.A`))
|
|
|
|
|
|
|
|
if !expected.MatchString(actual) {
|
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 12:53:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
|
2019-09-26 11:36:44 -05:00
|
|
|
changes := &plans.Changes{
|
|
|
|
Resources: []*plans.ResourceInstanceChangeSrc{
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.A[0]"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.CreateThenDelete,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.A[1]"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.CreateThenDelete,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.B[0]"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Update,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: mustResourceInstanceAddr("test_object.B[1]"),
|
|
|
|
ChangeSrc: plans.ChangeSrc{
|
|
|
|
Action: plans.Update,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-07 12:53:12 -06:00
|
|
|
}
|
|
|
|
|
2019-11-19 16:41:45 -06:00
|
|
|
state := states.NewState()
|
|
|
|
root := state.EnsureModule(addrs.RootModuleInstance)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.A[0]").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-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.A[1]").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-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B[0]").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")},
|
2019-11-19 16:41:45 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
root.SetResourceInstanceCurrent(
|
|
|
|
mustResourceInstanceAddr("test_object.B[1]").Resource,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte(`{"id":"B","test_list":["x"]}`),
|
2020-09-24 10:57:43 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{mustConfigResourceAddr("test_object.A")},
|
2019-11-19 16:41:45 -06:00
|
|
|
},
|
2020-04-01 12:13:40 -05:00
|
|
|
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
|
2019-11-19 16:41:45 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
g := cbdTestGraph(t, "transform-cbd-destroy-edge-both-count", changes, state)
|
2017-02-07 12:53:12 -06:00
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
2019-09-26 11:36:44 -05:00
|
|
|
expected := regexp.MustCompile(strings.TrimSpace(`
|
2019-11-19 16:41:45 -06:00
|
|
|
test_object.A\[0\]
|
|
|
|
test_object.A\[0\] \(destroy deposed \w+\)
|
2019-09-26 11:36:44 -05:00
|
|
|
test_object.A\[0\]
|
|
|
|
test_object.B\[0\]
|
|
|
|
test_object.B\[1\]
|
2019-11-19 16:41:45 -06:00
|
|
|
test_object.A\[1\]
|
|
|
|
test_object.A\[1\] \(destroy deposed \w+\)
|
2019-09-26 11:36:44 -05:00
|
|
|
test_object.A\[1\]
|
|
|
|
test_object.B\[0\]
|
|
|
|
test_object.B\[1\]
|
|
|
|
test_object.B\[0\]
|
|
|
|
test_object.A\[0\]
|
|
|
|
test_object.A\[1\]
|
|
|
|
test_object.B\[1\]
|
|
|
|
test_object.A\[0\]
|
|
|
|
test_object.A\[1\]
|
|
|
|
`))
|
|
|
|
|
|
|
|
if !expected.MatchString(actual) {
|
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 12:53:12 -06:00
|
|
|
}
|
|
|
|
}
|