2014-05-29 00:07:47 +02:00
|
|
|
package terraform
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
import "sync"
|
2014-07-10 22:38:04 +02:00
|
|
|
|
2014-05-29 00:07:47 +02:00
|
|
|
// MockResourceProvider implements ResourceProvider but mocks out all the
|
|
|
|
// calls for testing purposes.
|
|
|
|
type MockResourceProvider struct {
|
2014-07-10 22:38:04 +02:00
|
|
|
sync.Mutex
|
|
|
|
|
2014-06-04 00:08:00 +02:00
|
|
|
// Anything you want, in case you need to store extra data with the mock.
|
|
|
|
Meta interface{}
|
|
|
|
|
2015-06-29 19:33:37 +02:00
|
|
|
CloseCalled bool
|
|
|
|
CloseError error
|
2014-09-29 08:50:37 +02:00
|
|
|
InputCalled bool
|
|
|
|
InputInput UIInput
|
|
|
|
InputConfig *ResourceConfig
|
|
|
|
InputReturnConfig *ResourceConfig
|
|
|
|
InputReturnError error
|
2014-09-29 18:13:15 +02:00
|
|
|
InputFn func(UIInput, *ResourceConfig) (*ResourceConfig, error)
|
2014-07-03 05:35:03 +02:00
|
|
|
ApplyCalled bool
|
2014-09-17 01:20:11 +02:00
|
|
|
ApplyInfo *InstanceInfo
|
|
|
|
ApplyState *InstanceState
|
2014-09-18 01:33:24 +02:00
|
|
|
ApplyDiff *InstanceDiff
|
|
|
|
ApplyFn func(*InstanceInfo, *InstanceState, *InstanceDiff) (*InstanceState, error)
|
2014-09-17 01:20:11 +02:00
|
|
|
ApplyReturn *InstanceState
|
2014-07-03 05:35:03 +02:00
|
|
|
ApplyReturnError error
|
|
|
|
ConfigureCalled bool
|
|
|
|
ConfigureConfig *ResourceConfig
|
2014-09-24 22:31:35 +02:00
|
|
|
ConfigureFn func(*ResourceConfig) error
|
2014-07-03 05:35:03 +02:00
|
|
|
ConfigureReturnError error
|
|
|
|
DiffCalled bool
|
2014-09-17 01:20:11 +02:00
|
|
|
DiffInfo *InstanceInfo
|
|
|
|
DiffState *InstanceState
|
2014-07-03 05:35:03 +02:00
|
|
|
DiffDesired *ResourceConfig
|
2014-09-18 01:33:24 +02:00
|
|
|
DiffFn func(*InstanceInfo, *InstanceState, *ResourceConfig) (*InstanceDiff, error)
|
|
|
|
DiffReturn *InstanceDiff
|
2014-07-03 05:35:03 +02:00
|
|
|
DiffReturnError error
|
|
|
|
RefreshCalled bool
|
2014-09-17 01:20:11 +02:00
|
|
|
RefreshInfo *InstanceInfo
|
|
|
|
RefreshState *InstanceState
|
|
|
|
RefreshFn func(*InstanceInfo, *InstanceState) (*InstanceState, error)
|
|
|
|
RefreshReturn *InstanceState
|
2014-07-03 05:35:03 +02:00
|
|
|
RefreshReturnError error
|
|
|
|
ResourcesCalled bool
|
|
|
|
ResourcesReturn []ResourceType
|
|
|
|
ValidateCalled bool
|
|
|
|
ValidateConfig *ResourceConfig
|
2014-09-25 06:38:23 +02:00
|
|
|
ValidateFn func(*ResourceConfig) ([]string, []error)
|
2014-07-03 05:35:03 +02:00
|
|
|
ValidateReturnWarns []string
|
|
|
|
ValidateReturnErrors []error
|
2014-07-11 20:09:19 +02:00
|
|
|
ValidateResourceFn func(string, *ResourceConfig) ([]string, []error)
|
2014-07-03 05:35:03 +02:00
|
|
|
ValidateResourceCalled bool
|
|
|
|
ValidateResourceType string
|
|
|
|
ValidateResourceConfig *ResourceConfig
|
|
|
|
ValidateResourceReturnWarns []string
|
|
|
|
ValidateResourceReturnErrors []error
|
2014-06-13 07:30:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:33:37 +02:00
|
|
|
func (p *MockResourceProvider) Close() error {
|
|
|
|
p.CloseCalled = true
|
|
|
|
return p.CloseError
|
|
|
|
}
|
|
|
|
|
2014-09-29 08:50:37 +02:00
|
|
|
func (p *MockResourceProvider) Input(
|
|
|
|
input UIInput, c *ResourceConfig) (*ResourceConfig, error) {
|
|
|
|
p.InputCalled = true
|
|
|
|
p.InputInput = input
|
|
|
|
p.InputConfig = c
|
2014-09-29 18:13:15 +02:00
|
|
|
if p.InputFn != nil {
|
|
|
|
return p.InputFn(input, c)
|
|
|
|
}
|
2014-09-29 08:50:37 +02:00
|
|
|
return p.InputReturnConfig, p.InputReturnError
|
|
|
|
}
|
|
|
|
|
2014-06-13 07:30:09 +02:00
|
|
|
func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-13 07:30:09 +02:00
|
|
|
p.ValidateCalled = true
|
|
|
|
p.ValidateConfig = c
|
2014-09-25 06:38:23 +02:00
|
|
|
if p.ValidateFn != nil {
|
|
|
|
return p.ValidateFn(c)
|
|
|
|
}
|
2014-06-13 07:30:09 +02:00
|
|
|
return p.ValidateReturnWarns, p.ValidateReturnErrors
|
2014-05-29 00:07:47 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 05:35:03 +02:00
|
|
|
func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-07-03 05:35:03 +02:00
|
|
|
p.ValidateResourceCalled = true
|
|
|
|
p.ValidateResourceType = t
|
|
|
|
p.ValidateResourceConfig = c
|
2014-07-11 20:09:19 +02:00
|
|
|
|
|
|
|
if p.ValidateResourceFn != nil {
|
|
|
|
return p.ValidateResourceFn(t, c)
|
|
|
|
}
|
|
|
|
|
2014-07-03 05:35:03 +02:00
|
|
|
return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:59:59 +02:00
|
|
|
func (p *MockResourceProvider) Configure(c *ResourceConfig) error {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-05-29 00:07:47 +02:00
|
|
|
p.ConfigureCalled = true
|
|
|
|
p.ConfigureConfig = c
|
2014-09-24 22:31:35 +02:00
|
|
|
|
|
|
|
if p.ConfigureFn != nil {
|
|
|
|
return p.ConfigureFn(c)
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:28:57 +02:00
|
|
|
return p.ConfigureReturnError
|
2014-05-29 00:07:47 +02:00
|
|
|
}
|
2014-06-03 23:26:31 +02:00
|
|
|
|
2014-06-19 00:35:03 +02:00
|
|
|
func (p *MockResourceProvider) Apply(
|
2014-09-17 01:20:11 +02:00
|
|
|
info *InstanceInfo,
|
|
|
|
state *InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *InstanceDiff) (*InstanceState, error) {
|
2015-10-14 19:43:51 +02:00
|
|
|
// We only lock while writing data. Reading is fine
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
2014-06-19 00:35:03 +02:00
|
|
|
p.ApplyCalled = true
|
2014-09-17 01:20:11 +02:00
|
|
|
p.ApplyInfo = info
|
2014-06-19 00:35:03 +02:00
|
|
|
p.ApplyState = state
|
|
|
|
p.ApplyDiff = diff
|
2015-10-14 19:43:51 +02:00
|
|
|
p.Unlock()
|
|
|
|
|
2014-06-19 00:35:03 +02:00
|
|
|
if p.ApplyFn != nil {
|
2014-09-17 01:20:11 +02:00
|
|
|
return p.ApplyFn(info, state, diff)
|
2014-06-19 00:35:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.ApplyReturn, p.ApplyReturnError
|
|
|
|
}
|
|
|
|
|
2014-06-04 01:42:21 +02:00
|
|
|
func (p *MockResourceProvider) Diff(
|
2014-09-17 01:20:11 +02:00
|
|
|
info *InstanceInfo,
|
|
|
|
state *InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
desired *ResourceConfig) (*InstanceDiff, error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-05 11:32:10 +02:00
|
|
|
p.DiffCalled = true
|
2014-09-17 01:20:11 +02:00
|
|
|
p.DiffInfo = info
|
2014-06-05 11:32:10 +02:00
|
|
|
p.DiffState = state
|
|
|
|
p.DiffDesired = desired
|
2014-06-05 15:57:06 +02:00
|
|
|
if p.DiffFn != nil {
|
2014-09-17 01:20:11 +02:00
|
|
|
return p.DiffFn(info, state, desired)
|
2014-06-05 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
2014-06-05 11:32:10 +02:00
|
|
|
return p.DiffReturn, p.DiffReturnError
|
2014-06-04 01:42:21 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 06:22:07 +02:00
|
|
|
func (p *MockResourceProvider) Refresh(
|
2014-09-17 01:20:11 +02:00
|
|
|
info *InstanceInfo,
|
|
|
|
s *InstanceState) (*InstanceState, error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-20 06:22:07 +02:00
|
|
|
p.RefreshCalled = true
|
2014-09-17 01:20:11 +02:00
|
|
|
p.RefreshInfo = info
|
2014-06-20 06:22:07 +02:00
|
|
|
p.RefreshState = s
|
|
|
|
|
|
|
|
if p.RefreshFn != nil {
|
2014-09-17 01:20:11 +02:00
|
|
|
return p.RefreshFn(info, s)
|
2014-06-20 06:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.RefreshReturn, p.RefreshReturnError
|
|
|
|
}
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
func (p *MockResourceProvider) Resources() []ResourceType {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
p.ResourcesCalled = true
|
|
|
|
return p.ResourcesReturn
|
|
|
|
}
|