terraform/internal/configs/configschema/empty_value_test.go

258 lines
5.2 KiB
Go
Raw Normal View History

package configschema
import (
"fmt"
"testing"
"github.com/apparentlymart/go-dump/dump"
"github.com/davecgh/go-spew/spew"
"github.com/zclconf/go-cty/cty"
)
func TestBlockEmptyValue(t *testing.T) {
tests := []struct {
Schema *Block
Want cty.Value
}{
{
&Block{},
cty.EmptyObjectVal,
},
{
&Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
cty.ObjectVal(map[string]cty.Value{
"str": cty.NullVal(cty.String),
}),
},
{
&Block{
BlockTypes: map[string]*NestedBlock{
"single": {
Nesting: NestingSingle,
Block: Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"single": cty.NullVal(cty.Object(map[string]cty.Type{
"str": cty.String,
})),
}),
},
configs/configschema: Introduce the NestingGroup mode for blocks In study of existing providers we've found a pattern we werent previously accounting for of using a nested block type to represent a group of arguments that relate to a particular feature that is always enabled but where it improves configuration readability to group all of its settings together in a nested block. The existing NestingSingle was not a good fit for this because it is designed under the assumption that the presence or absence of the block has some significance in enabling or disabling the relevant feature, and so for these always-active cases we'd generate a misleading plan where the settings for the feature appear totally absent, rather than showing the default values that will be selected. NestingGroup is, therefore, a slight variation of NestingSingle where presence vs. absence of the block is not distinguishable (it's never null) and instead its contents are treated as unset when the block is absent. This then in turn causes any default values associated with the nested arguments to be honored and displayed in the plan whenever the block is not explicitly configured. The current SDK cannot activate this mode, but that's okay because its "legacy type system" opt-out flag allows it to force a block to be processed in this way anyway. We're adding this now so that we can introduce the feature in a future SDK without causing a breaking change to the protocol, since the set of possible block nesting modes is not extensible.
2019-04-09 00:32:53 +02:00
{
&Block{
BlockTypes: map[string]*NestedBlock{
"group": {
Nesting: NestingGroup,
Block: Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"group": cty.ObjectVal(map[string]cty.Value{
"str": cty.NullVal(cty.String),
}),
}),
},
{
&Block{
BlockTypes: map[string]*NestedBlock{
"list": {
Nesting: NestingList,
Block: Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"list": cty.ListValEmpty(cty.Object(map[string]cty.Type{
"str": cty.String,
})),
}),
},
{
&Block{
BlockTypes: map[string]*NestedBlock{
"list_dynamic": {
Nesting: NestingList,
Block: Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.DynamicPseudoType, Required: true},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"list_dynamic": cty.EmptyTupleVal,
}),
},
{
&Block{
BlockTypes: map[string]*NestedBlock{
"map": {
Nesting: NestingMap,
Block: Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"map": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"str": cty.String,
})),
}),
},
{
&Block{
BlockTypes: map[string]*NestedBlock{
"map_dynamic": {
Nesting: NestingMap,
Block: Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.DynamicPseudoType, Required: true},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"map_dynamic": cty.EmptyObjectVal,
}),
},
{
&Block{
BlockTypes: map[string]*NestedBlock{
"set": {
Nesting: NestingSet,
Block: Block{
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"set": cty.SetValEmpty(cty.Object(map[string]cty.Type{
"str": cty.String,
})),
}),
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%#v", test.Schema), func(t *testing.T) {
got := test.Schema.EmptyValue()
if !test.Want.RawEquals(got) {
t.Errorf("wrong result\nschema: %s\ngot: %s\nwant: %s", spew.Sdump(test.Schema), dump.Value(got), dump.Value(test.Want))
}
})
}
}
// Attribute EmptyValue() is well covered by the Block tests above; these tests
// focus on the behavior with NestedType field inside an Attribute
func TestAttributeEmptyValue(t *testing.T) {
tests := []struct {
Schema *Attribute
Want cty.Value
}{
{
&Attribute{},
cty.NilVal,
},
{
&Attribute{
Type: cty.String,
},
cty.NullVal(cty.String),
},
{
&Attribute{
NestedType: &Object{
Nesting: NestingSingle,
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
cty.NullVal(cty.Object(map[string]cty.Type{
"str": cty.String,
})),
},
{
&Attribute{
NestedType: &Object{
Nesting: NestingList,
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
cty.NullVal(cty.List(
cty.Object(map[string]cty.Type{
"str": cty.String,
}),
)),
},
{
&Attribute{
NestedType: &Object{
Nesting: NestingMap,
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
cty.NullVal(cty.Map(
cty.Object(map[string]cty.Type{
"str": cty.String,
}),
)),
},
{
&Attribute{
NestedType: &Object{
Nesting: NestingSet,
Attributes: map[string]*Attribute{
"str": {Type: cty.String, Required: true},
},
},
},
cty.NullVal(cty.Set(
cty.Object(map[string]cty.Type{
"str": cty.String,
}),
)),
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%#v", test.Schema), func(t *testing.T) {
got := test.Schema.EmptyValue()
if !test.Want.RawEquals(got) {
t.Errorf("wrong result\nschema: %s\ngot: %s\nwant: %s", spew.Sdump(test.Schema), dump.Value(got), dump.Value(test.Want))
}
})
}
}