change mock return values to pointers

This allows up to detect an unset value from the zero value so that
defaults can be implemented, while still allowing tests to return
specific values of their choosing.
This commit is contained in:
James Bardin 2021-01-09 15:33:38 -05:00
parent ae52190c01
commit 5fe848b642
8 changed files with 147 additions and 117 deletions

View File

@ -1418,7 +1418,7 @@ func TestContext2Apply_dataBasic(t *testing.T) {
p := testProvider("null") p := testProvider("null")
p.ApplyResourceChangeFn = testApplyFn p.ApplyResourceChangeFn = testApplyFn
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yo"), "id": cty.StringVal("yo"),
"foo": cty.NullVal(cty.String), "foo": cty.NullVal(cty.String),
@ -10402,13 +10402,12 @@ func TestContext2Apply_ProviderMeta_refresh_set(t *testing.T) {
}, },
} }
rrcPMs := map[string]cty.Value{} rrcPMs := map[string]cty.Value{}
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse { p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
rrcPMs[req.TypeName] = req.ProviderMeta rrcPMs[req.TypeName] = req.ProviderMeta
newState, err := p.GetSchemaReturn.ResourceTypes[req.TypeName].CoerceValue(p.ReadResourceResponse.NewState) newState, err := p.GetSchemaReturn.ResourceTypes[req.TypeName].CoerceValue(req.PriorState)
if err != nil { if err != nil {
panic(err) panic(err)
} }
resp := p.ReadResourceResponse
resp.NewState = newState resp.NewState = newState
return resp return resp
} }
@ -10792,7 +10791,7 @@ func TestContext2Apply_ProviderMeta_refreshdata_setNoSchema(t *testing.T) {
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
}, },
}) })
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yo"), "id": cty.StringVal("yo"),
"foo": cty.StringVal("bar"), "foo": cty.StringVal("bar"),
@ -10848,7 +10847,7 @@ func TestContext2Apply_ProviderMeta_refreshdata_setInvalid(t *testing.T) {
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
}, },
}) })
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yo"), "id": cty.StringVal("yo"),
"foo": cty.StringVal("bar"), "foo": cty.StringVal("bar"),
@ -11497,7 +11496,7 @@ output "output" {
nullP.ApplyResourceChangeFn = testApplyFn nullP.ApplyResourceChangeFn = testApplyFn
nullP.PlanResourceChangeFn = testDiffFn nullP.PlanResourceChangeFn = testDiffFn
nullP.ReadDataSourceResponse = providers.ReadDataSourceResponse{ nullP.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("ID"), "id": cty.StringVal("ID"),
"output": cty.StringVal("valid"), "output": cty.StringVal("valid"),

View File

@ -22,7 +22,7 @@ func TestContextImport_basic(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -63,7 +63,7 @@ func TestContextImport_countIndex(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -125,7 +125,7 @@ func TestContextImport_collision(t *testing.T) {
}), }),
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -164,7 +164,7 @@ func TestContextImport_missingType(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
m := testModule(t, "import-provider") m := testModule(t, "import-provider")
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
@ -205,7 +205,7 @@ func TestContextImport_missingType(t *testing.T) {
func TestContextImport_moduleProvider(t *testing.T) { func TestContextImport_moduleProvider(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -269,7 +269,7 @@ func TestContextImport_providerModule(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -341,7 +341,7 @@ func TestContextImport_providerConfig(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -396,7 +396,7 @@ func TestContextImport_providerConfigResources(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -435,7 +435,7 @@ func TestContextImport_refresh(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -448,7 +448,7 @@ func TestContextImport_refresh(t *testing.T) {
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{ NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"), "id": cty.StringVal("foo"),
"foo": cty.StringVal("bar"), "foo": cty.StringVal("bar"),
@ -486,7 +486,7 @@ func TestContextImport_refreshNil(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -534,7 +534,7 @@ func TestContextImport_module(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -576,7 +576,7 @@ func TestContextImport_moduleDepth2(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -618,7 +618,7 @@ func TestContextImport_moduleDiff(t *testing.T) {
}, },
}) })
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -674,7 +674,7 @@ func TestContextImport_multiState(t *testing.T) {
}, },
} }
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -743,7 +743,7 @@ func TestContextImport_multiStateSame(t *testing.T) {
}, },
} }
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",
@ -844,7 +844,7 @@ resource "test_resource" "unused" {
}, },
} }
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "test_resource", TypeName: "test_resource",
@ -854,7 +854,7 @@ resource "test_resource" "unused" {
}, },
}, },
} }
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "test_resource", TypeName: "test_resource",

