github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/hook_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 6 "github.com/zclconf/go-cty/cty" 7 8 "github.com/hashicorp/terraform-plugin-sdk/internal/addrs" 9 "github.com/hashicorp/terraform-plugin-sdk/internal/plans" 10 "github.com/hashicorp/terraform-plugin-sdk/internal/providers" 11 "github.com/hashicorp/terraform-plugin-sdk/internal/states" 12 ) 13 14 func TestNilHook_impl(t *testing.T) { 15 var _ Hook = new(NilHook) 16 } 17 18 // testHook is a Hook implementation that logs the calls it receives. 19 // It is intended for testing that core code is emitting the correct hooks 20 // for a given situation. 21 type testHook struct { 22 Calls []*testHookCall 23 } 24 25 var _ Hook = (*testHook)(nil) 26 27 // testHookCall represents a single call in testHook. 28 // This hook just logs string names to make it easy to write "want" expressions 29 // in tests that can DeepEqual against the real calls. 30 type testHookCall struct { 31 Action string 32 InstanceID string 33 } 34 35 func (h *testHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { 36 h.Calls = append(h.Calls, &testHookCall{"PreApply", addr.String()}) 37 return HookActionContinue, nil 38 } 39 40 func (h *testHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) { 41 h.Calls = append(h.Calls, &testHookCall{"PostApply", addr.String()}) 42 return HookActionContinue, nil 43 } 44 45 func (h *testHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) { 46 h.Calls = append(h.Calls, &testHookCall{"PreDiff", addr.String()}) 47 return HookActionContinue, nil 48 } 49 50 func (h *testHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { 51 h.Calls = append(h.Calls, &testHookCall{"PostDiff", addr.String()}) 52 return HookActionContinue, nil 53 } 54 55 func (h *testHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { 56 h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstance", addr.String()}) 57 return HookActionContinue, nil 58 } 59 60 func (h *testHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { 61 h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstance", addr.String()}) 62 return HookActionContinue, nil 63 } 64 65 func (h *testHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) { 66 h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstanceStep", addr.String()}) 67 return HookActionContinue, nil 68 } 69 70 func (h *testHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) { 71 h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstanceStep", addr.String()}) 72 return HookActionContinue, nil 73 } 74 75 func (h *testHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) { 76 h.Calls = append(h.Calls, &testHookCall{"ProvisionOutput", addr.String()}) 77 } 78 79 func (h *testHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) { 80 h.Calls = append(h.Calls, &testHookCall{"PreRefresh", addr.String()}) 81 return HookActionContinue, nil 82 } 83 84 func (h *testHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) { 85 h.Calls = append(h.Calls, &testHookCall{"PostRefresh", addr.String()}) 86 return HookActionContinue, nil 87 } 88 89 func (h *testHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) { 90 h.Calls = append(h.Calls, &testHookCall{"PreImportState", addr.String()}) 91 return HookActionContinue, nil 92 } 93 94 func (h *testHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) { 95 h.Calls = append(h.Calls, &testHookCall{"PostImportState", addr.String()}) 96 return HookActionContinue, nil 97 } 98 99 func (h *testHook) PostStateUpdate(new *states.State) (HookAction, error) { 100 h.Calls = append(h.Calls, &testHookCall{"PostStateUpdate", ""}) 101 return HookActionContinue, nil 102 }