diff --git a/terraform/context_components_test.go b/terraform/context_components_test.go new file mode 100644 index 000000000..354257bed --- /dev/null +++ b/terraform/context_components_test.go @@ -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, + }, + }, + } +} diff --git a/terraform/resource_provider_mock_test.go b/terraform/resource_provider_mock_test.go index 245e8ecd4..0e2a49d3d 100644 --- a/terraform/resource_provider_mock_test.go +++ b/terraform/resource_provider_mock_test.go @@ -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(), + }, + }, + } +} diff --git a/terraform/resource_provisioner_mock_test.go b/terraform/resource_provisioner_mock_test.go index 57795b425..bfac5f5f1 100644 --- a/terraform/resource_provisioner_mock_test.go +++ b/terraform/resource_provisioner_mock_test.go @@ -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(), + } +}