terraform: unit test Same function on ResourceDiff

This commit is contained in:
Mitchell Hashimoto 2014-07-22 19:39:48 -07:00
parent d5e3d5a196
commit 7221e01110
2 changed files with 86 additions and 0 deletions

View File

@ -222,6 +222,12 @@ func (d *ResourceDiff) RequiresNew() bool {
// just checking that the same attributes are changing, a destroy
// isn't suddenly happening, etc.
func (d *ResourceDiff) Same(d2 *ResourceDiff) bool {
if d == nil && d2 == nil {
return true
} else if d == nil || d2 == nil {
return false
}
if d.Destroy != d2.Destroy {
return false
}

View File

@ -128,6 +128,86 @@ func TestResourceDiff_RequiresNew_nil(t *testing.T) {
}
}
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)
}
}
}
func TestReadWriteDiff(t *testing.T) {
diff := &Diff{
Resources: map[string]*ResourceDiff{