test that providers are configured for calls

Have the MockProvider ensure that Configure is always called before any
methods that may require a configured provider.

Ensure the MockProvider *Called fields are zeroed out when re-using the
provider instance.
This commit is contained in:
James Bardin 2021-10-07 16:48:56 -04:00
parent d05fa3049e
commit ad9944e523
2 changed files with 42 additions and 0 deletions

View File

@ -297,6 +297,11 @@ func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) (resp provi
p.ReadResourceCalled = true
p.ReadResourceRequest = r
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ReadResource %q", r.TypeName))
return resp
}
if p.ReadResourceFn != nil {
return p.ReadResourceFn(r)
}
@ -330,6 +335,11 @@ func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
p.Lock()
defer p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before PlanResourceChange %q", r.TypeName))
return resp
}
p.PlanResourceChangeCalled = true
p.PlanResourceChangeRequest = r
@ -400,6 +410,11 @@ func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeReques
p.ApplyResourceChangeRequest = r
p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ApplyResourceChange %q", r.TypeName))
return resp
}
if p.ApplyResourceChangeFn != nil {
return p.ApplyResourceChangeFn(r)
}
@ -460,6 +475,11 @@ func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateReques
p.Lock()
defer p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ImportResourceState %q", r.TypeName))
return resp
}
p.ImportResourceStateCalled = true
p.ImportResourceStateRequest = r
if p.ImportResourceStateFn != nil {
@ -494,6 +514,11 @@ func (p *MockProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp p
p.Lock()
defer p.Unlock()
if !p.ConfigureProviderCalled {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ReadDataSource %q", r.TypeName))
return resp
}
p.ReadDataSourceCalled = true
p.ReadDataSourceRequest = r

View File

@ -166,6 +166,23 @@ func testSetResourceInstanceTainted(module *states.Module, resource, attrsJson,
func testProviderFuncFixed(rp providers.Interface) providers.Factory {
return func() (providers.Interface, error) {
if p, ok := rp.(*MockProvider); ok {
// make sure none of the methods were "called" on this new instance
p.GetProviderSchemaCalled = false
p.ValidateProviderConfigCalled = false
p.ValidateResourceConfigCalled = false
p.ValidateDataResourceConfigCalled = false
p.UpgradeResourceStateCalled = false
p.ConfigureProviderCalled = false
p.StopCalled = false
p.ReadResourceCalled = false
p.PlanResourceChangeCalled = false
p.ApplyResourceChangeCalled = false
p.ImportResourceStateCalled = false
p.ReadDataSourceCalled = false
p.CloseCalled = false
}
return rp, nil
}
}