github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/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  	ReadDataApplyCalled            bool
    48  	ReadDataApplyInfo              *InstanceInfo
    49  	ReadDataApplyDiff              *InstanceDiff
    50  	ReadDataApplyFn                func(*InstanceInfo, *InstanceDiff) (*InstanceState, error)
    51  	ReadDataApplyReturn            *InstanceState
    52  	ReadDataApplyReturnError       error
    53  	ReadDataDiffCalled             bool
    54  	ReadDataDiffInfo               *InstanceInfo
    55  	ReadDataDiffDesired            *ResourceConfig
    56  	ReadDataDiffFn                 func(*InstanceInfo, *ResourceConfig) (*InstanceDiff, error)
    57  	ReadDataDiffReturn             *InstanceDiff
    58  	ReadDataDiffReturnError        error
    59  	DataSourcesCalled              bool
    60  	DataSourcesReturn              []DataSource
    61  	ValidateCalled                 bool
    62  	ValidateConfig                 *ResourceConfig
    63  	ValidateFn                     func(*ResourceConfig) ([]string, []error)
    64  	ValidateReturnWarns            []string
    65  	ValidateReturnErrors           []error
    66  	ValidateResourceFn             func(string, *ResourceConfig) ([]string, []error)
    67  	ValidateResourceCalled         bool
    68  	ValidateResourceType           string
    69  	ValidateResourceConfig         *ResourceConfig
    70  	ValidateResourceReturnWarns    []string
    71  	ValidateResourceReturnErrors   []error
    72  	ValidateDataSourceFn           func(string, *ResourceConfig) ([]string, []error)
    73  	ValidateDataSourceCalled       bool
    74  	ValidateDataSourceType         string
    75  	ValidateDataSourceConfig       *ResourceConfig
    76  	ValidateDataSourceReturnWarns  []string
    77  	ValidateDataSourceReturnErrors []error
    78  
    79  	ImportStateCalled      bool
    80  	ImportStateInfo        *InstanceInfo
    81  	ImportStateID          string
    82  	ImportStateReturn      []*InstanceState
    83  	ImportStateReturnError error
    84  	ImportStateFn          func(*InstanceInfo, string) ([]*InstanceState, error)
    85  }
    86  
    87  func (p *MockResourceProvider) Close() error {
    88  	p.CloseCalled = true
    89  	return p.CloseError
    90  }
    91  
    92  func (p *MockResourceProvider) Input(
    93  	input UIInput, c *ResourceConfig) (*ResourceConfig, error) {
    94  	p.InputCalled = true
    95  	p.InputInput = input
    96  	p.InputConfig = c
    97  	if p.InputFn != nil {
    98  		return p.InputFn(input, c)
    99  	}
   100  	return p.InputReturnConfig, p.InputReturnError
   101  }
   102  
   103  func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) {
   104  	p.Lock()
   105  	defer p.Unlock()
   106  
   107  	p.ValidateCalled = true
   108  	p.ValidateConfig = c
   109  	if p.ValidateFn != nil {
   110  		return p.ValidateFn(c)
   111  	}
   112  	return p.ValidateReturnWarns, p.ValidateReturnErrors
   113  }
   114  
   115  func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) {
   116  	p.Lock()
   117  	defer p.Unlock()
   118  
   119  	p.ValidateResourceCalled = true
   120  	p.ValidateResourceType = t
   121  	p.ValidateResourceConfig = c
   122  
   123  	if p.ValidateResourceFn != nil {
   124  		return p.ValidateResourceFn(t, c)
   125  	}
   126  
   127  	return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors
   128  }
   129  
   130  func (p *MockResourceProvider) Configure(c *ResourceConfig) error {
   131  	p.Lock()
   132  	defer p.Unlock()
   133  
   134  	p.ConfigureCalled = true
   135  	p.ConfigureConfig = c
   136  
   137  	if p.ConfigureFn != nil {
   138  		return p.ConfigureFn(c)
   139  	}
   140  
   141  	return p.ConfigureReturnError
   142  }
   143  
   144  func (p *MockResourceProvider) Apply(
   145  	info *InstanceInfo,
   146  	state *InstanceState,
   147  	diff *InstanceDiff) (*InstanceState, error) {
   148  	// We only lock while writing data. Reading is fine
   149  	p.Lock()
   150  	p.ApplyCalled = true
   151  	p.ApplyInfo = info
   152  	p.ApplyState = state
   153  	p.ApplyDiff = diff
   154  	p.Unlock()
   155  
   156  	if p.ApplyFn != nil {
   157  		return p.ApplyFn(info, state, diff)
   158  	}
   159  
   160  	return p.ApplyReturn, p.ApplyReturnError
   161  }
   162  
   163  func (p *MockResourceProvider) Diff(
   164  	info *InstanceInfo,
   165  	state *InstanceState,
   166  	desired *ResourceConfig) (*InstanceDiff, error) {
   167  	p.Lock()
   168  	defer p.Unlock()
   169  
   170  	p.DiffCalled = true
   171  	p.DiffInfo = info
   172  	p.DiffState = state
   173  	p.DiffDesired = desired
   174  	if p.DiffFn != nil {
   175  		return p.DiffFn(info, state, desired)
   176  	}
   177  
   178  	return p.DiffReturn, p.DiffReturnError
   179  }
   180  
   181  func (p *MockResourceProvider) Refresh(
   182  	info *InstanceInfo,
   183  	s *InstanceState) (*InstanceState, error) {
   184  	p.Lock()
   185  	defer p.Unlock()
   186  
   187  	p.RefreshCalled = true
   188  	p.RefreshInfo = info
   189  	p.RefreshState = s
   190  
   191  	if p.RefreshFn != nil {
   192  		return p.RefreshFn(info, s)
   193  	}
   194  
   195  	return p.RefreshReturn, p.RefreshReturnError
   196  }
   197  
   198  func (p *MockResourceProvider) Resources() []ResourceType {
   199  	p.Lock()
   200  	defer p.Unlock()
   201  
   202  	p.ResourcesCalled = true
   203  	return p.ResourcesReturn
   204  }
   205  
   206  func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) {
   207  	p.Lock()
   208  	defer p.Unlock()
   209  
   210  	p.ImportStateCalled = true
   211  	p.ImportStateInfo = info
   212  	p.ImportStateID = id
   213  	if p.ImportStateFn != nil {
   214  		return p.ImportStateFn(info, id)
   215  	}
   216  
   217  	return p.ImportStateReturn, p.ImportStateReturnError
   218  }
   219  
   220  func (p *MockResourceProvider) ValidateDataSource(t string, c *ResourceConfig) ([]string, []error) {
   221  	p.Lock()
   222  	defer p.Unlock()
   223  
   224  	p.ValidateDataSourceCalled = true
   225  	p.ValidateDataSourceType = t
   226  	p.ValidateDataSourceConfig = c
   227  
   228  	if p.ValidateDataSourceFn != nil {
   229  		return p.ValidateDataSourceFn(t, c)
   230  	}
   231  
   232  	return p.ValidateDataSourceReturnWarns, p.ValidateDataSourceReturnErrors
   233  }
   234  
   235  func (p *MockResourceProvider) ReadDataDiff(
   236  	info *InstanceInfo,
   237  	desired *ResourceConfig) (*InstanceDiff, error) {
   238  	p.Lock()
   239  	defer p.Unlock()
   240  
   241  	p.ReadDataDiffCalled = true
   242  	p.ReadDataDiffInfo = info
   243  	p.ReadDataDiffDesired = desired
   244  	if p.ReadDataDiffFn != nil {
   245  		return p.ReadDataDiffFn(info, desired)
   246  	}
   247  
   248  	return p.ReadDataDiffReturn, p.ReadDataDiffReturnError
   249  }
   250  
   251  func (p *MockResourceProvider) ReadDataApply(
   252  	info *InstanceInfo,
   253  	d *InstanceDiff) (*InstanceState, error) {
   254  	p.Lock()
   255  	defer p.Unlock()
   256  
   257  	p.ReadDataApplyCalled = true
   258  	p.ReadDataApplyInfo = info
   259  	p.ReadDataApplyDiff = d
   260  
   261  	if p.ReadDataApplyFn != nil {
   262  		return p.ReadDataApplyFn(info, d)
   263  	}
   264  
   265  	return p.ReadDataApplyReturn, p.ReadDataApplyReturnError
   266  }
   267  
   268  func (p *MockResourceProvider) DataSources() []DataSource {
   269  	p.Lock()
   270  	defer p.Unlock()
   271  
   272  	p.DataSourcesCalled = true
   273  	return p.DataSourcesReturn
   274  }