add new provider and provisioner mocks

MockProvider and MockProvisioner implement the new plugin interfaces,
and are built following the patterns used by the legacy
MockResourceProvider and MockResourceProvisioner
This commit is contained in:
James Bardin 2018-08-15 14:29:46 -04:00 committed by Martin Atkins
parent faa46d4727
commit bddfd0412c
2 changed files with 335 additions and 0 deletions

239
terraform/provider_mock.go Normal file
View File

@ -0,0 +1,239 @@
package terraform
import (
"sync"
"github.com/hashicorp/terraform/providers"
)
var _ providers.Interface = (*MockProvider)(nil)
// MockProvider implements providers.Interface but mocks out all the
// calls for testing purposes.
type MockProvider struct {
sync.Mutex
// Anything you want, in case you need to store extra data with the mock.
Meta interface{}
GetSchemaCalled bool
GetSchemaResponse providers.GetSchemaResponse
ValidateProviderConfigCalled bool
ValidateProviderConfigResponse providers.ValidateProviderConfigResponse
ValidateProviderConfigRequest providers.ValidateProviderConfigRequest
ValidateProviderConfigFn func(providers.ValidateProviderConfigRequest) providers.ValidateProviderConfigResponse
ValidateResourceTypeConfigCalled bool
ValidateResourceTypeConfigTypeName string
ValidateResourceTypeConfigResponse providers.ValidateResourceTypeConfigResponse
ValidateResourceTypeConfigRequest providers.ValidateResourceTypeConfigRequest
ValidateResourceTypeConfigFn func(providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse
ValidateDataSourceConfigCalled bool
ValidateDataSourceConfigTypeName string
ValidateDataSourceConfigResponse providers.ValidateDataSourceConfigResponse
ValidateDataSourceConfigRequest providers.ValidateDataSourceConfigRequest
ValidateDataSourceConfigFn func(providers.ValidateDataSourceConfigRequest) providers.ValidateDataSourceConfigResponse
UpgradeResourceStateCalled bool
UpgradeResourceStateTypeName string
UpgradeResourceStateResponse providers.UpgradeResourceStateResponse
UpgradeResourceStateRequest providers.UpgradeResourceStateRequest
UpgradeResourceStateFn func(providers.UpgradeResourceStateRequest) providers.UpgradeResourceStateResponse
ConfigureCalled bool
ConfigureResponse providers.ConfigureResponse
ConfigureRequest providers.ConfigureRequest
ConfigureFn func(providers.ConfigureRequest) providers.ConfigureResponse
StopCalled bool
StopFn func() error
StopResponse error
ReadResourceCalled bool
ReadResourceResponse providers.ReadResourceResponse
ReadResourceRequest providers.ReadResourceRequest
ReadResourceFn func(providers.ReadResourceRequest) providers.ReadResourceResponse
PlanResourceChangeCalled bool
PlanResourceChangeResponse providers.PlanResourceChangeResponse
PlanResourceChangeRequest providers.PlanResourceChangeRequest
PlanResourceChangeFn func(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse
ApplyResourceChangeCalled bool
ApplyResourceChangeResponse providers.ApplyResourceChangeResponse
ApplyResourceChangeRequest providers.ApplyResourceChangeRequest
ApplyResourceChangeFn func(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse
ImportResourceStateCalled bool
ImportResourceStateResponse providers.ImportResourceStateResponse
ImportResourceStateRequest providers.ImportResourceStateRequest
ImportResourceStateFn func(providers.ImportResourceStateRequest) providers.ImportResourceStateResponse
ReadDataSourceCalled bool
ReadDataSourceResponse providers.ReadDataSourceResponse
ReadDataSourceRequest providers.ReadDataSourceRequest
ReadDataSourceFn func(providers.ReadDataSourceRequest) providers.ReadDataSourceResponse
CloseCalled bool
CloseError error
}
func (p *MockProvider) GetSchema() providers.GetSchemaResponse {
p.Lock()
defer p.Unlock()
p.GetSchemaCalled = true
return p.GetSchemaResponse
}
func (p *MockProvider) ValidateProviderConfig(r providers.ValidateProviderConfigRequest) providers.ValidateProviderConfigResponse {
p.Lock()
defer p.Unlock()
p.ValidateProviderConfigCalled = true
p.ValidateProviderConfigRequest = r
if p.ValidateProviderConfigFn != nil {
return p.ValidateProviderConfigFn(r)
}
return p.ValidateProviderConfigResponse
}
func (p *MockProvider) ValidateResourceTypeConfig(r providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse {
p.Lock()
defer p.Unlock()
p.ValidateResourceTypeConfigCalled = true
p.ValidateResourceTypeConfigRequest = r
if p.ValidateResourceTypeConfigFn != nil {
return p.ValidateResourceTypeConfigFn(r)
}
return p.ValidateResourceTypeConfigResponse
}
func (p *MockProvider) ValidateDataSourceConfig(r providers.ValidateDataSourceConfigRequest) providers.ValidateDataSourceConfigResponse {
p.Lock()
defer p.Unlock()
p.ValidateDataSourceConfigCalled = true
p.ValidateDataSourceConfigRequest = r
if p.ValidateDataSourceConfigFn != nil {
return p.ValidateDataSourceConfigFn(r)
}
return p.ValidateDataSourceConfigResponse
}
func (p *MockProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequest) providers.UpgradeResourceStateResponse {
p.Lock()
defer p.Unlock()
p.UpgradeResourceStateCalled = true
p.UpgradeResourceStateRequest = r
if p.UpgradeResourceStateFn != nil {
return p.UpgradeResourceStateFn(r)
}
return p.UpgradeResourceStateResponse
}
func (p *MockProvider) Configure(r providers.ConfigureRequest) providers.ConfigureResponse {
p.Lock()
defer p.Unlock()
p.ConfigureCalled = true
p.ConfigureRequest = r
if p.ConfigureFn != nil {
return p.ConfigureFn(r)
}
return p.ConfigureResponse
}
func (p *MockProvider) Stop() error {
p.Lock()
defer p.Unlock()
p.StopCalled = true
if p.StopFn != nil {
return p.StopFn()
}
return p.StopResponse
}
func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) providers.ReadResourceResponse {
p.Lock()
defer p.Unlock()
p.ReadResourceCalled = true
p.ReadResourceRequest = r
if p.ReadResourceFn != nil {
return p.ReadResourceFn(r)
}
return p.ReadResourceResponse
}
func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
p.PlanResourceChangeCalled = true
p.PlanResourceChangeRequest = r
if p.PlanResourceChangeFn != nil {
return p.PlanResourceChangeFn(r)
}
return p.PlanResourceChangeResponse
}
func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
p.Lock()
p.ApplyResourceChangeCalled = true
p.ApplyResourceChangeRequest = r
p.Unlock()
if p.ApplyResourceChangeFn != nil {
return p.ApplyResourceChangeFn(r)
}
return p.ApplyResourceChangeResponse
}
func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateRequest) providers.ImportResourceStateResponse {
p.Lock()
defer p.Unlock()
p.ImportResourceStateCalled = true
p.ImportResourceStateRequest = r
if p.ImportResourceStateFn != nil {
return p.ImportResourceStateFn(r)
}
return p.ImportResourceStateResponse
}
func (p *MockProvider) ReadDataSource(r providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
p.Lock()
defer p.Unlock()
p.ReadDataSourceCalled = true
p.ReadDataSourceRequest = r
if p.ReadDataSourceFn != nil {
return p.ReadDataSourceFn(r)
}
return p.ReadDataSourceResponse
}
func (p *MockProvider) Close() error {
p.CloseCalled = true
return p.CloseError
}

