github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/terraform/resource_provider_mock.go (about)

     1  package terraform
     2  
     3  import "sync"
     4  
     5  // MockResourceProvider implements ResourceProvider but mocks out all the
     6  // calls for testing purposes.
     7  type MockResourceProvider struct {
     8  	sync.Mutex
     9  
    10  	// Anything you want, in case you need to store extra data with the mock.
    11  	Meta interface{}
    12  
    13  	CloseCalled                  bool
    14  	CloseError                   error
    15  	InputCalled                  bool
    16  	InputInput                   UIInput
    17  	InputConfig                  *ResourceConfig
    18  	InputReturnConfig            *ResourceConfig
    19  	InputReturnError             error
    20  	InputFn                      func(UIInput, *ResourceConfig) (*ResourceConfig, error)
    21  	ApplyCalled                  bool
    22  	ApplyInfo                    *InstanceInfo
    23  	ApplyState                   *InstanceState
    24  	ApplyDiff                    *InstanceDiff
    25  	ApplyFn                      func(*InstanceInfo, *InstanceState, *InstanceDiff) (*InstanceState, error)
    26  	ApplyReturn                  *InstanceState
    27  	ApplyReturnError             error
    28  	ConfigureCalled              bool
    29  	ConfigureConfig              *ResourceConfig
    30  	ConfigureFn                  func(*ResourceConfig) error
    31  	ConfigureReturnError         error
    32  	DiffCalled                   bool
    33  	DiffInfo                     *InstanceInfo
    34  	DiffState                    *InstanceState
    35  	DiffDesired                  *ResourceConfig
    36  	DiffFn                       func(*InstanceInfo, *InstanceState, *ResourceConfig) (*InstanceDiff, error)
    37  	DiffReturn                   *InstanceDiff
    38  	DiffReturnError              error
    39  	RefreshCalled                bool
    40  	RefreshInfo                  *InstanceInfo
    41  	RefreshState                 *InstanceState
    42  	RefreshFn                    func(*InstanceInfo, *InstanceState) (*InstanceState, error)
    43  	RefreshReturn                *InstanceState
    44  	RefreshReturnError           error
    45  	ResourcesCalled              bool
    46  	ResourcesReturn              []ResourceType
    47  	ValidateCalled               bool
    48  	ValidateConfig               *ResourceConfig
    49  	ValidateFn                   func(*ResourceConfig) ([]string, []error)
    50  	ValidateReturnWarns          []string
    51  	ValidateReturnErrors         []error
    52  	ValidateResourceFn           func(string, *ResourceConfig) ([]string, []error)
    53  	ValidateResourceCalled       bool
    54  	ValidateResourceType         string
    55  	ValidateResourceConfig       *ResourceConfig
    56  	ValidateResourceReturnWarns  []string
    57  	ValidateResourceReturnErrors []error
    58  }
    59  
    60  func (p *MockResourceProvider) Close() error {
    61  	p.CloseCalled = true
    62  	return p.CloseError
    63  }
    64  
    65  func (p *MockResourceProvider) Input(
    66  	input UIInput, c *ResourceConfig) (*ResourceConfig, error) {
    67  	p.InputCalled = true
    68  	p.InputInput = input
    69  	p.InputConfig = c
    70  	if p.InputFn != nil {
    71  		return p.InputFn(input, c)
    72  	}
    73  	return p.InputReturnConfig, p.InputReturnError
    74  }
    75  
    76  func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) {
    77  	p.Lock()
    78  	defer p.Unlock()
    79  
    80  	p.ValidateCalled = true
    81  	p.ValidateConfig = c
    82  	if p.ValidateFn != nil {
    83  		return p.ValidateFn(c)
    84  	}
    85  	return p.ValidateReturnWarns, p.ValidateReturnErrors
    86  }
    87  
    88  func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) {
    89  	p.Lock()
    90  	defer p.Unlock()
    91  
    92  	p.ValidateResourceCalled = true
    93  	p.ValidateResourceType = t
    94  	p.ValidateResourceConfig = c
    95  
    96  	if p.ValidateResourceFn != nil {
    97  		return p.ValidateResourceFn(t, c)
    98  	}
    99  
   100  	return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors
   101  }
   102  
   103  func (p *MockResourceProvider) Configure(c *ResourceConfig) error {
   104  	p.Lock()
   105  	defer p.Unlock()
   106  
   107  	p.ConfigureCalled = true
   108  	p.ConfigureConfig = c
   109  
   110  	if p.ConfigureFn != nil {
   111  		return p.ConfigureFn(c)
   112  	}
   113  
   114  	return p.ConfigureReturnError
   115  }
   116  
   117  func (p *MockResourceProvider) Apply(
   118  	info *InstanceInfo,
   119  	state *InstanceState,
   120  	diff *InstanceDiff) (*InstanceState, error) {
   121  	// We only lock while writing data. Reading is fine
   122  	p.Lock()
   123  	p.ApplyCalled = true
   124  	p.ApplyInfo = info
   125  	p.ApplyState = state
   126  	p.ApplyDiff = diff
   127  	p.Unlock()
   128  
   129  	if p.ApplyFn != nil {
   130  		return p.ApplyFn(info, state, diff)
   131  	}
   132  
   133  	return p.ApplyReturn, p.ApplyReturnError
   134  }
   135  
   136  func (p *MockResourceProvider) Diff(
   137  	info *InstanceInfo,
   138  	state *InstanceState,
   139  	desired *ResourceConfig) (*InstanceDiff, error) {
   140  	p.Lock()
   141  	defer p.Unlock()
   142  
   143  	p.DiffCalled = true
   144  	p.DiffInfo = info
   145  	p.DiffState = state
   146  	p.DiffDesired = desired
   147  	if p.DiffFn != nil {
   148  		return p.DiffFn(info, state, desired)
   149  	}
   150  
   151  	return p.DiffReturn, p.DiffReturnError
   152  }
   153  
   154  func (p *MockResourceProvider) Refresh(
   155  	info *InstanceInfo,
   156  	s *InstanceState) (*InstanceState, error) {
   157  	p.Lock()
   158  	defer p.Unlock()
   159  
   160  	p.RefreshCalled = true
   161  	p.RefreshInfo = info
   162  	p.RefreshState = s
   163  
   164  	if p.RefreshFn != nil {
   165  		return p.RefreshFn(info, s)
   166  	}
   167  
   168  	return p.RefreshReturn, p.RefreshReturnError
   169  }
   170  
   171  func (p *MockResourceProvider) Resources() []ResourceType {
   172  	p.Lock()
   173  	defer p.Unlock()
   174  
   175  	p.ResourcesCalled = true
   176  	return p.ResourcesReturn
   177  }