github.com/magodo/terraform@v0.11.12-beta1/terraform/resource_provisioner_mock.go (about)

     1  package terraform
     2  
     3  import "sync"
     4  
     5  // MockResourceProvisioner implements ResourceProvisioner but mocks out all the
     6  // calls for testing purposes.
     7  type MockResourceProvisioner struct {
     8  	sync.Mutex
     9  	// Anything you want, in case you need to store extra data with the mock.
    10  	Meta interface{}
    11  
    12  	ApplyCalled      bool
    13  	ApplyOutput      UIOutput
    14  	ApplyState       *InstanceState
    15  	ApplyConfig      *ResourceConfig
    16  	ApplyFn          func(*InstanceState, *ResourceConfig) error
    17  	ApplyReturnError error
    18  
    19  	ValidateCalled       bool
    20  	ValidateConfig       *ResourceConfig
    21  	ValidateFn           func(c *ResourceConfig) ([]string, []error)
    22  	ValidateReturnWarns  []string
    23  	ValidateReturnErrors []error
    24  
    25  	StopCalled      bool
    26  	StopFn          func() error
    27  	StopReturnError error
    28  }
    29  
    30  func (p *MockResourceProvisioner) Validate(c *ResourceConfig) ([]string, []error) {
    31  	p.Lock()
    32  	defer p.Unlock()
    33  
    34  	p.ValidateCalled = true
    35  	p.ValidateConfig = c
    36  	if p.ValidateFn != nil {
    37  		return p.ValidateFn(c)
    38  	}
    39  	return p.ValidateReturnWarns, p.ValidateReturnErrors
    40  }
    41  
    42  func (p *MockResourceProvisioner) Apply(
    43  	output UIOutput,
    44  	state *InstanceState,
    45  	c *ResourceConfig) error {
    46  	p.Lock()
    47  
    48  	p.ApplyCalled = true
    49  	p.ApplyOutput = output
    50  	p.ApplyState = state
    51  	p.ApplyConfig = c
    52  	if p.ApplyFn != nil {
    53  		fn := p.ApplyFn
    54  		p.Unlock()
    55  		return fn(state, c)
    56  	}
    57  
    58  	defer p.Unlock()
    59  	return p.ApplyReturnError
    60  }
    61  
    62  func (p *MockResourceProvisioner) Stop() error {
    63  	p.Lock()
    64  	defer p.Unlock()
    65  
    66  	p.StopCalled = true
    67  	if p.StopFn != nil {
    68  		return p.StopFn()
    69  	}
    70  
    71  	return p.StopReturnError
    72  }