View File

@ -0,0 +1,96 @@
package terraform
import (
"sync"
"github.com/hashicorp/terraform/provisioners"
)
var _ provisioners.Interface = (*MockProvisioner)(nil)
// MockProvisioner implements provisioners.Interface but mocks out all the
// calls for testing purposes.
type MockProvisioner struct {
sync.Mutex
// Anything you want, in case you need to store extra data with the mock.
Meta interface{}
GetSchemaCalled bool
GetSchemaResponse provisioners.GetSchemaResponse
ValidateProvisionerConfigCalled bool
ValidateProvisionerConfigRequest provisioners.ValidateProvisionerConfigRequest
ValidateProvisionerConfigResponse provisioners.ValidateProvisionerConfigResponse
ValidateProvisionerConfigFn func(provisioners.ValidateProvisionerConfigRequest) provisioners.ValidateProvisionerConfigResponse
ProvisionResourceCalled bool
ProvisionResourceRequest provisioners.ProvisionResourceRequest
ProvisionResourceResponse provisioners.ProvisionResourceResponse
ProvisionResourceFn func(provisioners.ProvisionResourceRequest) provisioners.ProvisionResourceResponse
StopCalled bool
StopResponse error
StopFn func() error
CloseCalled bool
CloseResponse error
CloseFn func() error
}
func (p *MockProvisioner) GetSchema() provisioners.GetSchemaResponse {
p.Lock()
defer p.Unlock()
p.GetSchemaCalled = true
return p.GetSchemaResponse
}
func (p *MockProvisioner) ValidateProvisionerConfig(r provisioners.ValidateProvisionerConfigRequest) provisioners.ValidateProvisionerConfigResponse {
p.Lock()
defer p.Unlock()
p.ValidateProvisionerConfigCalled = true
p.ValidateProvisionerConfigRequest = r
if p.ValidateProvisionerConfigFn != nil {
return p.ValidateProvisionerConfigFn(r)
}
return p.ValidateProvisionerConfigResponse
}
func (p *MockProvisioner) ProvisionResource(r provisioners.ProvisionResourceRequest) provisioners.ProvisionResourceResponse {
p.Lock()
p.ProvisionResourceCalled = true
p.ProvisionResourceRequest = r
if p.ProvisionResourceFn != nil {
fn := p.ProvisionResourceFn
p.Unlock()
return fn(r)
}
defer p.Unlock()
return p.ProvisionResourceResponse
}
func (p *MockProvisioner) Stop() error {
p.Lock()
defer p.Unlock()
p.StopCalled = true
if p.StopFn != nil {
return p.StopFn()
}
return p.StopResponse
}
func (p *MockProvisioner) Close() error {
p.Lock()
defer p.Unlock()
p.CloseCalled = true
if p.CloseFn != nil {
return p.CloseFn()
}
return p.CloseResponse
}