2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-03-28 16:54:08 +02:00
|
|
|
"os"
|
2017-01-19 05:47:56 +01:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-08 21:48:32 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2017-03-01 19:59:17 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2019-02-08 21:48:32 +01:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-09-28 23:04:57 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2018-10-09 00:22:59 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestLocal returns a configured Local struct with temporary paths and
|
|
|
|
// in-memory ContextOpts.
|
|
|
|
//
|
|
|
|
// No operations will be called on the returned value, so you can still set
|
|
|
|
// public fields without any locks.
|
2018-03-28 16:54:08 +02:00
|
|
|
func TestLocal(t *testing.T) (*Local, func()) {
|
2018-03-21 02:43:02 +01:00
|
|
|
t.Helper()
|
|
|
|
tempDir := testTempDir(t)
|
2018-10-31 16:45:03 +01:00
|
|
|
|
|
|
|
local := New()
|
|
|
|
local.StatePath = filepath.Join(tempDir, "state.tfstate")
|
|
|
|
local.StateOutPath = filepath.Join(tempDir, "state.tfstate")
|
|
|
|
local.StateBackupPath = filepath.Join(tempDir, "state.tfstate.bak")
|
|
|
|
local.StateWorkspaceDir = filepath.Join(tempDir, "state.tfstate.d")
|
|
|
|
local.ContextOpts = &terraform.ContextOpts{}
|
|
|
|
|
|
|
|
local.ShowDiagnostics = func(vals ...interface{}) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
diags = diags.Append(vals...)
|
|
|
|
for _, diag := range diags {
|
|
|
|
// NOTE: Since the caller here is not directly the TestLocal
|
|
|
|
// function, t.Helper doesn't apply and so the log source
|
|
|
|
// isn't correctly shown in the test log output. This seems
|
|
|
|
// unavoidable as long as this is happening so indirectly.
|
2019-02-08 21:48:32 +01:00
|
|
|
desc := diag.Description()
|
|
|
|
if desc.Detail != "" {
|
|
|
|
t.Logf("%s: %s", desc.Summary, desc.Detail)
|
|
|
|
} else {
|
|
|
|
t.Log(desc.Summary)
|
|
|
|
}
|
2018-10-31 16:45:03 +01:00
|
|
|
if local.CLI != nil {
|
2019-02-08 21:48:32 +01:00
|
|
|
local.CLI.Error(desc.Summary)
|
2018-03-21 02:43:02 +01:00
|
|
|
}
|
2018-10-31 16:45:03 +01:00
|
|
|
}
|
2018-03-21 02:43:02 +01:00
|
|
|
}
|
2018-10-31 16:45:03 +01:00
|
|
|
|
2018-03-28 16:54:08 +02:00
|
|
|
cleanup := func() {
|
|
|
|
if err := os.RemoveAll(tempDir); err != nil {
|
2018-09-28 23:04:57 +02:00
|
|
|
t.Fatal("error cleanup up test:", err)
|
2018-03-28 16:54:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return local, cleanup
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLocalProvider modifies the ContextOpts of the *Local parameter to
|
|
|
|
// have a provider with the given name.
|
2018-09-28 23:04:57 +02:00
|
|
|
func TestLocalProvider(t *testing.T, b *Local, name string, schema *terraform.ProviderSchema) *terraform.MockProvider {
|
|
|
|
// Build a mock resource provider for in-memory operations
|
|
|
|
p := new(terraform.MockProvider)
|
2019-02-08 21:48:32 +01:00
|
|
|
|
|
|
|
if schema == nil {
|
|
|
|
schema = &terraform.ProviderSchema{} // default schema is empty
|
|
|
|
}
|
2021-01-12 22:13:10 +01:00
|
|
|
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}
|
|
|
|
}
|
2019-02-08 21:48:32 +01:00
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
2019-02-08 21:48:32 +01:00
|
|
|
rSchema, _ := schema.SchemaForResourceType(addrs.ManagedResourceMode, req.TypeName)
|
|
|
|
if rSchema == nil {
|
|
|
|
rSchema = &configschema.Block{} // default schema is empty
|
|
|
|
}
|
|
|
|
plannedVals := map[string]cty.Value{}
|
|
|
|
for name, attrS := range rSchema.Attributes {
|
|
|
|
val := req.ProposedNewState.GetAttr(name)
|
|
|
|
if attrS.Computed && val.IsNull() {
|
|
|
|
val = cty.UnknownVal(attrS.Type)
|
|
|
|
}
|
|
|
|
plannedVals[name] = val
|
|
|
|
}
|
|
|
|
for name := range rSchema.BlockTypes {
|
|
|
|
// For simplicity's sake we just copy the block attributes over
|
|
|
|
// verbatim, since this package's mock providers are all relatively
|
|
|
|
// simple -- we're testing the backend, not esoteric provider features.
|
|
|
|
plannedVals[name] = req.ProposedNewState.GetAttr(name)
|
|
|
|
}
|
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
return providers.PlanResourceChangeResponse{
|
2019-02-08 21:48:32 +01:00
|
|
|
PlannedState: cty.ObjectVal(plannedVals),
|
2018-09-28 23:04:57 +02:00
|
|
|
PlannedPrivate: req.PriorPrivate,
|
2018-08-25 01:13:50 +02:00
|
|
|
}
|
2018-09-28 23:04:57 +02:00
|
|
|
}
|
2018-10-04 22:04:10 +02:00
|
|
|
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
|
|
|
|
return providers.ReadResourceResponse{NewState: req.PriorState}
|
|
|
|
}
|
2018-12-12 17:01:18 +01:00
|
|
|
p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
|
|
|
|
return providers.ReadDataSourceResponse{State: req.Config}
|
|
|
|
}
|
2018-09-28 23:04:57 +02:00
|
|
|
|
|
|
|
// Initialize the opts
|
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = &terraform.ContextOpts{}
|
|
|
|
}
|
|
|
|
|
2021-01-26 20:39:11 +01:00
|
|
|
// Set up our provider
|
2020-03-31 00:30:56 +02:00
|
|
|
b.ContextOpts.Providers = map[addrs.Provider]providers.Factory{
|
2020-04-01 21:07:05 +02:00
|
|
|
addrs.NewDefaultProvider(name): providers.FactoryFixed(p),
|
2020-03-31 00:30:56 +02:00
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-09-28 23:04:57 +02:00
|
|
|
return p
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-01 19:59:17 +01:00
|
|
|
// TestLocalSingleState is a backend implementation that wraps Local
|
|
|
|
// and modifies it to only support single states (returns
|
2018-10-31 16:45:03 +01:00
|
|
|
// ErrWorkspacesNotSupported for multi-state operations).
|
2017-03-01 19:59:17 +01:00
|
|
|
//
|
|
|
|
// This isn't an actual use case, this is exported just to provide a
|
|
|
|
// easy way to test that behavior.
|
|
|
|
type TestLocalSingleState struct {
|
2018-10-31 16:45:03 +01:00
|
|
|
*Local
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// TestNewLocalSingle is a factory for creating a TestLocalSingleState.
|
|
|
|
// This function matches the signature required for backend/init.
|
|
|
|
func TestNewLocalSingle() backend.Backend {
|
|
|
|
return &TestLocalSingleState{Local: New()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) Workspaces() ([]string, error) {
|
|
|
|
return nil, backend.ErrWorkspacesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) DeleteWorkspace(string) error {
|
|
|
|
return backend.ErrWorkspacesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) StateMgr(name string) (statemgr.Full, error) {
|
2017-03-01 19:59:17 +01:00
|
|
|
if name != backend.DefaultStateName {
|
2018-10-31 16:45:03 +01:00
|
|
|
return nil, backend.ErrWorkspacesNotSupported
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
return b.Local.StateMgr(name)
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// TestLocalNoDefaultState is a backend implementation that wraps
|
|
|
|
// Local and modifies it to support named states, but not the
|
|
|
|
// default state. It returns ErrDefaultWorkspaceNotSupported when
|
|
|
|
// the DefaultStateName is used.
|
|
|
|
type TestLocalNoDefaultState struct {
|
|
|
|
*Local
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// TestNewLocalNoDefault is a factory for creating a TestLocalNoDefaultState.
|
|
|
|
// This function matches the signature required for backend/init.
|
|
|
|
func TestNewLocalNoDefault() backend.Backend {
|
|
|
|
return &TestLocalNoDefaultState{Local: New()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) Workspaces() ([]string, error) {
|
|
|
|
workspaces, err := b.Local.Workspaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := workspaces[:0]
|
|
|
|
for _, name := range workspaces {
|
|
|
|
if name != backend.DefaultStateName {
|
|
|
|
filtered = append(filtered, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) DeleteWorkspace(name string) error {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return backend.ErrDefaultWorkspaceNotSupported
|
|
|
|
}
|
|
|
|
return b.Local.DeleteWorkspace(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) StateMgr(name string) (statemgr.Full, error) {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return nil, backend.ErrDefaultWorkspaceNotSupported
|
|
|
|
}
|
|
|
|
return b.Local.StateMgr(name)
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func testTempDir(t *testing.T) string {
|
|
|
|
d, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
2018-10-09 00:22:59 +02:00
|
|
|
|
|
|
|
func testStateFile(t *testing.T, path string, s *states.State) {
|
|
|
|
stateFile := statemgr.NewFilesystem(path)
|
|
|
|
stateFile.WriteState(s)
|
2018-10-17 23:05:57 +02:00
|
|
|
}
|
2020-04-01 21:07:05 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-08-11 17:23:42 +02:00
|
|
|
|
|
|
|
// assertBackendStateUnlocked attempts to lock the backend state. Failure
|
|
|
|
// indicates that the state was indeed locked and therefore this function will
|
|
|
|
// return true.
|
|
|
|
func assertBackendStateUnlocked(t *testing.T, b *Local) bool {
|
|
|
|
t.Helper()
|
|
|
|
stateMgr, _ := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if _, err := stateMgr.Lock(statemgr.NewLockInfo()); err != nil {
|
|
|
|
t.Errorf("state is already locked: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertBackendStateLocked attempts to lock the backend state. Failure
|
|
|
|
// indicates that the state was already locked and therefore this function will
|
|
|
|
// return false.
|
|
|
|
func assertBackendStateLocked(t *testing.T, b *Local) bool {
|
|
|
|
t.Helper()
|
|
|
|
stateMgr, _ := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if _, err := stateMgr.Lock(statemgr.NewLockInfo()); err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
t.Error("unexpected success locking state")
|
|
|
|
return true
|
|
|
|
}
|