@ -1,16 +1,40 @@
package schema
import (
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/zclconf/go-cty/cty"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/configs/configschema"
)
var (
ignoreUnexported = cmpopts . IgnoreUnexported ( cty . Type { } )
equateEmpty = cmpopts . EquateEmpty ( )
)
// add the implicit "id" attribute for test resources
func testResource ( block * configschema . Block ) * configschema . Block {
if block . Attributes == nil {
block . Attributes = make ( map [ string ] * configschema . Attribute )
}
if block . BlockTypes == nil {
block . BlockTypes = make ( map [ string ] * configschema . NestedBlock )
}
if block . Attributes [ "id" ] == nil {
block . Attributes [ "id" ] = & configschema . Attribute {
Type : cty . String ,
Optional : true ,
Computed : true ,
}
}
return block
}
func TestSchemaMapCoreConfigSchema ( t * testing . T ) {
tests := map [ string ] struct {
Schema map [ string ] * Schema
@ -18,7 +42,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
} {
"empty" : {
map [ string ] * Schema { } ,
& configschema . Block { } ,
testResource ( & configschema . Block { } ) ,
} ,
"primitives" : {
map [ string ] * Schema {
@ -41,7 +65,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
Computed : true ,
} ,
} ,
& configschema . Block {
testResource ( & configschema . Block {
Attributes : map [ string ] * configschema . Attribute {
"int" : {
Type : cty . Number ,
@ -63,7 +87,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
} ,
} ,
BlockTypes : map [ string ] * configschema . NestedBlock { } ,
} ,
} ) ,
} ,
"simple collections" : {
map [ string ] * Schema {
@ -96,7 +120,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
// for pre-existing schemas.
} ,
} ,
& configschema . Block {
testResource ( & configschema . Block {
Attributes : map [ string ] * configschema . Attribute {
"list" : {
Type : cty . List ( cty . Number ) ,
@ -116,7 +140,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
} ,
} ,
BlockTypes : map [ string ] * configschema . NestedBlock { } ,
} ,
} ) ,
} ,
"incorrectly-specified collections" : {
// Historically we tolerated setting a type directly as the Elem
@ -140,7 +164,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
Elem : TypeBool ,
} ,
} ,
& configschema . Block {
testResource ( & configschema . Block {
Attributes : map [ string ] * configschema . Attribute {
"list" : {
Type : cty . List ( cty . Number ) ,
@ -156,7 +180,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
} ,
} ,
BlockTypes : map [ string ] * configschema . NestedBlock { } ,
} ,
} ) ,
} ,
"sub-resource collections" : {
map [ string ] * Schema {
@ -184,7 +208,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
} ,
} ,
} ,
& configschema . Block {
testResource ( & configschema . Block {
Attributes : map [ string ] * configschema . Attribute { } ,
BlockTypes : map [ string ] * configschema . NestedBlock {
"list" : {
@ -203,7 +227,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
Block : configschema . Block { } ,
} ,
} ,
} ,
} ) ,
} ,
"nested attributes and blocks" : {
map [ string ] * Schema {
@ -233,7 +257,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
} ,
} ,
} ,
& configschema . Block {
testResource ( & configschema . Block {
Attributes : map [ string ] * configschema . Attribute { } ,
BlockTypes : map [ string ] * configschema . NestedBlock {
"foo" : & configschema . NestedBlock {
@ -255,7 +279,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
MinItems : 1 , // because schema is Required
} ,
} ,
} ,
} ) ,
} ,
"sensitive" : {
map [ string ] * Schema {
@ -265,7 +289,7 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
Sensitive : true ,
} ,
} ,
& configschema . Block {
testResource ( & configschema . Block {
Attributes : map [ string ] * configschema . Attribute {
"string" : {
Type : cty . String ,
@ -274,15 +298,15 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
} ,
} ,
BlockTypes : map [ string ] * configschema . NestedBlock { } ,
} ,
} ) ,
} ,
}
for name , test := range tests {
t . Run ( name , func ( t * testing . T ) {
got := schemaMap ( test . Schema ) . CoreConfigSchema ( )
if ! reflect. Deep Equal( got , test . Want ) {
t. Errorf ( "wrong result\ngot: %swant: %s" , spew . Sdump ( got ) , spew . Sdump ( test . Want ) )
if ! cmp. Equal( got , test . Want , equateEmpty , ignoreUnexported ) {
cmp. Diff ( got , test . Want , equateEmpty , ignoreUnexported )
}
} )
}