fixup tests for MockProvider changes

Resetting the *Called fields and enforcing configuration broke a few
tests.
This commit is contained in:
James Bardin 2021-10-08 08:41:58 -04:00
parent 22b400b8de
commit 03f71c2f06
4 changed files with 18 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package terraform
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"strings" "strings"
"testing" "testing"
@ -419,7 +420,12 @@ resource "test_object" "a" {
}, },
}, },
} }
// This is called from the first instance of this provider, so we can't
// check p.ReadResourceCalled after plan.
readResourceCalled := false
p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) { p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
readResourceCalled = true
newVal, err := cty.Transform(req.PriorState, func(path cty.Path, v cty.Value) (cty.Value, error) { newVal, err := cty.Transform(req.PriorState, func(path cty.Path, v cty.Value) (cty.Value, error) {
if len(path) == 1 && path[0] == (cty.GetAttrStep{Name: "arg"}) { if len(path) == 1 && path[0] == (cty.GetAttrStep{Name: "arg"}) {
return cty.StringVal("current"), nil return cty.StringVal("current"), nil
@ -435,7 +441,10 @@ resource "test_object" "a" {
NewState: newVal, NewState: newVal,
} }
} }
upgradeResourceStateCalled := false
p.UpgradeResourceStateFn = func(req providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) { p.UpgradeResourceStateFn = func(req providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) {
upgradeResourceStateCalled = true
t.Logf("UpgradeResourceState %s", req.RawStateJSON) t.Logf("UpgradeResourceState %s", req.RawStateJSON)
// In the destroy-with-refresh codepath we end up calling // In the destroy-with-refresh codepath we end up calling
@ -479,10 +488,10 @@ resource "test_object" "a" {
}) })
assertNoErrors(t, diags) assertNoErrors(t, diags)
if !p.UpgradeResourceStateCalled { if !upgradeResourceStateCalled {
t.Errorf("Provider's UpgradeResourceState wasn't called; should've been") t.Errorf("Provider's UpgradeResourceState wasn't called; should've been")
} }
if !p.ReadResourceCalled { if !readResourceCalled {
t.Errorf("Provider's ReadResource wasn't called; should've been") t.Errorf("Provider's ReadResource wasn't called; should've been")
} }
@ -682,7 +691,7 @@ func TestContext2Plan_destroyNoProviderConfig(t *testing.T) {
p.ValidateProviderConfigFn = func(req providers.ValidateProviderConfigRequest) (resp providers.ValidateProviderConfigResponse) { p.ValidateProviderConfigFn = func(req providers.ValidateProviderConfigRequest) (resp providers.ValidateProviderConfigResponse) {
v := req.Config.GetAttr("test_string") v := req.Config.GetAttr("test_string")
if v.IsNull() || !v.IsKnown() || v.AsString() != "ok" { if v.IsNull() || !v.IsKnown() || v.AsString() != "ok" {
resp.Diagnostics = resp.Diagnostics.Append(errors.New("invalid provider configuration")) resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("invalid provider configuration: %#v", req.Config))
} }
return resp return resp
} }

View File

@ -26,6 +26,7 @@ func TestNodePlanDeposedResourceInstanceObject_Execute(t *testing.T) {
) )
p := testProvider("test") p := testProvider("test")
p.ConfigureProvider(providers.ConfigureProviderRequest{})
p.UpgradeResourceStateResponse = &providers.UpgradeResourceStateResponse{ p.UpgradeResourceStateResponse = &providers.UpgradeResourceStateResponse{
UpgradedState: cty.ObjectVal(map[string]cty.Value{ UpgradedState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("bar"), "id": cty.StringVal("bar"),
@ -106,6 +107,7 @@ func TestNodeDestroyDeposedResourceInstanceObject_Execute(t *testing.T) {
} }
p := testProvider("test") p := testProvider("test")
p.ConfigureProvider(providers.ConfigureProviderRequest{})
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(schema) p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(schema)
p.UpgradeResourceStateResponse = &providers.UpgradeResourceStateResponse{ p.UpgradeResourceStateResponse = &providers.UpgradeResourceStateResponse{

View File

@ -33,6 +33,7 @@ func TestNodeResourcePlanOrphanExecute(t *testing.T) {
) )
p := simpleMockProvider() p := simpleMockProvider()
p.ConfigureProvider(providers.ConfigureProviderRequest{})
ctx := &MockEvalContext{ ctx := &MockEvalContext{
StateState: state.SyncWrapper(), StateState: state.SyncWrapper(),
RefreshStateState: state.DeepCopy().SyncWrapper(), RefreshStateState: state.DeepCopy().SyncWrapper(),
@ -93,6 +94,7 @@ func TestNodeResourcePlanOrphanExecute_alreadyDeleted(t *testing.T) {
changes := plans.NewChanges() changes := plans.NewChanges()
p := simpleMockProvider() p := simpleMockProvider()
p.ConfigureProvider(providers.ConfigureProviderRequest{})
p.ReadResourceResponse = &providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.NullVal(p.GetProviderSchemaResponse.ResourceTypes["test_string"].Block.ImpliedType()), NewState: cty.NullVal(p.GetProviderSchemaResponse.ResourceTypes["test_string"].Block.ImpliedType()),
} }

View File

@ -24,6 +24,7 @@ func TestGraphNodeImportStateExecute(t *testing.T) {
}, },
}, },
} }
provider.ConfigureProvider(providers.ConfigureProviderRequest{})
ctx := &MockEvalContext{ ctx := &MockEvalContext{
StateState: state.SyncWrapper(), StateState: state.SyncWrapper(),
@ -64,6 +65,7 @@ func TestGraphNodeImportStateExecute(t *testing.T) {
func TestGraphNodeImportStateSubExecute(t *testing.T) { func TestGraphNodeImportStateSubExecute(t *testing.T) {
state := states.NewState() state := states.NewState()
provider := testProvider("aws") provider := testProvider("aws")
provider.ConfigureProvider(providers.ConfigureProviderRequest{})
ctx := &MockEvalContext{ ctx := &MockEvalContext{
StateState: state.SyncWrapper(), StateState: state.SyncWrapper(),
ProviderProvider: provider, ProviderProvider: provider,