github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/legacy/terraform/resource_provisioner_mock.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package terraform
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/terramate-io/tf/configs/configschema"
    10  )
    11  
    12  // MockResourceProvisioner implements ResourceProvisioner but mocks out all the
    13  // calls for testing purposes.
    14  type MockResourceProvisioner struct {
    15  	sync.Mutex
    16  	// Anything you want, in case you need to store extra data with the mock.
    17  	Meta interface{}
    18  
    19  	GetConfigSchemaCalled       bool
    20  	GetConfigSchemaReturnSchema *configschema.Block
    21  	GetConfigSchemaReturnError  error
    22  
    23  	ApplyCalled      bool
    24  	ApplyOutput      UIOutput
    25  	ApplyState       *InstanceState
    26  	ApplyConfig      *ResourceConfig
    27  	ApplyFn          func(*InstanceState, *ResourceConfig) error
    28  	ApplyReturnError error
    29  
    30  	ValidateCalled       bool
    31  	ValidateConfig       *ResourceConfig
    32  	ValidateFn           func(c *ResourceConfig) ([]string, []error)
    33  	ValidateReturnWarns  []string
    34  	ValidateReturnErrors []error
    35  
    36  	StopCalled      bool
    37  	StopFn          func() error
    38  	StopReturnError error
    39  }
    40  
    41  var _ ResourceProvisioner = (*MockResourceProvisioner)(nil)
    42  
    43  func (p *MockResourceProvisioner) GetConfigSchema() (*configschema.Block, error) {
    44  	p.GetConfigSchemaCalled = true
    45  	return p.GetConfigSchemaReturnSchema, p.GetConfigSchemaReturnError
    46  }
    47  
    48  func (p *MockResourceProvisioner) Validate(c *ResourceConfig) ([]string, []error) {
    49  	p.Lock()
    50  	defer p.Unlock()
    51  
    52  	p.ValidateCalled = true
    53  	p.ValidateConfig = c
    54  	if p.ValidateFn != nil {
    55  		return p.ValidateFn(c)
    56  	}
    57  	return p.ValidateReturnWarns, p.ValidateReturnErrors
    58  }
    59  
    60  func (p *MockResourceProvisioner) Apply(
    61  	output UIOutput,
    62  	state *InstanceState,
    63  	c *ResourceConfig) error {
    64  	p.Lock()
    65  
    66  	p.ApplyCalled = true
    67  	p.ApplyOutput = output
    68  	p.ApplyState = state
    69  	p.ApplyConfig = c
    70  	if p.ApplyFn != nil {
    71  		fn := p.ApplyFn
    72  		p.Unlock()
    73  		return fn(state, c)
    74  	}
    75  
    76  	defer p.Unlock()
    77  	return p.ApplyReturnError
    78  }
    79  
    80  func (p *MockResourceProvisioner) Stop() error {
    81  	p.Lock()
    82  	defer p.Unlock()
    83  
    84  	p.StopCalled = true
    85  	if p.StopFn != nil {
    86  		return p.StopFn()
    87  	}
    88  
    89  	return p.StopReturnError
    90  }