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

View File

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

View File

@ -5,12 +5,14 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/hashicorp/terraform/providers" "github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/internal/initwd" "github.com/hashicorp/terraform/internal/initwd"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
) )
@ -19,7 +21,7 @@ func TestLocal_refresh(t *testing.T) {
defer cleanup() defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil 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{
@ -42,7 +44,7 @@ func TestLocal_refresh(t *testing.T) {
checkState(t, b.StateOutPath, ` checkState(t, b.StateOutPath, `
test_instance.foo: test_instance.foo:
ID = yes 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) b, cleanup := TestLocal(t)
defer cleanup() defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil 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"), "id": cty.StringVal("yes"),
@ -72,7 +74,7 @@ func TestLocal_refreshNoConfig(t *testing.T) {
checkState(t, b.StateOutPath, ` checkState(t, b.StateOutPath, `
test_instance.foo: test_instance.foo:
ID = yes 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) b, cleanup := TestLocal(t)
defer cleanup() defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil 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"), "id": cty.StringVal("yes"),
@ -105,7 +107,7 @@ func TestLocal_refreshNilModuleWithInput(t *testing.T) {
checkState(t, b.StateOutPath, ` checkState(t, b.StateOutPath, `
test_instance.foo: test_instance.foo:
ID = yes 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) b, cleanup := TestLocal(t)
defer cleanup() defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
Provider: &configschema.Block{ Provider: &configschema.Block{
@ -163,7 +165,7 @@ func TestLocal_refreshInput(t *testing.T) {
checkState(t, b.StateOutPath, ` checkState(t, b.StateOutPath, `
test_instance.foo: test_instance.foo:
ID = yes 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) b, cleanup := TestLocal(t)
defer cleanup() defer cleanup()
p := TestLocalProvider(t, b, "test", refreshFixtureSchema()) p := TestLocalProvider(t, b, "test", refreshFixtureSchema())
terraform.TestStateFile(t, b.StatePath, testRefreshState()) testStateFile(t, b.StatePath, testRefreshState())
p.ReadResourceFn = nil 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"), "id": cty.StringVal("yes"),
@ -196,7 +198,7 @@ func TestLocal_refreshValidate(t *testing.T) {
checkState(t, b.StateOutPath, ` checkState(t, b.StateOutPath, `
test_instance.foo: test_instance.foo:
ID = yes 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. // testRefreshState is just a common state that we use for testing refresh.
func testRefreshState() *terraform.State { func testRefreshState() *states.State {
return &terraform.State{ state := states.NewState()
Version: 2, root := state.EnsureModule(addrs.RootModuleInstance)
Modules: []*terraform.ModuleState{ root.SetResourceInstanceCurrent(
&terraform.ModuleState{ mustResourceInstanceAddr("test_instance.foo").Resource,
Path: []string{"root"}, &states.ResourceInstanceObjectSrc{
Resources: map[string]*terraform.ResourceState{ Status: states.ObjectReady,
"test_instance.foo": &terraform.ResourceState{ AttrsJSON: []byte(`{"id":"bar"}`),
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
Outputs: map[string]*terraform.OutputState{},
},
}, },
} mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
)
return state
} }
// refreshFixtureSchema returns a schema suitable for processing the // 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 // Setup our provider
b.ContextOpts.Providers = map[addrs.Provider]providers.Factory{ b.ContextOpts.Providers = map[addrs.Provider]providers.Factory{
addrs.NewLegacyProvider(name): providers.FactoryFixed(p), addrs.NewDefaultProvider(name): providers.FactoryFixed(p),
} }
return p return p
@ -209,3 +209,19 @@ func testStateFile(t *testing.T, path string, s *states.State) {
stateFile := statemgr.NewFilesystem(path) stateFile := statemgr.NewFilesystem(path)
stateFile.WriteState(s) 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
}