mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-09 07:33:58 -06:00
5654a676d9
Rather than overloading InstanceDiff with a "Stub" attribute that is going to be largely meaningless, we are just going to skip pre/post-diff hooks altogether. This is under the notion that we will eventually not need to "stub" a diff for scale-out, stateless nodes on refresh at all, so diff behaviour won't be necessary at that point, so we should not assume that hooks will run at this stage anyway. Also as part of this removed the CountHook test that is now failing because CountHook is out of scope of the new behaviour.
244 lines
5.2 KiB
Go
244 lines
5.2 KiB
Go
package local
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestCountHook_impl(t *testing.T) {
|
|
var _ terraform.Hook = new(CountHook)
|
|
}
|
|
|
|
func TestCountHookPostDiff_DestroyDeposed(t *testing.T) {
|
|
h := new(CountHook)
|
|
|
|
resources := map[string]*terraform.InstanceDiff{
|
|
"lorem": &terraform.InstanceDiff{DestroyDeposed: true},
|
|
}
|
|
|
|
n := &terraform.InstanceInfo{} // TODO
|
|
|
|
for _, d := range resources {
|
|
h.PostDiff(n, d)
|
|
}
|
|
|
|
expected := new(CountHook)
|
|
expected.ToAdd = 0
|
|
expected.ToChange = 0
|
|
expected.ToRemoveAndAdd = 0
|
|
expected.ToRemove = 1
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
expected, h)
|
|
}
|
|
}
|
|
|
|
func TestCountHookPostDiff_DestroyOnly(t *testing.T) {
|
|
h := new(CountHook)
|
|
|
|
resources := map[string]*terraform.InstanceDiff{
|
|
"foo": &terraform.InstanceDiff{Destroy: true},
|
|
"bar": &terraform.InstanceDiff{Destroy: true},
|
|
"lorem": &terraform.InstanceDiff{Destroy: true},
|
|
"ipsum": &terraform.InstanceDiff{Destroy: true},
|
|
}
|
|
|
|
n := &terraform.InstanceInfo{} // TODO
|
|
|
|
for _, d := range resources {
|
|
h.PostDiff(n, d)
|
|
}
|
|
|
|
expected := new(CountHook)
|
|
expected.ToAdd = 0
|
|
expected.ToChange = 0
|
|
expected.ToRemoveAndAdd = 0
|
|
expected.ToRemove = 4
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
expected, h)
|
|
}
|
|
}
|
|
|
|
func TestCountHookPostDiff_AddOnly(t *testing.T) {
|
|
h := new(CountHook)
|
|
|
|
resources := map[string]*terraform.InstanceDiff{
|
|
"foo": &terraform.InstanceDiff{
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{RequiresNew: true},
|
|
},
|
|
},
|
|
"bar": &terraform.InstanceDiff{
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{RequiresNew: true},
|
|
},
|
|
},
|
|
"lorem": &terraform.InstanceDiff{
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{RequiresNew: true},
|
|
},
|
|
},
|
|
}
|
|
|
|
n := &terraform.InstanceInfo{}
|
|
|
|
for _, d := range resources {
|
|
h.PostDiff(n, d)
|
|
}
|
|
|
|
expected := new(CountHook)
|
|
expected.ToAdd = 3
|
|
expected.ToChange = 0
|
|
expected.ToRemoveAndAdd = 0
|
|
expected.ToRemove = 0
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
expected, h)
|
|
}
|
|
}
|
|
|
|
func TestCountHookPostDiff_ChangeOnly(t *testing.T) {
|
|
h := new(CountHook)
|
|
|
|
resources := map[string]*terraform.InstanceDiff{
|
|
"foo": &terraform.InstanceDiff{
|
|
Destroy: false,
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{},
|
|
},
|
|
},
|
|
"bar": &terraform.InstanceDiff{
|
|
Destroy: false,
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{},
|
|
},
|
|
},
|
|
"lorem": &terraform.InstanceDiff{
|
|
Destroy: false,
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{},
|
|
},
|
|
},
|
|
}
|
|
|
|
n := &terraform.InstanceInfo{}
|
|
|
|
for _, d := range resources {
|
|
h.PostDiff(n, d)
|
|
}
|
|
|
|
expected := new(CountHook)
|
|
expected.ToAdd = 0
|
|
expected.ToChange = 3
|
|
expected.ToRemoveAndAdd = 0
|
|
expected.ToRemove = 0
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
expected, h)
|
|
}
|
|
}
|
|
|
|
func TestCountHookPostDiff_Mixed(t *testing.T) {
|
|
h := new(CountHook)
|
|
|
|
resources := map[string]*terraform.InstanceDiff{
|
|
"foo": &terraform.InstanceDiff{
|
|
Destroy: true,
|
|
},
|
|
"bar": &terraform.InstanceDiff{},
|
|
"lorem": &terraform.InstanceDiff{
|
|
Destroy: false,
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{},
|
|
},
|
|
},
|
|
"ipsum": &terraform.InstanceDiff{Destroy: true},
|
|
}
|
|
|
|
n := &terraform.InstanceInfo{}
|
|
|
|
for _, d := range resources {
|
|
h.PostDiff(n, d)
|
|
}
|
|
|
|
expected := new(CountHook)
|
|
expected.ToAdd = 0
|
|
expected.ToChange = 1
|
|
expected.ToRemoveAndAdd = 0
|
|
expected.ToRemove = 2
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
expected, h)
|
|
}
|
|
}
|
|
|
|
func TestCountHookPostDiff_NoChange(t *testing.T) {
|
|
h := new(CountHook)
|
|
|
|
resources := map[string]*terraform.InstanceDiff{
|
|
"foo": &terraform.InstanceDiff{},
|
|
"bar": &terraform.InstanceDiff{},
|
|
"lorem": &terraform.InstanceDiff{},
|
|
"ipsum": &terraform.InstanceDiff{},
|
|
}
|
|
|
|
n := &terraform.InstanceInfo{}
|
|
|
|
for _, d := range resources {
|
|
h.PostDiff(n, d)
|
|
}
|
|
|
|
expected := new(CountHook)
|
|
expected.ToAdd = 0
|
|
expected.ToChange = 0
|
|
expected.ToRemoveAndAdd = 0
|
|
expected.ToRemove = 0
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
expected, h)
|
|
}
|
|
}
|
|
|
|
func TestCountHookPostDiff_DataSource(t *testing.T) {
|
|
h := new(CountHook)
|
|
|
|
resources := map[string]*terraform.InstanceDiff{
|
|
"data.foo": &terraform.InstanceDiff{
|
|
Destroy: true,
|
|
},
|
|
"data.bar": &terraform.InstanceDiff{},
|
|
"data.lorem": &terraform.InstanceDiff{
|
|
Destroy: false,
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
"foo": &terraform.ResourceAttrDiff{},
|
|
},
|
|
},
|
|
"data.ipsum": &terraform.InstanceDiff{Destroy: true},
|
|
}
|
|
|
|
for k, d := range resources {
|
|
n := &terraform.InstanceInfo{Id: k}
|
|
h.PostDiff(n, d)
|
|
}
|
|
|
|
expected := new(CountHook)
|
|
expected.ToAdd = 0
|
|
expected.ToChange = 0
|
|
expected.ToRemoveAndAdd = 0
|
|
expected.ToRemove = 0
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
expected, h)
|
|
}
|
|
}
|