github.com/opentofu/opentofu@v1.7.1/internal/tofu/hook_test.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package tofu
     7  
     8  import (
     9  	"sync"
    10  	"testing"
    11  
    12  	"github.com/zclconf/go-cty/cty"
    13  
    14  	"github.com/opentofu/opentofu/internal/addrs"
    15  	"github.com/opentofu/opentofu/internal/plans"
    16  	"github.com/opentofu/opentofu/internal/providers"
    17  	"github.com/opentofu/opentofu/internal/states"
    18  )
    19  
    20  func TestNilHook_impl(t *testing.T) {
    21  	var _ Hook = new(NilHook)
    22  }
    23  
    24  // testHook is a Hook implementation that logs the calls it receives.
    25  // It is intended for testing that core code is emitting the correct hooks
    26  // for a given situation.
    27  type testHook struct {
    28  	mu    sync.Mutex
    29  	Calls []*testHookCall
    30  }
    31  
    32  var _ Hook = (*testHook)(nil)
    33  
    34  // testHookCall represents a single call in testHook.
    35  // This hook just logs string names to make it easy to write "want" expressions
    36  // in tests that can DeepEqual against the real calls.
    37  type testHookCall struct {
    38  	Action     string
    39  	InstanceID string
    40  }
    41  
    42  func (h *testHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
    43  	h.mu.Lock()
    44  	defer h.mu.Unlock()
    45  	h.Calls = append(h.Calls, &testHookCall{"PreApply", addr.String()})
    46  	return HookActionContinue, nil
    47  }
    48  
    49  func (h *testHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) {
    50  	h.mu.Lock()
    51  	defer h.mu.Unlock()
    52  	h.Calls = append(h.Calls, &testHookCall{"PostApply", addr.String()})
    53  	return HookActionContinue, nil
    54  }
    55  
    56  func (h *testHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) {
    57  	h.mu.Lock()
    58  	defer h.mu.Unlock()
    59  	h.Calls = append(h.Calls, &testHookCall{"PreDiff", addr.String()})
    60  	return HookActionContinue, nil
    61  }
    62  
    63  func (h *testHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
    64  	h.mu.Lock()
    65  	defer h.mu.Unlock()
    66  	h.Calls = append(h.Calls, &testHookCall{"PostDiff", addr.String()})
    67  	return HookActionContinue, nil
    68  }
    69  
    70  func (h *testHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
    71  	h.mu.Lock()
    72  	defer h.mu.Unlock()
    73  	h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstance", addr.String()})
    74  	return HookActionContinue, nil
    75  }
    76  
    77  func (h *testHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
    78  	h.mu.Lock()
    79  	defer h.mu.Unlock()
    80  	h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstance", addr.String()})
    81  	return HookActionContinue, nil
    82  }
    83  
    84  func (h *testHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) {
    85  	h.mu.Lock()
    86  	defer h.mu.Unlock()
    87  	h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstanceStep", addr.String()})
    88  	return HookActionContinue, nil
    89  }
    90  
    91  func (h *testHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) {
    92  	h.mu.Lock()
    93  	defer h.mu.Unlock()
    94  	h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstanceStep", addr.String()})
    95  	return HookActionContinue, nil
    96  }
    97  
    98  func (h *testHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) {
    99  	h.mu.Lock()
   100  	defer h.mu.Unlock()
   101  	h.Calls = append(h.Calls, &testHookCall{"ProvisionOutput", addr.String()})
   102  }
   103  
   104  func (h *testHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) {
   105  	h.mu.Lock()
   106  	defer h.mu.Unlock()
   107  	h.Calls = append(h.Calls, &testHookCall{"PreRefresh", addr.String()})
   108  	return HookActionContinue, nil
   109  }
   110  
   111  func (h *testHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) {
   112  	h.mu.Lock()
   113  	defer h.mu.Unlock()
   114  	h.Calls = append(h.Calls, &testHookCall{"PostRefresh", addr.String()})
   115  	return HookActionContinue, nil
   116  }
   117  
   118  func (h *testHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
   119  	h.mu.Lock()
   120  	defer h.mu.Unlock()
   121  	h.Calls = append(h.Calls, &testHookCall{"PreImportState", addr.String()})
   122  	return HookActionContinue, nil
   123  }
   124  
   125  func (h *testHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
   126  	h.mu.Lock()
   127  	defer h.mu.Unlock()
   128  	h.Calls = append(h.Calls, &testHookCall{"PostImportState", addr.String()})
   129  	return HookActionContinue, nil
   130  }
   131  
   132  func (h *testHook) PrePlanImport(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
   133  	h.mu.Lock()
   134  	defer h.mu.Unlock()
   135  	h.Calls = append(h.Calls, &testHookCall{"PrePlanImport", addr.String()})
   136  	return HookActionContinue, nil
   137  }
   138  
   139  func (h *testHook) PostPlanImport(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
   140  	h.mu.Lock()
   141  	defer h.mu.Unlock()
   142  	h.Calls = append(h.Calls, &testHookCall{"PostPlanImport", addr.String()})
   143  	return HookActionContinue, nil
   144  }
   145  
   146  func (h *testHook) PreApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) {
   147  	h.mu.Lock()
   148  	defer h.mu.Unlock()
   149  	h.Calls = append(h.Calls, &testHookCall{"PreApplyImport", addr.String()})
   150  	return HookActionContinue, nil
   151  }
   152  
   153  func (h *testHook) PostApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) {
   154  	h.mu.Lock()
   155  	defer h.mu.Unlock()
   156  	h.Calls = append(h.Calls, &testHookCall{"PostApplyImport", addr.String()})
   157  	return HookActionContinue, nil
   158  }
   159  
   160  func (h *testHook) Stopping() {
   161  	h.mu.Lock()
   162  	defer h.mu.Unlock()
   163  	h.Calls = append(h.Calls, &testHookCall{"Stopping", ""})
   164  }
   165  
   166  func (h *testHook) PostStateUpdate(new *states.State) (HookAction, error) {
   167  	h.mu.Lock()
   168  	defer h.mu.Unlock()
   169  	h.Calls = append(h.Calls, &testHookCall{"PostStateUpdate", ""})
   170  	return HookActionContinue, nil
   171  }