opentofu/terraform/eval_validate_selfref_test.go

94 lines
1.6 KiB
Go
Raw Normal View History

2017-01-25 23:00:45 -06:00
package terraform
import (
"fmt"
"testing"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcltest"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
2017-01-25 23:00:45 -06:00
)
func TestEvalValidateSelfRef(t *testing.T) {
rAddr := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
}
tests := []struct {
Name string
Addr addrs.Referenceable
Expr hcl.Expression
Err bool
2017-01-25 23:00:45 -06:00
}{
{
"no references at all",
rAddr,
hcltest.MockExprLiteral(cty.StringVal("bar")),
2017-01-25 23:00:45 -06:00
false,
},
{
"non self reference",
rAddr,
hcltest.MockExprTraversalSrc("aws_instance.bar.id"),
2017-01-25 23:00:45 -06:00
false,
},
{
"self reference",
rAddr,
hcltest.MockExprTraversalSrc("aws_instance.foo.id"),
2017-01-25 23:00:45 -06:00
true,
},
{
"self reference other index",
rAddr,
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
2017-01-25 23:00:45 -06:00
false,
},
{
"self reference same index",
rAddr.Instance(addrs.IntKey(4)),
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
2017-01-25 23:00:45 -06:00
true,
},
{
"self reference whole",
rAddr.Instance(addrs.IntKey(4)),
hcltest.MockExprTraversalSrc("aws_instance.foo"),
2017-01-25 23:00:45 -06:00
true,
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d-%s", i, test.Name), func(t *testing.T) {
body := hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"foo": {
Name: "foo",
Expr: test.Expr,
},
},
})
2017-01-25 23:00:45 -06:00
n := &EvalValidateSelfRef{
Addr: test.Addr,
Config: body,
}
2017-01-25 23:00:45 -06:00
result, err := n.Eval(nil)
if result != nil {
t.Fatal("result should always be nil")
}
if (err != nil) != test.Err {
2017-01-25 23:00:45 -06:00
t.Fatalf("err: %s", err)
}
})
}
}