2017-01-19 05:49:31 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-01-19 05:49:31 +01:00
|
|
|
)
|
|
|
|
|
2019-02-26 01:06:09 +01:00
|
|
|
func TestBackendPrepare(t *testing.T) {
|
2017-01-19 05:49:31 +01:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
B *Backend
|
2018-03-21 02:43:02 +01:00
|
|
|
Config map[string]cty.Value
|
2019-02-26 01:06:09 +01:00
|
|
|
Expect map[string]cty.Value
|
2017-01-19 05:49:31 +01:00
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Basic required field",
|
|
|
|
&Backend{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Required: true,
|
|
|
|
Type: TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-03-21 02:43:02 +01:00
|
|
|
map[string]cty.Value{},
|
2019-02-26 01:06:09 +01:00
|
|
|
map[string]cty.Value{},
|
2017-01-19 05:49:31 +01:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2019-03-02 00:45:06 +01:00
|
|
|
{
|
|
|
|
"Null config",
|
|
|
|
&Backend{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Required: true,
|
|
|
|
Type: TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
map[string]cty.Value{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2017-01-19 05:49:31 +01:00
|
|
|
{
|
|
|
|
"Basic required field set",
|
|
|
|
&Backend{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Required: true,
|
|
|
|
Type: TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-03-21 02:43:02 +01:00
|
|
|
map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
2017-01-19 05:49:31 +01:00
|
|
|
},
|
2019-02-26 01:06:09 +01:00
|
|
|
map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"unused default",
|
|
|
|
&Backend{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Optional: true,
|
|
|
|
Type: TypeString,
|
|
|
|
Default: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
},
|
|
|
|
map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"default",
|
|
|
|
&Backend{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]cty.Value{},
|
|
|
|
map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("baz"),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"default func",
|
|
|
|
&Backend{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: func() (interface{}, error) {
|
|
|
|
return "baz", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]cty.Value{},
|
|
|
|
map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("baz"),
|
|
|
|
},
|
2017-01-19 05:49:31 +01:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
2019-03-02 00:45:06 +01:00
|
|
|
cfgVal := cty.NullVal(cty.Object(map[string]cty.Type{}))
|
|
|
|
if tc.Config != nil {
|
|
|
|
cfgVal = cty.ObjectVal(tc.Config)
|
|
|
|
}
|
|
|
|
configVal, diags := tc.B.PrepareConfig(cfgVal)
|
2018-03-21 02:43:02 +01:00
|
|
|
if diags.HasErrors() != tc.Err {
|
2019-02-26 01:06:09 +01:00
|
|
|
for _, d := range diags {
|
|
|
|
t.Error(d.Description())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.Err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := cty.ObjectVal(tc.Expect)
|
|
|
|
if !expect.RawEquals(configVal) {
|
|
|
|
t.Fatalf("\nexpected: %#v\ngot: %#v\n", expect, configVal)
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendConfigure(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
B *Backend
|
2018-03-21 02:43:02 +01:00
|
|
|
Config map[string]cty.Value
|
2017-01-19 05:49:31 +01:00
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Basic config",
|
|
|
|
&Backend{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: func(ctx context.Context) error {
|
|
|
|
d := FromContextBackendConfig(ctx)
|
|
|
|
if d.Get("foo").(int) != 42 {
|
|
|
|
return fmt.Errorf("bad config data")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2018-03-21 02:43:02 +01:00
|
|
|
map[string]cty.Value{
|
|
|
|
"foo": cty.NumberIntVal(42),
|
2017-01-19 05:49:31 +01:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags := tc.B.Configure(cty.ObjectVal(tc.Config))
|
|
|
|
if diags.HasErrors() != tc.Err {
|
|
|
|
t.Errorf("wrong number of diagnostics")
|
2017-01-19 05:49:31 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|