2017-01-18 22:47:56 -06:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2017-02-27 15:43:31 -06:00
|
|
|
"errors"
|
2017-02-21 18:07:27 -06:00
|
|
|
"io/ioutil"
|
2017-01-18 22:47:56 -06:00
|
|
|
"os"
|
2017-02-28 18:18:16 -06:00
|
|
|
"path/filepath"
|
2017-02-21 18:07:27 -06:00
|
|
|
"reflect"
|
2017-01-18 22:47:56 -06:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-17 10:42:17 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2021-05-17 14:43:35 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/states/statefile"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2017-01-18 22:47:56 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocal_impl(t *testing.T) {
|
2018-10-31 10:45:03 -05:00
|
|
|
var _ backend.Enhanced = New()
|
|
|
|
var _ backend.Local = New()
|
|
|
|
var _ backend.CLI = New()
|
2017-01-18 22:47:56 -06:00
|
|
|
}
|
|
|
|
|
2017-03-09 04:47:21 -06:00
|
|
|
func TestLocal_backend(t *testing.T) {
|
2017-03-23 10:14:13 -05:00
|
|
|
defer testTmpDir(t)()
|
2018-10-31 10:45:03 -05:00
|
|
|
b := New()
|
2018-02-20 21:09:54 -06:00
|
|
|
backend.TestBackendStates(t, b)
|
|
|
|
backend.TestBackendStateLocks(t, b, b)
|
2017-03-09 04:47:21 -06:00
|
|
|
}
|
|
|
|
|
2017-01-18 22:47:56 -06:00
|
|
|
func checkState(t *testing.T, path, expected string) {
|
2017-11-07 20:23:08 -06:00
|
|
|
t.Helper()
|
2017-01-18 22:47:56 -06:00
|
|
|
// Read the state
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-28 16:04:57 -05:00
|
|
|
state, err := statefile.Read(f)
|
2017-01-18 22:47:56 -06:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-28 16:04:57 -05:00
|
|
|
actual := state.State.String()
|
2017-01-18 22:47:56 -06:00
|
|
|
expected = strings.TrimSpace(expected)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("state does not match! actual:\n%s\n\nexpected:\n%s", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 18:07:27 -06:00
|
|
|
|
2017-02-28 18:18:16 -06:00
|
|
|
func TestLocal_StatePaths(t *testing.T) {
|
2018-10-31 10:45:03 -05:00
|
|
|
b := New()
|
2017-02-28 18:18:16 -06:00
|
|
|
|
|
|
|
// Test the defaults
|
|
|
|
path, out, back := b.StatePaths("")
|
|
|
|
|
|
|
|
if path != DefaultStateFilename {
|
|
|
|
t.Fatalf("expected %q, got %q", DefaultStateFilename, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out != DefaultStateFilename {
|
|
|
|
t.Fatalf("expected %q, got %q", DefaultStateFilename, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
dfltBackup := DefaultStateFilename + DefaultBackupExtension
|
|
|
|
if back != dfltBackup {
|
|
|
|
t.Fatalf("expected %q, got %q", dfltBackup, back)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check with env
|
|
|
|
testEnv := "test_env"
|
|
|
|
path, out, back = b.StatePaths(testEnv)
|
|
|
|
|
2017-05-30 19:13:43 -05:00
|
|
|
expectedPath := filepath.Join(DefaultWorkspaceDir, testEnv, DefaultStateFilename)
|
2017-02-28 18:18:16 -06:00
|
|
|
expectedOut := expectedPath
|
|
|
|
expectedBackup := expectedPath + DefaultBackupExtension
|
|
|
|
|
|
|
|
if path != expectedPath {
|
|
|
|
t.Fatalf("expected %q, got %q", expectedPath, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out != expectedOut {
|
|
|
|
t.Fatalf("expected %q, got %q", expectedOut, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if back != expectedBackup {
|
|
|
|
t.Fatalf("expected %q, got %q", expectedBackup, back)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-21 18:07:27 -06:00
|
|
|
func TestLocal_addAndRemoveStates(t *testing.T) {
|
|
|
|
defer testTmpDir(t)()
|
|
|
|
dflt := backend.DefaultStateName
|
|
|
|
expectedStates := []string{dflt}
|
|
|
|
|
2018-10-31 10:45:03 -05:00
|
|
|
b := New()
|
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 16:24:45 -05:00
|
|
|
states, err := b.Workspaces()
|
2017-02-21 18:07:27 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(states, expectedStates) {
|
2017-02-22 13:53:43 -06:00
|
|
|
t.Fatalf("expected []string{%q}, got %q", dflt, states)
|
2017-02-21 18:07:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedA := "test_A"
|
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 16:24:45 -05:00
|
|
|
if _, err := b.StateMgr(expectedA); err != nil {
|
2017-02-21 18:07:27 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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 16:24:45 -05:00
|
|
|
states, err = b.Workspaces()
|
2017-02-22 13:53:43 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-21 18:07:27 -06:00
|
|
|
|
|
|
|
expectedStates = append(expectedStates, expectedA)
|
|
|
|
if !reflect.DeepEqual(states, expectedStates) {
|
|
|
|
t.Fatalf("expected %q, got %q", expectedStates, states)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedB := "test_B"
|
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 16:24:45 -05:00
|
|
|
if _, err := b.StateMgr(expectedB); err != nil {
|
2017-02-21 18:07:27 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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 16:24:45 -05:00
|
|
|
states, err = b.Workspaces()
|
2017-02-22 13:53:43 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-21 18:07:27 -06:00
|
|
|
|
|
|
|
expectedStates = append(expectedStates, expectedB)
|
|
|
|
if !reflect.DeepEqual(states, expectedStates) {
|
|
|
|
t.Fatalf("expected %q, got %q", expectedStates, 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 16:24:45 -05:00
|
|
|
if err := b.DeleteWorkspace(expectedA); err != nil {
|
2017-02-21 18:07:27 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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 16:24:45 -05:00
|
|
|
states, err = b.Workspaces()
|
2017-02-22 13:53:43 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-21 18:07:27 -06:00
|
|
|
|
|
|
|
expectedStates = []string{dflt, expectedB}
|
|
|
|
if !reflect.DeepEqual(states, expectedStates) {
|
|
|
|
t.Fatalf("expected %q, got %q", expectedStates, 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 16:24:45 -05:00
|
|
|
if err := b.DeleteWorkspace(expectedB); err != nil {
|
2017-02-21 18:07:27 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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 16:24:45 -05:00
|
|
|
states, err = b.Workspaces()
|
2017-02-22 13:53:43 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-21 18:07:27 -06:00
|
|
|
|
|
|
|
expectedStates = []string{dflt}
|
|
|
|
if !reflect.DeepEqual(states, expectedStates) {
|
|
|
|
t.Fatalf("expected %q, got %q", expectedStates, 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 16:24:45 -05:00
|
|
|
if err := b.DeleteWorkspace(dflt); err == nil {
|
2017-02-21 18:07:27 -06:00
|
|
|
t.Fatal("expected error deleting default state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 18:18:16 -06:00
|
|
|
// a local backend which returns sentinel errors for NamedState methods to
|
2017-02-27 15:43:31 -06:00
|
|
|
// verify it's being called.
|
|
|
|
type testDelegateBackend struct {
|
|
|
|
*Local
|
2017-04-21 17:57:39 -05:00
|
|
|
|
|
|
|
// return a sentinel error on these calls
|
|
|
|
stateErr bool
|
|
|
|
statesErr bool
|
|
|
|
deleteErr bool
|
2017-02-27 15:43:31 -06:00
|
|
|
}
|
2017-02-22 13:53:43 -06:00
|
|
|
|
core: Functional-style API for terraform.Context
Previously terraform.Context was built in an unfortunate way where all of
the data was provided up front in terraform.NewContext and then mutated
directly by subsequent operations. That made the data flow hard to follow,
commonly leading to bugs, and also meant that we were forced to take
various actions too early in terraform.NewContext, rather than waiting
until a more appropriate time during an operation.
This (enormous) commit changes terraform.Context so that its fields are
broadly just unchanging data about the execution context (current
workspace name, available plugins, etc) whereas the main data Terraform
works with arrives via individual method arguments and is returned in
return values.
Specifically, this means that terraform.Context no longer "has-a" config,
state, and "planned changes", instead holding on to those only temporarily
during an operation. The caller is responsible for propagating the outcome
of one step into the next step so that the data flow between operations is
actually visible.
However, since that's a change to the main entry points in the "terraform"
package, this commit also touches every file in the codebase which
interacted with those APIs. Most of the noise here is in updating tests
to take the same actions using the new API style, but this also affects
the main-code callers in the backends and in the command package.
My goal here was to refactor without changing observable behavior, but in
practice there are a couple externally-visible behavior variations here
that seemed okay in service of the broader goal:
- The "terraform graph" command is no longer hooked directly into the
core graph builders, because that's no longer part of the public API.
However, I did include a couple new Context functions whose contract
is to produce a UI-oriented graph, and _for now_ those continue to
return the physical graph we use for those operations. There's no
exported API for generating the "validate" and "eval" graphs, because
neither is particularly interesting in its own right, and so
"terraform graph" no longer supports those graph types.
- terraform.NewContext no longer has the responsibility for collecting
all of the provider schemas up front. Instead, we wait until we need
them. However, that means that some of our error messages now have a
slightly different shape due to unwinding through a differently-shaped
call stack. As of this commit we also end up reloading the schemas
multiple times in some cases, which is functionally acceptable but
likely represents a performance regression. I intend to rework this to
use caching, but I'm saving that for a later commit because this one is
big enough already.
The proximal reason for this change is to resolve the chicken/egg problem
whereby there was previously no single point where we could apply "moved"
statements to the previous run state before creating a plan. With this
change in place, we can now do that as part of Context.Plan, prior to
forking the input state into the three separate state artifacts we use
during planning.
However, this is at least the third project in a row where the previous
API design led to piling more functionality into terraform.NewContext and
then working around the incorrect order of operations that produces, so
I intend that by paying the cost/risk of this large diff now we can in
turn reduce the cost/risk of future projects that relate to our main
workflow actions.
2021-08-24 14:06:38 -05:00
|
|
|
var errTestDelegateState = errors.New("state called")
|
|
|
|
var errTestDelegateStates = errors.New("states called")
|
|
|
|
var errTestDelegateDeleteState = errors.New("delete called")
|
2017-02-22 13:53:43 -06:00
|
|
|
|
2018-09-26 17:33:02 -05:00
|
|
|
func (b *testDelegateBackend) StateMgr(name string) (statemgr.Full, error) {
|
2017-04-21 17:57:39 -05:00
|
|
|
if b.stateErr {
|
|
|
|
return nil, errTestDelegateState
|
|
|
|
}
|
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 16:24:45 -05:00
|
|
|
s := statemgr.NewFilesystem("terraform.tfstate")
|
2017-04-21 17:57:39 -05:00
|
|
|
return s, nil
|
2017-02-27 15:43:31 -06:00
|
|
|
}
|
2017-02-22 13:53:43 -06:00
|
|
|
|
2018-09-26 17:33:02 -05:00
|
|
|
func (b *testDelegateBackend) Workspaces() ([]string, error) {
|
2017-04-21 17:57:39 -05:00
|
|
|
if b.statesErr {
|
|
|
|
return nil, errTestDelegateStates
|
|
|
|
}
|
|
|
|
return []string{"default"}, nil
|
2017-02-27 15:43:31 -06:00
|
|
|
}
|
2017-02-22 13:53:43 -06:00
|
|
|
|
2018-09-26 17:33:02 -05:00
|
|
|
func (b *testDelegateBackend) DeleteWorkspace(name string) error {
|
2017-04-21 17:57:39 -05:00
|
|
|
if b.deleteErr {
|
|
|
|
return errTestDelegateDeleteState
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-22 13:53:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify that the MultiState methods are dispatched to the correct Backend.
|
|
|
|
func TestLocal_multiStateBackend(t *testing.T) {
|
2017-02-27 15:43:31 -06:00
|
|
|
// assign a separate backend where we can read the state
|
2018-10-31 10:45:03 -05:00
|
|
|
b := NewWithBackend(&testDelegateBackend{
|
|
|
|
stateErr: true,
|
|
|
|
statesErr: true,
|
|
|
|
deleteErr: true,
|
|
|
|
})
|
2017-02-22 13:53:43 -06: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 16:24:45 -05:00
|
|
|
if _, err := b.StateMgr("test"); err != errTestDelegateState {
|
2017-02-27 15:43:31 -06:00
|
|
|
t.Fatal("expected errTestDelegateState, got:", err)
|
2017-02-22 13:53:43 -06: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 16:24:45 -05:00
|
|
|
if _, err := b.Workspaces(); err != errTestDelegateStates {
|
2017-02-27 15:43:31 -06:00
|
|
|
t.Fatal("expected errTestDelegateStates, got:", err)
|
2017-02-22 13:53:43 -06: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 16:24:45 -05:00
|
|
|
if err := b.DeleteWorkspace("test"); err != errTestDelegateDeleteState {
|
2017-02-27 15:43:31 -06:00
|
|
|
t.Fatal("expected errTestDelegateDeleteState, got:", err)
|
2017-02-22 13:53:43 -06:00
|
|
|
}
|
2017-04-21 17:57:39 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 18:07:27 -06:00
|
|
|
// change into a tmp dir and return a deferable func to change back and cleanup
|
|
|
|
func testTmpDir(t *testing.T) func() {
|
|
|
|
tmp, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
old, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Chdir(tmp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// ignore errors and try to clean up
|
|
|
|
os.Chdir(old)
|
|
|
|
os.RemoveAll(tmp)
|
|
|
|
}
|
|
|
|
}
|