update MockProvider usage

This commit is contained in:
James Bardin 2021-01-12 16:13:10 -05:00
parent 2e2c9e07e5
commit 3bc7d77230
15 changed files with 432 additions and 321 deletions

View File

@ -27,7 +27,7 @@ func TestLocal_applyBasic(t *testing.T) {
defer cleanup()
p := TestLocalProvider(t, b, "test", applyFixtureSchema())
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
"ami": cty.StringVal("bar"),
})}
@ -70,7 +70,7 @@ func TestLocal_applyEmptyDir(t *testing.T) {
defer cleanup()
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{"id": cty.StringVal("yes")})}
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{"id": cty.StringVal("yes")})}
op, configCleanup := testOperationApply(t, "./testdata/empty")
defer configCleanup()
@ -101,7 +101,7 @@ func TestLocal_applyEmptyDirDestroy(t *testing.T) {
defer cleanup()
p := TestLocalProvider(t, b, "test", &terraform.ProviderSchema{})
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{}
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{}
op, configCleanup := testOperationApply(t, "./testdata/empty")
defer configCleanup()
@ -193,7 +193,7 @@ func TestLocal_applyBackendFail(t *testing.T) {
defer cleanup()
p := TestLocalProvider(t, b, "test", applyFixtureSchema())
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
"ami": cty.StringVal("bar"),
})}

View File

@ -24,7 +24,7 @@ func TestLocal_refresh(t *testing.T) {
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
p.ReadResourceResponse = &providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
})}
@ -76,7 +76,7 @@ func TestLocal_refreshInput(t *testing.T) {
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
p.ReadResourceResponse = &providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
})}
p.ConfigureFn = func(req providers.ConfigureRequest) (resp providers.ConfigureResponse) {
@ -119,7 +119,7 @@ func TestLocal_refreshValidate(t *testing.T) {
p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
p.ReadResourceResponse = &providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
})}
@ -165,7 +165,7 @@ func TestLocal_refreshValidateProviderConfigured(t *testing.T) {
p := TestLocalProvider(t, b, "test", schema)
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
p.ReadResourceResponse = &providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
})}

View File

@ -72,7 +72,21 @@ func TestLocalProvider(t *testing.T, b *Local, name string, schema *terraform.Pr
if schema == nil {
schema = &terraform.ProviderSchema{} // default schema is empty
}
p.GetSchemaReturn = schema
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{Block: schema.Provider},
ProviderMeta: providers.Schema{Block: schema.ProviderMeta},
ResourceTypes: map[string]providers.Schema{},
DataSources: map[string]providers.Schema{},
}
for name, res := range schema.ResourceTypes {
p.GetSchemaResponse.ResourceTypes[name] = providers.Schema{
Block: res,
Version: int64(schema.ResourceTypeSchemaVersions[name]),
}
}
for name, dat := range schema.DataSources {
p.GetSchemaResponse.DataSources[name] = providers.Schema{Block: dat}
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
rSchema, _ := schema.SchemaForResourceType(addrs.ManagedResourceMode, req.TypeName)

View File

@ -175,7 +175,7 @@ func testLocalBackend(t *testing.T, remote *Remote) backend.Enhanced {
},
},
})
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
})}

View File