View File

@ -1873,7 +1873,7 @@ func TestContext2Plan_computedInFunction(t *testing.T) {
}, },
} }
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"computed": cty.ListVal([]cty.Value{ "computed": cty.ListVal([]cty.Value{
cty.StringVal("foo"), cty.StringVal("foo"),
@ -2015,7 +2015,7 @@ func TestContext2Plan_dataResourceBecomesComputed(t *testing.T) {
schema := p.GetSchemaReturn.DataSources["aws_data_source"] schema := p.GetSchemaReturn.DataSources["aws_data_source"]
ty := schema.ImpliedType() ty := schema.ImpliedType()
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
// This should not be called, because the configuration for the // This should not be called, because the configuration for the
// data resource contains an unknown value for "foo". // data resource contains an unknown value for "foo".
Diagnostics: tfdiags.Diagnostics(nil).Append(fmt.Errorf("ReadDataSource called, but should not have been")), Diagnostics: tfdiags.Diagnostics(nil).Append(fmt.Errorf("ReadDataSource called, but should not have been")),
@ -5412,7 +5412,7 @@ output "out" {
}) })
p := testProvider("aws") p := testProvider("aws")
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("data_id"), "id": cty.StringVal("data_id"),
"foo": cty.StringVal("foo"), "foo": cty.StringVal("foo"),
@ -5459,7 +5459,7 @@ resource "aws_instance" "foo" {
}) })
p := testProvider("aws") p := testProvider("aws")
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("data_id"), "id": cty.StringVal("data_id"),
"foo": cty.StringVal("foo"), "foo": cty.StringVal("foo"),
@ -6114,7 +6114,7 @@ data "test_data_source" "foo" {}
}, },
} }
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("data_id"), "id": cty.StringVal("data_id"),
"foo": cty.StringVal("foo"), "foo": cty.StringVal("foo"),
@ -6261,7 +6261,7 @@ data "test_data_source" "d" {
`}) `})
p := testProvider("test") p := testProvider("test")
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("this"), "id": cty.StringVal("this"),
"foo": cty.NullVal(cty.String), "foo": cty.NullVal(cty.String),

View File

@ -49,7 +49,7 @@ func TestContext2Refresh(t *testing.T) {
} }
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: readState, NewState: readState,
} }
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
@ -514,7 +514,7 @@ func TestContext2Refresh_delete(t *testing.T) {
}) })
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.NullVal(p.GetSchemaReturn.ResourceTypes["aws_instance"].ImpliedType()), NewState: cty.NullVal(p.GetSchemaReturn.ResourceTypes["aws_instance"].ImpliedType()),
} }
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
@ -542,7 +542,7 @@ func TestContext2Refresh_ignoreUncreated(t *testing.T) {
}) })
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{ NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"), "id": cty.StringVal("foo"),
}), }),
@ -700,7 +700,7 @@ func TestContext2Refresh_noState(t *testing.T) {
}) })
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{ NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"), "id": cty.StringVal("foo"),
}), }),
@ -785,7 +785,7 @@ func TestContext2Refresh_outputPartial(t *testing.T) {
} }
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.NullVal(p.GetSchemaReturn.ResourceTypes["aws_instance"].ImpliedType()), NewState: cty.NullVal(p.GetSchemaReturn.ResourceTypes["aws_instance"].ImpliedType()),
} }
@ -841,7 +841,7 @@ func TestContext2Refresh_stateBasic(t *testing.T) {
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: readStateVal, NewState: readStateVal,
} }
@ -1142,7 +1142,7 @@ func TestContext2Refresh_vars(t *testing.T) {
p.ReadResourceFn = nil p.ReadResourceFn = nil
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
p.ReadResourceResponse = providers.ReadResourceResponse{ p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: readStateVal, NewState: readStateVal,
} }
@ -1333,7 +1333,7 @@ func TestContext2Refresh_schemaUpgradeFlatmap(t *testing.T) {
"test_thing": 5, "test_thing": 5,
}, },
} }
p.UpgradeResourceStateResponse = providers.UpgradeResourceStateResponse{ p.UpgradeResourceStateResponse = &providers.UpgradeResourceStateResponse{
UpgradedState: cty.ObjectVal(map[string]cty.Value{ UpgradedState: cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("foo"), "name": cty.StringVal("foo"),
}), }),
@ -1420,7 +1420,7 @@ func TestContext2Refresh_schemaUpgradeJSON(t *testing.T) {
"test_thing": 5, "test_thing": 5,
}, },
} }
p.UpgradeResourceStateResponse = providers.UpgradeResourceStateResponse{ p.UpgradeResourceStateResponse = &providers.UpgradeResourceStateResponse{
UpgradedState: cty.ObjectVal(map[string]cty.Value{ UpgradedState: cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("foo"), "name": cty.StringVal("foo"),
}), }),
@ -1545,7 +1545,7 @@ func TestContext2Refresh_dataResourceDependsOn(t *testing.T) {
}, },
} }
p.PlanResourceChangeFn = testDiffFn p.PlanResourceChangeFn = testDiffFn
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{ p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{ State: cty.ObjectVal(map[string]cty.Value{
"compute": cty.StringVal("value"), "compute": cty.StringVal("value"),
}), }),

