opentofu/terraform/transform_destroy_cbd_test.go
James Bardin 470362b051 fix CBD tests to work on real data
The CBDEdgeTransformer tests worked on fake data structures, with a
synthetic graph, and configs that didn't match. Update them to generate
a more complete graph, with real node implementations, from real
configs.

The output graph is filtered down to instances, and the results still
functionally match the original expected test results, with some minor
additions due to using the real implementation.
2019-10-24 12:01:50 -04:00

199 lines
4.3 KiB
Go

package terraform
import (
"regexp"
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/plans"
)
func cbdTestGraph(t *testing.T, mod string, changes *plans.Changes) *Graph {
module := testModule(t, mod)
applyBuilder := &ApplyGraphBuilder{
Config: module,
Changes: changes,
Components: simpleMockComponentFactory(),
Schemas: simpleTestSchemas(),
}
g, err := (&BasicGraphBuilder{
Steps: cbdTestSteps(applyBuilder.Steps()),
Name: "ApplyGraphBuilder",
}).Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}
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
}
}
if !found {
panic("CBDEdgeTransformer not found")
}
return steps[:i+1]
}
// 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)
}
}
return g
}
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,
},
},
},
}
g := cbdTestGraph(t, "transform-destroy-cbd-edge-basic", changes)
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.B
test_object.B
test_object.A
`))
if !expected.MatchString(actual) {
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
}
}
func TestCBDEdgeTransformer_depNonCBDCount(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[0]"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Update,
},
},
{
Addr: mustResourceInstanceAddr("test_object.B[1]"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Update,
},
},
},
}
g := cbdTestGraph(t, "transform-cbd-destroy-edge-count", changes)
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.B\[0\]
test_object.B\[1\]
test_object.B\[0\]
test_object.A
test_object.B\[1\]
test_object.A`))
if !expected.MatchString(actual) {
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
}
}
func TestCBDEdgeTransformer_depNonCBDCountBoth(t *testing.T) {
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,
},
},
},
}
g := cbdTestGraph(t, "transform-cbd-destroy-edge-both-count", changes)
actual := strings.TrimSpace(g.String())
expected := regexp.MustCompile(strings.TrimSpace(`
test_object.A \(destroy deposed \w+\)
test_object.A\[0\]
test_object.A\[1\]
test_object.B\[0\]
test_object.B\[1\]
test_object.A \(destroy deposed \w+\)
test_object.A\[0\]
test_object.A\[1\]
test_object.B\[0\]
test_object.B\[1\]
test_object.A\[0\]
test_object.A\[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) {
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
}
}