2017-01-25 23:00:45 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-05 12:33:29 -05:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-05-10 19:57:02 -05:00
|
|
|
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hcltest"
|
2018-05-04 21:24:06 -05:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-01-25 23:00:45 -06:00
|
|
|
)
|
|
|
|
|
2020-12-08 09:52:39 -06:00
|
|
|
func TestValidateSelfRef(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
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
|
|
|
}{
|
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
"no references at all",
|
|
|
|
rAddr,
|
|
|
|
hcltest.MockExprLiteral(cty.StringVal("bar")),
|
2017-01-25 23:00:45 -06:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"non self reference",
|
2018-05-04 21:24:06 -05:00
|
|
|
rAddr,
|
|
|
|
hcltest.MockExprTraversalSrc("aws_instance.bar.id"),
|
2017-01-25 23:00:45 -06:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"self reference",
|
2018-05-04 21:24:06 -05:00
|
|
|
rAddr,
|
|
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo.id"),
|
2017-01-25 23:00:45 -06:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"self reference other index",
|
2018-05-04 21:24:06 -05:00
|
|
|
rAddr,
|
|
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
|
2017-01-25 23:00:45 -06:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"self reference same index",
|
2018-05-04 21:24:06 -05:00
|
|
|
rAddr.Instance(addrs.IntKey(4)),
|
|
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo[4].id"),
|
2017-01-25 23:00:45 -06:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2018-05-04 21:24:06 -05:00
|
|
|
"self reference whole",
|
|
|
|
rAddr.Instance(addrs.IntKey(4)),
|
|
|
|
hcltest.MockExprTraversalSrc("aws_instance.foo"),
|
2017-01-25 23:00:45 -06:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
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
|
|
|
|
2018-05-11 18:17:06 -05:00
|
|
|
ps := &ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"aws_instance": &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {
|
|
|
|
Type: cty.String,
|
|
|
|
Required: true,
|
|
|
|
},
|
2018-05-10 19:57:02 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-05-04 21:24:06 -05:00
|
|
|
}
|
2018-05-11 18:17:06 -05:00
|
|
|
|
2020-12-08 09:52:39 -06:00
|
|
|
diags := validateSelfRef(test.Addr, body, ps)
|
2018-05-10 19:57:02 -05:00
|
|
|
if diags.HasErrors() != test.Err {
|
|
|
|
if test.Err {
|
|
|
|
t.Errorf("unexpected success; want error")
|
|
|
|
} else {
|
|
|
|
t.Errorf("unexpected error\n\n%s", diags.Err())
|
|
|
|
}
|
2017-01-25 23:00:45 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|