mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
f82c192c49
Now that any configuration processing requires schema, we need either a standalone schema or a provider/provisioner configured with one a lot more often in tests. To avoid adding loads of extra boilerplate to the tests, these new helper functions produce objects pre-configured with a schema that should be useful for a number of different cases, and can be customized further for more interesting situations. A lot of our tests can then just use these pre-defined object and attribute names in their fixtures in situations where the canned schemas here are good enough.
88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
package terraform
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/config/configschema"
|
|
)
|
|
|
|
func TestMockResourceProvider_impl(t *testing.T) {
|
|
var _ ResourceProvider = new(MockResourceProvider)
|
|
var _ ResourceProviderCloser = new(MockResourceProvider)
|
|
}
|
|
|
|
// testProviderComponentFactory creates a componentFactory that contains only
|
|
// a single given.
|
|
func testProviderComponentFactory(name string, provider ResourceProvider) *basicComponentFactory {
|
|
return &basicComponentFactory{
|
|
providers: map[string]ResourceProviderFactory{
|
|
name: ResourceProviderFactoryFixed(provider),
|
|
},
|
|
}
|
|
}
|
|
|
|
// mockProviderWithConfigSchema is a test helper to concisely create a mock
|
|
// provider with the given schema for its own configuration.
|
|
func mockProviderWithConfigSchema(schema *configschema.Block) *MockResourceProvider {
|
|
return &MockResourceProvider{
|
|
GetSchemaReturn: &ProviderSchema{
|
|
Provider: schema,
|
|
},
|
|
}
|
|
}
|
|
|
|
// mockProviderWithResourceTypeSchema is a test helper to concisely create a mock
|
|
// provider with a schema containing a single resource type.
|
|
func mockProviderWithResourceTypeSchema(name string, schema *configschema.Block) *MockResourceProvider {
|
|
return &MockResourceProvider{
|
|
GetSchemaReturn: &ProviderSchema{
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
name: schema,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// 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,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// simpleMockProvider returns a MockResourceProvider that is pre-configured
|
|
// 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.
|
|
func simpleMockProvider() *MockResourceProvider {
|
|
return &MockResourceProvider{
|
|
GetSchemaReturn: &ProviderSchema{
|
|
Provider: simpleTestSchema(),
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
"test_object": simpleTestSchema(),
|
|
},
|
|
DataSources: map[string]*configschema.Block{
|
|
"test_object": simpleTestSchema(),
|
|
},
|
|
},
|
|
}
|
|
}
|