mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
EvalValidateSelfRef needs schema in order to extract references. It was previously expecting a *configschema.Block directly, but we weren't actually passing that in from anywhere except the tests because it's not available directly in that form during the evaltree for node_resource_validate. Instead, we now pass in the whole *ProviderSchema for the associated provider and have this EvalNode find the schema itself based on the address. This breaks some of the generality of this node (now only really works for resource addresses) but that's okay since we have no other use-case right now anyway.
116 lines
2.2 KiB
Go
116 lines
2.2 KiB
Go
package terraform
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/config/configschema"
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/hashicorp/hcl2/hcltest"
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
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
|
|
}{
|
|
{
|
|
"no references at all",
|
|
rAddr,
|
|
hcltest.MockExprLiteral(cty.StringVal("bar")),
|
|
false,
|
|
},
|
|
|
|
{
|
|
"non self reference",
|
|
rAddr,
|
|
hcltest.MockExprTraversalSrc("aws_instance.bar.id"),
|
|
false,
|
|
},
|
|
|
|
{
|
|
"self reference",
|
|
rAddr,
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo.id"),
|
|
true,
|
|
},
|
|
|
|
{
|
|
"self reference other index",
|
|
rAddr,
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
|
|
false,
|
|
},
|
|
|
|
{
|
|
"self reference same index",
|
|
rAddr.Instance(addrs.IntKey(4)),
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
|
|
true,
|
|
},
|
|
|
|
{
|
|
"self reference whole",
|
|
rAddr.Instance(addrs.IntKey(4)),
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo"),
|
|
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,
|
|
},
|
|
},
|
|
})
|
|
|
|
ps := &ProviderSchema{
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
"aws_instance": &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {
|
|
Type: cty.String,
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
n := &EvalValidateSelfRef{
|
|
Addr: test.Addr,
|
|
Config: body,
|
|
ProviderSchema: &ps,
|
|
}
|
|
result, err := n.Eval(nil)
|
|
if result != nil {
|
|
t.Fatal("result should always be nil")
|
|
}
|
|
diags := tfdiags.Diagnostics(nil).Append(err)
|
|
if diags.HasErrors() != test.Err {
|
|
if test.Err {
|
|
t.Errorf("unexpected success; want error")
|
|
} else {
|
|
t.Errorf("unexpected error\n\n%s", diags.Err())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|