2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2016-09-16 16:18:43 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-10 17:59:47 -05:00
|
|
|
"github.com/go-test/deep"
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2021-09-10 09:58:44 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
2016-09-16 16:18:43 -05:00
|
|
|
)
|
|
|
|
|
2020-10-14 08:10:37 -05:00
|
|
|
func TestNodeModuleVariablePath(t *testing.T) {
|
2020-04-07 16:11:32 -05:00
|
|
|
n := &nodeModuleVariable{
|
|
|
|
Addr: addrs.RootModuleInstance.InputVariable("foo"),
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: &configs.Variable{
|
2021-09-10 09:58:44 -05:00
|
|
|
Name: "foo",
|
|
|
|
Type: cty.String,
|
|
|
|
ConstraintType: cty.String,
|
2018-05-04 21:24:06 -05:00
|
|
|
},
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
|
2018-05-10 18:20:34 -05:00
|
|
|
want := addrs.RootModuleInstance
|
|
|
|
got := n.Path()
|
|
|
|
if got.String() != want.String() {
|
|
|
|
t.Fatalf("wrong module address %s; want %s", got, want)
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 08:10:37 -05:00
|
|
|
func TestNodeModuleVariableReferenceableName(t *testing.T) {
|
2020-04-07 16:11:32 -05:00
|
|
|
n := &nodeExpandModuleVariable{
|
|
|
|
Addr: addrs.InputVariable{Name: "foo"},
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: &configs.Variable{
|
2021-09-10 09:58:44 -05:00
|
|
|
Name: "foo",
|
|
|
|
Type: cty.String,
|
|
|
|
ConstraintType: cty.String,
|
2018-05-04 21:24:06 -05:00
|
|
|
},
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
{
|
|
|
|
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()
|
2020-10-14 08:10:37 -05:00
|
|
|
wantSelfPath := addrs.RootModuleInstance
|
2018-05-04 21:24:06 -05:00
|
|
|
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)
|
|
|
|
}
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
2018-05-04 21:24:06 -05:00
|
|
|
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
|
2020-10-14 08:10:37 -05:00
|
|
|
func TestNodeModuleVariableReference(t *testing.T) {
|
2020-04-07 16:11:32 -05:00
|
|
|
n := &nodeExpandModuleVariable{
|
2020-10-14 08:10:37 -05:00
|
|
|
Addr: addrs.InputVariable{Name: "foo"},
|
|
|
|
Module: addrs.RootModule.Child("bar"),
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: &configs.Variable{
|
2021-09-10 09:58:44 -05:00
|
|
|
Name: "foo",
|
|
|
|
Type: cty.String,
|
|
|
|
ConstraintType: cty.String,
|
2018-05-04 21:24:06 -05:00
|
|
|
},
|
|
|
|
Expr: &hclsyntax.ScopeTraversalExpr{
|
|
|
|
Traversal: hcl.Traversal{
|
|
|
|
hcl.TraverseRoot{Name: "var"},
|
|
|
|
hcl.TraverseAttr{Name: "foo"},
|
|
|
|
},
|
|
|
|
},
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
|
2018-05-10 17:59:47 -05:00
|
|
|
want := []*addrs.Reference{
|
2018-05-04 21:24:06 -05:00
|
|
|
{
|
|
|
|
Subject: addrs.InputVariable{Name: "foo"},
|
|
|
|
},
|
|
|
|
}
|
2018-05-10 17:59:47 -05:00
|
|
|
got := n.References()
|
|
|
|
for _, problem := range deep.Equal(got, want) {
|
|
|
|
t.Error(problem)
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 08:10:37 -05:00
|
|
|
func TestNodeModuleVariableReference_grandchild(t *testing.T) {
|
2020-04-07 16:11:32 -05:00
|
|
|
n := &nodeExpandModuleVariable{
|
2020-10-14 08:10:37 -05:00
|
|
|
Addr: addrs.InputVariable{Name: "foo"},
|
|
|
|
Module: addrs.RootModule.Child("bar"),
|
2018-05-04 21:24:06 -05:00
|
|
|
Config: &configs.Variable{
|
2021-09-10 09:58:44 -05:00
|
|
|
Name: "foo",
|
|
|
|
Type: cty.String,
|
|
|
|
ConstraintType: cty.String,
|
2018-05-04 21:24:06 -05:00
|
|
|
},
|
|
|
|
Expr: &hclsyntax.ScopeTraversalExpr{
|
|
|
|
Traversal: hcl.Traversal{
|
|
|
|
hcl.TraverseRoot{Name: "var"},
|
|
|
|
hcl.TraverseAttr{Name: "foo"},
|
|
|
|
},
|
|
|
|
},
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
|
2018-05-10 17:59:47 -05:00
|
|
|
want := []*addrs.Reference{
|
2018-05-04 21:24:06 -05:00
|
|
|
{
|
|
|
|
Subject: addrs.InputVariable{Name: "foo"},
|
|
|
|
},
|
|
|
|
}
|
2018-05-10 17:59:47 -05:00
|
|
|
got := n.References()
|
|
|
|
for _, problem := range deep.Equal(got, want) {
|
|
|
|
t.Error(problem)
|
2016-09-16 16:18:43 -05:00
|
|
|
}
|
|
|
|
}
|