2017-02-17 00:29:19 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
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/addrs"
|
2017-02-17 00:29:19 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/backend/local"
|
2017-08-01 22:41:34 +02:00
|
|
|
"github.com/hashicorp/terraform/backend/remote-state/inmem"
|
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2017-02-17 00:29:19 +01:00
|
|
|
"github.com/hashicorp/terraform/state"
|
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"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-02-17 00:29:19 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
func TestWorkspace_createAndChange(t *testing.T) {
|
2017-02-17 00:29:19 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
newCmd := &WorkspaceNewCommand{}
|
2017-02-17 00:29:19 +01:00
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
current := newCmd.Workspace()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-31 00:06:13 +02:00
|
|
|
t.Fatal("current workspace should be 'default'")
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
args := []string{"test"}
|
2017-02-17 00:29:19 +01:00
|
|
|
ui := new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
newCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := newCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
current = newCmd.Workspace()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != "test" {
|
2017-05-31 00:06:13 +02:00
|
|
|
t.Fatalf("current workspace should be 'test', got %q", current)
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
selCmd := &WorkspaceSelectCommand{}
|
2017-02-17 00:29:19 +01:00
|
|
|
args = []string{backend.DefaultStateName}
|
|
|
|
ui = new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
selCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := selCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
current = newCmd.Workspace()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-31 00:06:13 +02:00
|
|
|
t.Fatal("current workspace should be 'default'")
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// Create some workspaces and test the list output.
|
2017-02-17 00:29:19 +01:00
|
|
|
// This also ensures we switch to the correct env after each call
|
2017-05-31 00:06:13 +02:00
|
|
|
func TestWorkspace_createAndList(t *testing.T) {
|
2017-02-17 00:29:19 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-03-04 00:19:56 +01:00
|
|
|
// make sure a vars file doesn't interfere
|
|
|
|
err := ioutil.WriteFile(
|
|
|
|
DefaultVarsFilename,
|
|
|
|
[]byte(`foo = "bar"`),
|
|
|
|
0644,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-17 00:29:19 +01:00
|
|
|
envs := []string{"test_a", "test_b", "test_c"}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// create multiple workspaces
|
2017-02-17 00:29:19 +01:00
|
|
|
for _, env := range envs {
|
|
|
|
ui := new(cli.MockUi)
|
2017-07-06 19:49:26 +02:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
2017-02-23 19:13:28 +01:00
|
|
|
if code := newCmd.Run([]string{env}); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
listCmd := &WorkspaceListCommand{}
|
2017-02-17 00:29:19 +01:00
|
|
|
ui := new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
listCmd.Meta = Meta{Ui: ui}
|
2017-02-17 00:29:19 +01:00
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
if code := listCmd.Run(nil); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
2017-02-23 19:13:28 +01:00
|
|
|
expected := "default\n test_a\n test_b\n* test_c"
|
|
|
|
|
2017-02-17 00:29:19 +01:00
|
|
|
if actual != expected {
|
2017-07-05 23:35:46 +02:00
|
|
|
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create some workspaces and test the show output.
|
|
|
|
func TestWorkspace_createAndShow(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// make sure a vars file doesn't interfere
|
|
|
|
err := ioutil.WriteFile(
|
|
|
|
DefaultVarsFilename,
|
|
|
|
[]byte(`foo = "bar"`),
|
|
|
|
0644,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure current workspace show outputs "default"
|
|
|
|
showCmd := &WorkspaceShowCommand{}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
showCmd.Meta = Meta{Ui: ui}
|
|
|
|
|
|
|
|
if code := showCmd.Run(nil); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
expected := "default"
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
newCmd := &WorkspaceNewCommand{}
|
|
|
|
|
|
|
|
env := []string{"test_a"}
|
|
|
|
|
|
|
|
// create test_a workspace
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
newCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := newCmd.Run(env); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
selCmd := &WorkspaceSelectCommand{}
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
selCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := selCmd.Run(env); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
showCmd = &WorkspaceShowCommand{}
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
showCmd.Meta = Meta{Ui: ui}
|
|
|
|
|
|
|
|
if code := showCmd.Run(nil); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
expected = "test_a"
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 22:16:09 +02:00
|
|
|
// Don't allow names that aren't URL safe
|
2017-05-31 00:06:13 +02:00
|
|
|
func TestWorkspace_createInvalid(t *testing.T) {
|
2017-03-27 22:16:09 +02:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
envs := []string{"test_a*", "test_b/foo", "../../../test_c", "好_d"}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// create multiple workspaces
|
2017-03-27 22:16:09 +02:00
|
|
|
for _, env := range envs {
|
|
|
|
ui := new(cli.MockUi)
|
2017-07-06 19:49:26 +02:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
2017-03-27 22:16:09 +02:00
|
|
|
if code := newCmd.Run([]string{env}); code == 0 {
|
|
|
|
t.Fatalf("expected failure: \n%s", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// list workspaces to make sure none were created
|
|
|
|
listCmd := &WorkspaceListCommand{}
|
2017-03-27 22:16:09 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
listCmd.Meta = Meta{Ui: ui}
|
|
|
|
|
|
|
|
if code := listCmd.Run(nil); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
expected := "* default"
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
func TestWorkspace_createWithState(t *testing.T) {
|
2017-02-17 00:29:19 +01:00
|
|
|
td := tempDir(t)
|
2017-08-01 22:41:34 +02:00
|
|
|
copy.CopyDir(testFixturePath("inmem-backend"), td)
|
2017-02-17 00:29:19 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-08-01 22:41:34 +02:00
|
|
|
defer inmem.Reset()
|
|
|
|
|
|
|
|
// init the backend
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
initCmd := &InitCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
if code := initCmd.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
2017-02-17 00:29:19 +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
|
|
|
originalState := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-02-17 00:29:19 +01:00
|
|
|
},
|
Initial steps towards AbsProviderConfig/LocalProviderConfig separation (#23978)
* Introduce "Local" terminology for non-absolute provider config addresses
In a future change AbsProviderConfig and LocalProviderConfig are going to
become two entirely distinct types, rather than Abs embedding Local as
written here. This naming change is in preparation for that subsequent
work, which will also include introducing a new "ProviderConfig" type
that is an interface that AbsProviderConfig and LocalProviderConfig both
implement.
This is intended to be largely just a naming change to get started, so
we can deal with all of the messy renaming. However, this did also require
a slight change in modeling where the Resource.DefaultProviderConfig
method has become Resource.DefaultProvider returning a Provider address
directly, because this method doesn't have enough information to construct
a true and accurate LocalProviderConfig -- it would need to refer to the
configuration to know what this module is calling the provider it has
selected.
In order to leave a trail to follow for subsequent work, all of the
changes here are intended to ensure that remaining work will become
obvious via compile-time errors when all of the following changes happen:
- The concept of "legacy" provider addresses is removed from the addrs
package, including removing addrs.NewLegacyProvider and
addrs.Provider.LegacyString.
- addrs.AbsProviderConfig stops having addrs.LocalProviderConfig embedded
in it and has an addrs.Provider and a string alias directly instead.
- The provider-schema-handling parts of Terraform core are updated to
work with addrs.Provider to identify providers, rather than legacy
strings.
In particular, there are still several codepaths here making legacy
provider address assumptions (in order to limit the scope of this change)
but I've made sure each one is doing something that relies on at least
one of the above changes not having been made yet.
* addrs: ProviderConfig interface
In a (very) few special situations in the main "terraform" package we need
to make runtime decisions about whether a provider config is absolute
or local.
We currently do that by exploiting the fact that AbsProviderConfig has
LocalProviderConfig nested inside of it and so in the local case we can
just ignore the wrapping AbsProviderConfig and use the embedded value.
In a future change we'll be moving away from that embedding and making
these two types distinct in order to represent that mapping between them
requires consulting a lookup table in the configuration, and so here we
introduce a new interface type ProviderConfig that can represent either
AbsProviderConfig or LocalProviderConfig decided dynamically at runtime.
This also includes the Config.ResolveAbsProviderAddr method that will
eventually be responsible for that local-to-absolute translation, so
that callers with access to the configuration can normalize to an
addrs.AbsProviderConfig given a non-nil addrs.ProviderConfig. That's
currently unused because existing callers are still relying on the
simplistic structural transform, but we'll switch them over in a later
commit.
* rename LocalType to LocalName
Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
2020-01-31 14:23:07 +01:00
|
|
|
addrs.LocalProviderConfig{LocalName: "test"}.Absolute(addrs.RootModuleInstance),
|
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
|
|
|
)
|
|
|
|
})
|
2017-02-17 00:29:19 +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
|
|
|
err := statemgr.NewFilesystem("test.tfstate").WriteState(originalState)
|
2017-02-17 00:29:19 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-02 00:13:04 +02:00
|
|
|
workspace := "test_workspace"
|
|
|
|
|
|
|
|
args := []string{"-state", "test.tfstate", workspace}
|
2017-08-01 22:41:34 +02:00
|
|
|
ui = new(cli.MockUi)
|
2017-05-31 00:06:13 +02:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
2017-02-17 00:29:19 +01:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
2017-02-23 19:13:28 +01:00
|
|
|
if code := newCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
newPath := filepath.Join(local.DefaultWorkspaceDir, "test", DefaultStateFilename)
|
2017-02-17 00:29:19 +01:00
|
|
|
envState := state.LocalState{Path: newPath}
|
|
|
|
err = envState.RefreshState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-02 00:13:04 +02:00
|
|
|
b := backend.TestBackendConfig(t, inmem.New(), nil)
|
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
|
|
|
sMgr, err := b.StateMgr(workspace)
|
2017-08-02 00:13:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := sMgr.State()
|
|
|
|
|
2018-11-07 19:49:10 +01:00
|
|
|
if got, want := newState.String(), originalState.String(); got != want {
|
|
|
|
t.Fatalf("states not equal\ngot: %s\nwant: %s", got, want)
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
func TestWorkspace_delete(t *testing.T) {
|
2017-02-17 00:29:19 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// create the workspace directories
|
2017-05-31 02:13:43 +02:00
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultWorkspaceDir, "test"), 0755); err != nil {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// create the workspace file
|
2017-02-17 00:29:19 +01:00
|
|
|
if err := os.MkdirAll(DefaultDataDir, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-31 02:13:43 +02:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(DefaultDataDir, local.DefaultWorkspaceFile), []byte("test"), 0644); err != nil {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
ui := new(cli.MockUi)
|
2017-05-31 00:06:13 +02:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2017-02-28 19:13:03 +01:00
|
|
|
Meta: Meta{Ui: ui},
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
current := delCmd.Workspace()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != "test" {
|
2017-05-31 00:06:13 +02:00
|
|
|
t.Fatal("wrong workspace:", current)
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// we can't delete our current workspace
|
2017-02-23 19:13:28 +01:00
|
|
|
args := []string{"test"}
|
2017-02-28 19:13:03 +01:00
|
|
|
if code := delCmd.Run(args); code == 0 {
|
2017-05-31 00:06:13 +02:00
|
|
|
t.Fatal("expected error deleting current workspace")
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
// change back to default
|
2017-05-31 02:13:43 +02:00
|
|
|
if err := delCmd.SetWorkspace(backend.DefaultStateName); err != nil {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
// try the delete again
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
delCmd.Meta.Ui = ui
|
|
|
|
if code := delCmd.Run(args); code != 0 {
|
2017-05-31 00:06:13 +02:00
|
|
|
t.Fatalf("error deleting workspace: %s", ui.ErrorWriter)
|
2017-02-28 19:13:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
current = delCmd.Workspace()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-31 00:06:13 +02:00
|
|
|
t.Fatalf("wrong workspace: %q", current)
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-31 00:06:13 +02:00
|
|
|
func TestWorkspace_deleteWithState(t *testing.T) {
|
2017-02-17 00:29:19 +01:00
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-05-31 00:06:13 +02:00
|
|
|
// create the workspace directories
|
2017-05-31 02:13:43 +02:00
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultWorkspaceDir, "test"), 0755); err != nil {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a non-empty state
|
|
|
|
originalState := &terraform.State{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
envStatePath := filepath.Join(local.DefaultWorkspaceDir, "test", DefaultStateFilename)
|
2017-02-17 00:29:19 +01:00
|
|
|
err := (&state.LocalState{Path: envStatePath}).WriteState(originalState)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2017-05-31 00:06:13 +02:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2017-02-17 00:29:19 +01:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
2017-02-23 19:13:28 +01:00
|
|
|
args := []string{"test"}
|
|
|
|
if code := delCmd.Run(args); code == 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("expected failure without -force.\noutput: %s", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
delCmd.Meta.Ui = ui
|
2017-02-17 00:29:19 +01:00
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
args = []string{"-force", "test"}
|
|
|
|
if code := delCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("failure: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(local.DefaultWorkspaceDir, "test")); !os.IsNotExist(err) {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatal("env 'test' still exists!")
|
|
|
|
}
|
|
|
|
}
|