@ -14,7 +14,6 @@ import (
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/statefile"
"github.com/hashicorp/terraform/terraform"
)
func TestApply_destroy(t *testing.T) {
@ -38,15 +37,17 @@ func TestApply_destroy(t *testing.T) {
statePath := testStateFile(t, originalState)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
ui := new(cli.MockUi)
@ -225,20 +226,24 @@ func TestApply_destroyTargeted(t *testing.T) {
statePath := testStateFile(t, originalState)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
},
},
},
"test_load_balancer": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
"instances": {Type: cty.List(cty.String), Optional: true},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{

View File

@ -177,9 +177,9 @@ func TestApply_parallelism(t *testing.T) {
for i := 0; i < 10; i++ {
name := fmt.Sprintf("test%d", i)
provider := &terraform.MockProvider{}
provider.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
name + "_instance": {},
provider.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
name + "_instance": {Block: &configschema.Block{}},
},
}
provider.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
@ -359,9 +359,10 @@ func TestApply_error(t *testing.T) {
resp.PlannedState = cty.ObjectVal(s)
return
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
@ -369,6 +370,7 @@ func TestApply_error(t *testing.T) {
},
},
},
},
}
args := []string{
@ -903,14 +905,16 @@ func TestApply_shutdown(t *testing.T) {
return
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
args := []string{
@ -959,12 +963,12 @@ func TestApply_state(t *testing.T) {
statePath := testStateFile(t, originalState)
p := applyFixtureProvider()
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
}
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
@ -1089,14 +1093,16 @@ func TestApply_vars(t *testing.T) {
}
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
@ -1143,14 +1149,16 @@ func TestApply_varFile(t *testing.T) {
}
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
@ -1207,14 +1215,16 @@ func TestApply_varFileDefault(t *testing.T) {
}
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
@ -1270,14 +1280,16 @@ func TestApply_varFileDefaultJSON(t *testing.T) {
}
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
@ -1327,7 +1339,7 @@ func TestApply_backup(t *testing.T) {
backupPath := testTempFile(t)
p := applyFixtureProvider()
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
@ -1381,7 +1393,7 @@ func TestApply_disableBackup(t *testing.T) {
statePath := testStateFile(t, originalState)
p := applyFixtureProvider()
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
@ -1538,27 +1550,29 @@ output = test
// applyFixtureSchema returns a schema suitable for processing the
// configuration in testdata/apply . This schema should be
// assigned to a mock provider named "test".
func applyFixtureSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
func applyFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
}
// applyFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/apply. This mock has
// GetSchemaReturn, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
// GetSchemaResponse, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
// with the plan/apply steps just passing through the data determined by
// Terraform Core.
func applyFixtureProvider() *terraform.MockProvider {
p := testProvider()
p.GetSchemaReturn = applyFixtureSchema()
p.GetSchemaResponse = applyFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,

View File

@ -9,7 +9,7 @@ import (
"testing"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/providers"
"github.com/mitchellh/cli"
"github.com/zclconf/go-cty/cty"
)
@ -62,16 +62,17 @@ func TestConsole_tfvars(t *testing.T) {
}
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
ui := cli.NewMockUi()
c := &ConsoleCommand{
Meta: Meta{
@ -110,14 +111,16 @@ func TestConsole_unsetRequiredVars(t *testing.T) {
defer testFixCwd(t, tmp, cwd)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
ui := cli.NewMockUi()
c := &ConsoleCommand{

View File

@ -92,20 +92,23 @@ func testProvider() *terraform.MockProvider {
return providers.ReadResourceResponse{NewState: req.PriorState}
}
p.GetSchemaReturn = testProviderSchema()
p.GetSchemaResponse = testProviderSchema()
return p
}
func testProviderSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
Provider: &configschema.Block{
func testProviderSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_resource": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
"foo": {Type: cty.String, Optional: true},
@ -124,14 +127,17 @@ func testProviderSchema() *terraform.ProviderSchema {
},
},
},
DataSources: map[string]*configschema.Block{
},
DataSources: map[string]providers.Schema{
"test_data_source": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"compute": {Type: cty.String, Optional: true},
"value": {Type: cty.String, Computed: true},
},
},
},
},
}
}
@ -139,7 +145,7 @@ func testSchemas() *terraform.Schemas {
provider := testProvider()
return &terraform.Schemas{
Providers: map[addrs.Provider]*terraform.ProviderSchema{
addrs.NewDefaultProvider("test"): provider.GetSchemaReturn,
addrs.NewDefaultProvider("test"): provider.ProviderSchema(),
},
}
}

View File

@ -14,7 +14,6 @@ import (
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/internal/copy"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags"
)
@ -33,7 +32,7 @@ func TestImport(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -43,14 +42,16 @@ func TestImport(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
args := []string{
@ -84,7 +85,7 @@ func TestImport_providerConfig(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -94,19 +95,23 @@ func TestImport_providerConfig(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
configured := false
@ -191,7 +196,7 @@ func TestImport_remoteState(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -201,19 +206,23 @@ func TestImport_remoteState(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
configured := false
@ -338,7 +347,7 @@ func TestImport_providerConfigWithVar(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -348,19 +357,23 @@ func TestImport_providerConfigWithVar(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
configured := false
@ -412,7 +425,7 @@ func TestImport_providerConfigWithDataSource(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -422,23 +435,29 @@ func TestImport_providerConfigWithDataSource(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
DataSources: map[string]*configschema.Block{
},
DataSources: map[string]providers.Schema{
"test_data": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"foo": {Type: cty.String, Optional: true},
},
},
},
},
@ -469,7 +488,7 @@ func TestImport_providerConfigWithVarDefault(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -479,19 +498,23 @@ func TestImport_providerConfigWithVarDefault(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
configured := false
@ -542,7 +565,7 @@ func TestImport_providerConfigWithVarFile(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -552,19 +575,23 @@ func TestImport_providerConfigWithVarFile(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
configured := false
@ -616,7 +643,7 @@ func TestImport_allowMissingResourceConfig(t *testing.T) {
}
p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
@ -626,14 +653,16 @@ func TestImport_allowMissingResourceConfig(t *testing.T) {
},
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
args := []string{
@ -753,14 +782,16 @@ func TestImportModuleVarFile(t *testing.T) {
statePath := testTempFile(t)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
},
},
}
providerSource, close := newMockProviderSource(t, map[string][]string{
@ -823,14 +854,16 @@ func TestImportModuleInputVariableEvaluation(t *testing.T) {
statePath := testTempFile(t)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {Type: cty.String, Optional: true},
},
},
},
},
}
providerSource, close := newMockProviderSource(t, map[string][]string{

View File

@ -172,7 +172,7 @@ func TestPlan_noState(t *testing.T) {
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
expected := cty.NullVal(p.GetSchemaReturn.ResourceTypes["test_instance"].ImpliedType())
expected := cty.NullVal(p.GetSchemaResponse.ResourceTypes["test_instance"].Block.ImpliedType())
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
}
@ -194,7 +194,7 @@ func TestPlan_outPath(t *testing.T) {
},
}
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.NullVal(cty.EmptyObject),
}
@ -292,9 +292,10 @@ func TestPlan_outBackend(t *testing.T) {
outPath := "foo"
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
@ -307,6 +308,7 @@ func TestPlan_outBackend(t *testing.T) {
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
@ -476,14 +478,16 @@ func TestPlan_validate(t *testing.T) {
defer testChdir(t, td)()
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
@ -589,14 +593,17 @@ func TestPlan_providerArgumentUnset(t *testing.T) {
p := planFixtureProvider()
// override the planFixtureProvider schema to include a required provider argument
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Required: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true, Computed: true},
@ -614,6 +621,7 @@ func TestPlan_providerArgumentUnset(t *testing.T) {
},
},
},
},
}
ui := new(cli.MockUi)
c := &PlanCommand{
@ -827,14 +835,16 @@ func TestPlan_shutdown(t *testing.T) {
return
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
code := c.Run([]string{
@ -883,10 +893,11 @@ func TestPlan_init_required(t *testing.T) {
// planFixtureSchema returns a schema suitable for processing the
// configuration in testdata/plan . This schema should be
// assigned to a mock provider named "test".
func planFixtureSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
func planFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
@ -904,16 +915,17 @@ func planFixtureSchema() *terraform.ProviderSchema {
},
},
},
},
}
}
// planFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/plan. This mock has
// GetSchemaReturn and PlanResourceChangeFn populated, with the plan
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
// step just passing through the new object proposed by Terraform Core.
func planFixtureProvider() *terraform.MockProvider {
p := testProvider()
p.GetSchemaReturn = planFixtureSchema()
p.GetSchemaResponse = planFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
@ -925,26 +937,28 @@ func planFixtureProvider() *terraform.MockProvider {
// planVarsFixtureSchema returns a schema suitable for processing the
// configuration in testdata/plan-vars . This schema should be
// assigned to a mock provider named "test".
func planVarsFixtureSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
func planVarsFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
}
// planVarsFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/plan-vars. This mock has
// GetSchemaReturn and PlanResourceChangeFn populated, with the plan
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
// step just passing through the new object proposed by Terraform Core.
func planVarsFixtureProvider() *terraform.MockProvider {
p := testProvider()
p.GetSchemaReturn = planVarsFixtureSchema()
p.GetSchemaResponse = planVarsFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,

View File

@ -22,7 +22,6 @@ import (
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/statefile"
"github.com/hashicorp/terraform/states/statemgr"
"github.com/hashicorp/terraform/terraform"
)
var equateEmpty = cmpopts.EquateEmpty()
@ -40,9 +39,9 @@ func TestRefresh(t *testing.T) {
},
}
p.GetSchemaReturn = refreshFixtureSchema()
p.GetSchemaResponse = refreshFixtureSchema()
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
@ -95,7 +94,7 @@ func TestRefresh_empty(t *testing.T) {
}
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
@ -132,9 +131,9 @@ func TestRefresh_lockedState(t *testing.T) {
},
}
p.GetSchemaReturn = refreshFixtureSchema()
p.GetSchemaResponse = refreshFixtureSchema()
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
@ -177,9 +176,9 @@ func TestRefresh_cwd(t *testing.T) {
},
}
p.GetSchemaReturn = refreshFixtureSchema()
p.GetSchemaResponse = refreshFixtureSchema()
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
@ -249,9 +248,9 @@ func TestRefresh_defaultState(t *testing.T) {
},
}
p.GetSchemaReturn = refreshFixtureSchema()
p.GetSchemaResponse = refreshFixtureSchema()
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
@ -312,9 +311,9 @@ func TestRefresh_outPath(t *testing.T) {
},
}
p.GetSchemaReturn = refreshFixtureSchema()
p.GetSchemaResponse = refreshFixtureSchema()
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
@ -365,7 +364,7 @@ func TestRefresh_var(t *testing.T) {
Ui: ui,
},
}
p.GetSchemaReturn = refreshVarFixtureSchema()
p.GetSchemaResponse = refreshVarFixtureSchema()
args := []string{
"-var", "foo=bar",
@ -396,7 +395,7 @@ func TestRefresh_varFile(t *testing.T) {
Ui: ui,
},
}
p.GetSchemaReturn = refreshVarFixtureSchema()
p.GetSchemaResponse = refreshVarFixtureSchema()
varFilePath := testTempFile(t)
if err := ioutil.WriteFile(varFilePath, []byte(refreshVarFile), 0644); err != nil {
@ -432,7 +431,7 @@ func TestRefresh_varFileDefault(t *testing.T) {
Ui: ui,
},
}
p.GetSchemaReturn = refreshVarFixtureSchema()
p.GetSchemaResponse = refreshVarFixtureSchema()
varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
@ -483,15 +482,17 @@ func TestRefresh_varsUnset(t *testing.T) {
Ui: ui,
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
args := []string{
@ -540,9 +541,9 @@ func TestRefresh_backup(t *testing.T) {
},
}
p.GetSchemaReturn = refreshFixtureSchema()
p.GetSchemaResponse = refreshFixtureSchema()
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("changed"),
}),
@ -604,9 +605,9 @@ func TestRefresh_disableBackup(t *testing.T) {
},
}
p.GetSchemaReturn = refreshFixtureSchema()
p.GetSchemaResponse = refreshFixtureSchema()
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{
p.ReadResourceResponse = &providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
@ -663,15 +664,17 @@ func TestRefresh_displaysOutputs(t *testing.T) {
Ui: ui,
},
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
args := []string{
@ -692,36 +695,42 @@ func TestRefresh_displaysOutputs(t *testing.T) {
// configuration in testdata/refresh . This schema should be
// assigned to a mock provider named "test".
func refreshFixtureSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
func refreshFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
}
// refreshVarFixtureSchema returns a schema suitable for processing the
// configuration in testdata/refresh-var . This schema should be
// assigned to a mock provider named "test".
func refreshVarFixtureSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
Provider: &configschema.Block{
func refreshVarFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
}

View File

@ -408,32 +408,36 @@ func TestShow_json_output_state(t *testing.T) {
// showFixtureSchema returns a schema suitable for processing the configuration
// in testdata/show. This schema should be assigned to a mock provider
// named "test".
func showFixtureSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
Provider: &configschema.Block{
func showFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Optional: true},
},
},
ResourceTypes: map[string]*configschema.Block{
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
}
// showFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/show. This mock has
// GetSchemaReturn, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
// GetSchemaResponse, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
// with the plan/apply steps just passing through the data determined by
// Terraform Core.
func showFixtureProvider() *terraform.MockProvider {
p := testProvider()
p.GetSchemaReturn = showFixtureSchema()
p.GetSchemaResponse = showFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
idVal := req.ProposedNewState.GetAttr("id")
amiVal := req.ProposedNewState.GetAttr("ami")

View File

@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
"github.com/zclconf/go-cty/cty"
)
@ -34,9 +33,10 @@ func TestStateShow(t *testing.T) {
statePath := testStateFile(t, state)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"foo": {Type: cty.String, Optional: true},
@ -44,6 +44,7 @@ func TestStateShow(t *testing.T) {
},
},
},
},
}
ui := new(cli.MockUi)
@ -107,9 +108,10 @@ func TestStateShow_multi(t *testing.T) {
statePath := testStateFile(t, state)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"foo": {Type: cty.String, Optional: true},
@ -117,6 +119,7 @@ func TestStateShow_multi(t *testing.T) {
},
},
},
},
}
ui := new(cli.MockUi)
@ -213,9 +216,10 @@ func TestStateShow_configured_provider(t *testing.T) {
statePath := testStateFile(t, state)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"foo": {Type: cty.String, Optional: true},
@ -223,6 +227,7 @@ func TestStateShow_configured_provider(t *testing.T) {
},
},
},
},
}
ui := new(cli.MockUi)

View File

@ -13,15 +13,16 @@ import (
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/providers"
)
func setupTest(fixturepath string, args ...string) (*cli.MockUi, int) {
ui := new(cli.MockUi)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"ami": {Type: cty.String, Optional: true},
},
@ -39,6 +40,7 @@ func setupTest(fixturepath string, args ...string) (*cli.MockUi, int) {
},
},
},
},
}
c := &ValidateCommand{
Meta: Meta{

View File

@ -184,14 +184,16 @@ func testSession(t *testing.T, test testSessionTest) {
t.Helper()
p := &terraform.MockProvider{}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
},
},
},
},
}
config, _, cleanup, configDiags := initwd.LoadConfigForTests(t, "testdata/config-fixture")