View File

@ -398,7 +398,7 @@ func TestContext2Validate_moduleBadResource(t *testing.T) {
}, },
}) })
p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{ p.ValidateResourceTypeConfigResponse = &providers.ValidateResourceTypeConfigResponse{
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")), Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
} }
@ -582,7 +582,7 @@ func TestContext2Validate_providerConfig_bad(t *testing.T) {
}, },
}) })
p.PrepareProviderConfigResponse = providers.PrepareProviderConfigResponse{ p.PrepareProviderConfigResponse = &providers.PrepareProviderConfigResponse{
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")), Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
} }
@ -618,7 +618,7 @@ func TestContext2Validate_providerConfig_skippedEmpty(t *testing.T) {
}, },
}) })
p.PrepareProviderConfigResponse = providers.PrepareProviderConfigResponse{ p.PrepareProviderConfigResponse = &providers.PrepareProviderConfigResponse{
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("should not be called")), Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("should not be called")),
} }
@ -714,7 +714,7 @@ func TestContext2Validate_provisionerConfig_bad(t *testing.T) {
}, },
}) })
p.PrepareProviderConfigResponse = providers.PrepareProviderConfigResponse{ p.PrepareProviderConfigResponse = &providers.PrepareProviderConfigResponse{
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")), Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
} }
@ -878,7 +878,7 @@ func TestContext2Validate_resourceConfig_bad(t *testing.T) {
}, },
}) })
p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{ p.ValidateResourceTypeConfigResponse = &providers.ValidateResourceTypeConfigResponse{
Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")), Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("bad")),
} }
@ -1877,7 +1877,7 @@ resource "test_instance" "a" {
}, },
} }
p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{ p.ValidateResourceTypeConfigResponse = &providers.ValidateResourceTypeConfigResponse{
Diagnostics: tfdiags.Diagnostics(nil).Append(tfdiags.SimpleWarning("don't frobble")), Diagnostics: tfdiags.Diagnostics(nil).Append(tfdiags.SimpleWarning("don't frobble")),
} }

View File

