opentofu/helper/schema/backend_test.go
Martin Atkins 5782357c28 backend: Update interface and implementations for new config loader
The new config loader requires some steps to happen in a different
order, particularly in regard to knowing the schema in order to
decode the configuration.

Here we lean directly on the configschema package, rather than
on helper/schema.Backend as before, because it's generally
sufficient for our needs here and this prepares us for the
helper/schema package later moving out into its own repository
to seed a "plugin SDK".
2018-10-16 18:39:12 -07:00

101 lines
1.7 KiB
Go

package schema
import (
"context"
"fmt"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestBackendValidate(t *testing.T) {
cases := []struct {
Name string
B *Backend
Config map[string]cty.Value
Err bool
}{
{
"Basic required field",
&Backend{
Schema: map[string]*Schema{
"foo": &Schema{
Required: true,
Type: TypeString,
},
},
},
map[string]cty.Value{},
true,
},
{
"Basic required field set",
&Backend{
Schema: map[string]*Schema{
"foo": &Schema{
Required: true,
Type: TypeString,
},
},
},
map[string]cty.Value{
"foo": cty.StringVal("bar"),
},
false,
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
diags := tc.B.ValidateConfig(cty.ObjectVal(tc.Config))
if diags.HasErrors() != tc.Err {
t.Errorf("wrong number of diagnostics")
}
})
}
}
func TestBackendConfigure(t *testing.T) {
cases := []struct {
Name string
B *Backend
Config map[string]cty.Value
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
},
},
map[string]cty.Value{
"foo": cty.NumberIntVal(42),
},
false,
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
diags := tc.B.Configure(cty.ObjectVal(tc.Config))
if diags.HasErrors() != tc.Err {
t.Errorf("wrong number of diagnostics")
}
})
}
}