github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/node_module_variable_test.go (about) 1 package terraform 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/go-test/deep" 8 "github.com/hashicorp/hcl/v2" 9 "github.com/hashicorp/hcl/v2/hclsyntax" 10 11 "github.com/hashicorp/terraform-plugin-sdk/internal/addrs" 12 "github.com/hashicorp/terraform-plugin-sdk/internal/configs" 13 ) 14 15 func TestNodeApplyableModuleVariablePath(t *testing.T) { 16 n := &NodeApplyableModuleVariable{ 17 Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"), 18 Config: &configs.Variable{ 19 Name: "foo", 20 }, 21 } 22 23 want := addrs.RootModuleInstance 24 got := n.Path() 25 if got.String() != want.String() { 26 t.Fatalf("wrong module address %s; want %s", got, want) 27 } 28 } 29 30 func TestNodeApplyableModuleVariableReferenceableName(t *testing.T) { 31 n := &NodeApplyableModuleVariable{ 32 Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"), 33 Config: &configs.Variable{ 34 Name: "foo", 35 }, 36 } 37 38 { 39 expected := []addrs.Referenceable{ 40 addrs.InputVariable{Name: "foo"}, 41 } 42 actual := n.ReferenceableAddrs() 43 if !reflect.DeepEqual(actual, expected) { 44 t.Fatalf("%#v != %#v", actual, expected) 45 } 46 } 47 48 { 49 gotSelfPath, gotReferencePath := n.ReferenceOutside() 50 wantSelfPath := addrs.RootModuleInstance.Child("child", addrs.NoKey) 51 wantReferencePath := addrs.RootModuleInstance 52 if got, want := gotSelfPath.String(), wantSelfPath.String(); got != want { 53 t.Errorf("wrong self path\ngot: %s\nwant: %s", got, want) 54 } 55 if got, want := gotReferencePath.String(), wantReferencePath.String(); got != want { 56 t.Errorf("wrong reference path\ngot: %s\nwant: %s", got, want) 57 } 58 } 59 60 } 61 62 func TestNodeApplyableModuleVariableReference(t *testing.T) { 63 n := &NodeApplyableModuleVariable{ 64 Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).InputVariable("foo"), 65 Config: &configs.Variable{ 66 Name: "foo", 67 }, 68 Expr: &hclsyntax.ScopeTraversalExpr{ 69 Traversal: hcl.Traversal{ 70 hcl.TraverseRoot{Name: "var"}, 71 hcl.TraverseAttr{Name: "foo"}, 72 }, 73 }, 74 } 75 76 want := []*addrs.Reference{ 77 { 78 Subject: addrs.InputVariable{Name: "foo"}, 79 }, 80 } 81 got := n.References() 82 for _, problem := range deep.Equal(got, want) { 83 t.Error(problem) 84 } 85 } 86 87 func TestNodeApplyableModuleVariableReference_grandchild(t *testing.T) { 88 n := &NodeApplyableModuleVariable{ 89 Addr: addrs.RootModuleInstance. 90 Child("child", addrs.NoKey). 91 Child("grandchild", addrs.NoKey). 92 InputVariable("foo"), 93 Config: &configs.Variable{ 94 Name: "foo", 95 }, 96 Expr: &hclsyntax.ScopeTraversalExpr{ 97 Traversal: hcl.Traversal{ 98 hcl.TraverseRoot{Name: "var"}, 99 hcl.TraverseAttr{Name: "foo"}, 100 }, 101 }, 102 } 103 104 want := []*addrs.Reference{ 105 { 106 Subject: addrs.InputVariable{Name: "foo"}, 107 }, 108 } 109 got := n.References() 110 for _, problem := range deep.Equal(got, want) { 111 t.Error(problem) 112 } 113 }