2017-02-16 17:29:19 -06:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-17 14:00:50 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 10:42:17 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
|
|
|
"github.com/hashicorp/terraform/internal/backend/local"
|
|
|
|
"github.com/hashicorp/terraform/internal/backend/remote-state/inmem"
|
2021-05-17 14:43:35 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2017-02-16 17:29:19 -06:00
|
|
|
"github.com/mitchellh/cli"
|
2020-11-18 12:41:33 -06:00
|
|
|
|
|
|
|
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
|
2017-02-16 17:29:19 -06:00
|
|
|
)
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
func TestWorkspace_createAndChange(t *testing.T) {
|
2017-02-16 17:29:19 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2017-02-16 17:29:19 -06:00
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
newCmd := &WorkspaceNewCommand{}
|
2017-02-16 17:29:19 -06:00
|
|
|
|
2020-06-16 11:23:15 -05:00
|
|
|
current, _ := newCmd.Workspace()
|
2017-02-16 17:29:19 -06:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-30 17:06:13 -05:00
|
|
|
t.Fatal("current workspace should be 'default'")
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
|
2017-02-23 12:13:28 -06:00
|
|
|
args := []string{"test"}
|
2017-02-16 17:29:19 -06:00
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
|
|
|
newCmd.Meta = Meta{Ui: ui, View: view}
|
2017-02-23 12:13:28 -06:00
|
|
|
if code := newCmd.Run(args); code != 0 {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:23:15 -05:00
|
|
|
current, _ = newCmd.Workspace()
|
2017-02-16 17:29:19 -06:00
|
|
|
if current != "test" {
|
2017-05-30 17:06:13 -05:00
|
|
|
t.Fatalf("current workspace should be 'test', got %q", current)
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
selCmd := &WorkspaceSelectCommand{}
|
2017-02-16 17:29:19 -06:00
|
|
|
args = []string{backend.DefaultStateName}
|
|
|
|
ui = new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
selCmd.Meta = Meta{Ui: ui, View: view}
|
2017-02-23 12:13:28 -06:00
|
|
|
if code := selCmd.Run(args); code != 0 {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:23:15 -05:00
|
|
|
current, _ = newCmd.Workspace()
|
2017-02-16 17:29:19 -06:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-30 17:06:13 -05:00
|
|
|
t.Fatal("current workspace should be 'default'")
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// Create some workspaces and test the list output.
|
2017-02-16 17:29:19 -06:00
|
|
|
// This also ensures we switch to the correct env after each call
|
2017-05-30 17:06:13 -05:00
|
|
|
func TestWorkspace_createAndList(t *testing.T) {
|
2017-02-16 17:29:19 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2017-02-16 17:29:19 -06:00
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-03-03 17:19:56 -06: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-16 17:29:19 -06:00
|
|
|
envs := []string{"test_a", "test_b", "test_c"}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// create multiple workspaces
|
2017-02-16 17:29:19 -06:00
|
|
|
for _, env := range envs {
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-07-06 12:49:26 -05:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
2021-02-16 06:19:22 -06:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-07-06 12:49:26 -05:00
|
|
|
}
|
2017-02-23 12:13:28 -06:00
|
|
|
if code := newCmd.Run([]string{env}); code != 0 {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
listCmd := &WorkspaceListCommand{}
|
2017-02-16 17:29:19 -06:00
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
|
|
|
listCmd.Meta = Meta{Ui: ui, View: view}
|
2017-02-16 17:29:19 -06:00
|
|
|
|
2017-02-23 12:13:28 -06:00
|
|
|
if code := listCmd.Run(nil); code != 0 {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
2017-02-23 12:13:28 -06:00
|
|
|
expected := "default\n test_a\n test_b\n* test_c"
|
|
|
|
|
2017-02-16 17:29:19 -06:00
|
|
|
if actual != expected {
|
2017-07-05 16:35:46 -05: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
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2017-07-05 16:35:46 -05:00
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
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)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
|
|
|
showCmd.Meta = Meta{Ui: ui, View: view}
|
2017-07-05 16:35:46 -05:00
|
|
|
|
|
|
|
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)
|
2021-02-16 06:19:22 -06:00
|
|
|
newCmd.Meta = Meta{Ui: ui, View: view}
|
2017-07-05 16:35:46 -05:00
|
|
|
if code := newCmd.Run(env); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
selCmd := &WorkspaceSelectCommand{}
|
|
|
|
ui = new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
selCmd.Meta = Meta{Ui: ui, View: view}
|
2017-07-05 16:35:46 -05:00
|
|
|
if code := selCmd.Run(env); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
showCmd = &WorkspaceShowCommand{}
|
|
|
|
ui = new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
showCmd.Meta = Meta{Ui: ui, View: view}
|
2017-07-05 16:35:46 -05:00
|
|
|
|
|
|
|
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-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:16:09 -05:00
|
|
|
// Don't allow names that aren't URL safe
|
2017-05-30 17:06:13 -05:00
|
|
|
func TestWorkspace_createInvalid(t *testing.T) {
|
2017-03-27 15:16:09 -05:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2017-03-27 15:16:09 -05:00
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
envs := []string{"test_a*", "test_b/foo", "../../../test_c", "好_d"}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// create multiple workspaces
|
2017-03-27 15:16:09 -05:00
|
|
|
for _, env := range envs {
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-07-06 12:49:26 -05:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
2021-02-16 06:19:22 -06:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-07-06 12:49:26 -05:00
|
|
|
}
|
2017-03-27 15:16:09 -05:00
|
|
|
if code := newCmd.Run([]string{env}); code == 0 {
|
|
|
|
t.Fatalf("expected failure: \n%s", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// list workspaces to make sure none were created
|
|
|
|
listCmd := &WorkspaceListCommand{}
|
2017-03-27 15:16:09 -05:00
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
|
|
|
listCmd.Meta = Meta{Ui: ui, View: view}
|
2017-03-27 15:16:09 -05:00
|
|
|
|
|
|
|
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-30 17:06:13 -05:00
|
|
|
func TestWorkspace_createWithState(t *testing.T) {
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2020-10-07 11:48:25 -05:00
|
|
|
testCopyDir(t, testFixturePath("inmem-backend"), td)
|
2017-02-16 17:29:19 -06:00
|
|
|
defer testChdir(t, td)()
|
2017-08-01 15:41:34 -05:00
|
|
|
defer inmem.Reset()
|
|
|
|
|
|
|
|
// init the backend
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-08-01 15:41:34 -05:00
|
|
|
initCmd := &InitCommand{
|
2021-02-16 06:19:22 -06:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-08-01 15:41:34 -05:00
|
|
|
}
|
|
|
|
if code := initCmd.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
2017-02-16 17:29:19 -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
|
|
|
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-16 17:29:19 -06:00
|
|
|
},
|
2020-02-13 14:32:58 -06:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 07:33:49 -05:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 13:19:52 -05:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 14:32:58 -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
|
|
|
)
|
|
|
|
})
|
2017-02-16 17:29:19 -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
|
|
|
err := statemgr.NewFilesystem("test.tfstate").WriteState(originalState)
|
2017-02-16 17:29:19 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-01 17:13:04 -05:00
|
|
|
workspace := "test_workspace"
|
|
|
|
|
|
|
|
args := []string{"-state", "test.tfstate", workspace}
|
2017-08-01 15:41:34 -05:00
|
|
|
ui = new(cli.MockUi)
|
2017-05-30 17:06:13 -05:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
2021-02-16 06:19:22 -06:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
2017-02-23 12:13:28 -06:00
|
|
|
if code := newCmd.Run(args); code != 0 {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-05-30 19:13:43 -05:00
|
|
|
newPath := filepath.Join(local.DefaultWorkspaceDir, "test", DefaultStateFilename)
|
2020-08-11 10:43:01 -05:00
|
|
|
envState := statemgr.NewFilesystem(newPath)
|
2017-02-16 17:29:19 -06:00
|
|
|
err = envState.RefreshState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-01 17:13:04 -05: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 16:24:45 -05:00
|
|
|
sMgr, err := b.StateMgr(workspace)
|
2017-08-01 17:13:04 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := sMgr.State()
|
|
|
|
|
2018-11-07 12:49:10 -06:00
|
|
|
if got, want := newState.String(), originalState.String(); got != want {
|
|
|
|
t.Fatalf("states not equal\ngot: %s\nwant: %s", got, want)
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
func TestWorkspace_delete(t *testing.T) {
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2017-02-16 17:29:19 -06:00
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// create the workspace directories
|
2017-05-30 19:13:43 -05:00
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultWorkspaceDir, "test"), 0755); err != nil {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// create the workspace file
|
2017-02-16 17:29:19 -06:00
|
|
|
if err := os.MkdirAll(DefaultDataDir, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-30 19:13:43 -05:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(DefaultDataDir, local.DefaultWorkspaceFile), []byte("test"), 0644); err != nil {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 12:13:03 -06:00
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-05-30 17:06:13 -05:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2021-02-16 06:19:22 -06:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
|
2020-06-16 11:23:15 -05:00
|
|
|
current, _ := delCmd.Workspace()
|
2017-02-16 17:29:19 -06:00
|
|
|
if current != "test" {
|
2017-05-30 17:06:13 -05:00
|
|
|
t.Fatal("wrong workspace:", current)
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// we can't delete our current workspace
|
2017-02-23 12:13:28 -06:00
|
|
|
args := []string{"test"}
|
2017-02-28 12:13:03 -06:00
|
|
|
if code := delCmd.Run(args); code == 0 {
|
2017-05-30 17:06:13 -05:00
|
|
|
t.Fatal("expected error deleting current workspace")
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
|
2017-02-28 12:13:03 -06:00
|
|
|
// change back to default
|
2017-05-30 19:13:43 -05:00
|
|
|
if err := delCmd.SetWorkspace(backend.DefaultStateName); err != nil {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 12:13:03 -06:00
|
|
|
// try the delete again
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
delCmd.Meta.Ui = ui
|
|
|
|
if code := delCmd.Run(args); code != 0 {
|
2017-05-30 17:06:13 -05:00
|
|
|
t.Fatalf("error deleting workspace: %s", ui.ErrorWriter)
|
2017-02-28 12:13:03 -06:00
|
|
|
}
|
|
|
|
|
2020-06-16 11:23:15 -05:00
|
|
|
current, _ = delCmd.Workspace()
|
2017-02-16 17:29:19 -06:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-30 17:06:13 -05:00
|
|
|
t.Fatalf("wrong workspace: %q", current)
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
|
|
|
}
|
2020-06-18 09:03:30 -05:00
|
|
|
|
|
|
|
func TestWorkspace_deleteInvalid(t *testing.T) {
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2020-06-18 09:03:30 -05:00
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// choose an invalid workspace name
|
|
|
|
workspace := "test workspace"
|
|
|
|
path := filepath.Join(local.DefaultWorkspaceDir, workspace)
|
|
|
|
|
|
|
|
// create the workspace directories
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2020-06-18 09:03:30 -05:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2021-02-16 06:19:22 -06:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2020-06-18 09:03:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete the workspace
|
|
|
|
if code := delCmd.Run([]string{workspace}); code != 0 {
|
|
|
|
t.Fatalf("error deleting workspace: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
t.Fatalf("should have deleted workspace, but %s still exists", path)
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("unexpected error for workspace path: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
func TestWorkspace_deleteWithState(t *testing.T) {
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2017-02-16 17:29:19 -06:00
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
// create the workspace directories
|
2017-05-30 19:13:43 -05:00
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultWorkspaceDir, "test"), 0755); err != nil {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a non-empty state
|
2020-11-18 12:41:33 -06:00
|
|
|
originalState := &legacy.State{
|
|
|
|
Modules: []*legacy.ModuleState{
|
2021-10-13 14:21:23 -05:00
|
|
|
{
|
2017-02-16 17:29:19 -06:00
|
|
|
Path: []string{"root"},
|
2020-11-18 12:41:33 -06:00
|
|
|
Resources: map[string]*legacy.ResourceState{
|
2021-10-13 14:21:23 -05:00
|
|
|
"test_instance.foo": {
|
2017-02-16 17:29:19 -06:00
|
|
|
Type: "test_instance",
|
2020-11-18 12:41:33 -06:00
|
|
|
Primary: &legacy.InstanceState{
|
2017-02-16 17:29:19 -06:00
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-11 10:43:01 -05:00
|
|
|
f, err := os.Create(filepath.Join(local.DefaultWorkspaceDir, "test", "terraform.tfstate"))
|
2017-02-16 17:29:19 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-11 10:43:01 -05:00
|
|
|
defer f.Close()
|
2020-11-18 12:41:33 -06:00
|
|
|
if err := legacy.WriteState(originalState, f); err != nil {
|
2020-08-11 10:43:01 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-16 17:29:19 -06:00
|
|
|
|
2021-10-13 14:21:23 -05:00
|
|
|
ui := cli.NewMockUi()
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-05-30 17:06:13 -05:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2021-02-16 06:19:22 -06:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-02-16 17:29:19 -06:00
|
|
|
}
|
2017-02-23 12:13:28 -06:00
|
|
|
args := []string{"test"}
|
|
|
|
if code := delCmd.Run(args); code == 0 {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatalf("expected failure without -force.\noutput: %s", ui.OutputWriter)
|
|
|
|
}
|
2021-10-13 14:21:23 -05:00
|
|
|
gotStderr := ui.ErrorWriter.String()
|
|
|
|
if want, got := `Workspace "test" is currently tracking the following resource instances`, gotStderr; !strings.Contains(got, want) {
|
|
|
|
t.Errorf("missing expected error message\nwant substring: %s\ngot:\n%s", want, got)
|
|
|
|
}
|
|
|
|
if want, got := `- test_instance.foo`, gotStderr; !strings.Contains(got, want) {
|
|
|
|
t.Errorf("error message doesn't mention the remaining instance\nwant substring: %s\ngot:\n%s", want, got)
|
|
|
|
}
|
2017-02-16 17:29:19 -06:00
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
2017-02-23 12:13:28 -06:00
|
|
|
delCmd.Meta.Ui = ui
|
2017-02-16 17:29:19 -06:00
|
|
|
|
2017-02-23 12:13:28 -06:00
|
|
|
args = []string{"-force", "test"}
|
|
|
|
if code := delCmd.Run(args); code != 0 {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatalf("failure: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-05-30 19:13:43 -05:00
|
|
|
if _, err := os.Stat(filepath.Join(local.DefaultWorkspaceDir, "test")); !os.IsNotExist(err) {
|
2017-02-16 17:29:19 -06:00
|
|
|
t.Fatal("env 'test' still exists!")
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 08:14:52 -05:00
|
|
|
|
|
|
|
func TestWorkspace_selectWithOrCreate(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := t.TempDir()
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
selectCmd := &WorkspaceSelectCommand{}
|
|
|
|
|
|
|
|
current, _ := selectCmd.Workspace()
|
|
|
|
if current != backend.DefaultStateName {
|
|
|
|
t.Fatal("current workspace should be 'default'")
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-or-create", "test"}
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
view, _ := testView(t)
|
|
|
|
selectCmd.Meta = Meta{Ui: ui, View: view}
|
|
|
|
if code := selectCmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
current, _ = selectCmd.Workspace()
|
|
|
|
if current != "test" {
|
|
|
|
t.Fatalf("current workspace should be 'test', got %q", current)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|