mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-29 10:21:01 -06:00
36d0a50427
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
106 lines
2.0 KiB
Go
106 lines
2.0 KiB
Go
package terraform
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/hashicorp/hcl/v2/hcltest"
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestValidateSelfRef(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,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
diags := validateSelfRef(test.Addr, body, ps)
|
|
if diags.HasErrors() != test.Err {
|
|
if test.Err {
|
|
t.Errorf("unexpected success; want error")
|
|
} else {
|
|
t.Errorf("unexpected error\n\n%s", diags.Err())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|