github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/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 "github.com/zclconf/go-cty/cty" 11 12 "github.com/hashicorp/terraform/internal/addrs" 13 "github.com/hashicorp/terraform/internal/configs" 14 ) 15 16 func TestNodeModuleVariablePath(t *testing.T) { 17 n := &nodeModuleVariable{ 18 Addr: addrs.RootModuleInstance.InputVariable("foo"), 19 Config: &configs.Variable{ 20 Name: "foo", 21 Type: cty.String, 22 ConstraintType: cty.String, 23 }, 24 } 25 26 want := addrs.RootModuleInstance 27 got := n.Path() 28 if got.String() != want.String() { 29 t.Fatalf("wrong module address %s; want %s", got, want) 30 } 31 } 32 33 func TestNodeModuleVariableReferenceableName(t *testing.T) { 34 n := &nodeExpandModuleVariable{ 35 Addr: addrs.InputVariable{Name: "foo"}, 36 Config: &configs.Variable{ 37 Name: "foo", 38 Type: cty.String, 39 ConstraintType: cty.String, 40 }, 41 } 42 43 { 44 expected := []addrs.Referenceable{ 45 addrs.InputVariable{Name: "foo"}, 46 } 47 actual := n.ReferenceableAddrs() 48 if !reflect.DeepEqual(actual, expected) { 49 t.Fatalf("%#v != %#v", actual, expected) 50 } 51 } 52 53 { 54 gotSelfPath, gotReferencePath := n.ReferenceOutside() 55 wantSelfPath := addrs.RootModuleInstance 56 wantReferencePath := addrs.RootModuleInstance 57 if got, want := gotSelfPath.String(), wantSelfPath.String(); got != want { 58 t.Errorf("wrong self path\ngot: %s\nwant: %s", got, want) 59 } 60 if got, want := gotReferencePath.String(), wantReferencePath.String(); got != want { 61 t.Errorf("wrong reference path\ngot: %s\nwant: %s", got, want) 62 } 63 } 64 65 } 66 67 func TestNodeModuleVariableReference(t *testing.T) { 68 n := &nodeExpandModuleVariable{ 69 Addr: addrs.InputVariable{Name: "foo"}, 70 Module: addrs.RootModule.Child("bar"), 71 Config: &configs.Variable{ 72 Name: "foo", 73 Type: cty.String, 74 ConstraintType: cty.String, 75 }, 76 Expr: &hclsyntax.ScopeTraversalExpr{ 77 Traversal: hcl.Traversal{ 78 hcl.TraverseRoot{Name: "var"}, 79 hcl.TraverseAttr{Name: "foo"}, 80 }, 81 }, 82 } 83 84 want := []*addrs.Reference{ 85 { 86 Subject: addrs.InputVariable{Name: "foo"}, 87 }, 88 } 89 got := n.References() 90 for _, problem := range deep.Equal(got, want) { 91 t.Error(problem) 92 } 93 } 94 95 func TestNodeModuleVariableReference_grandchild(t *testing.T) { 96 n := &nodeExpandModuleVariable{ 97 Addr: addrs.InputVariable{Name: "foo"}, 98 Module: addrs.RootModule.Child("bar"), 99 Config: &configs.Variable{ 100 Name: "foo", 101 Type: cty.String, 102 ConstraintType: cty.String, 103 }, 104 Expr: &hclsyntax.ScopeTraversalExpr{ 105 Traversal: hcl.Traversal{ 106 hcl.TraverseRoot{Name: "var"}, 107 hcl.TraverseAttr{Name: "foo"}, 108 }, 109 }, 110 } 111 112 want := []*addrs.Reference{ 113 { 114 Subject: addrs.InputVariable{Name: "foo"}, 115 }, 116 } 117 got := n.References() 118 for _, problem := range deep.Equal(got, want) { 119 t.Error(problem) 120 } 121 }