core: mock provider factory functions for testing
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.
This commit is contained in:
parent
4833d2aa79
commit
f82c192c49
|
@ -0,0 +1,79 @@
|
|||
package terraform
|
||||
|
||||
import (
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/hashicorp/terraform/config/configschema"
|
||||
)
|
||||
|
||||
// simpleMockComponentFactory returns a component factory pre-configured with
|
||||
// one provider and one provisioner, both called "test".
|
||||
//
|
||||
// The provider is built with simpleMockProvider and the provisioner with
|
||||
// simpleMockProvisioner, and all schemas used in both are as built by
|
||||
// function simpleTestSchema.
|
||||
//
|
||||
// Each call to this function produces an entirely-separate set of objects,
|
||||
// so the caller can feel free to modify the returned value to further
|
||||
// customize the mocks contained within.
|
||||
func simpleMockComponentFactory() *basicComponentFactory {
|
||||
// We create these out here, rather than in the factory functions below,
|
||||
// because we want each call to the factory to return the _same_ instance,
|
||||
// so that test code can customize it before passing this component
|
||||
// factory into real code under test.
|
||||
provider := simpleMockProvider()
|
||||
provisioner := simpleMockProvisioner()
|
||||
return &basicComponentFactory{
|
||||
providers: map[string]ResourceProviderFactory{
|
||||
"test": func() (ResourceProvider, error) {
|
||||
return provider, nil
|
||||
},
|
||||
},
|
||||
provisioners: map[string]ResourceProvisionerFactory{
|
||||
"test": func() (ResourceProvisioner, error) {
|
||||
return provisioner, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// simpleTestSchema returns a block schema that contains a few optional
|
||||
// attributes for use in tests.
|
||||
//
|
||||
// The returned schema contains the following optional attributes:
|
||||
//
|
||||
// test_string, of type string
|
||||
// test_number, of type number
|
||||
// test_bool, of type bool
|
||||
// test_list, of type list(string)
|
||||
// test_map, of type map(string)
|
||||
//
|
||||
// Each call to this function produces an entirely new schema instance, so
|
||||
// callers can feel free to modify it once returned.
|
||||
func simpleTestSchema() *configschema.Block {
|
||||
return &configschema.Block{
|
||||
Attributes: map[string]*configschema.Attribute{
|
||||
"test_string": {
|
||||
Type: cty.String,
|
||||
Optional: true,
|
||||
},
|
||||
"test_number": {
|
||||
Type: cty.String,
|
||||
Optional: true,
|
||||
},
|
||||
"test_bool": {
|
||||
Type: cty.String,
|
||||
Optional: true,
|
||||
},
|
||||
"test_list": {
|
||||
Type: cty.String,
|
||||
Optional: true,
|
||||
},
|
||||
"test_map": {
|
||||
Type: cty.String,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
|
@ -54,3 +54,34 @@ func mockProviderWithDataSourceSchema(name string, schema *configschema.Block) *
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 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(),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,3 +7,23 @@ import (
|
|||
func TestMockResourceProvisioner_impl(t *testing.T) {
|
||||
var _ ResourceProvisioner = new(MockResourceProvisioner)
|
||||
}
|
||||
|
||||
// simpleMockProvisioner returns a MockResourceProvisioner that is pre-configured
|
||||
// with schema for its own config, with the same content as returned by
|
||||
// function simpleTestSchema.
|
||||
//
|
||||
// For most reasonable uses the returned provisioner 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 simpleMockProvider, both registered as "test".
|
||||
//
|
||||
// The returned provisioner 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
|
||||
// simpleTestProvisioner produces entirely new instances of all of the nested
|
||||
// objects so that callers can mutate without affecting mock objects.
|
||||
func simpleMockProvisioner() *MockResourceProvisioner {
|
||||
return &MockResourceProvisioner{
|
||||
GetConfigSchemaReturnSchema: simpleTestSchema(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue