2019-01-24 01:14:34 +01:00
|
|
|
package jsonconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2021-06-14 15:22:22 +02:00
|
|
|
"github.com/hashicorp/hcl/v2/hcltest"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2019-01-24 01:14:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalExpressions(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-06-14 15:22:22 +02:00
|
|
|
Input hcl.Body
|
|
|
|
Want expressions
|
2019-01-24 01:14:34 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
&hclsyntax.Body{
|
|
|
|
Attributes: hclsyntax.Attributes{
|
|
|
|
"foo": &hclsyntax.Attribute{
|
|
|
|
Expr: &hclsyntax.LiteralValueExpr{
|
|
|
|
Val: cty.StringVal("bar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-14 15:22:22 +02:00
|
|
|
expressions{
|
|
|
|
"foo": expression{
|
|
|
|
ConstantValue: json.RawMessage([]byte(`"bar"`)),
|
|
|
|
References: []string(nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"foo": {
|
|
|
|
Name: "foo",
|
|
|
|
Expr: hcltest.MockExprTraversalSrc(`var.list[1]`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
expressions{
|
|
|
|
"foo": expression{
|
|
|
|
References: []string{"var.list[1]", "var.list"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
2019-01-24 01:14:34 +01:00
|
|
|
"foo": {
|
2021-06-14 15:22:22 +02:00
|
|
|
Name: "foo",
|
|
|
|
Expr: hcltest.MockExprTraversalSrc(`data.template_file.foo[1].vars["baz"]`),
|
2019-01-24 01:14:34 +01:00
|
|
|
},
|
|
|
|
},
|
2021-06-14 15:22:22 +02:00
|
|
|
}),
|
|
|
|
expressions{
|
|
|
|
"foo": expression{
|
|
|
|
References: []string{"data.template_file.foo[1].vars[\"baz\"]", "data.template_file.foo[1].vars", "data.template_file.foo[1]", "data.template_file.foo"},
|
|
|
|
},
|
2019-01-24 01:14:34 +01:00
|
|
|
},
|
2021-06-14 15:22:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"foo": {
|
|
|
|
Name: "foo",
|
|
|
|
Expr: hcltest.MockExprTraversalSrc(`module.foo.bar`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2019-01-24 01:14:34 +01:00
|
|
|
expressions{
|
|
|
|
"foo": expression{
|
2021-06-14 15:22:22 +02:00
|
|
|
References: []string{"module.foo.bar", "module.foo"},
|
2019-01-24 01:14:34 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-09-03 19:53:52 +02:00
|
|
|
{
|
|
|
|
hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Blocks: hcl.Blocks{
|
|
|
|
{
|
|
|
|
Type: "block_to_attr",
|
|
|
|
Body: hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"foo": {
|
|
|
|
Name: "foo",
|
|
|
|
Expr: hcltest.MockExprTraversalSrc(`module.foo.bar`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
expressions{
|
|
|
|
"block_to_attr": expression{
|
|
|
|
References: []string{"module.foo.bar", "module.foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-01-24 01:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2021-06-14 15:22:22 +02:00
|
|
|
schema := &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"foo": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2021-09-03 19:53:52 +02:00
|
|
|
"block_to_attr": {
|
|
|
|
Type: cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"foo": cty.String,
|
|
|
|
})),
|
|
|
|
},
|
2021-06-14 15:22:22 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
got := marshalExpressions(test.Input, schema)
|
2019-01-24 01:14:34 +01:00
|
|
|
if !reflect.DeepEqual(got, test.Want) {
|
2021-06-14 15:22:22 +02:00
|
|
|
t.Errorf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
|
2019-01-24 01:14:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalExpression(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Input hcl.Expression
|
|
|
|
Want expression
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
expression{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got := marshalExpression(test.Input)
|
|
|
|
if !reflect.DeepEqual(got, test.Want) {
|
|
|
|
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|