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