2014-08-17 23:45:43 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2014-08-18 00:01:27 +02:00
|
|
|
"fmt"
|
2014-08-18 00:07:01 +02:00
|
|
|
"reflect"
|
2014-08-17 23:45:43 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-08-18 05:23:25 +02:00
|
|
|
func TestProvider_impl(t *testing.T) {
|
|
|
|
var _ terraform.ResourceProvider = new(Provider)
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:01:27 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-18 04:45:26 +02:00
|
|
|
ConfigureFunc: func(d *ResourceData) (interface{}, error) {
|
2014-08-18 00:01:27 +02:00
|
|
|
if d.Get("foo").(int) == 42 {
|
2014-08-18 04:45:26 +02:00
|
|
|
return nil, nil
|
2014-08-18 00:01:27 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 04:45:26 +02:00
|
|
|
return nil, fmt.Errorf("nope")
|
2014-08-18 00:01:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 42,
|
|
|
|
},
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
P: &Provider{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-18 04:45:26 +02:00
|
|
|
ConfigureFunc: func(d *ResourceData) (interface{}, error) {
|
2014-08-18 00:01:27 +02:00
|
|
|
if d.Get("foo").(int) == 42 {
|
2014-08-18 04:45:26 +02:00
|
|
|
return nil, nil
|
2014-08-18 00:01:27 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 04:45:26 +02:00
|
|
|
return nil, fmt.Errorf("nope")
|
2014-08-18 00:01:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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))
|
2015-10-08 14:48:04 +02:00
|
|
|
if err != nil != tc.Err {
|
2014-08-18 00:01:27 +02:00
|
|
|
t.Fatalf("%d: %s", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:07:01 +02:00
|
|
|
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: "bar"},
|
2014-08-18 04:32:11 +02:00
|
|
|
terraform.ResourceType{Name: "foo"},
|
2014-08-18 00:07:01 +02:00
|
|
|
},
|
|
|
|
},
|
2016-04-26 18:39:39 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
P: &Provider{
|
|
|
|
ResourcesMap: map[string]*Resource{
|
|
|
|
"foo": nil,
|
|
|
|
"bar": &Resource{Importer: &ResourceImporter{}},
|
|
|
|
"baz": nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Result: []terraform.ResourceType{
|
|
|
|
terraform.ResourceType{Name: "bar", Importable: true},
|
|
|
|
terraform.ResourceType{Name: "baz"},
|
|
|
|
terraform.ResourceType{Name: "foo"},
|
|
|
|
},
|
|
|
|
},
|
2014-08-18 00:07:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
actual := tc.P.Resources()
|
|
|
|
if !reflect.DeepEqual(actual, tc.Result) {
|
|
|
|
t.Fatalf("%d: %#v", i, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 01:52:04 +02:00
|
|
|
func TestProviderValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
P *Provider
|
|
|
|
Config map[string]interface{}
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
P: &Provider{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Config: nil,
|
|
|
|
Err: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
c, err := config.NewRawConfig(tc.Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, es := tc.P.Validate(terraform.NewResourceConfig(c))
|
2015-10-08 14:48:04 +02:00
|
|
|
if len(es) > 0 != tc.Err {
|
2015-06-24 01:52:04 +02:00
|
|
|
t.Fatalf("%d: %#v", i, es)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:45:43 +02:00
|
|
|
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{
|
2014-08-18 00:07:01 +02:00
|
|
|
ResourcesMap: map[string]*Resource{
|
2014-08-17 23:45:43 +02:00
|
|
|
"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))
|
2015-10-08 14:48:04 +02:00
|
|
|
if len(es) > 0 != tc.Err {
|
2014-08-17 23:45:43 +02:00
|
|
|
t.Fatalf("%d: %#v", i, es)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 00:26:31 +02:00
|
|
|
|
2016-05-04 20:18:45 +02:00
|
|
|
func TestProviderImportState_setsId(t *testing.T) {
|
|
|
|
var val string
|
|
|
|
stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) {
|
|
|
|
val = d.Id()
|
|
|
|
return []*ResourceData{d}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Provider{
|
|
|
|
ResourcesMap: map[string]*Resource{
|
|
|
|
"foo": &Resource{
|
|
|
|
Importer: &ResourceImporter{
|
|
|
|
State: stateFunc,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := p.ImportState(&terraform.InstanceInfo{
|
|
|
|
Type: "foo",
|
2016-05-04 21:42:02 +02:00
|
|
|
}, "bar")
|
2016-05-04 20:18:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val != "bar" {
|
|
|
|
t.Fatal("should set id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 01:36:31 +02:00
|
|
|
func TestProviderImportState_setsType(t *testing.T) {
|
|
|
|
var tVal string
|
|
|
|
stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) {
|
|
|
|
d.SetId("foo")
|
|
|
|
tVal = d.State().Ephemeral.Type
|
|
|
|
return []*ResourceData{d}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Provider{
|
|
|
|
ResourcesMap: map[string]*Resource{
|
|
|
|
"foo": &Resource{
|
|
|
|
Importer: &ResourceImporter{
|
|
|
|
State: stateFunc,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := p.ImportState(&terraform.InstanceInfo{
|
|
|
|
Type: "foo",
|
2016-05-04 21:42:02 +02:00
|
|
|
}, "bar")
|
2016-05-01 01:36:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tVal != "foo" {
|
|
|
|
t.Fatal("should set type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 00:26:31 +02:00
|
|
|
func TestProviderMeta(t *testing.T) {
|
|
|
|
p := new(Provider)
|
|
|
|
if v := p.Meta(); v != nil {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := 42
|
|
|
|
p.SetMeta(42)
|
|
|
|
if v := p.Meta(); !reflect.DeepEqual(v, expected) {
|
2014-08-25 06:50:35 +02:00
|
|
|
t.Fatalf("bad: %#v", v)
|
2014-08-20 00:26:31 +02:00
|
|
|
}
|
|
|
|
}
|