mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 16:31:10 -06:00
terraform: tests for module variable node
This commit is contained in:
parent
3fb83f013e
commit
0d815872e1
@ -34,7 +34,7 @@ func (n *NodeApplyableModuleVariable) Path() []string {
|
||||
return n.PathValue[:len(n.PathValue)-1]
|
||||
}
|
||||
|
||||
return nil
|
||||
return rootModulePath
|
||||
}
|
||||
|
||||
// GraphNodeReferenceGlobal
|
||||
@ -58,15 +58,18 @@ func (n *NodeApplyableModuleVariable) References() []string {
|
||||
}
|
||||
|
||||
// Can't depend on anything if we're in the root
|
||||
path := n.Path()
|
||||
if len(path) < 2 {
|
||||
if len(n.PathValue) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Otherwise, we depend on anything that is in our value, but
|
||||
// specifically in the namespace of the parent path.
|
||||
// Create the prefix based on the path
|
||||
prefix := modulePrefixStr(path) + "."
|
||||
var prefix string
|
||||
if p := n.Path(); len(p) > 0 {
|
||||
prefix = modulePrefixStr(p)
|
||||
}
|
||||
|
||||
result := ReferencesFromConfig(n.Value)
|
||||
return modulePrefixList(result, prefix)
|
||||
}
|
||||
|
66
terraform/node_module_variable_test.go
Normal file
66
terraform/node_module_variable_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/config"
|
||||
)
|
||||
|
||||
func TestNodeApplyableModuleVariablePath(t *testing.T) {
|
||||
n := &NodeApplyableModuleVariable{
|
||||
PathValue: []string{"root", "child"},
|
||||
Config: &config.Variable{Name: "foo"},
|
||||
}
|
||||
|
||||
expected := []string{"root"}
|
||||
actual := n.Path()
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("%#v != %#v", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeApplyableModuleVariableReferenceableName(t *testing.T) {
|
||||
n := &NodeApplyableModuleVariable{
|
||||
PathValue: []string{"root", "child"},
|
||||
Config: &config.Variable{Name: "foo"},
|
||||
}
|
||||
|
||||
expected := []string{"module.child.var.foo"}
|
||||
actual := n.ReferenceableName()
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("%#v != %#v", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeApplyableModuleVariableReference(t *testing.T) {
|
||||
n := &NodeApplyableModuleVariable{
|
||||
PathValue: []string{"root", "child"},
|
||||
Config: &config.Variable{Name: "foo"},
|
||||
Value: config.TestRawConfig(t, map[string]interface{}{
|
||||
"foo": `${var.foo}`,
|
||||
}),
|
||||
}
|
||||
|
||||
expected := []string{"var.foo"}
|
||||
actual := n.References()
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("%#v != %#v", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeApplyableModuleVariableReference_grandchild(t *testing.T) {
|
||||
n := &NodeApplyableModuleVariable{
|
||||
PathValue: []string{"root", "child", "grandchild"},
|
||||
Config: &config.Variable{Name: "foo"},
|
||||
Value: config.TestRawConfig(t, map[string]interface{}{
|
||||
"foo": `${var.foo}`,
|
||||
}),
|
||||
}
|
||||
|
||||
expected := []string{"module.child.var.foo"}
|
||||
actual := n.References()
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("%#v != %#v", actual, expected)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user