wrap multiple provider creations into a factory fn
When a test uses multiple instances of the same provider, we may need to have separate objects to prevent overwriting of the MockProvider state. Create a completely new MockProvider in each factory function call rather than re-using the original provider value.
This commit is contained in:
parent
903ae5edfd
commit
73b1263a86
|
@ -577,14 +577,18 @@ func TestContext2Apply_refCount(t *testing.T) {
|
||||||
|
|
||||||
func TestContext2Apply_providerAlias(t *testing.T) {
|
func TestContext2Apply_providerAlias(t *testing.T) {
|
||||||
m := testModule(t, "apply-provider-alias")
|
m := testModule(t, "apply-provider-alias")
|
||||||
p := testProvider("aws")
|
|
||||||
p.PlanResourceChangeFn = testDiffFn
|
// Each provider instance must be completely independent to ensure that we
|
||||||
p.ApplyResourceChangeFn = testApplyFn
|
// are verifying the correct state of each.
|
||||||
|
p := func() (providers.Interface, error) {
|
||||||
|
p := testProvider("aws")
|
||||||
|
p.PlanResourceChangeFn = testDiffFn
|
||||||
|
p.ApplyResourceChangeFn = testApplyFn
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
ctx := testContext2(t, &ContextOpts{
|
ctx := testContext2(t, &ContextOpts{
|
||||||
Providers: map[addrs.Provider]providers.Factory{
|
Providers: map[addrs.Provider]providers.Factory{
|
||||||
addrs.NewDefaultProvider("aws"): func() (providers.Interface, error) {
|
addrs.NewDefaultProvider("aws"): p,
|
||||||
return p, nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -612,15 +616,18 @@ func TestContext2Apply_providerAlias(t *testing.T) {
|
||||||
func TestContext2Apply_providerAliasConfigure(t *testing.T) {
|
func TestContext2Apply_providerAliasConfigure(t *testing.T) {
|
||||||
m := testModule(t, "apply-provider-alias-configure")
|
m := testModule(t, "apply-provider-alias-configure")
|
||||||
|
|
||||||
p2 := testProvider("another")
|
// Each provider instance must be completely independent to ensure that we
|
||||||
p2.ApplyResourceChangeFn = testApplyFn
|
// are verifying the correct state of each.
|
||||||
p2.PlanResourceChangeFn = testDiffFn
|
p := func() (providers.Interface, error) {
|
||||||
|
p := testProvider("another")
|
||||||
|
p.ApplyResourceChangeFn = testApplyFn
|
||||||
|
p.PlanResourceChangeFn = testDiffFn
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
ctx := testContext2(t, &ContextOpts{
|
ctx := testContext2(t, &ContextOpts{
|
||||||
Providers: map[addrs.Provider]providers.Factory{
|
Providers: map[addrs.Provider]providers.Factory{
|
||||||
addrs.NewDefaultProvider("another"): func() (providers.Interface, error) {
|
addrs.NewDefaultProvider("another"): p,
|
||||||
return p2, nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -633,17 +640,29 @@ 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.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
p = func() (providers.Interface, error) {
|
||||||
atomic.AddInt32(&configCount, 1)
|
p := testProvider("another")
|
||||||
|
p.ApplyResourceChangeFn = testApplyFn
|
||||||
|
p.PlanResourceChangeFn = testDiffFn
|
||||||
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
||||||
|
atomic.AddInt32(&configCount, 1)
|
||||||
|
|
||||||
foo := req.Config.GetAttr("foo").AsString()
|
foo := req.Config.GetAttr("foo").AsString()
|
||||||
if foo != "bar" {
|
if foo != "bar" {
|
||||||
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("foo: %#v", foo))
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("foo: %#v", foo))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
return p, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx = testContext2(t, &ContextOpts{
|
||||||
|
Providers: map[addrs.Provider]providers.Factory{
|
||||||
|
addrs.NewDefaultProvider("another"): p,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
state, diags := ctx.Apply(plan, m)
|
state, diags := ctx.Apply(plan, m)
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
t.Fatalf("diags: %s", diags.Err())
|
t.Fatalf("diags: %s", diags.Err())
|
||||||
|
@ -1544,8 +1563,11 @@ func TestContext2Apply_destroySkipsCBD(t *testing.T) {
|
||||||
|
|
||||||
func TestContext2Apply_destroyModuleVarProviderConfig(t *testing.T) {
|
func TestContext2Apply_destroyModuleVarProviderConfig(t *testing.T) {
|
||||||
m := testModule(t, "apply-destroy-mod-var-provider-config")
|
m := testModule(t, "apply-destroy-mod-var-provider-config")
|
||||||
p := testProvider("aws")
|
p := func() (providers.Interface, error) {
|
||||||
p.PlanResourceChangeFn = testDiffFn
|
p := testProvider("aws")
|
||||||
|
p.PlanResourceChangeFn = testDiffFn
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
state := states.NewState()
|
state := states.NewState()
|
||||||
root := state.EnsureModule(addrs.RootModuleInstance)
|
root := state.EnsureModule(addrs.RootModuleInstance)
|
||||||
root.SetResourceInstanceCurrent(
|
root.SetResourceInstanceCurrent(
|
||||||
|
@ -1558,9 +1580,7 @@ func TestContext2Apply_destroyModuleVarProviderConfig(t *testing.T) {
|
||||||
)
|
)
|
||||||
ctx := testContext2(t, &ContextOpts{
|
ctx := testContext2(t, &ContextOpts{
|
||||||
Providers: map[addrs.Provider]providers.Factory{
|
Providers: map[addrs.Provider]providers.Factory{
|
||||||
addrs.NewDefaultProvider("aws"): func() (providers.Interface, error) {
|
addrs.NewDefaultProvider("aws"): p,
|
||||||
return p, nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -85,8 +85,7 @@ func TestContext2Input_provider(t *testing.T) {
|
||||||
func TestContext2Input_providerMulti(t *testing.T) {
|
func TestContext2Input_providerMulti(t *testing.T) {
|
||||||
m := testModule(t, "input-provider-multi")
|
m := testModule(t, "input-provider-multi")
|
||||||
|
|
||||||
p := testProvider("aws")
|
getProviderSchemaResponse := getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
|
||||||
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
|
|
||||||
Provider: &configschema.Block{
|
Provider: &configschema.Block{
|
||||||
Attributes: map[string]*configschema.Attribute{
|
Attributes: map[string]*configschema.Attribute{
|
||||||
"foo": {
|
"foo": {
|
||||||
|
@ -108,6 +107,17 @@ func TestContext2Input_providerMulti(t *testing.T) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// In order to update the provider to check only the configure calls during
|
||||||
|
// apply, we will need to inject a new factory function after plan. We must
|
||||||
|
// use a closure around the factory, because in order for the inputs to
|
||||||
|
// work during apply we need to maintain the same context value, preventing
|
||||||
|
// us from assigning a new Providers map.
|
||||||
|
providerFactory := func() (providers.Interface, error) {
|
||||||
|
p := testProvider("aws")
|
||||||
|
p.GetProviderSchemaResponse = getProviderSchemaResponse
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
inp := &MockUIInput{
|
inp := &MockUIInput{
|
||||||
InputReturnMap: map[string]string{
|
InputReturnMap: map[string]string{
|
||||||
"provider.aws.foo": "bar",
|
"provider.aws.foo": "bar",
|
||||||
|
@ -118,7 +128,7 @@ func TestContext2Input_providerMulti(t *testing.T) {
|
||||||
ctx := testContext2(t, &ContextOpts{
|
ctx := testContext2(t, &ContextOpts{
|
||||||
Providers: map[addrs.Provider]providers.Factory{
|
Providers: map[addrs.Provider]providers.Factory{
|
||||||
addrs.NewDefaultProvider("aws"): func() (providers.Interface, error) {
|
addrs.NewDefaultProvider("aws"): func() (providers.Interface, error) {
|
||||||
return p, nil
|
return providerFactory()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
UIInput: inp,
|
UIInput: inp,
|
||||||
|
@ -134,12 +144,18 @@ func TestContext2Input_providerMulti(t *testing.T) {
|
||||||
plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
|
plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
|
||||||
assertNoErrors(t, diags)
|
assertNoErrors(t, diags)
|
||||||
|
|
||||||
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
providerFactory = func() (providers.Interface, error) {
|
||||||
lock.Lock()
|
p := testProvider("aws")
|
||||||
defer lock.Unlock()
|
p.GetProviderSchemaResponse = getProviderSchemaResponse
|
||||||
actual = append(actual, req.Config.GetAttr("foo").AsString())
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
||||||
return
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
actual = append(actual, req.Config.GetAttr("foo").AsString())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, diags := ctx.Apply(plan, m); diags.HasErrors() {
|
if _, diags := ctx.Apply(plan, m); diags.HasErrors() {
|
||||||
t.Fatalf("apply errors: %s", diags.Err())
|
t.Fatalf("apply errors: %s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue