github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/terraform/hook_test.go (about)

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