2014-06-05 08:57:06 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2014-06-18 22:34:39 -05:00
|
|
|
"bytes"
|
|
|
|
"reflect"
|
2014-06-10 13:22:32 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-06-05 08:57:06 -05:00
|
|
|
)
|
|
|
|
|
2014-06-19 16:57:36 -05:00
|
|
|
func TestDiff_Empty(t *testing.T) {
|
|
|
|
diff := new(Diff)
|
|
|
|
if !diff.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
diff.Resources = map[string]*ResourceDiff{
|
|
|
|
"nodeA": &ResourceDiff{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !diff.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
diff.Resources["nodeA"].Attributes = map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
2014-06-26 00:19:27 -05:00
|
|
|
|
|
|
|
diff.Resources["nodeA"].Attributes = nil
|
|
|
|
diff.Resources["nodeA"].Destroy = true
|
|
|
|
|
|
|
|
if diff.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
2014-06-19 16:57:36 -05:00
|
|
|
}
|
|
|
|
|
2014-06-10 13:22:32 -05:00
|
|
|
func TestDiff_String(t *testing.T) {
|
|
|
|
diff := &Diff{
|
|
|
|
Resources: map[string]*ResourceDiff{
|
|
|
|
"nodeA": &ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
"bar": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
NewComputed: true,
|
|
|
|
},
|
2014-06-10 13:30:54 -05:00
|
|
|
"longfoo": &ResourceAttrDiff{
|
2014-06-10 13:33:59 -05:00
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
2014-06-10 13:30:54 -05:00
|
|
|
},
|
2014-06-10 13:22:32 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|
2014-06-05 22:17:03 -05:00
|
|
|
|
2014-06-10 13:22:32 -05:00
|
|
|
actual := strings.TrimSpace(diff.String())
|
|
|
|
expected := strings.TrimSpace(diffStrBasic)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 13:22:32 -05:00
|
|
|
|
2014-06-30 22:56:25 -05:00
|
|
|
func TestResourceDiff_Empty(t *testing.T) {
|
|
|
|
var rd *ResourceDiff
|
|
|
|
|
|
|
|
if !rd.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
rd = new(ResourceDiff)
|
|
|
|
|
|
|
|
if !rd.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
rd = &ResourceDiff{Destroy: true}
|
|
|
|
|
|
|
|
if rd.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
rd = &ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if rd.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 20:10:38 -05:00
|
|
|
func TestResourceDiff_RequiresNew(t *testing.T) {
|
|
|
|
rd := &ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if rd.RequiresNew() {
|
|
|
|
t.Fatal("should not require new")
|
|
|
|
}
|
|
|
|
|
|
|
|
rd.Attributes["foo"].RequiresNew = true
|
|
|
|
|
|
|
|
if !rd.RequiresNew() {
|
|
|
|
t.Fatal("should require new")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceDiff_RequiresNew_nil(t *testing.T) {
|
|
|
|
var rd *ResourceDiff
|
|
|
|
|
|
|
|
if rd.RequiresNew() {
|
|
|
|
t.Fatal("should not require new")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 21:39:48 -05:00
|
|
|
func TestResourceDiffSame(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
One, Two *ResourceDiff
|
|
|
|
Same bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&ResourceDiff{},
|
|
|
|
&ResourceDiff{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&ResourceDiff{Destroy: false},
|
|
|
|
&ResourceDiff{Destroy: true},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&ResourceDiff{Destroy: true},
|
|
|
|
&ResourceDiff{Destroy: true},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"bar": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{RequiresNew: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{RequiresNew: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
actual := tc.One.Same(tc.Two)
|
|
|
|
if actual != tc.Same {
|
|
|
|
t.Fatalf("Fail %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 22:34:39 -05:00
|
|
|
func TestReadWriteDiff(t *testing.T) {
|
|
|
|
diff := &Diff{
|
|
|
|
Resources: map[string]*ResourceDiff{
|
|
|
|
"nodeA": &ResourceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
"bar": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
NewComputed: true,
|
|
|
|
},
|
|
|
|
"longfoo": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := WriteDiff(diff, buf); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := ReadDiff(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, diff) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:22:32 -05:00
|
|
|
const diffStrBasic = `
|
2014-06-10 13:37:04 -05:00
|
|
|
CREATE: nodeA
|
2014-06-10 13:30:54 -05:00
|
|
|
bar: "foo" => "<computed>"
|
|
|
|
foo: "foo" => "bar"
|
2014-06-10 13:33:59 -05:00
|
|
|
longfoo: "foo" => "bar" (forces new resource)
|
2014-06-10 13:22:32 -05:00
|
|
|
`
|