mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-15 11:13:09 -06:00
ad6bb4a1d5
This function was previously checking for a path length greater than one because the older path format included an always present "root" element at the start. We now need to check for a totally-empty list, because otherwise we fail to add the expected prefix to the front of a path with only one element. This also includes some adjustments to the related tests and transforms that do not change behavior but do make the test results easier to understand and debug.
190 lines
4.2 KiB
Go
190 lines
4.2 KiB
Go
package terraform
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
)
|
|
|
|
func TestMissingProvisionerTransformer(t *testing.T) {
|
|
mod := testModule(t, "transform-provisioner-basic")
|
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
{
|
|
tf := &ConfigTransformer{Config: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &ProvisionerTransformer{}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformMissingProvisionerBasicStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
func TestMissingProvisionerTransformer_module(t *testing.T) {
|
|
mod := testModule(t, "transform-provisioner-module")
|
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
{
|
|
concreteResource := func(a *NodeAbstractResourceInstance) dag.Vertex {
|
|
return a
|
|
}
|
|
|
|
var state State
|
|
state.init()
|
|
state.Modules = []*ModuleState{
|
|
&ModuleState{
|
|
Path: []string{"root"},
|
|
Resources: map[string]*ResourceState{
|
|
"aws_instance.foo": &ResourceState{
|
|
Primary: &InstanceState{ID: "foo"},
|
|
},
|
|
},
|
|
},
|
|
|
|
&ModuleState{
|
|
Path: []string{"root", "child"},
|
|
Resources: map[string]*ResourceState{
|
|
"aws_instance.foo": &ResourceState{
|
|
Primary: &InstanceState{ID: "foo"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
tf := &StateTransformer{
|
|
Concrete: concreteResource,
|
|
State: &state,
|
|
}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
t.Logf("graph after StateTransformer:\n%s", g.StringWithNodeTypes())
|
|
}
|
|
|
|
{
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
t.Logf("graph after MissingProvisionerTransformer:\n%s", g.StringWithNodeTypes())
|
|
}
|
|
|
|
{
|
|
transform := &ProvisionerTransformer{}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
t.Logf("graph after ProvisionerTransformer:\n%s", g.StringWithNodeTypes())
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformMissingProvisionerModuleStr)
|
|
if actual != expected {
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestCloseProvisionerTransformer(t *testing.T) {
|
|
mod := testModule(t, "transform-provisioner-basic")
|
|
|
|
g := Graph{Path: addrs.RootModuleInstance}
|
|
{
|
|
tf := &ConfigTransformer{Config: mod}
|
|
if err := tf.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &AttachResourceConfigTransformer{Config: mod}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &ProvisionerTransformer{}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
transform := &CloseProvisionerTransformer{}
|
|
if err := transform.Transform(&g); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
expected := strings.TrimSpace(testTransformCloseProvisionerBasicStr)
|
|
if actual != expected {
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
}
|
|
}
|
|
|
|
const testTransformMissingProvisionerBasicStr = `
|
|
aws_instance.web
|
|
provisioner.shell
|
|
provisioner.shell
|
|
`
|
|
|
|
const testTransformMissingProvisionerModuleStr = `
|
|
aws_instance.foo
|
|
provisioner.shell
|
|
module.child.aws_instance.foo
|
|
module.child.provisioner.shell
|
|
module.child.provisioner.shell
|
|
provisioner.shell
|
|
`
|
|
|
|
const testTransformCloseProvisionerBasicStr = `
|
|
aws_instance.web
|
|
provisioner.shell
|
|
provisioner.shell
|
|
provisioner.shell (close)
|
|
aws_instance.web
|
|
`
|