2014-07-08 13:01:22 -05:00
|
|
|
package terraform
|
|
|
|
|
2018-05-04 14:06:05 -05:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2018-07-05 12:33:29 -05:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-05-04 14:06:05 -05:00
|
|
|
)
|
2016-07-29 12:17:48 -05:00
|
|
|
|
2014-07-08 13:01:22 -05:00
|
|
|
// MockResourceProvisioner implements ResourceProvisioner but mocks out all the
|
|
|
|
// calls for testing purposes.
|
|
|
|
type MockResourceProvisioner struct {
|
2016-07-29 12:17:48 -05:00
|
|
|
sync.Mutex
|
2014-07-08 13:01:22 -05:00
|
|
|
// Anything you want, in case you need to store extra data with the mock.
|
|
|
|
Meta interface{}
|
|
|
|
|
2018-05-04 14:06:05 -05:00
|
|
|
GetConfigSchemaCalled bool
|
|
|
|
GetConfigSchemaReturnSchema *configschema.Block
|
|
|
|
GetConfigSchemaReturnError error
|
|
|
|
|
2014-07-08 13:01:22 -05:00
|
|
|
ApplyCalled bool
|
2014-10-04 11:20:05 -05:00
|
|
|
ApplyOutput UIOutput
|
2014-09-16 18:20:11 -05:00
|
|
|
ApplyState *InstanceState
|
2014-07-08 13:01:22 -05:00
|
|
|
ApplyConfig *ResourceConfig
|
2014-09-16 18:20:11 -05:00
|
|
|
ApplyFn func(*InstanceState, *ResourceConfig) error
|
2014-07-08 13:01:22 -05:00
|
|
|
ApplyReturnError error
|
|
|
|
|
|
|
|
ValidateCalled bool
|
|
|
|
ValidateConfig *ResourceConfig
|
2014-07-09 17:02:00 -05:00
|
|
|
ValidateFn func(c *ResourceConfig) ([]string, []error)
|
2014-07-08 13:01:22 -05:00
|
|
|
ValidateReturnWarns []string
|
|
|
|
ValidateReturnErrors []error
|
2016-12-22 13:33:26 -06:00
|
|
|
|
|
|
|
StopCalled bool
|
|
|
|
StopFn func() error
|
|
|
|
StopReturnError error
|
2014-07-08 13:01:22 -05:00
|
|
|
}
|
|
|
|
|
2018-05-04 14:06:05 -05:00
|
|
|
var _ ResourceProvisioner = (*MockResourceProvisioner)(nil)
|
|
|
|
|
|
|
|
func (p *MockResourceProvisioner) GetConfigSchema() (*configschema.Block, error) {
|
|
|
|
p.GetConfigSchemaCalled = true
|
|
|
|
return p.GetConfigSchemaReturnSchema, p.GetConfigSchemaReturnError
|
|
|
|
}
|
|
|
|
|
2014-07-08 13:01:22 -05:00
|
|
|
func (p *MockResourceProvisioner) Validate(c *ResourceConfig) ([]string, []error) {
|
2016-07-29 12:17:48 -05:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-07-08 13:01:22 -05:00
|
|
|
p.ValidateCalled = true
|
|
|
|
p.ValidateConfig = c
|
2014-07-09 17:02:00 -05:00
|
|
|
if p.ValidateFn != nil {
|
|
|
|
return p.ValidateFn(c)
|
|
|
|
}
|
2014-07-08 13:01:22 -05:00
|
|
|
return p.ValidateReturnWarns, p.ValidateReturnErrors
|
|
|
|
}
|
|
|
|
|
2014-10-04 11:20:05 -05:00
|
|
|
func (p *MockResourceProvisioner) Apply(
|
|
|
|
output UIOutput,
|
|
|
|
state *InstanceState,
|
|
|
|
c *ResourceConfig) error {
|
2016-07-29 12:17:48 -05:00
|
|
|
p.Lock()
|
|
|
|
|
2014-07-08 13:01:22 -05:00
|
|
|
p.ApplyCalled = true
|
2014-10-04 11:20:05 -05:00
|
|
|
p.ApplyOutput = output
|
2014-07-08 13:01:22 -05:00
|
|
|
p.ApplyState = state
|
|
|
|
p.ApplyConfig = c
|
|
|
|
if p.ApplyFn != nil {
|
2016-12-22 13:33:26 -06:00
|
|
|
fn := p.ApplyFn
|
|
|
|
p.Unlock()
|
|
|
|
return fn(state, c)
|
2014-07-08 13:01:22 -05:00
|
|
|
}
|
2016-12-22 13:33:26 -06:00
|
|
|
|
|
|
|
defer p.Unlock()
|
2014-07-22 11:56:39 -05:00
|
|
|
return p.ApplyReturnError
|
2014-07-08 13:01:22 -05:00
|
|
|
}
|
2016-12-22 13:33:26 -06:00
|
|
|
|
|
|
|
func (p *MockResourceProvisioner) Stop() error {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
p.StopCalled = true
|
|
|
|
if p.StopFn != nil {
|
|
|
|
return p.StopFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.StopReturnError
|
|
|
|
}
|