opentofu/terraform/node_module_variable_test.go
James Bardin c59ecac870 rename module variables and remove extra methods
The variable nodes are not only used during plan and apply, so remove
those from there names. The "plan" node is now
`nodeExpandModuleVariable` and the "apply" node is now just
`nodeModuleVariable`.

Remove unnecessary methods, as the nodeModuleVariable is no longer used
in the full graph transformations.
2020-04-08 14:41:52 -04:00

111 lines
2.4 KiB
Go

package terraform
import (
"reflect"
"testing"
"github.com/go-test/deep"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
)
func TestnodeModuleVariablePath(t *testing.T) {
n := &nodeModuleVariable{
Addr: addrs.RootModuleInstance.InputVariable("foo"),
Config: &configs.Variable{
Name: "foo",
},
}
want := addrs.RootModuleInstance
got := n.Path()
if got.String() != want.String() {
t.Fatalf("wrong module address %s; want %s", got, want)
}
}
func TestnodeModuleVariableReferenceableName(t *testing.T) {
n := &nodeExpandModuleVariable{
Addr: addrs.InputVariable{Name: "foo"},
Config: &configs.Variable{
Name: "foo",
},
}
{
expected := []addrs.Referenceable{
addrs.InputVariable{Name: "foo"},
}
actual := n.ReferenceableAddrs()
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("%#v != %#v", actual, expected)
}
}
{
gotSelfPath, gotReferencePath := n.ReferenceOutside()
wantSelfPath := addrs.RootModuleInstance.Child("child", addrs.NoKey)
wantReferencePath := addrs.RootModuleInstance
if got, want := gotSelfPath.String(), wantSelfPath.String(); got != want {
t.Errorf("wrong self path\ngot: %s\nwant: %s", got, want)
}
if got, want := gotReferencePath.String(), wantReferencePath.String(); got != want {
t.Errorf("wrong reference path\ngot: %s\nwant: %s", got, want)
}
}
}
func TestnodeModuleVariableReference(t *testing.T) {
n := &nodeExpandModuleVariable{
Addr: addrs.InputVariable{Name: "foo"},
Config: &configs.Variable{
Name: "foo",
},
Expr: &hclsyntax.ScopeTraversalExpr{
Traversal: hcl.Traversal{
hcl.TraverseRoot{Name: "var"},
hcl.TraverseAttr{Name: "foo"},
},
},
}
want := []*addrs.Reference{
{
Subject: addrs.InputVariable{Name: "foo"},
},
}
got := n.References()
for _, problem := range deep.Equal(got, want) {
t.Error(problem)
}
}
func TestnodeModuleVariableReference_grandchild(t *testing.T) {
n := &nodeExpandModuleVariable{
Addr: addrs.InputVariable{Name: "foo"},
Config: &configs.Variable{
Name: "foo",
},
Expr: &hclsyntax.ScopeTraversalExpr{
Traversal: hcl.Traversal{
hcl.TraverseRoot{Name: "var"},
hcl.TraverseAttr{Name: "foo"},
},
},
}
want := []*addrs.Reference{
{
Subject: addrs.InputVariable{Name: "foo"},
},
}
got := n.References()
for _, problem := range deep.Equal(got, want) {
t.Error(problem)
}
}