core: Allow ProviderMock to fetch its own schema without deadlock

This commit is contained in:
Martin Atkins 2018-09-07 16:18:06 -07:00
parent 426a976c93
commit d1da65b6be
1 changed files with 18 additions and 4 deletions

View File

@ -102,6 +102,13 @@ type MockProvider struct {
func (p *MockProvider) GetSchema() providers.GetSchemaResponse { func (p *MockProvider) GetSchema() providers.GetSchemaResponse {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
return p.getSchema()
}
func (p *MockProvider) getSchema() providers.GetSchemaResponse {
// This version of getSchema doesn't do any locking, so it's suitable to
// call from other methods of this mock as long as they are already
// holding the lock.
p.GetSchemaCalled = true p.GetSchemaCalled = true
ret := providers.GetSchemaResponse{ ret := providers.GetSchemaResponse{
@ -191,9 +198,16 @@ func (p *MockProvider) Configure(r providers.ConfigureRequest) providers.Configu
p.ConfigureRequest = r p.ConfigureRequest = r
if p.ConfigureFn != nil { if p.ConfigureFn != nil {
return providers.ConfigureResponse{ resp := p.getSchema()
Diagnostics: tfdiags.Diagnostics(nil).Append(fmt.Errorf("legacy ConfigureFn handling in MockProvider not actually implemented yet")), schema := resp.Provider.Block
rc := NewResourceConfigShimmed(r.Config, schema)
ret := providers.ConfigureResponse{}
err := p.ConfigureFn(rc)
if err != nil {
ret.Diagnostics = ret.Diagnostics.Append(err)
} }
return ret
} }
if p.ConfigureNewFn != nil { if p.ConfigureNewFn != nil {
return p.ConfigureNewFn(r) return p.ConfigureNewFn(r)
@ -233,7 +247,7 @@ func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
p.PlanResourceChangeRequest = r p.PlanResourceChangeRequest = r
if p.DiffFn != nil { if p.DiffFn != nil {
ps := p.GetSchema() ps := p.getSchema()
if ps.ResourceTypes == nil || ps.ResourceTypes[r.TypeName].Block == nil { if ps.ResourceTypes == nil || ps.ResourceTypes[r.TypeName].Block == nil {
return providers.PlanResourceChangeResponse{ return providers.PlanResourceChangeResponse{
Diagnostics: tfdiags.Diagnostics(nil).Append(fmt.Printf("mock provider has no schema for resource type %s", r.TypeName)), Diagnostics: tfdiags.Diagnostics(nil).Append(fmt.Printf("mock provider has no schema for resource type %s", r.TypeName)),
@ -294,7 +308,7 @@ func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeReques
// a diff here well enough that _most_ of our legacy ApplyFns in old // a diff here well enough that _most_ of our legacy ApplyFns in old
// tests still see the behavior they are expecting. New tests should // tests still see the behavior they are expecting. New tests should
// not use this, and should instead use ApplyResourceChangeFn directly. // not use this, and should instead use ApplyResourceChangeFn directly.
providerSchema := p.GetSchema() providerSchema := p.getSchema()
schema, ok := providerSchema.ResourceTypes[r.TypeName] schema, ok := providerSchema.ResourceTypes[r.TypeName]
if !ok { if !ok {
return providers.ApplyResourceChangeResponse{ return providers.ApplyResourceChangeResponse{