backend/local: refactor tests with modern state and default providers (#24524)

This commit is contained in:
Kristin Laemmert 2020-04-01 15:07:05 -04:00 committed by Martin Atkins
parent 3f6ce3c588
commit 32062b00a2
4 changed files with 50 additions and 38 deletions

View File

@ -59,7 +59,7 @@ func TestLocal_applyBasic(t *testing.T) {
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
ami = bar
`)
}
@ -176,7 +176,7 @@ func TestLocal_applyError(t *testing.T) {
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = foo
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
ami = bar
`)
}
@ -226,7 +226,7 @@ func TestLocal_applyBackendFail(t *testing.T) {
checkState(t, "errored.tfstate", `
test_instance.foo:
ID = yes
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
ami = bar
`)
}

View File

@ -216,7 +216,7 @@ func TestLocal_planDeposedOnly(t *testing.T) {
}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("test"),
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
@ -660,7 +660,7 @@ func testPlanState() *states.State {
}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("test"),
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
@ -687,7 +687,7 @@ func testPlanState_withDataSource() *states.State {
}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("test"),
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
@ -704,7 +704,7 @@ func testPlanState_withDataSource() *states.State {
}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("test"),
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
@ -731,7 +731,7 @@ func testPlanState_tainted() *states.State {
}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("test"),
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)

View File

@ -5,12 +5,14 @@ import (
"fmt"
"testing"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/internal/initwd"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/terraform"
"github.com/zclconf/go-cty/cty"
)
@ -19,7 +21,7 @@ func TestLocal_refresh(t *testing.T) {
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState())
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
@ -42,7 +44,7 @@ func TestLocal_refresh(t *testing.T) {
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
`)
}
@ -50,7 +52,7 @@ func TestLocal_refreshNoConfig(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState())
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
@ -72,7 +74,7 @@ func TestLocal_refreshNoConfig(t *testing.T) {
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
`)
}
@ -81,7 +83,7 @@ func TestLocal_refreshNilModuleWithInput(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState())
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
@ -105,7 +107,7 @@ func TestLocal_refreshNilModuleWithInput(t *testing.T) {
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
`)
}
@ -113,7 +115,7 @@ func TestLocal_refreshInput(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState())
testStateFile(t, b.StatePath, testRefreshState())
p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{
@ -163,7 +165,7 @@ func TestLocal_refreshInput(t *testing.T) {
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
`)
}
@ -171,7 +173,7 @@ func TestLocal_refreshValidate(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState())
testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil
p.ReadResourceResponse = providers.ReadResourceResponse{NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
@ -196,7 +198,7 @@ func TestLocal_refreshValidate(t *testing.T) {
checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
provider = provider["registry.terraform.io/-/test"]
provider = provider["registry.terraform.io/hashicorp/test"]
`)
}
@ -213,24 +215,18 @@ func testOperationRefresh(t *testing.T, configDir string) (*backend.Operation, f
}
// testRefreshState is just a common state that we use for testing refresh.
func testRefreshState() *terraform.State {
return &terraform.State{
Version: 2,
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
func testRefreshState() *states.State {
state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_instance.foo").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"bar"}`),
},
},
},
Outputs: map[string]*terraform.OutputState{},
},
},
}
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
)
return state
}
// refreshFixtureSchema returns a schema suitable for processing the

View File

@ -113,7 +113,7 @@ func TestLocalProvider(t *testing.T, b *Local, name string, schema *terraform.Pr
// Setup our provider
b.ContextOpts.Providers = map[addrs.Provider]providers.Factory{
addrs.NewLegacyProvider(name): providers.FactoryFixed(p),
addrs.NewDefaultProvider(name): providers.FactoryFixed(p),
}
return p
@ -209,3 +209,19 @@ func testStateFile(t *testing.T, path string, s *states.State) {
stateFile := statemgr.NewFilesystem(path)
stateFile.WriteState(s)
}
func mustProviderConfig(s string) addrs.AbsProviderConfig {
p, diags := addrs.ParseAbsProviderConfigStr(s)
if diags.HasErrors() {
panic(diags.Err())
}
return p
}
func mustResourceInstanceAddr(s string) addrs.AbsResourceInstance {
addr, diags := addrs.ParseAbsResourceInstanceStr(s)
if diags.HasErrors() {
panic(diags.Err())
}
return addr
}