154 lines
2.5 KiB
Go
154 lines
2.5 KiB
Go
package schema
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestProviderConfigure(t *testing.T) {
|
|
cases := []struct {
|
|
P *Provider
|
|
Config map[string]interface{}
|
|
Err bool
|
|
}{
|
|
{
|
|
P: &Provider{},
|
|
Config: nil,
|
|
Err: false,
|
|
},
|
|
|
|
{
|
|
P: &Provider{
|
|
Schema: map[string]*Schema{
|
|
"foo": &Schema{
|
|
Type: TypeInt,
|
|
Optional: true,
|
|
},
|
|
},
|
|
|
|
ConfigureFunc: func(d *ResourceData) error {
|
|
if d.Get("foo").(int) == 42 {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("nope")
|
|
},
|
|
},
|
|
Config: map[string]interface{}{
|
|
"foo": 42,
|
|
},
|
|
Err: false,
|
|
},
|
|
|
|
{
|
|
P: &Provider{
|
|
Schema: map[string]*Schema{
|
|
"foo": &Schema{
|
|
Type: TypeInt,
|
|
Optional: true,
|
|
},
|
|
},
|
|
|
|
ConfigureFunc: func(d *ResourceData) error {
|
|
if d.Get("foo").(int) == 42 {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("nope")
|
|
},
|
|
},
|
|
Config: map[string]interface{}{
|
|
"foo": 52,
|
|
},
|
|
Err: true,
|
|
},
|
|
}
|
|
|
|
for i, tc := range cases {
|
|
c, err := config.NewRawConfig(tc.Config)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
err = tc.P.Configure(terraform.NewResourceConfig(c))
|
|
if (err != nil) != tc.Err {
|
|
t.Fatalf("%d: %s", i, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviderResources(t *testing.T) {
|
|
cases := []struct {
|
|
P *Provider
|
|
Result []terraform.ResourceType
|
|
}{
|
|
{
|
|
P: &Provider{},
|
|
Result: []terraform.ResourceType{},
|
|
},
|
|
|
|
{
|
|
P: &Provider{
|
|
ResourcesMap: map[string]*Resource{
|
|
"foo": nil,
|
|
"bar": nil,
|
|
},
|
|
},
|
|
Result: []terraform.ResourceType{
|
|
terraform.ResourceType{Name: "foo"},
|
|
terraform.ResourceType{Name: "bar"},
|
|
},
|
|
},
|
|
}
|
|
|
|
for i, tc := range cases {
|
|
actual := tc.P.Resources()
|
|
if !reflect.DeepEqual(actual, tc.Result) {
|
|
t.Fatalf("%d: %#v", i, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviderValidateResource(t *testing.T) {
|
|
cases := []struct {
|
|
P *Provider
|
|
Type string
|
|
Config map[string]interface{}
|
|
Err bool
|
|
}{
|
|
{
|
|
P: &Provider{},
|
|
Type: "foo",
|
|
Config: nil,
|
|
Err: true,
|
|
},
|
|
|
|
{
|
|
P: &Provider{
|
|
ResourcesMap: map[string]*Resource{
|
|
"foo": &Resource{},
|
|
},
|
|
},
|
|
Type: "foo",
|
|
Config: nil,
|
|
Err: false,
|
|
},
|
|
}
|
|
|
|
for i, tc := range cases {
|
|
c, err := config.NewRawConfig(tc.Config)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
_, es := tc.P.ValidateResource(tc.Type, terraform.NewResourceConfig(c))
|
|
if (len(es) > 0) != tc.Err {
|
|
t.Fatalf("%d: %#v", i, es)
|
|
}
|
|
}
|
|
}
|