github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/resource_provider_mock_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema" 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 func TestMockResourceProvider_impl(t *testing.T) { 11 var _ ResourceProvider = new(MockResourceProvider) 12 var _ ResourceProviderCloser = new(MockResourceProvider) 13 } 14 15 // mockProviderWithConfigSchema is a test helper to concisely create a mock 16 // provider with the given schema for its own configuration. 17 func mockProviderWithConfigSchema(schema *configschema.Block) *MockProvider { 18 return &MockProvider{ 19 GetSchemaReturn: &ProviderSchema{ 20 Provider: schema, 21 }, 22 } 23 } 24 25 // mockProviderWithResourceTypeSchema is a test helper to concisely create a mock 26 // provider with a schema containing a single resource type. 27 func mockProviderWithResourceTypeSchema(name string, schema *configschema.Block) *MockProvider { 28 return &MockProvider{ 29 GetSchemaReturn: &ProviderSchema{ 30 Provider: &configschema.Block{ 31 Attributes: map[string]*configschema.Attribute{ 32 "string": { 33 Type: cty.String, 34 Optional: true, 35 }, 36 "list": { 37 Type: cty.List(cty.String), 38 Optional: true, 39 }, 40 "root": { 41 Type: cty.Map(cty.String), 42 Optional: true, 43 }, 44 }, 45 }, 46 ResourceTypes: map[string]*configschema.Block{ 47 name: schema, 48 }, 49 }, 50 } 51 } 52 53 // simpleMockProvider returns a MockProvider that is pre-configured 54 // with schema for its own config, for a resource type called "test_object" and 55 // for a data source also called "test_object". 56 // 57 // All three schemas have the same content as returned by function 58 // simpleTestSchema. 59 // 60 // For most reasonable uses the returned provider must be registered in a 61 // componentFactory under the name "test". Use simpleMockComponentFactory 62 // to obtain a pre-configured componentFactory containing the result of 63 // this function along with simpleMockProvisioner, both registered as "test". 64 // 65 // The returned provider has no other behaviors by default, but the caller may 66 // modify it in order to stub any other required functionality, or modify 67 // the default schema stored in the field GetSchemaReturn. Each new call to 68 // simpleTestProvider produces entirely new instances of all of the nested 69 // objects so that callers can mutate without affecting mock objects. 70 func simpleMockProvider() *MockProvider { 71 return &MockProvider{ 72 GetSchemaReturn: &ProviderSchema{ 73 Provider: simpleTestSchema(), 74 ResourceTypes: map[string]*configschema.Block{ 75 "test_object": simpleTestSchema(), 76 }, 77 DataSources: map[string]*configschema.Block{ 78 "test_object": simpleTestSchema(), 79 }, 80 }, 81 } 82 }