From 0d815872e1ff1b952c03a88ceacac07c92ea8c72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 16 Sep 2016 14:18:43 -0700 Subject: [PATCH] terraform: tests for module variable node --- terraform/node_module_variable.go | 11 +++-- terraform/node_module_variable_test.go | 66 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 terraform/node_module_variable_test.go diff --git a/terraform/node_module_variable.go b/terraform/node_module_variable.go index 0fb3eb27a..9b7ddbf2f 100644 --- a/terraform/node_module_variable.go +++ b/terraform/node_module_variable.go @@ -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) } diff --git a/terraform/node_module_variable_test.go b/terraform/node_module_variable_test.go new file mode 100644 index 000000000..4fb6663b9 --- /dev/null +++ b/terraform/node_module_variable_test.go @@ -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) + } +}