mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
After the refactoring to integrate HCL2 many of the tests were no longer using correct types, attribute names, etc. This is a bulk update of all of the tests to make them compile again, with minimal changes otherwise. Although the tests now compile, many of them do not yet pass. The tests will be gradually repaired in subsequent commits, as we continue to complete the refactoring and retrofit work.
207 lines
5.2 KiB
Go
207 lines
5.2 KiB
Go
package terraform
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
)
|
|
|
|
func TestDestroyEdgeTransformer_basic(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
|
|
tf := &DestroyEdgeTransformer{
|
|
Config: testModule(t, "transform-destroy-edge-basic"),
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeBasicStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestDestroyEdgeTransformer_create(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
|
|
g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
|
|
tf := &DestroyEdgeTransformer{
|
|
Config: testModule(t, "transform-destroy-edge-basic"),
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeCreatorStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestDestroyEdgeTransformer_multi(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.C"})
|
|
tf := &DestroyEdgeTransformer{
|
|
Config: testModule(t, "transform-destroy-edge-multi"),
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeMultiStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestDestroyEdgeTransformer_selfRef(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
|
tf := &DestroyEdgeTransformer{
|
|
Config: testModule(t, "transform-destroy-edge-self-ref"),
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeSelfRefStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestDestroyEdgeTransformer_module(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"})
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "aws_instance.a"})
|
|
tf := &DestroyEdgeTransformer{
|
|
Config: testModule(t, "transform-destroy-edge-module"),
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformDestroyEdgeModuleStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestDestroyEdgeTransformer_moduleOnly(t *testing.T) {
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.a"})
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.b"})
|
|
g.Add(&graphNodeDestroyerTest{AddrString: "module.child.aws_instance.c"})
|
|
tf := &DestroyEdgeTransformer{
|
|
Config: testModule(t, "transform-destroy-edge-module-only"),
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(`
|
|
module.child.aws_instance.a (destroy)
|
|
module.child.aws_instance.b (destroy)
|
|
module.child.aws_instance.c (destroy)
|
|
module.child.aws_instance.b (destroy)
|
|
module.child.aws_instance.c (destroy)
|
|
module.child.aws_instance.c (destroy)
|
|
`)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
type graphNodeCreatorTest struct {
|
|
AddrString string
|
|
Refs []string
|
|
}
|
|
|
|
func (n *graphNodeCreatorTest) Name() string { return n.CreateAddr().String() }
|
|
func (n *graphNodeCreatorTest) CreateAddr() *ResourceAddress {
|
|
addr, err := ParseResourceAddress(n.AddrString)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return addr
|
|
}
|
|
|
|
func (n *graphNodeCreatorTest) References() []string { return n.Refs }
|
|
|
|
type graphNodeDestroyerTest struct {
|
|
AddrString string
|
|
CBD bool
|
|
Modified bool
|
|
}
|
|
|
|
func (n *graphNodeDestroyerTest) Name() string {
|
|
result := n.DestroyAddr().String() + " (destroy)"
|
|
if n.Modified {
|
|
result += " (modified)"
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (n *graphNodeDestroyerTest) CreateBeforeDestroy() bool { return n.CBD }
|
|
|
|
func (n *graphNodeDestroyerTest) ModifyCreateBeforeDestroy(v bool) error {
|
|
n.Modified = true
|
|
return nil
|
|
}
|
|
|
|
func (n *graphNodeDestroyerTest) DestroyAddr() *ResourceAddress {
|
|
addr, err := ParseResourceAddress(n.AddrString)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return addr
|
|
}
|
|
|
|
const testTransformDestroyEdgeBasicStr = `
|
|
test.A (destroy)
|
|
test.B (destroy)
|
|
test.B (destroy)
|
|
`
|
|
|
|
const testTransformDestroyEdgeCreatorStr = `
|
|
test.A
|
|
test.A (destroy)
|
|
test.A (destroy)
|
|
test.B (destroy)
|
|
test.B (destroy)
|
|
`
|
|
|
|
const testTransformDestroyEdgeMultiStr = `
|
|
test.A (destroy)
|
|
test.B (destroy)
|
|
test.C (destroy)
|
|
test.B (destroy)
|
|
test.C (destroy)
|
|
test.C (destroy)
|
|
`
|
|
|
|
const testTransformDestroyEdgeSelfRefStr = `
|
|
test.A (destroy)
|
|
`
|
|
|
|
const testTransformDestroyEdgeModuleStr = `
|
|
aws_instance.a (destroy)
|
|
module.child.aws_instance.b (destroy)
|
|
aws_instance.a (destroy)
|
|
`
|