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