github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/resource_provider_mock.go (about)

     1  package terraform
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // MockResourceProvider implements ResourceProvider but mocks out all the
     8  // calls for testing purposes.
     9  type MockResourceProvider struct {
    10  	sync.Mutex
    11  
    12  	// Anything you want, in case you need to store extra data with the mock.
    13  	Meta interface{}
    14  
    15  	CloseCalled                    bool
    16  	CloseError                     error
    17  	GetSchemaCalled                bool
    18  	GetSchemaRequest               *ProviderSchemaRequest
    19  	GetSchemaReturn                *ProviderSchema
    20  	GetSchemaReturnError           error
    21  	InputCalled                    bool
    22  	InputInput                     UIInput
    23  	InputConfig                    *ResourceConfig
    24  	InputReturnConfig              *ResourceConfig
    25  	InputReturnError               error
    26  	InputFn                        func(UIInput, *ResourceConfig) (*ResourceConfig, error)
    27  	ApplyCalled                    bool
    28  	ApplyInfo                      *InstanceInfo
    29  	ApplyState                     *InstanceState
    30  	ApplyDiff                      *InstanceDiff
    31  	ApplyFn                        func(*InstanceInfo, *InstanceState, *InstanceDiff) (*InstanceState, error)
    32  	ApplyReturn                    *InstanceState
    33  	ApplyReturnError               error
    34  	ConfigureCalled                bool
    35  	ConfigureConfig                *ResourceConfig
    36  	ConfigureFn                    func(*ResourceConfig) error
    37  	ConfigureReturnError           error
    38  	DiffCalled                     bool
    39  	DiffInfo                       *InstanceInfo
    40  	DiffState                      *InstanceState
    41  	DiffDesired                    *ResourceConfig
    42  	DiffFn                         func(*InstanceInfo, *InstanceState, *ResourceConfig) (*InstanceDiff, error)
    43  	DiffReturn                     *InstanceDiff
    44  	DiffReturnError                error
    45  	RefreshCalled                  bool
    46  	RefreshInfo                    *InstanceInfo
    47  	RefreshState                   *InstanceState
    48  	RefreshFn                      func(*InstanceInfo, *InstanceState) (*InstanceState, error)
    49  	RefreshReturn                  *InstanceState
    50  	RefreshReturnError             error
    51  	ResourcesCalled                bool
    52  	ResourcesReturn                []ResourceType
    53  	ReadDataApplyCalled            bool
    54  	ReadDataApplyInfo              *InstanceInfo
    55  	ReadDataApplyDiff              *InstanceDiff
    56  	ReadDataApplyFn                func(*InstanceInfo, *InstanceDiff) (*InstanceState, error)
    57  	ReadDataApplyReturn            *InstanceState
    58  	ReadDataApplyReturnError       error
    59  	ReadDataDiffCalled             bool
    60  	ReadDataDiffInfo               *InstanceInfo
    61  	ReadDataDiffDesired            *ResourceConfig
    62  	ReadDataDiffFn                 func(*InstanceInfo, *ResourceConfig) (*InstanceDiff, error)
    63  	ReadDataDiffReturn             *InstanceDiff
    64  	ReadDataDiffReturnError        error
    65  	StopCalled                     bool
    66  	StopFn                         func() error
    67  	StopReturnError                error
    68  	DataSourcesCalled              bool
    69  	DataSourcesReturn              []DataSource
    70  	ValidateCalled                 bool
    71  	ValidateConfig                 *ResourceConfig
    72  	ValidateFn                     func(*ResourceConfig) ([]string, []error)
    73  	ValidateReturnWarns            []string
    74  	ValidateReturnErrors           []error
    75  	ValidateResourceFn             func(string, *ResourceConfig) ([]string, []error)
    76  	ValidateResourceCalled         bool
    77  	ValidateResourceType           string
    78  	ValidateResourceConfig         *ResourceConfig
    79  	ValidateResourceReturnWarns    []string
    80  	ValidateResourceReturnErrors   []error
    81  	ValidateDataSourceFn           func(string, *ResourceConfig) ([]string, []error)
    82  	ValidateDataSourceCalled       bool
    83  	ValidateDataSourceType         string
    84  	ValidateDataSourceConfig       *ResourceConfig
    85  	ValidateDataSourceReturnWarns  []string
    86  	ValidateDataSourceReturnErrors []error
    87  
    88  	ImportStateCalled      bool
    89  	ImportStateInfo        *InstanceInfo
    90  	ImportStateID          string
    91  	ImportStateReturn      []*InstanceState
    92  	ImportStateReturnError error
    93  	ImportStateFn          func(*InstanceInfo, string) ([]*InstanceState, error)
    94  }
    95  
    96  func (p *MockResourceProvider) Close() error {
    97  	p.CloseCalled = true
    98  	return p.CloseError
    99  }
   100  
   101  func (p *MockResourceProvider) GetSchema(req *ProviderSchemaRequest) (*ProviderSchema, error) {
   102  	p.Lock()
   103  	defer p.Unlock()
   104  
   105  	p.GetSchemaCalled = true
   106  	p.GetSchemaRequest = req
   107  	return p.GetSchemaReturn, p.GetSchemaReturnError
   108  }
   109  
   110  func (p *MockResourceProvider) Input(
   111  	input UIInput, c *ResourceConfig) (*ResourceConfig, error) {
   112  	p.Lock()
   113  	defer p.Unlock()
   114  	p.InputCalled = true
   115  	p.InputInput = input
   116  	p.InputConfig = c
   117  	if p.InputFn != nil {
   118  		return p.InputFn(input, c)
   119  	}
   120  	return p.InputReturnConfig, p.InputReturnError
   121  }
   122  
   123  func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) {
   124  	p.Lock()
   125  	defer p.Unlock()
   126  
   127  	p.ValidateCalled = true
   128  	p.ValidateConfig = c
   129  	if p.ValidateFn != nil {
   130  		return p.ValidateFn(c)
   131  	}
   132  	return p.ValidateReturnWarns, p.ValidateReturnErrors
   133  }
   134  
   135  func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) {
   136  	p.Lock()
   137  	defer p.Unlock()
   138  
   139  	p.ValidateResourceCalled = true
   140  	p.ValidateResourceType = t
   141  	p.ValidateResourceConfig = c
   142  
   143  	if p.ValidateResourceFn != nil {
   144  		return p.ValidateResourceFn(t, c)
   145  	}
   146  
   147  	return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors
   148  }
   149  
   150  func (p *MockResourceProvider) Configure(c *ResourceConfig) error {
   151  	p.Lock()
   152  	defer p.Unlock()
   153  
   154  	p.ConfigureCalled = true
   155  	p.ConfigureConfig = c
   156  
   157  	if p.ConfigureFn != nil {
   158  		return p.ConfigureFn(c)
   159  	}
   160  
   161  	return p.ConfigureReturnError
   162  }
   163  
   164  func (p *MockResourceProvider) Stop() error {
   165  	p.Lock()
   166  	defer p.Unlock()
   167  
   168  	p.StopCalled = true
   169  	if p.StopFn != nil {
   170  		return p.StopFn()
   171  	}
   172  
   173  	return p.StopReturnError
   174  }
   175  
   176  func (p *MockResourceProvider) Apply(
   177  	info *InstanceInfo,
   178  	state *InstanceState,
   179  	diff *InstanceDiff) (*InstanceState, error) {
   180  	// We only lock while writing data. Reading is fine
   181  	p.Lock()
   182  	p.ApplyCalled = true
   183  	p.ApplyInfo = info
   184  	p.ApplyState = state
   185  	p.ApplyDiff = diff
   186  	p.Unlock()
   187  
   188  	if p.ApplyFn != nil {
   189  		return p.ApplyFn(info, state, diff)
   190  	}
   191  
   192  	return p.ApplyReturn.DeepCopy(), p.ApplyReturnError
   193  }
   194  
   195  func (p *MockResourceProvider) Diff(
   196  	info *InstanceInfo,
   197  	state *InstanceState,
   198  	desired *ResourceConfig) (*InstanceDiff, error) {
   199  	p.Lock()
   200  	defer p.Unlock()
   201  
   202  	p.DiffCalled = true
   203  	p.DiffInfo = info
   204  	p.DiffState = state
   205  	p.DiffDesired = desired
   206  	if p.DiffFn != nil {
   207  		return p.DiffFn(info, state, desired)
   208  	}
   209  
   210  	return p.DiffReturn.DeepCopy(), p.DiffReturnError
   211  }
   212  
   213  func (p *MockResourceProvider) Refresh(
   214  	info *InstanceInfo,
   215  	s *InstanceState) (*InstanceState, error) {
   216  	p.Lock()
   217  	defer p.Unlock()
   218  
   219  	p.RefreshCalled = true
   220  	p.RefreshInfo = info
   221  	p.RefreshState = s
   222  
   223  	if p.RefreshFn != nil {
   224  		return p.RefreshFn(info, s)
   225  	}
   226  
   227  	return p.RefreshReturn.DeepCopy(), p.RefreshReturnError
   228  }
   229  
   230  func (p *MockResourceProvider) Resources() []ResourceType {
   231  	p.Lock()
   232  	defer p.Unlock()
   233  
   234  	p.ResourcesCalled = true
   235  	return p.ResourcesReturn
   236  }
   237  
   238  func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) {
   239  	p.Lock()
   240  	defer p.Unlock()
   241  
   242  	p.ImportStateCalled = true
   243  	p.ImportStateInfo = info
   244  	p.ImportStateID = id
   245  	if p.ImportStateFn != nil {
   246  		return p.ImportStateFn(info, id)
   247  	}
   248  
   249  	var result []*InstanceState
   250  	if p.ImportStateReturn != nil {
   251  		result = make([]*InstanceState, len(p.ImportStateReturn))
   252  		for i, v := range p.ImportStateReturn {
   253  			result[i] = v.DeepCopy()
   254  		}
   255  	}
   256  
   257  	return result, p.ImportStateReturnError
   258  }
   259  
   260  func (p *MockResourceProvider) ValidateDataSource(t string, c *ResourceConfig) ([]string, []error) {
   261  	p.Lock()
   262  	defer p.Unlock()
   263  
   264  	p.ValidateDataSourceCalled = true
   265  	p.ValidateDataSourceType = t
   266  	p.ValidateDataSourceConfig = c
   267  
   268  	if p.ValidateDataSourceFn != nil {
   269  		return p.ValidateDataSourceFn(t, c)
   270  	}
   271  
   272  	return p.ValidateDataSourceReturnWarns, p.ValidateDataSourceReturnErrors
   273  }
   274  
   275  func (p *MockResourceProvider) ReadDataDiff(
   276  	info *InstanceInfo,
   277  	desired *ResourceConfig) (*InstanceDiff, error) {
   278  	p.Lock()
   279  	defer p.Unlock()
   280  
   281  	p.ReadDataDiffCalled = true
   282  	p.ReadDataDiffInfo = info
   283  	p.ReadDataDiffDesired = desired
   284  	if p.ReadDataDiffFn != nil {
   285  		return p.ReadDataDiffFn(info, desired)
   286  	}
   287  
   288  	return p.ReadDataDiffReturn.DeepCopy(), p.ReadDataDiffReturnError
   289  }
   290  
   291  func (p *MockResourceProvider) ReadDataApply(
   292  	info *InstanceInfo,
   293  	d *InstanceDiff) (*InstanceState, error) {
   294  	p.Lock()
   295  	defer p.Unlock()
   296  
   297  	p.ReadDataApplyCalled = true
   298  	p.ReadDataApplyInfo = info
   299  	p.ReadDataApplyDiff = d
   300  
   301  	if p.ReadDataApplyFn != nil {
   302  		return p.ReadDataApplyFn(info, d)
   303  	}
   304  
   305  	return p.ReadDataApplyReturn.DeepCopy(), p.ReadDataApplyReturnError
   306  }
   307  
   308  func (p *MockResourceProvider) DataSources() []DataSource {
   309  	p.Lock()
   310  	defer p.Unlock()
   311  
   312  	p.DataSourcesCalled = true
   313  	return p.DataSourcesReturn
   314  }