@ -26,7 +26,7 @@ func TestNodePlanDeposedResourceInstanceObject_Execute(t *testing.T) {
) )
p := testProvider("test") p := testProvider("test")
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"),
}), }),
@ -86,7 +86,7 @@ func TestNodeDestroyDeposedResourceInstanceObject_Execute(t *testing.T) {
) )
p := testProvider("test") p := testProvider("test")
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"),
}), }),

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"sync" "sync"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json" ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/zclconf/go-cty/cty/msgpack" "github.com/zclconf/go-cty/cty/msgpack"
@ -26,30 +25,30 @@ type MockProvider struct {
GetSchemaReturn *ProviderSchema // This is using ProviderSchema directly rather than providers.GetSchemaResponse for compatibility with old tests GetSchemaReturn *ProviderSchema // This is using ProviderSchema directly rather than providers.GetSchemaResponse for compatibility with old tests
PrepareProviderConfigCalled bool PrepareProviderConfigCalled bool
PrepareProviderConfigResponse providers.PrepareProviderConfigResponse PrepareProviderConfigResponse *providers.PrepareProviderConfigResponse
PrepareProviderConfigRequest providers.PrepareProviderConfigRequest PrepareProviderConfigRequest providers.PrepareProviderConfigRequest
PrepareProviderConfigFn func(providers.PrepareProviderConfigRequest) providers.PrepareProviderConfigResponse PrepareProviderConfigFn func(providers.PrepareProviderConfigRequest) providers.PrepareProviderConfigResponse
ValidateResourceTypeConfigCalled bool ValidateResourceTypeConfigCalled bool
ValidateResourceTypeConfigTypeName string ValidateResourceTypeConfigTypeName string
ValidateResourceTypeConfigResponse providers.ValidateResourceTypeConfigResponse ValidateResourceTypeConfigResponse *providers.ValidateResourceTypeConfigResponse
ValidateResourceTypeConfigRequest providers.ValidateResourceTypeConfigRequest ValidateResourceTypeConfigRequest providers.ValidateResourceTypeConfigRequest
ValidateResourceTypeConfigFn func(providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse ValidateResourceTypeConfigFn func(providers.ValidateResourceTypeConfigRequest) providers.ValidateResourceTypeConfigResponse
ValidateDataSourceConfigCalled bool ValidateDataSourceConfigCalled bool
ValidateDataSourceConfigTypeName string ValidateDataSourceConfigTypeName string
ValidateDataSourceConfigResponse providers.ValidateDataSourceConfigResponse ValidateDataSourceConfigResponse *providers.ValidateDataSourceConfigResponse
ValidateDataSourceConfigRequest providers.ValidateDataSourceConfigRequest ValidateDataSourceConfigRequest providers.ValidateDataSourceConfigRequest
ValidateDataSourceConfigFn func(providers.ValidateDataSourceConfigRequest) providers.ValidateDataSourceConfigResponse ValidateDataSourceConfigFn func(providers.ValidateDataSourceConfigRequest) providers.ValidateDataSourceConfigResponse
UpgradeResourceStateCalled bool UpgradeResourceStateCalled bool
UpgradeResourceStateTypeName string UpgradeResourceStateTypeName string
UpgradeResourceStateResponse providers.UpgradeResourceStateResponse UpgradeResourceStateResponse *providers.UpgradeResourceStateResponse
UpgradeResourceStateRequest providers.UpgradeResourceStateRequest UpgradeResourceStateRequest providers.UpgradeResourceStateRequest
UpgradeResourceStateFn func(providers.UpgradeResourceStateRequest) providers.UpgradeResourceStateResponse UpgradeResourceStateFn func(providers.UpgradeResourceStateRequest) providers.UpgradeResourceStateResponse
ConfigureCalled bool ConfigureCalled bool
ConfigureResponse providers.ConfigureResponse ConfigureResponse *providers.ConfigureResponse
ConfigureRequest providers.ConfigureRequest ConfigureRequest providers.ConfigureRequest
ConfigureFn func(providers.ConfigureRequest) providers.ConfigureResponse ConfigureFn func(providers.ConfigureRequest) providers.ConfigureResponse
@ -58,27 +57,27 @@ type MockProvider struct {
StopResponse error StopResponse error
ReadResourceCalled bool ReadResourceCalled bool
ReadResourceResponse providers.ReadResourceResponse ReadResourceResponse *providers.ReadResourceResponse
ReadResourceRequest providers.ReadResourceRequest ReadResourceRequest providers.ReadResourceRequest
ReadResourceFn func(providers.ReadResourceRequest) providers.ReadResourceResponse ReadResourceFn func(providers.ReadResourceRequest) providers.ReadResourceResponse
PlanResourceChangeCalled bool PlanResourceChangeCalled bool
PlanResourceChangeResponse providers.PlanResourceChangeResponse PlanResourceChangeResponse *providers.PlanResourceChangeResponse
PlanResourceChangeRequest providers.PlanResourceChangeRequest PlanResourceChangeRequest providers.PlanResourceChangeRequest
PlanResourceChangeFn func(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse PlanResourceChangeFn func(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse
ApplyResourceChangeCalled bool ApplyResourceChangeCalled bool
ApplyResourceChangeResponse providers.ApplyResourceChangeResponse ApplyResourceChangeResponse *providers.ApplyResourceChangeResponse
ApplyResourceChangeRequest providers.ApplyResourceChangeRequest ApplyResourceChangeRequest providers.ApplyResourceChangeRequest
ApplyResourceChangeFn func(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse ApplyResourceChangeFn func(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse
ImportResourceStateCalled bool ImportResourceStateCalled bool
ImportResourceStateResponse providers.ImportResourceStateResponse ImportResourceStateResponse *providers.ImportResourceStateResponse
ImportResourceStateRequest providers.ImportResourceStateRequest ImportResourceStateRequest providers.ImportResourceStateRequest
ImportResourceStateFn func(providers.ImportResourceStateRequest) providers.ImportResourceStateResponse ImportResourceStateFn func(providers.ImportResourceStateRequest) providers.ImportResourceStateResponse
ReadDataSourceCalled bool ReadDataSourceCalled bool
ReadDataSourceResponse providers.ReadDataSourceResponse ReadDataSourceResponse *providers.ReadDataSourceResponse
ReadDataSourceRequest providers.ReadDataSourceRequest ReadDataSourceRequest providers.ReadDataSourceRequest
ReadDataSourceFn func(providers.ReadDataSourceRequest) providers.ReadDataSourceResponse ReadDataSourceFn func(providers.ReadDataSourceRequest) providers.ReadDataSourceResponse
@ -140,7 +139,7 @@ func (p *MockProvider) getDatasourceSchema(name string) providers.Schema {
return dataSchema return dataSchema
} }
func (p *MockProvider) PrepareProviderConfig(r providers.PrepareProviderConfigRequest) providers.PrepareProviderConfigResponse { func (p *MockProvider) PrepareProviderConfig(r providers.PrepareProviderConfigRequest) (resp providers.PrepareProviderConfigResponse) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
@ -149,8 +148,13 @@ func (p *MockProvider) PrepareProviderConfig(r providers.PrepareProviderConfigRe
if p.PrepareProviderConfigFn != nil { if p.PrepareProviderConfigFn != nil {
return p.PrepareProviderConfigFn(r) return p.PrepareProviderConfigFn(r)
} }
p.PrepareProviderConfigResponse.PreparedConfig = r.Config
return p.PrepareProviderConfigResponse if p.PrepareProviderConfigResponse != nil {
return *p.PrepareProviderConfigResponse
}
resp.PreparedConfig = r.Config
return resp
} }
func (p *MockProvider) ValidateResourceTypeConfig(r providers.ValidateResourceTypeConfigRequest) (resp providers.ValidateResourceTypeConfigResponse) { func (p *MockProvider) ValidateResourceTypeConfig(r providers.ValidateResourceTypeConfigRequest) (resp providers.ValidateResourceTypeConfigResponse) {
@ -173,7 +177,11 @@ func (p *MockProvider) ValidateResourceTypeConfig(r providers.ValidateResourceTy
return p.ValidateResourceTypeConfigFn(r) return p.ValidateResourceTypeConfigFn(r)
} }
return p.ValidateResourceTypeConfigResponse if p.ValidateResourceTypeConfigResponse != nil {
return *p.ValidateResourceTypeConfigResponse
}
return resp
} }
func (p *MockProvider) ValidateDataSourceConfig(r providers.ValidateDataSourceConfigRequest) (resp providers.ValidateDataSourceConfigResponse) { func (p *MockProvider) ValidateDataSourceConfig(r providers.ValidateDataSourceConfigRequest) (resp providers.ValidateDataSourceConfigResponse) {
@ -195,10 +203,14 @@ func (p *MockProvider) ValidateDataSourceConfig(r providers.ValidateDataSourceCo
return p.ValidateDataSourceConfigFn(r) return p.ValidateDataSourceConfigFn(r)
} }
return p.ValidateDataSourceConfigResponse if p.ValidateDataSourceConfigResponse != nil {
return *p.ValidateDataSourceConfigResponse
} }
func (p *MockProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequest) providers.UpgradeResourceStateResponse { return resp
}
func (p *MockProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
@ -213,9 +225,10 @@ func (p *MockProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequ
return p.UpgradeResourceStateFn(r) return p.UpgradeResourceStateFn(r)
} }
resp := p.UpgradeResourceStateResponse if p.UpgradeResourceStateResponse != nil {
return *p.UpgradeResourceStateResponse
}
if resp.UpgradedState == cty.NilVal {
switch { switch {
case r.RawStateFlatmap != nil: case r.RawStateFlatmap != nil:
v, err := hcl2shim.HCL2ValueFromFlatmap(r.RawStateFlatmap, schemaType) v, err := hcl2shim.HCL2ValueFromFlatmap(r.RawStateFlatmap, schemaType)
@ -233,11 +246,11 @@ func (p *MockProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequ
} }
resp.UpgradedState = v resp.UpgradedState = v
} }
}
return resp return resp
} }
func (p *MockProvider) Configure(r providers.ConfigureRequest) providers.ConfigureResponse { func (p *MockProvider) Configure(r providers.ConfigureRequest) (resp providers.ConfigureResponse) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
@ -248,7 +261,11 @@ func (p *MockProvider) Configure(r providers.ConfigureRequest) providers.Configu
return p.ConfigureFn(r) return p.ConfigureFn(r)
} }
return p.ConfigureResponse if p.ConfigureResponse != nil {
return *p.ConfigureResponse
}
return resp
} }
func (p *MockProvider) Stop() error { func (p *MockProvider) Stop() error {
@ -265,7 +282,7 @@ func (p *MockProvider) Stop() error {
return p.StopResponse return p.StopResponse
} }
func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) providers.ReadResourceResponse { func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
@ -276,24 +293,25 @@ func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) providers.R
return p.ReadResourceFn(r) return p.ReadResourceFn(r)
} }
resp := p.ReadResourceResponse if p.ReadResourceResponse != nil {
if resp.NewState != cty.NilVal { resp = *p.ReadResourceResponse
// make sure the NewState fits the schema
// This isn't always the case for the existing tests // Make sure the NewState conforms to the schema.
// This isn't always the case for the existing tests.
newState, err := p.GetSchemaReturn.ResourceTypes[r.TypeName].CoerceValue(resp.NewState) newState, err := p.GetSchemaReturn.ResourceTypes[r.TypeName].CoerceValue(resp.NewState)
if err != nil { if err != nil {
panic(err) resp.Diagnostics = resp.Diagnostics.Append(err)
} }
resp.NewState = newState resp.NewState = newState
return resp return resp
} }
// just return the same state we received // otherwise just return the same state we received
resp.NewState = r.PriorState resp.NewState = r.PriorState
return resp return resp
} }
func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse { func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
@ -304,10 +322,14 @@ func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
return p.PlanResourceChangeFn(r) return p.PlanResourceChangeFn(r)
} }
return p.PlanResourceChangeResponse if p.PlanResourceChangeResponse != nil {
return *p.PlanResourceChangeResponse
} }
func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse { return resp
}
func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
p.Lock() p.Lock()
p.ApplyResourceChangeCalled = true p.ApplyResourceChangeCalled = true
p.ApplyResourceChangeRequest = r p.ApplyResourceChangeRequest = r
@ -317,7 +339,10 @@ func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeReques
return p.ApplyResourceChangeFn(r) return p.ApplyResourceChangeFn(r)
} }
return p.ApplyResourceChangeResponse if p.ApplyResourceChangeResponse != nil {
return *p.ApplyResourceChangeResponse
}
return resp
} }
func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) { func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) {
@ -330,8 +355,10 @@ func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateReques
return p.ImportResourceStateFn(r) return p.ImportResourceStateFn(r)
} }
if p.ImportResourceStateResponse != nil {
resp = *p.ImportResourceStateResponse
// fixup the cty value to match the schema // fixup the cty value to match the schema
for i, res := range p.ImportResourceStateResponse.ImportedResources { for i, res := range resp.ImportedResources {
schema := p.GetSchemaReturn.ResourceTypes[res.TypeName] schema := p.GetSchemaReturn.ResourceTypes[res.TypeName]
if schema == nil { if schema == nil {
resp.Diagnostics = resp.Diagnostics.Append(errors.New("no schema found for " + res.TypeName)) resp.Diagnostics = resp.Diagnostics.Append(errors.New("no schema found for " + res.TypeName))
@ -345,13 +372,14 @@ func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateReques
return resp return resp
} }
p.ImportResourceStateResponse.ImportedResources[i] = res resp.ImportedResources[i] = res
}
} }
return p.ImportResourceStateResponse return resp
} }
func (p *MockProvider) ReadDataSource(r providers.ReadDataSourceRequest) providers.ReadDataSourceResponse { func (p *MockProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp providers.ReadDataSourceResponse) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
@ -362,7 +390,10 @@ func (p *MockProvider) ReadDataSource(r providers.ReadDataSourceRequest) provide
return p.ReadDataSourceFn(r) return p.ReadDataSourceFn(r)
} }
return p.ReadDataSourceResponse if p.ReadDataSourceResponse != nil {
resp = *p.ReadDataSourceResponse
}
return resp
} }
func (p *MockProvider) Close() error { func (p *MockProvider) Close() error {

View File

@ -14,7 +14,7 @@ import (
func TestGraphNodeImportStateExecute(t *testing.T) { func TestGraphNodeImportStateExecute(t *testing.T) {
state := states.NewState() state := states.NewState()
provider := testProvider("aws") provider := testProvider("aws")
provider.ImportResourceStateResponse = providers.ImportResourceStateResponse{ provider.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {
TypeName: "aws_instance", TypeName: "aws_instance",