2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2014-08-18 04:45:26 +02:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2015-03-06 02:04:04 +01:00
|
|
|
"strconv"
|
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{
|
2015-03-19 01:07:29 +01:00
|
|
|
SchemaVersion: 2,
|
2014-08-18 05:20:11 +02:00
|
|
|
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",
|
|
|
|
},
|
2015-03-19 01:07:29 +01:00
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "2",
|
|
|
|
},
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
core: Use .% instead of .# for maps in state
The flatmapped representation of state prior to this commit encoded maps
and lists (and therefore by extension, sets) with a key corresponding to
the number of elements, or the unknown variable indicator under a .# key
and then individual items. For example, the list ["a", "b", "c"] would
have been encoded as:
listname.# = 3
listname.0 = "a"
listname.1 = "b"
listname.2 = "c"
And the map {"key1": "value1", "key2", "value2"} would have been encoded
as:
mapname.# = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
Sets use the hash code as the key - for example a set with a (fictional)
hashcode calculation may look like:
setname.# = 2
setname.12312512 = "value1"
setname.56345233 = "value2"
Prior to the work done to extend the type system, this was sufficient
since the internal representation of these was effectively the same.
However, following the separation of maps and lists into distinct
first-class types, this encoding presents a problem: given a state file,
it is impossible to tell the encoding of an empty list and an empty map
apart. This presents problems for the type checker during interpolation,
as many interpolation functions will operate on only one of these two
structures.
This commit therefore changes the representation in state of maps to use
a "%" as the key for the number of elements. Consequently the map above
will now be encoded as:
mapname.% = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
This has the effect of an empty list (or set) now being encoded as:
listname.# = 0
And an empty map now being encoded as:
mapname.% = 0
Therefore we can eliminate some nasty guessing logic from the resource
variable supplier for interpolation, at the cost of having to migrate
state up front (to follow in a subsequent commit).
In order to reduce the number of potential situations in which resources
would be "forced new", we continue to accept "#" as the count key when
reading maps via helper/schema. There is no situation under which we can
allow "#" as an actual map key in any case, as it would not be
distinguishable from a list or set in state.
2016-06-05 10:34:43 +02: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,
|
|
|
|
},
|
|
|
|
},
|
2015-08-03 22:52:04 +02:00
|
|
|
SchemaVersion: 3,
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
2015-08-03 22:52:04 +02:00
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "3",
|
|
|
|
},
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2015-08-03 22:52:04 +02:00
|
|
|
t.Fatalf("expected:\n%#v\n\ngot:\n%#v", expected, actual)
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 10:41:08 +01:00
|
|
|
func TestResourceApply_isNewResource(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFunc := func(d *ResourceData, m interface{}) error {
|
|
|
|
d.Set("foo", "updated")
|
|
|
|
if d.IsNewResource() {
|
|
|
|
d.Set("foo", "new-resource")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.Create = func(d *ResourceData, m interface{}) error {
|
|
|
|
d.SetId("foo")
|
|
|
|
d.Set("foo", "created")
|
|
|
|
return updateFunc(d, m)
|
|
|
|
}
|
|
|
|
r.Update = updateFunc
|
|
|
|
|
|
|
|
d := &terraform.InstanceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"foo": &terraform.ResourceAttrDiff{
|
|
|
|
New: "bla-blah",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// positive test
|
|
|
|
var s *terraform.InstanceState = nil
|
|
|
|
|
|
|
|
actual, err := r.Apply(s, d, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &terraform.InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "foo",
|
|
|
|
"foo": "new-resource",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("actual: %#v\nexpected: %#v",
|
|
|
|
actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// negative test
|
|
|
|
s = &terraform.InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "foo",
|
|
|
|
"foo": "new-resource",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err = r.Apply(s, d, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = &terraform.InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "foo",
|
|
|
|
"foo": "updated",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("actual: %#v\nexpected: %#v",
|
|
|
|
actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 23:23:22 +02:00
|
|
|
func TestResourceInternalValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
2016-04-17 02:27:00 +02:00
|
|
|
In *Resource
|
|
|
|
Writable bool
|
|
|
|
Err bool
|
2014-08-13 23:23:22 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
true,
|
2016-04-17 02:27:00 +02:00
|
|
|
true,
|
2014-08-13 23:23:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// No optional and no required
|
|
|
|
{
|
|
|
|
&Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
2016-04-17 02:27:00 +02:00
|
|
|
true,
|
2014-08-13 23:23:22 +02:00
|
|
|
},
|
2015-09-19 20:11:49 +02:00
|
|
|
|
|
|
|
// Update undefined for non-ForceNew field
|
|
|
|
{
|
|
|
|
&Resource{
|
|
|
|
Create: func(d *ResourceData, meta interface{}) error { return nil },
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"boo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
2016-04-17 02:27:00 +02:00
|
|
|
true,
|
2015-09-19 20:11:49 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Update defined for ForceNew field
|
|
|
|
{
|
|
|
|
&Resource{
|
|
|
|
Create: func(d *ResourceData, meta interface{}) error { return nil },
|
|
|
|
Update: func(d *ResourceData, meta interface{}) error { return nil },
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"goo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
2016-04-17 02:27:00 +02:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// non-writable doesn't need Update, Create or Delete
|
|
|
|
{
|
|
|
|
&Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"goo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// non-writable *must not* have Create
|
|
|
|
{
|
|
|
|
&Resource{
|
|
|
|
Create: func(d *ResourceData, meta interface{}) error { return nil },
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"goo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
2015-09-19 20:11:49 +02:00
|
|
|
},
|
2014-08-13 23:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
2016-04-17 02:27:00 +02:00
|
|
|
err := tc.In.InternalValidate(schemaMap{}, tc.Writable)
|
2015-10-08 14:48:04 +02:00
|
|
|
if err != nil != tc.Err {
|
2014-08-13 23:23:22 +02:00
|
|
|
t.Fatalf("%d: bad: %s", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 04:45:26 +02:00
|
|
|
|
|
|
|
func TestResourceRefresh(t *testing.T) {
|
|
|
|
r := &Resource{
|
2015-03-19 01:07:29 +01:00
|
|
|
SchemaVersion: 2,
|
2014-08-18 04:45:26 +02:00
|
|
|
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",
|
|
|
|
},
|
2015-03-19 01:07:29 +01:00
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "2",
|
|
|
|
},
|
2014-08-18 04:45:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-05-14 05:15:13 +02:00
|
|
|
func TestResourceRefresh_blankId(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("foo")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &terraform.InstanceState{
|
|
|
|
ID: "",
|
|
|
|
Attributes: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, 42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 02:04:04 +01:00
|
|
|
|
|
|
|
func TestResourceRefresh_needsMigration(t *testing.T) {
|
|
|
|
// Schema v2 it deals only in newfoo, which tracks foo as an int
|
|
|
|
r := &Resource{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"newfoo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
return d.Set("newfoo", d.Get("newfoo").(int)+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.MigrateState = func(
|
|
|
|
v int,
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
|
|
|
// Real state migration functions will probably switch on this value,
|
|
|
|
// but we'll just assert on it for now.
|
|
|
|
if v != 1 {
|
|
|
|
t.Fatalf("Expected StateSchemaVersion to be 1, got %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta != 42 {
|
|
|
|
t.Fatal("Expected meta to be passed through to the migration function")
|
|
|
|
}
|
|
|
|
|
|
|
|
oldfoo, err := strconv.ParseFloat(s.Attributes["oldfoo"], 64)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %#v", err)
|
|
|
|
}
|
2015-10-08 14:48:04 +02:00
|
|
|
s.Attributes["newfoo"] = strconv.Itoa(int(oldfoo * 10))
|
2015-03-06 02:04:04 +01:00
|
|
|
delete(s.Attributes, "oldfoo")
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// State is v1 and deals in oldfoo, which tracked foo as a float at 1/10th
|
|
|
|
// the scale of newfoo
|
|
|
|
s := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"oldfoo": "1.2",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, 42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "bar",
|
|
|
|
"newfoo": "13",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad:\n\nexpected: %#v\ngot: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceRefresh_noMigrationNeeded(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"newfoo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
return d.Set("newfoo", d.Get("newfoo").(int)+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.MigrateState = func(
|
|
|
|
v int,
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
|
|
|
t.Fatal("Migrate function shouldn't be called!")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"newfoo": "12",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "bar",
|
|
|
|
"newfoo": "13",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad:\n\nexpected: %#v\ngot: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceRefresh_stateSchemaVersionUnset(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
// Version 1 > Version 0
|
|
|
|
SchemaVersion: 1,
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"newfoo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
return d.Set("newfoo", d.Get("newfoo").(int)+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.MigrateState = func(
|
|
|
|
v int,
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
|
|
|
s.Attributes["newfoo"] = s.Attributes["oldfoo"]
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"oldfoo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := r.Refresh(s, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "bar",
|
|
|
|
"newfoo": "13",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"schema_version": "1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad:\n\nexpected: %#v\ngot: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceRefresh_migrateStateErr(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"newfoo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Read = func(d *ResourceData, m interface{}) error {
|
|
|
|
t.Fatal("Read should never be called!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r.MigrateState = func(
|
|
|
|
v int,
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
|
|
|
return s, fmt.Errorf("triggering an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"oldfoo": "12",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.Refresh(s, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, but got none!")
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 18:17:26 +02:00
|
|
|
|
|
|
|
func TestResourceData(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
state := &terraform.InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"id": "foo",
|
|
|
|
"foo": "42",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data := r.Data(state)
|
|
|
|
if data.Id() != "foo" {
|
|
|
|
t.Fatalf("err: %s", data.Id())
|
|
|
|
}
|
|
|
|
if v := data.Get("foo"); v != 42 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2016-05-04 22:37:35 +02:00
|
|
|
|
|
|
|
// Set expectations
|
|
|
|
state.Meta = map[string]string{
|
|
|
|
"schema_version": "2",
|
|
|
|
}
|
|
|
|
|
|
|
|
result := data.State()
|
|
|
|
if !reflect.DeepEqual(result, state) {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
2016-04-26 18:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceData_blank(t *testing.T) {
|
|
|
|
r := &Resource{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data := r.Data(nil)
|
|
|
|
if data.Id() != "" {
|
|
|
|
t.Fatalf("err: %s", data.Id())
|
|
|
|
}
|
|
|
|
if v := data.Get("foo"); v != 0 {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|