2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2014-08-18 04:45:26 +02:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2014-08-13 23:23:22 +02:00
|
|
|
"testing"
|
2014-08-18 04:45:26 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-08-13 23:23:22 +02:00
|
|
|
)
|
|
|
|
|
2014-08-18 05:20:11 +02:00
|
|
|
func TestResourceApply_create(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
called := false
|
|
|
|
r.Create = func(d *ResourceData, m interface{}) error {
|
|
|
|
called = true
|
|
|
|
d.SetId("foo")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
var s *terraform.InstanceState = nil
|
2014-08-18 05:20:11 +02:00
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
d := &terraform.InstanceDiff{
|
2014-08-18 05:20:11 +02:00
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"foo": &terraform.ResourceAttrDiff{
|
|
|
|
New: "42",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Apply(s, d, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
expected := &terraform.InstanceState{
|
2014-08-18 05:20:11 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "foo",
|
|
|
|
"foo": "42",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceApply_destroy(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
called := false
|
|
|
|
r.Delete = func(d *ResourceData, m interface{}) error {
|
|
|
|
called = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
s := &terraform.InstanceState{
|
2014-08-18 05:20:11 +02:00
|
|
|
ID: "bar",
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
d := &terraform.InstanceDiff{
|
2014-08-18 05:20:11 +02:00
|
|
|
Destroy: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Apply(s, d, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !called {
|
|
|
|
t.Fatal("delete not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 09:28:41 +02:00
|
|
|
func TestResourceApply_destroyCreate(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"tags": &Schema{
|
|
|
|
Type: TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
change := false
|
|
|
|
r.Create = func(d *ResourceData, m interface{}) error {
|
|
|
|
change = d.HasChange("tags")
|
|
|
|
d.SetId("foo")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.Delete = func(d *ResourceData, m interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var s *terraform.InstanceState = &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"tags.Name": "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &terraform.InstanceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"foo": &terraform.ResourceAttrDiff{
|
|
|
|
New: "42",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
"tags.Name": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "foo",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Apply(s, d, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !change {
|
|
|
|
t.Fatal("should have change")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &terraform.InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "foo",
|
|
|
|
"foo": "42",
|
2015-01-15 23:12:24 +01:00
|
|
|
"tags.#": "1",
|
2014-10-21 09:28:41 +02:00
|
|
|
"tags.Name": "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 05:20:11 +02:00
|
|
|
func TestResourceApply_destroyPartial(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Delete = func(d *ResourceData, m interface{}) error {
|
|
|
|
d.Set("foo", 42)
|
|
|
|
return fmt.Errorf("some error")
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
s := &terraform.InstanceState{
|
2014-08-18 05:20:11 +02:00
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
d := &terraform.InstanceDiff{
|
2014-08-18 05:20:11 +02:00
|
|
|
Destroy: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Apply(s, d, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
expected := &terraform.InstanceState{
|
2014-08-18 05:20:11 +02:00
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
2014-08-20 19:37:59 +02:00
|
|
|
"id": "bar",
|
2014-08-18 05:20:11 +02:00
|
|
|
"foo": "42",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceApply_update(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Update = func(d *ResourceData, m interface{}) error {
|
|
|
|
d.Set("foo", 42)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
s := &terraform.InstanceState{
|
2014-08-18 05:20:11 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
d := &terraform.InstanceDiff{
|
2014-08-18 05:20:11 +02:00
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"foo": &terraform.ResourceAttrDiff{
|
|
|
|
New: "13",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Apply(s, d, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
expected := &terraform.InstanceState{
|
2014-08-18 05:20:11 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "foo",
|
|
|
|
"foo": "42",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 18:26:48 +02:00
|
|
|
func TestResourceApply_updateNoCallback(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Update = nil
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
s := &terraform.InstanceState{
|
2014-08-19 18:26:48 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
d := &terraform.InstanceDiff{
|
2014-08-19 18:26:48 +02:00
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"foo": &terraform.ResourceAttrDiff{
|
|
|
|
New: "13",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Apply(s, d, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
expected := &terraform.InstanceState{
|
2014-08-19 18:26:48 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 23:23:22 +02:00
|
|
|
func TestResourceInternalValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
In *Resource
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// No optional and no required
|
|
|
|
{
|
|
|
|
&Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := tc.In.InternalValidate()
|
|
|
|
if (err != nil) != tc.Err {
|
|
|
|
t.Fatalf("%d: bad: %s", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 04:45:26 +02:00
|
|
|
|
|
|
|
func TestResourceRefresh(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
if m != 42 {
|
|
|
|
return fmt.Errorf("meta not passed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Set("foo", d.Get("foo").(int)+1)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
s := &terraform.InstanceState{
|
2014-08-18 04:45:26 +02:00
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
expected := &terraform.InstanceState{
|
2014-08-18 04:45:26 +02:00
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
2014-08-20 19:37:59 +02:00
|
|
|
"id": "bar",
|
2014-08-18 04:45:26 +02:00
|
|
|
"foo": "13",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, 42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 03:19:02 +02:00
|
|
|
|
|
|
|
func TestResourceRefresh_delete(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
s := &terraform.InstanceState{
|
2014-08-20 03:19:02 +02:00
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, 42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2015-01-11 00:39:29 +01:00
|
|
|
|
|
|
|
func TestResourceRefresh_existsError(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Exists = func(*ResourceData, interface{}) (bool, error) {
|
|
|
|
return false, fmt.Errorf("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
panic("shouldn't be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, 42)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, s) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceRefresh_noExists(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Exists = func(*ResourceData, interface{}) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
panic("shouldn't be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, 42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("should have no state")
|
|
|
|
}
|
|
|
|
}
|