2014-06-05 15:57:06 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2014-06-10 20:22:32 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-06-05 15:57:06 +02:00
|
|
|
)
|
|
|
|
|
2014-09-24 23:37:24 +02:00
|
|
|
func TestDiffEmpty(t *testing.T) {
|
|
|
|
diff := new(Diff)
|
|
|
|
if !diff.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := diff.AddModule(rootModulePath)
|
|
|
|
mod.Resources["nodeA"] = &InstanceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:51:45 +02:00
|
|
|
func TestModuleDiff_ChangeType(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Diff *ModuleDiff
|
|
|
|
Result DiffChangeType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&ModuleDiff{},
|
|
|
|
DiffNone,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ModuleDiff{
|
|
|
|
Resources: map[string]*InstanceDiff{
|
|
|
|
"foo": &InstanceDiff{Destroy: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffDestroy,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ModuleDiff{
|
|
|
|
Resources: map[string]*InstanceDiff{
|
|
|
|
"foo": &InstanceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffUpdate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ModuleDiff{
|
|
|
|
Resources: map[string]*InstanceDiff{
|
|
|
|
"foo": &InstanceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffCreate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&ModuleDiff{
|
|
|
|
Resources: map[string]*InstanceDiff{
|
|
|
|
"foo": &InstanceDiff{
|
|
|
|
Destroy: true,
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffUpdate,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
actual := tc.Diff.ChangeType()
|
|
|
|
if actual != tc.Result {
|
|
|
|
t.Fatalf("%d: %s", i, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:43:21 +02:00
|
|
|
func TestModuleDiff_Empty(t *testing.T) {
|
|
|
|
diff := new(ModuleDiff)
|
2014-06-19 23:57:36 +02:00
|
|
|
if !diff.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
diff.Resources = map[string]*InstanceDiff{
|
|
|
|
"nodeA": &InstanceDiff{},
|
2014-06-19 23:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 07:19:27 +02:00
|
|
|
|
|
|
|
diff.Resources["nodeA"].Attributes = nil
|
|
|
|
diff.Resources["nodeA"].Destroy = true
|
|
|
|
|
|
|
|
if diff.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
2014-06-19 23:57:36 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:43:21 +02:00
|
|
|
func TestModuleDiff_String(t *testing.T) {
|
|
|
|
diff := &ModuleDiff{
|
2014-09-18 01:33:24 +02:00
|
|
|
Resources: map[string]*InstanceDiff{
|
|
|
|
"nodeA": &InstanceDiff{
|
2014-06-10 20:22:32 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
"bar": &ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
NewComputed: true,
|
|
|
|
},
|
2014-06-10 20:30:54 +02:00
|
|
|
"longfoo": &ResourceAttrDiff{
|
2014-06-10 20:33:59 +02:00
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
2014-06-10 20:30:54 +02:00
|
|
|
},
|
2014-06-10 20:22:32 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-05 15:57:06 +02:00
|
|
|
}
|
2014-06-06 05:17:03 +02:00
|
|
|
|
2014-06-10 20:22:32 +02:00
|
|
|
actual := strings.TrimSpace(diff.String())
|
2014-09-23 20:43:21 +02:00
|
|
|
expected := strings.TrimSpace(moduleDiffStrBasic)
|
2014-06-10 20:22:32 +02:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n%s", actual)
|
2014-06-05 15:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 20:22:32 +02:00
|
|
|
|
2014-09-25 02:51:45 +02:00
|
|
|
func TestInstanceDiff_ChangeType(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Diff *InstanceDiff
|
|
|
|
Result DiffChangeType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&InstanceDiff{},
|
|
|
|
DiffNone,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&InstanceDiff{Destroy: true},
|
|
|
|
DiffDestroy,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&InstanceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffUpdate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&InstanceDiff{
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffCreate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&InstanceDiff{
|
|
|
|
Destroy: true,
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffDestroyCreate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&InstanceDiff{
|
|
|
|
DestroyTainted: true,
|
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DiffDestroyCreate,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
actual := tc.Diff.ChangeType()
|
|
|
|
if actual != tc.Result {
|
|
|
|
t.Fatalf("%d: %s", i, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInstanceDiff_Empty(t *testing.T) {
|
2014-09-18 01:33:24 +02:00
|
|
|
var rd *InstanceDiff
|
2014-07-01 05:56:25 +02:00
|
|
|
|
|
|
|
if !rd.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
rd = new(InstanceDiff)
|
2014-07-01 05:56:25 +02:00
|
|
|
|
|
|
|
if !rd.Empty() {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
rd = &InstanceDiff{Destroy: true}
|
2014-07-01 05:56:25 +02:00
|
|
|
|
|
|
|
if rd.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
rd = &InstanceDiff{
|
2014-07-01 05:56:25 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if rd.Empty() {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:51:45 +02:00
|
|
|
func TestInstanceDiff_RequiresNew(t *testing.T) {
|
2014-09-18 01:33:24 +02:00
|
|
|
rd := &InstanceDiff{
|
2014-06-18 03:10:38 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:51:45 +02:00
|
|
|
func TestInstanceDiff_RequiresNew_nil(t *testing.T) {
|
2014-09-18 01:33:24 +02:00
|
|
|
var rd *InstanceDiff
|
2014-06-18 03:10:38 +02:00
|
|
|
|
|
|
|
if rd.RequiresNew() {
|
|
|
|
t.Fatal("should not require new")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:51:45 +02:00
|
|
|
func TestInstanceDiffSame(t *testing.T) {
|
2014-07-23 04:39:48 +02:00
|
|
|
cases := []struct {
|
2014-09-18 01:33:24 +02:00
|
|
|
One, Two *InstanceDiff
|
2014-07-23 04:39:48 +02:00
|
|
|
Same bool
|
|
|
|
}{
|
|
|
|
{
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{},
|
|
|
|
&InstanceDiff{},
|
2014-07-23 04:39:48 +02:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{Destroy: false},
|
|
|
|
&InstanceDiff{Destroy: true},
|
2014-07-23 04:39:48 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{Destroy: true},
|
|
|
|
&InstanceDiff{Destroy: true},
|
2014-07-23 04:39:48 +02:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{
|
2014-07-23 04:39:48 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{
|
2014-07-23 04:39:48 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{
|
2014-07-23 04:39:48 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"bar": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{
|
2014-07-23 04:39:48 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{
|
2014-07-23 04:39:48 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{RequiresNew: true},
|
|
|
|
},
|
|
|
|
},
|
2014-09-18 01:33:24 +02:00
|
|
|
&InstanceDiff{
|
2014-07-23 04:39:48 +02:00
|
|
|
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-09-23 20:43:21 +02:00
|
|
|
const moduleDiffStrBasic = `
|
2014-06-10 20:37:04 +02:00
|
|
|
CREATE: nodeA
|
2014-06-10 20:30:54 +02:00
|
|
|
bar: "foo" => "<computed>"
|
|
|
|
foo: "foo" => "bar"
|
2014-06-10 20:33:59 +02:00
|
|
|
longfoo: "foo" => "bar" (forces new resource)
|
2014-06-10 20:22:32 +02:00
|
|
|
`
|