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
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackendValidate(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 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{},
|
2017-01-19 05:49:31 +01:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"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
|
|
|
},
|
|
|
|
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.ValidateConfig(cty.ObjectVal(tc.Config))
|
|
|
|
if diags.HasErrors() != tc.Err {
|
|
|
|
t.Errorf("wrong number of diagnostics")
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|