diff --git a/backend/local/hook_count.go b/backend/local/hook_count.go index 59fc2cc10..f9d619ef2 100644 --- a/backend/local/hook_count.go +++ b/backend/local/hook_count.go @@ -71,9 +71,9 @@ func (h *CountHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generat case plans.Create: h.Added++ case plans.Delete: - h.Changed++ - case plans.Update: h.Removed++ + case plans.Update: + h.Changed++ } } } diff --git a/backend/local/hook_count_test.go b/backend/local/hook_count_test.go index 3aeb238b3..938e730df 100644 --- a/backend/local/hook_count_test.go +++ b/backend/local/hook_count_test.go @@ -257,3 +257,79 @@ func TestCountHookPostDiff_DataSource(t *testing.T) { expected, h) } } + +func TestCountHookApply_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{}, + }, + }, + } + + for k := range resources { + addr := addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "test_instance", + Name: k, + }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) + + h.PreApply(addr, states.CurrentGen, plans.Update, cty.DynamicVal, cty.DynamicVal) + h.PostApply(addr, states.CurrentGen, cty.DynamicVal, nil) + } + + expected := &CountHook{pending: make(map[string]plans.Action)} + expected.Added = 0 + expected.Changed = 3 + expected.Removed = 0 + + if !reflect.DeepEqual(expected, h) { + t.Fatalf("Expected:\n%#v\nGot:\n%#v\n", expected, h) + } +} + +func TestCountHookApply_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}, + } + + for k := range resources { + addr := addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "test_instance", + Name: k, + }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) + + h.PreApply(addr, states.CurrentGen, plans.Delete, cty.DynamicVal, cty.DynamicVal) + h.PostApply(addr, states.CurrentGen, cty.DynamicVal, nil) + } + + expected := &CountHook{pending: make(map[string]plans.Action)} + expected.Added = 0 + expected.Changed = 0 + expected.Removed = 4 + + if !reflect.DeepEqual(expected, h) { + t.Fatalf("Expected:\n%#v\nGot:\n%#v\n", expected, h) + } +}