2014-06-03 16:26:31 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-05-07 19:18:17 -05:00
|
|
|
|
2019-12-04 10:30:20 -06:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-07-05 12:33:29 -05:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-08-17 14:32:35 -05:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2018-05-24 14:53:34 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-06-03 16:26:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMockResourceProvider_impl(t *testing.T) {
|
|
|
|
var _ ResourceProvider = new(MockResourceProvider)
|
2015-06-29 12:33:37 -05:00
|
|
|
var _ ResourceProviderCloser = new(MockResourceProvider)
|
2014-06-03 16:26:31 -05:00
|
|
|
}
|
2018-05-07 19:18:17 -05:00
|
|
|
|
|
|
|
// testProviderComponentFactory creates a componentFactory that contains only
|
|
|
|
// a single given.
|
2018-08-17 14:32:35 -05:00
|
|
|
func testProviderComponentFactory(name string, provider providers.Interface) *basicComponentFactory {
|
2018-05-07 19:18:17 -05:00
|
|
|
return &basicComponentFactory{
|
2019-12-04 10:30:20 -06:00
|
|
|
providers: map[addrs.Provider]providers.Factory{
|
2020-04-01 12:13:40 -05:00
|
|
|
addrs.NewDefaultProvider(name): providers.FactoryFixed(provider),
|
2018-05-07 19:18:17 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockProviderWithConfigSchema is a test helper to concisely create a mock
|
|
|
|
// provider with the given schema for its own configuration.
|
2018-08-17 14:32:35 -05:00
|
|
|
func mockProviderWithConfigSchema(schema *configschema.Block) *MockProvider {
|
|
|
|
return &MockProvider{
|
2018-08-24 18:13:50 -05:00
|
|
|
GetSchemaReturn: &ProviderSchema{
|
|
|
|
Provider: schema,
|
2018-05-07 19:18:17 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockProviderWithResourceTypeSchema is a test helper to concisely create a mock
|
|
|
|
// provider with a schema containing a single resource type.
|
2018-08-17 14:32:35 -05:00
|
|
|
func mockProviderWithResourceTypeSchema(name string, schema *configschema.Block) *MockProvider {
|
|
|
|
return &MockProvider{
|
2018-08-24 18:13:50 -05:00
|
|
|
GetSchemaReturn: &ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"string": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"list": {
|
|
|
|
Type: cty.List(cty.String),
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"root": {
|
|
|
|
Type: cty.Map(cty.String),
|
|
|
|
Optional: true,
|
2018-05-24 14:53:34 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-24 18:13:50 -05:00
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
name: schema,
|
2018-05-07 19:18:17 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockProviderWithDataSourceSchema is a test helper to concisely create a mock
|
|
|
|
// provider with a schema containing a single data source.
|
|
|
|
func mockProviderWithDataSourceSchema(name string, schema *configschema.Block) *MockResourceProvider {
|
|
|
|
return &MockResourceProvider{
|
|
|
|
GetSchemaReturn: &ProviderSchema{
|
|
|
|
DataSources: map[string]*configschema.Block{
|
|
|
|
name: schema,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 18:08:10 -05:00
|
|
|
|
2018-08-17 14:32:35 -05:00
|
|
|
// simpleMockProvider returns a MockProvider that is pre-configured
|
2018-05-09 18:08:10 -05:00
|
|
|
// with schema for its own config, for a resource type called "test_object" and
|
|
|
|
// for a data source also called "test_object".
|
|
|
|
//
|
|
|
|
// All three schemas have the same content as returned by function
|
|
|
|
// simpleTestSchema.
|
|
|
|
//
|
|
|
|
// For most reasonable uses the returned provider must be registered in a
|
|
|
|
// componentFactory under the name "test". Use simpleMockComponentFactory
|
|
|
|
// to obtain a pre-configured componentFactory containing the result of
|
|
|
|
// this function along with simpleMockProvisioner, both registered as "test".
|
|
|
|
//
|
|
|
|
// The returned provider has no other behaviors by default, but the caller may
|
|
|
|
// modify it in order to stub any other required functionality, or modify
|
|
|
|
// the default schema stored in the field GetSchemaReturn. Each new call to
|
|
|
|
// simpleTestProvider produces entirely new instances of all of the nested
|
|
|
|
// objects so that callers can mutate without affecting mock objects.
|
2018-08-17 14:32:35 -05:00
|
|
|
func simpleMockProvider() *MockProvider {
|
|
|
|
return &MockProvider{
|
2018-08-24 18:13:50 -05:00
|
|
|
GetSchemaReturn: &ProviderSchema{
|
|
|
|
Provider: simpleTestSchema(),
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_object": simpleTestSchema(),
|
2018-08-17 14:32:35 -05:00
|
|
|
},
|
2018-08-24 18:13:50 -05:00
|
|
|
DataSources: map[string]*configschema.Block{
|
|
|
|
"test_object": simpleTestSchema(),
|
2018-05-09 18:08:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|