Merge pull request #29741 from hashicorp/jbardin/test-fix

fix test fixture with multiple provider instances
This commit is contained in:
James Bardin 2021-10-13 09:53:02 -04:00 committed by GitHub
commit 87e852c832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 26 deletions

View File

@ -577,12 +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")
// Each provider instance must be completely independent to ensure that we
// are verifying the correct state of each.
p := func() (providers.Interface, error) {
p := testProvider("aws") p := testProvider("aws")
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
p.ApplyResourceChangeFn = testApplyFn 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"): testProviderFuncFixed(p), addrs.NewDefaultProvider("aws"): p,
}, },
}) })
@ -610,13 +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"): testProviderFuncFixed(p2), addrs.NewDefaultProvider("another"): p,
}, },
}) })
@ -629,7 +640,11 @@ 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) {
p := testProvider("another")
p.ApplyResourceChangeFn = testApplyFn
p.PlanResourceChangeFn = testDiffFn
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
atomic.AddInt32(&configCount, 1) atomic.AddInt32(&configCount, 1)
foo := req.Config.GetAttr("foo").AsString() foo := req.Config.GetAttr("foo").AsString()
@ -639,6 +654,14 @@ func TestContext2Apply_providerAliasConfigure(t *testing.T) {
return return
} }
return p, nil
}
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() {
@ -1540,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 := func() (providers.Interface, error) {
p := testProvider("aws") p := testProvider("aws")
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
return p, nil
}
state := states.NewState() state := states.NewState()
child := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey)) child := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
child.SetResourceInstanceCurrent( child.SetResourceInstanceCurrent(
@ -1554,7 +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"): testProviderFuncFixed(p), addrs.NewDefaultProvider("aws"): p,
}, },
}) })

View File

@ -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",
@ -117,7 +127,9 @@ 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"): testProviderFuncFixed(p), addrs.NewDefaultProvider("aws"): func() (providers.Interface, error) {
return providerFactory()
},
}, },
UIInput: inp, UIInput: inp,
}) })
@ -132,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)
providerFactory = func() (providers.Interface, error) {
p := testProvider("aws")
p.GetProviderSchemaResponse = getProviderSchemaResponse
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) { p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
actual = append(actual, req.Config.GetAttr("foo").AsString()) actual = append(actual, req.Config.GetAttr("foo").AsString())
return 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())
} }