remove old mock provider ConfigureFn

This commit is contained in:
James Bardin 2020-10-08 12:26:12 -04:00
parent 0a6853a3f8
commit ea5ee39f38
7 changed files with 115 additions and 157 deletions

View File

@ -660,19 +660,15 @@ func TestContext2Apply_providerAliasConfigure(t *testing.T) {
// Configure to record calls AFTER Plan above // Configure to record calls AFTER Plan above
var configCount int32 var configCount int32
p2.ConfigureFn = func(c *ResourceConfig) error { p2.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
atomic.AddInt32(&configCount, 1) atomic.AddInt32(&configCount, 1)
foo, ok := c.Get("foo") foo := req.Config.GetAttr("foo").AsString()
if !ok {
return fmt.Errorf("foo is not found")
}
if foo != "bar" { if foo != "bar" {
return fmt.Errorf("foo: %#v", foo) resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("foo: %#v", foo))
} }
return nil return
} }
state, diags := ctx.Apply() state, diags := ctx.Apply()
@ -2477,16 +2473,18 @@ func TestContext2Apply_moduleInheritAlias(t *testing.T) {
p.ApplyFn = testApplyFn p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
if _, ok := c.Get("value"); !ok { val := req.Config.GetAttr("value")
return nil if val.IsNull() {
return
} }
if _, ok := c.Get("root"); ok { root := req.Config.GetAttr("root")
return fmt.Errorf("child should not get root") if !root.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("child should not get root"))
} }
return nil return
} }
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
@ -2601,19 +2599,18 @@ func TestContext2Apply_moduleOrphanInheritAlias(t *testing.T) {
p.ApplyFn = testApplyFn p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
called := false p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
p.ConfigureFn = func(c *ResourceConfig) error { val := req.Config.GetAttr("value")
called = true if val.IsNull() {
return
if _, ok := c.Get("child"); !ok {
return nil
} }
if _, ok := c.Get("root"); ok { root := req.Config.GetAttr("root")
return fmt.Errorf("child should not get root") if !root.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("child should not get root"))
} }
return nil return
} }
// Create a state with an orphan module // Create a state with an orphan module
@ -2645,7 +2642,7 @@ func TestContext2Apply_moduleOrphanInheritAlias(t *testing.T) {
t.Fatalf("diags: %s", diags.Err()) t.Fatalf("diags: %s", diags.Err())
} }
if !called { if !p.ConfigureCalled {
t.Fatal("must call configure") t.Fatal("must call configure")
} }
@ -2658,12 +2655,13 @@ func TestContext2Apply_moduleOrphanProvider(t *testing.T) {
p.ApplyFn = testApplyFn p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
if _, ok := c.Get("value"); !ok { val := req.Config.GetAttr("value")
return fmt.Errorf("value is not found") if val.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found"))
} }
return nil return
} }
// Create a state with an orphan module // Create a state with an orphan module
@ -2701,12 +2699,13 @@ func TestContext2Apply_moduleOrphanGrandchildProvider(t *testing.T) {
p.ApplyFn = testApplyFn p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
if _, ok := c.Get("value"); !ok { val := req.Config.GetAttr("value")
return fmt.Errorf("value is not found") if val.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found"))
} }
return nil return
} }
// Create a state with an orphan module that is nested (grandchild) // Create a state with an orphan module that is nested (grandchild)
@ -2746,15 +2745,17 @@ func TestContext2Apply_moduleGrandchildProvider(t *testing.T) {
var callLock sync.Mutex var callLock sync.Mutex
called := false called := false
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
if _, ok := c.Get("value"); !ok { val := req.Config.GetAttr("value")
return fmt.Errorf("value is not found") if val.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found"))
} }
callLock.Lock() callLock.Lock()
called = true called = true
callLock.Unlock() callLock.Unlock()
return nil return
} }
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
@ -3985,20 +3986,13 @@ func TestContext2Apply_providerComputedVar(t *testing.T) {
}, },
}) })
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
if c.IsComputed("value") { val := req.Config.GetAttr("value")
return fmt.Errorf("value is computed") if val.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found"))
return
} }
return
v, ok := c.Get("value")
if !ok {
return fmt.Errorf("value is not found")
}
if v != "yes" {
return fmt.Errorf("value is not 'yes': %v", v)
}
return nil
} }
if _, diags := ctx.Plan(); diags.HasErrors() { if _, diags := ctx.Plan(); diags.HasErrors() {
@ -4016,15 +4010,13 @@ func TestContext2Apply_providerConfigureDisabled(t *testing.T) {
p.ApplyFn = testApplyFn p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
called := false p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
p.ConfigureFn = func(c *ResourceConfig) error { val := req.Config.GetAttr("value")
called = true if val.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("value is not found"))
if _, ok := c.Get("value"); !ok {
return fmt.Errorf("value is not found")
} }
return nil return
} }
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
@ -4042,7 +4034,7 @@ func TestContext2Apply_providerConfigureDisabled(t *testing.T) {
t.Fatalf("apply errors: %s", diags.Err()) t.Fatalf("apply errors: %s", diags.Err())
} }
if !called { if !p.ConfigureCalled {
t.Fatal("configure never called") t.Fatal("configure never called")
} }
} }
@ -8834,12 +8826,15 @@ func TestContext2Apply_providerWithLocals(t *testing.T) {
providerRegion := "" providerRegion := ""
// this should not be overridden during destroy // this should not be overridden during destroy
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
if r, ok := c.Get("region"); ok { val := req.Config.GetAttr("region")
providerRegion = r.(string) if !val.IsNull() {
providerRegion = val.AsString()
} }
return nil
return
} }
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
p.ApplyFn = testApplyFn p.ApplyFn = testApplyFn
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{
@ -11385,7 +11380,7 @@ output "output" {
testP.GetSchemaReturn = schemaFn("test") testP.GetSchemaReturn = schemaFn("test")
providerConfig := "" providerConfig := ""
testP.ConfigureNewFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) { testP.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
value := req.Config.GetAttr("value") value := req.Config.GetAttr("value")
if value.IsKnown() && !value.IsNull() { if value.IsKnown() && !value.IsNull() {
providerConfig = value.AsString() providerConfig = value.AsString()

View File

@ -1,7 +1,7 @@
package terraform package terraform
import ( import (
"fmt" "errors"
"strings" "strings"
"testing" "testing"
@ -196,15 +196,13 @@ func TestContextImport_moduleProvider(t *testing.T) {
}, },
} }
configured := false p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
p.ConfigureFn = func(c *ResourceConfig) error { foo := req.Config.GetAttr("foo").AsString()
configured = true if foo != "bar" {
resp.Diagnostics = resp.Diagnostics.Append(errors.New("not bar"))
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
return fmt.Errorf("bad")
} }
return nil return
} }
m := testModule(t, "import-provider") m := testModule(t, "import-provider")
@ -229,7 +227,7 @@ func TestContextImport_moduleProvider(t *testing.T) {
t.Fatalf("unexpected errors: %s", diags.Err()) t.Fatalf("unexpected errors: %s", diags.Err())
} }
if !configured { if !p.ConfigureCalled {
t.Fatal("didn't configure provider") t.Fatal("didn't configure provider")
} }
@ -258,15 +256,13 @@ func TestContextImport_providerModule(t *testing.T) {
}, },
} }
configured := false p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
p.ConfigureFn = func(c *ResourceConfig) error { foo := req.Config.GetAttr("foo").AsString()
configured = true if foo != "bar" {
resp.Diagnostics = resp.Diagnostics.Append(errors.New("not bar"))
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
return fmt.Errorf("bad")
} }
return nil return
} }
_, diags := ctx.Import(&ImportOpts{ _, diags := ctx.Import(&ImportOpts{
@ -283,7 +279,7 @@ func TestContextImport_providerModule(t *testing.T) {
t.Fatalf("unexpected errors: %s", diags.Err()) t.Fatalf("unexpected errors: %s", diags.Err())
} }
if !configured { if !p.ConfigureCalled {
t.Fatal("didn't configure provider") t.Fatal("didn't configure provider")
} }
} }

View File

@ -56,9 +56,9 @@ func TestContext2Input_provider(t *testing.T) {
}) })
var actual interface{} var actual interface{}
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
actual = c.Config["foo"] actual = req.Config.GetAttr("foo").AsString()
return nil return
} }
p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
return nil, c.CheckSet([]string{"foo"}) return nil, c.CheckSet([]string{"foo"})
@ -145,11 +145,11 @@ func TestContext2Input_providerMulti(t *testing.T) {
t.Fatalf("plan errors: %s", diags.Err()) t.Fatalf("plan errors: %s", diags.Err())
} }
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
actual = append(actual, c.Config["foo"]) actual = append(actual, req.Config.GetAttr("foo").AsString())
return nil return
} }
if _, diags := ctx.Apply(); diags.HasErrors() { if _, diags := ctx.Apply(); diags.HasErrors() {
t.Fatalf("apply errors: %s", diags.Err()) t.Fatalf("apply errors: %s", diags.Err())
@ -217,9 +217,9 @@ func TestContext2Input_providerId(t *testing.T) {
}) })
var actual interface{} var actual interface{}
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
actual = c.Config["foo"] actual = req.Config.GetAttr("foo").AsString()
return nil return
} }
input.InputReturnMap = map[string]string{ input.InputReturnMap = map[string]string{
@ -290,9 +290,9 @@ func TestContext2Input_providerOnly(t *testing.T) {
} }
var actual interface{} var actual interface{}
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
actual = c.Config["foo"] actual = req.Config.GetAttr("foo").AsString()
return nil return
} }
if err := ctx.Input(InputModeProvider); err != nil { if err := ctx.Input(InputModeProvider); err != nil {
@ -344,15 +344,10 @@ func TestContext2Input_providerVars(t *testing.T) {
} }
var actual interface{} var actual interface{}
/*p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
c.Config["bar"] = "baz" actual = req.Config.GetAttr("foo").AsString()
return c, nil return
}*/
p.ConfigureFn = func(c *ResourceConfig) error {
actual, _ = c.Get("foo")
return nil
} }
if diags := ctx.Input(InputModeStd); diags.HasErrors() { if diags := ctx.Input(InputModeStd); diags.HasErrors() {
t.Fatalf("input errors: %s", diags.Err()) t.Fatalf("input errors: %s", diags.Err())
} }
@ -384,16 +379,6 @@ func TestContext2Input_providerVarsModuleInherit(t *testing.T) {
UIInput: input, UIInput: input,
}) })
/*p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) {
if errs := c.CheckSet([]string{"access_key"}); len(errs) > 0 {
return c, errs[0]
}
return c, nil
}*/
p.ConfigureFn = func(c *ResourceConfig) error {
return nil
}
if diags := ctx.Input(InputModeStd); diags.HasErrors() { if diags := ctx.Input(InputModeStd); diags.HasErrors() {
t.Fatalf("input errors: %s", diags.Err()) t.Fatalf("input errors: %s", diags.Err())
} }
@ -414,13 +399,6 @@ func TestContext2Input_submoduleTriggersInvalidCount(t *testing.T) {
UIInput: input, UIInput: input,
}) })
/*p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) {
return c, nil
}*/
p.ConfigureFn = func(c *ResourceConfig) error {
return nil
}
if diags := ctx.Input(InputModeStd); diags.HasErrors() { if diags := ctx.Input(InputModeStd); diags.HasErrors() {
t.Fatalf("input errors: %s", diags.Err()) t.Fatalf("input errors: %s", diags.Err())
} }

