2014-05-28 17:07:47 -05:00
|
|
|
package terraform
|
|
|
|
|
2014-07-10 15:38:04 -05:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-05-28 17:07:47 -05:00
|
|
|
// MockResourceProvider implements ResourceProvider but mocks out all the
|
|
|
|
// calls for testing purposes.
|
|
|
|
type MockResourceProvider struct {
|
2014-07-10 15:38:04 -05:00
|
|
|
sync.Mutex
|
|
|
|
|
2014-06-03 17:08:00 -05:00
|
|
|
// Anything you want, in case you need to store extra data with the mock.
|
|
|
|
Meta interface{}
|
|
|
|
|
2014-07-02 22:35:03 -05:00
|
|
|
ApplyCalled bool
|
|
|
|
ApplyState *ResourceState
|
|
|
|
ApplyDiff *ResourceDiff
|
|
|
|
ApplyFn func(*ResourceState, *ResourceDiff) (*ResourceState, error)
|
|
|
|
ApplyReturn *ResourceState
|
|
|
|
ApplyReturnError error
|
|
|
|
ConfigureCalled bool
|
|
|
|
ConfigureConfig *ResourceConfig
|
|
|
|
ConfigureReturnError error
|
|
|
|
DiffCalled bool
|
|
|
|
DiffState *ResourceState
|
|
|
|
DiffDesired *ResourceConfig
|
|
|
|
DiffFn func(*ResourceState, *ResourceConfig) (*ResourceDiff, error)
|
|
|
|
DiffReturn *ResourceDiff
|
|
|
|
DiffReturnError error
|
|
|
|
RefreshCalled bool
|
|
|
|
RefreshState *ResourceState
|
|
|
|
RefreshFn func(*ResourceState) (*ResourceState, error)
|
|
|
|
RefreshReturn *ResourceState
|
|
|
|
RefreshReturnError error
|
|
|
|
ResourcesCalled bool
|
|
|
|
ResourcesReturn []ResourceType
|
|
|
|
ValidateCalled bool
|
|
|
|
ValidateConfig *ResourceConfig
|
|
|
|
ValidateReturnWarns []string
|
|
|
|
ValidateReturnErrors []error
|
2014-07-11 13:09:19 -05:00
|
|
|
ValidateResourceFn func(string, *ResourceConfig) ([]string, []error)
|
2014-07-02 22:35:03 -05:00
|
|
|
ValidateResourceCalled bool
|
|
|
|
ValidateResourceType string
|
|
|
|
ValidateResourceConfig *ResourceConfig
|
|
|
|
ValidateResourceReturnWarns []string
|
|
|
|
ValidateResourceReturnErrors []error
|
2014-06-13 00:30:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) {
|
2014-07-10 15:38:04 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-13 00:30:09 -05:00
|
|
|
p.ValidateCalled = true
|
|
|
|
p.ValidateConfig = c
|
|
|
|
return p.ValidateReturnWarns, p.ValidateReturnErrors
|
2014-05-28 17:07:47 -05:00
|
|
|
}
|
|
|
|
|
2014-07-02 22:35:03 -05:00
|
|
|
func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) {
|
2014-07-10 15:38:04 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-07-02 22:35:03 -05:00
|
|
|
p.ValidateResourceCalled = true
|
|
|
|
p.ValidateResourceType = t
|
|
|
|
p.ValidateResourceConfig = c
|
2014-07-11 13:09:19 -05:00
|
|
|
|
|
|
|
if p.ValidateResourceFn != nil {
|
|
|
|
return p.ValidateResourceFn(t, c)
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:35:03 -05:00
|
|
|
return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:59:59 -05:00
|
|
|
func (p *MockResourceProvider) Configure(c *ResourceConfig) error {
|
2014-07-10 15:38:04 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-05-28 17:07:47 -05:00
|
|
|
p.ConfigureCalled = true
|
|
|
|
p.ConfigureConfig = c
|
2014-06-06 02:28:57 -05:00
|
|
|
return p.ConfigureReturnError
|
2014-05-28 17:07:47 -05:00
|
|
|
}
|
2014-06-03 16:26:31 -05:00
|
|
|
|
2014-06-18 17:35:03 -05:00
|
|
|
func (p *MockResourceProvider) Apply(
|
|
|
|
state *ResourceState,
|
|
|
|
diff *ResourceDiff) (*ResourceState, error) {
|
2014-07-10 15:38:04 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-18 17:35:03 -05:00
|
|
|
p.ApplyCalled = true
|
|
|
|
p.ApplyState = state
|
|
|
|
p.ApplyDiff = diff
|
|
|
|
if p.ApplyFn != nil {
|
|
|
|
return p.ApplyFn(state, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.ApplyReturn, p.ApplyReturnError
|
|
|
|
}
|
|
|
|
|
2014-06-03 18:42:21 -05:00
|
|
|
func (p *MockResourceProvider) Diff(
|
2014-06-05 09:27:01 -05:00
|
|
|
state *ResourceState,
|
2014-06-12 19:59:59 -05:00
|
|
|
desired *ResourceConfig) (*ResourceDiff, error) {
|
2014-07-10 15:38:04 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-05 04:32:10 -05:00
|
|
|
p.DiffCalled = true
|
|
|
|
p.DiffState = state
|
|
|
|
p.DiffDesired = desired
|
2014-06-05 08:57:06 -05:00
|
|
|
if p.DiffFn != nil {
|
|
|
|
return p.DiffFn(state, desired)
|
|
|
|
}
|
|
|
|
|
2014-06-05 04:32:10 -05:00
|
|
|
return p.DiffReturn, p.DiffReturnError
|
2014-06-03 18:42:21 -05:00
|
|
|
}
|
|
|
|
|
2014-06-19 23:22:07 -05:00
|
|
|
func (p *MockResourceProvider) Refresh(
|
|
|
|
s *ResourceState) (*ResourceState, error) {
|
2014-07-10 15:38:04 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-19 23:22:07 -05:00
|
|
|
p.RefreshCalled = true
|
|
|
|
p.RefreshState = s
|
|
|
|
|
|
|
|
if p.RefreshFn != nil {
|
|
|
|
return p.RefreshFn(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.RefreshReturn, p.RefreshReturnError
|
|
|
|
}
|
|
|
|
|
2014-06-03 16:26:31 -05:00
|
|
|
func (p *MockResourceProvider) Resources() []ResourceType {
|
2014-07-10 15:38:04 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-03 16:26:31 -05:00
|
|
|
p.ResourcesCalled = true
|
|
|
|
return p.ResourcesReturn
|
|
|
|
}
|