View File

@ -1016,12 +1016,13 @@ func TestContext2Plan_moduleProviderInherit(t *testing.T) {
}, },
}, },
} }
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
if v, ok := c.Get("from"); !ok || v.(string) != "root" { from := req.Config.GetAttr("from")
return fmt.Errorf("bad") if from.IsNull() || from.AsString() != "root" {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("not root"))
} }
return nil return
} }
p.DiffFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) { p.DiffFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
from := req.Config.GetAttr("from").AsString() from := req.Config.GetAttr("from").AsString()
@ -1078,14 +1079,14 @@ func TestContext2Plan_moduleProviderInheritDeep(t *testing.T) {
}, },
} }
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
v, ok := c.Get("from") v := req.Config.GetAttr("from")
if !ok || v.(string) != "root" { if v.IsNull() || v.AsString() != "root" {
return fmt.Errorf("bad") resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("not root"))
} }
from = v.AsString()
from = v.(string) return
return nil
} }
p.DiffFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) { p.DiffFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
@ -1135,20 +1136,23 @@ func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) {
}, },
}, },
} }
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
var buf bytes.Buffer var buf bytes.Buffer
if v, ok := c.Get("from"); ok { from := req.Config.GetAttr("from")
buf.WriteString(v.(string) + "\n") if !from.IsNull() {
buf.WriteString(from.AsString() + "\n")
} }
if v, ok := c.Get("to"); ok { to := req.Config.GetAttr("to")
buf.WriteString(v.(string) + "\n") if !to.IsNull() {
buf.WriteString(to.AsString() + "\n")
} }
l.Lock() l.Lock()
defer l.Unlock() defer l.Unlock()
calls = append(calls, buf.String()) calls = append(calls, buf.String())
return nil return
} }
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
return p, nil return p, nil
}, },
@ -4434,9 +4438,9 @@ func TestContext2Plan_provider(t *testing.T) {
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
var value interface{} var value interface{}
p.ConfigureFn = func(c *ResourceConfig) error { p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
value, _ = c.Get("foo") value = req.Config.GetAttr("foo").AsString()
return nil return
} }
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{

View File

@ -173,14 +173,13 @@ func TestContext2Validate_computedVar(t *testing.T) {
return nil, c.CheckSet([]string{"value"}) return nil, c.CheckSet([]string{"value"})
} }
p.ConfigureFn = func(c *ResourceConfig) error {
return fmt.Errorf("Configure should not be called for provider")
}
diags := c.Validate() diags := c.Validate()
if diags.HasErrors() { if diags.HasErrors() {
t.Fatalf("unexpected error: %s", diags.Err()) t.Fatalf("unexpected error: %s", diags.Err())
} }
if p.ConfigureCalled {
t.Fatal("Configure should not be called for provider")
}
} }
func TestContext2Validate_computedInFunction(t *testing.T) { func TestContext2Validate_computedInFunction(t *testing.T) {

View File

@ -51,7 +51,7 @@ type MockProvider struct {
ConfigureCalled bool ConfigureCalled bool
ConfigureResponse providers.ConfigureResponse ConfigureResponse providers.ConfigureResponse
ConfigureRequest providers.ConfigureRequest ConfigureRequest providers.ConfigureRequest
ConfigureNewFn func(providers.ConfigureRequest) providers.ConfigureResponse // Named ConfigureNewFn so we can still have the legacy ConfigureFn declared below ConfigureFn func(providers.ConfigureRequest) providers.ConfigureResponse
StopCalled bool StopCalled bool
StopFn func() error StopFn func() error
@ -88,10 +88,8 @@ type MockProvider struct {
CloseCalled bool CloseCalled bool
CloseError error CloseError error
ValidateFn func(c *ResourceConfig) (ws []string, es []error) ValidateFn func(c *ResourceConfig) (ws []string, es []error)
ConfigureFn func(c *ResourceConfig) error
//ValidateFn func(providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse //ValidateFn func(providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse
//ConfigureFn func(providers.ConfigureRequest) providers.ConfigureResponse
DiffFn func(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse DiffFn func(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse
ApplyFn func(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse ApplyFn func(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse
} }
@ -232,19 +230,7 @@ func (p *MockProvider) Configure(r providers.ConfigureRequest) providers.Configu
p.ConfigureRequest = r p.ConfigureRequest = r
if p.ConfigureFn != nil { if p.ConfigureFn != nil {
resp := p.getSchema() return p.ConfigureFn(r)
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 {
return p.ConfigureNewFn(r)
} }
return p.ConfigureResponse return p.ConfigureResponse

View File

@ -1,5 +1,5 @@
provider "aws" { provider "aws" {
value = "${test_instance.foo.value}" value = test_instance.foo.id
} }
resource "aws_instance" "bar" {} resource "aws_instance" "bar" {}