2024-02-08 04:48:59 -05:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 11:33:06 -04:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2017-02-16 18:29:19 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-03-02 11:58:11 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/arguments"
|
2026-02-10 08:31:06 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/workdir"
|
2025-04-30 11:28:19 -04:00
|
|
|
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/backend"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/backend/local"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/backend/remote-state/inmem"
|
2024-03-04 09:25:14 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/encryption"
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/states"
|
2026-03-19 11:03:16 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/states/statefile"
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/states/statemgr"
|
2017-02-16 18:29:19 -05:00
|
|
|
)
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
func TestWorkspace_createAndChange(t *testing.T) {
|
2017-02-16 18:29:19 -05:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2026-02-10 08:31:06 -05:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
|
|
|
|
},
|
|
|
|
|
}
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2025-04-30 11:28:19 -04:00
|
|
|
current, _ := newCmd.Workspace(t.Context())
|
2017-02-16 18:29:19 -05:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-30 18:06:13 -04:00
|
|
|
t.Fatal("current workspace should be 'default'")
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-23 13:13:28 -05:00
|
|
|
args := []string{"test"}
|
2026-03-03 05:44:56 -05:00
|
|
|
newCmdView, newCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
newCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: newCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code := newCmd.Run(args)
|
|
|
|
|
newCmdOutput := newCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, newCmdOutput.Stderr())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 11:28:19 -04:00
|
|
|
current, _ = newCmd.Workspace(t.Context())
|
2017-02-16 18:29:19 -05:00
|
|
|
if current != "test" {
|
2017-05-30 18:06:13 -04:00
|
|
|
t.Fatalf("current workspace should be 'test', got %q", current)
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
selCmd := &WorkspaceSelectCommand{}
|
2017-02-16 18:29:19 -05:00
|
|
|
args = []string{backend.DefaultStateName}
|
2026-03-03 05:44:56 -05:00
|
|
|
selectCmdView, selectCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
selCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: selectCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code = selCmd.Run(args)
|
|
|
|
|
selectCmdOutput := selectCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, selectCmdOutput.Stderr())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 11:28:19 -04:00
|
|
|
current, _ = newCmd.Workspace(t.Context())
|
2017-02-16 18:29:19 -05:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-30 18:06:13 -04:00
|
|
|
t.Fatal("current workspace should be 'default'")
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// Create some workspaces and test the list output.
|
2017-02-16 18:29:19 -05:00
|
|
|
// This also ensures we switch to the correct env after each call
|
2017-05-30 18:06:13 -04:00
|
|
|
func TestWorkspace_createAndList(t *testing.T) {
|
2017-02-16 18:29:19 -05:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2017-03-03 18:19:56 -05:00
|
|
|
// make sure a vars file doesn't interfere
|
2023-09-07 12:53:12 -04:00
|
|
|
err := os.WriteFile(
|
2017-03-03 18:19:56 -05:00
|
|
|
DefaultVarsFilename,
|
|
|
|
|
[]byte(`foo = "bar"`),
|
|
|
|
|
0644,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 18:29:19 -05:00
|
|
|
envs := []string{"test_a", "test_b", "test_c"}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// create multiple workspaces
|
2017-02-16 18:29:19 -05:00
|
|
|
for _, env := range envs {
|
2026-03-03 05:44:56 -05:00
|
|
|
newCmdView, newCmdDone := testView(t)
|
2017-07-06 13:49:26 -04:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
2026-02-10 08:31:06 -05:00
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: newCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
},
|
2017-07-06 13:49:26 -04:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code := newCmd.Run([]string{env})
|
|
|
|
|
newCmdOutput := newCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, newCmdOutput.Stderr())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
listCmd := &WorkspaceListCommand{}
|
2026-03-03 05:44:56 -05:00
|
|
|
listCmdView, listCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
listCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: listCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
code := listCmd.Run(nil)
|
|
|
|
|
listCmdOutput := listCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, listCmdOutput.Stderr())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
actual := strings.TrimSpace(listCmdOutput.Stdout())
|
2017-02-23 13:13:28 -05:00
|
|
|
expected := "default\n test_a\n test_b\n* test_c"
|
|
|
|
|
|
2017-02-16 18:29:19 -05:00
|
|
|
if actual != expected {
|
2017-07-05 17:35:46 -04: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 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-07-05 17:35:46 -04:00
|
|
|
|
|
|
|
|
// make sure a vars file doesn't interfere
|
2023-09-07 12:53:12 -04:00
|
|
|
err := os.WriteFile(
|
2017-07-05 17:35:46 -04:00
|
|
|
DefaultVarsFilename,
|
|
|
|
|
[]byte(`foo = "bar"`),
|
|
|
|
|
0644,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make sure current workspace show outputs "default"
|
|
|
|
|
showCmd := &WorkspaceShowCommand{}
|
2026-03-03 05:44:56 -05:00
|
|
|
showCmdView, showCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
showCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: showCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2017-07-05 17:35:46 -04:00
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
code := showCmd.Run(nil)
|
|
|
|
|
showCmdOutput := showCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, showCmdOutput.Stderr())
|
2017-07-05 17:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
actual := strings.TrimSpace(showCmdOutput.Stdout())
|
2017-07-05 17:35:46 -04:00
|
|
|
expected := "default"
|
|
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
|
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newCmd := &WorkspaceNewCommand{}
|
|
|
|
|
|
|
|
|
|
env := []string{"test_a"}
|
|
|
|
|
|
|
|
|
|
// create test_a workspace
|
2026-03-03 05:44:56 -05:00
|
|
|
newCmdView, newCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
newCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: newCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code = newCmd.Run(env)
|
|
|
|
|
newCmdOutput := newCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, newCmdOutput.Stderr())
|
2017-07-05 17:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selCmd := &WorkspaceSelectCommand{}
|
2026-03-03 05:44:56 -05:00
|
|
|
selCmdView, selCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
selCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: selCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code = selCmd.Run(env)
|
|
|
|
|
selCmdOutput := selCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, selCmdOutput.Stderr())
|
2017-07-05 17:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showCmd = &WorkspaceShowCommand{}
|
2026-03-03 05:44:56 -05:00
|
|
|
showCmdView, showCmdDone = testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
showCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: showCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2017-07-05 17:35:46 -04:00
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
code = showCmd.Run(nil)
|
|
|
|
|
showCmdOutput = showCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, showCmdOutput.Stderr())
|
2017-07-05 17:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
actual = strings.TrimSpace(showCmdOutput.Stdout())
|
2017-07-05 17:35:46 -04:00
|
|
|
expected = "test_a"
|
|
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
|
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 16:16:09 -04:00
|
|
|
// Don't allow names that aren't URL safe
|
2017-05-30 18:06:13 -04:00
|
|
|
func TestWorkspace_createInvalid(t *testing.T) {
|
2017-03-27 16:16:09 -04:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-03-27 16:16:09 -04:00
|
|
|
|
|
|
|
|
envs := []string{"test_a*", "test_b/foo", "../../../test_c", "好_d"}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// create multiple workspaces
|
2017-03-27 16:16:09 -04:00
|
|
|
for _, env := range envs {
|
2026-03-03 05:44:56 -05:00
|
|
|
view, done := testView(t)
|
2017-07-06 13:49:26 -04:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
2026-02-10 08:31:06 -05:00
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
|
|
|
|
View: view,
|
|
|
|
|
},
|
2017-07-06 13:49:26 -04:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code := newCmd.Run([]string{env})
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatalf("expected failure: \n%s", output.All())
|
2017-03-27 16:16:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// list workspaces to make sure none were created
|
|
|
|
|
listCmd := &WorkspaceListCommand{}
|
2026-03-03 05:44:56 -05:00
|
|
|
listCmdView, listCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
listCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: listCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2017-03-27 16:16:09 -04:00
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
code := listCmd.Run(nil)
|
|
|
|
|
listCmdOutput := listCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, listCmdOutput.Stderr())
|
2017-03-27 16:16:09 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
actual := strings.TrimSpace(listCmdOutput.Stdout())
|
2017-03-27 16:16:09 -04:00
|
|
|
expected := "* default"
|
|
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
|
t.Fatalf("\nexpected: %q\nactual: %q", expected, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
func TestWorkspace_createWithState(t *testing.T) {
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2020-10-07 12:48:25 -04:00
|
|
|
testCopyDir(t, testFixturePath("inmem-backend"), td)
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-08-01 16:41:34 -04:00
|
|
|
defer inmem.Reset()
|
|
|
|
|
|
|
|
|
|
// init the backend
|
2026-03-03 05:44:56 -05:00
|
|
|
initCmdView, initCmdDone := testView(t)
|
2017-08-01 16:41:34 -04:00
|
|
|
initCmd := &InitCommand{
|
2026-02-10 08:31:06 -05:00
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: initCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
},
|
2017-08-01 16:41:34 -04:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code := initCmd.Run(nil)
|
|
|
|
|
initCmdOutput := initCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: \n%s", initCmdOutput.Stderr())
|
2017-08-01 16:41:34 -04:00
|
|
|
}
|
2017-02-16 18:29:19 -05: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 17:24:45 -04: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 18:29:19 -05:00
|
|
|
},
|
2020-02-13 15:32:58 -05:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 08:33:49 -04:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 14:19:52 -04:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 15:32:58 -05:00
|
|
|
},
|
2024-11-05 18:08:23 -05:00
|
|
|
addrs.NoKey,
|
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 17:24:45 -04:00
|
|
|
)
|
|
|
|
|
})
|
2017-02-16 18:29:19 -05:00
|
|
|
|
statemgr+remote: context.Context parameters
This extends statemgr.Persistent, statemgr.Locker and remote.Client to
all expect context.Context parameters, and then updates all of the existing
implementations of those interfaces to support them.
All of the calls to statemgr.Persistent and statemgr.Locker methods outside
of tests are consistently context.TODO() for now, because the caller
landscape of these interfaces has some complications:
1. statemgr.Locker is also used by the clistate package for its state
implementation that was derived from statemgr.Filesystem's predecessor,
even though what clistate manages is not actually "state" in the sense
of package statemgr. The callers of that are not yet ready to provide
real contexts.
In a future commit we'll either need to plumb context through to all of
the clistate callers, or continue the effort to separate statemgr from
clistate by introducing a clistate-specific "locker" API for it
to use instead.
2. We call statemgr.Persistent and statemgr.Locker methods in situations
where the active context might have already been cancelled, and so we'll
need to make sure to ignore cancellation when calling those.
This is mainly limited to PersistState and Unlock, since both need to
be able to complete after a cancellation, but there are various
codepaths that perform a Lock, Refresh, Persist, Unlock sequence and so
it isn't yet clear where is the best place to enforce the invariant that
Persist and Unlock must not be called with a cancelable context. We'll
deal with that more in subsequent commits.
Within the various state manager and remote client implementations the
contexts _are_ wired together as best as possible with how these subsystems
are already laid out, and so once we deal with the problems above and make
callers provide suitable contexts they should be able to reach all of the
leaf API clients that might want to generate OpenTelemetry traces.
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-07 19:57:57 -04:00
|
|
|
err := statemgr.WriteAndPersist(t.Context(), statemgr.NewFilesystem("test.tfstate", encryption.StateEncryptionDisabled()), originalState, nil)
|
2017-02-16 18:29:19 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-01 18:13:04 -04:00
|
|
|
workspace := "test_workspace"
|
|
|
|
|
|
|
|
|
|
args := []string{"-state", "test.tfstate", workspace}
|
2026-03-03 05:44:56 -05:00
|
|
|
newCmdView, newCmdDone := testView(t)
|
2017-05-30 18:06:13 -04:00
|
|
|
newCmd := &WorkspaceNewCommand{
|
2026-02-10 08:31:06 -05:00
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: newCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
},
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code = newCmd.Run(args)
|
|
|
|
|
newCmdOutput := newCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, newCmdOutput.Stderr())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-02 11:58:11 -05:00
|
|
|
newPath := filepath.Join(local.DefaultWorkspaceDir, "test", arguments.DefaultStateFilename)
|
2024-03-04 09:25:14 -05:00
|
|
|
envState := statemgr.NewFilesystem(newPath, encryption.StateEncryptionDisabled())
|
statemgr+remote: context.Context parameters
This extends statemgr.Persistent, statemgr.Locker and remote.Client to
all expect context.Context parameters, and then updates all of the existing
implementations of those interfaces to support them.
All of the calls to statemgr.Persistent and statemgr.Locker methods outside
of tests are consistently context.TODO() for now, because the caller
landscape of these interfaces has some complications:
1. statemgr.Locker is also used by the clistate package for its state
implementation that was derived from statemgr.Filesystem's predecessor,
even though what clistate manages is not actually "state" in the sense
of package statemgr. The callers of that are not yet ready to provide
real contexts.
In a future commit we'll either need to plumb context through to all of
the clistate callers, or continue the effort to separate statemgr from
clistate by introducing a clistate-specific "locker" API for it
to use instead.
2. We call statemgr.Persistent and statemgr.Locker methods in situations
where the active context might have already been cancelled, and so we'll
need to make sure to ignore cancellation when calling those.
This is mainly limited to PersistState and Unlock, since both need to
be able to complete after a cancellation, but there are various
codepaths that perform a Lock, Refresh, Persist, Unlock sequence and so
it isn't yet clear where is the best place to enforce the invariant that
Persist and Unlock must not be called with a cancelable context. We'll
deal with that more in subsequent commits.
Within the various state manager and remote client implementations the
contexts _are_ wired together as best as possible with how these subsystems
are already laid out, and so once we deal with the problems above and make
callers provide suitable contexts they should be able to reach all of the
leaf API clients that might want to generate OpenTelemetry traces.
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-07 19:57:57 -04:00
|
|
|
err = envState.RefreshState(t.Context())
|
2017-02-16 18:29:19 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 09:25:14 -05:00
|
|
|
b := backend.TestBackendConfig(t, inmem.New(encryption.StateEncryptionDisabled()), nil)
|
2025-05-06 16:50:41 -04:00
|
|
|
sMgr, err := b.StateMgr(t.Context(), workspace)
|
2017-08-01 18:13:04 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newState := sMgr.State()
|
|
|
|
|
|
2018-11-07 13:49:10 -05:00
|
|
|
if got, want := newState.String(), originalState.String(); got != want {
|
|
|
|
|
t.Fatalf("states not equal\ngot: %s\nwant: %s", got, want)
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
func TestWorkspace_delete(t *testing.T) {
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// create the workspace directories
|
2017-05-30 20:13:43 -04:00
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultWorkspaceDir, "test"), 0755); err != nil {
|
2017-02-16 18:29:19 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// create the workspace file
|
2026-02-10 08:31:06 -05:00
|
|
|
if err := os.MkdirAll(workdir.DefaultDataDir, 0755); err != nil {
|
2017-02-16 18:29:19 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-02-10 08:31:06 -05:00
|
|
|
if err := os.WriteFile(filepath.Join(workdir.DefaultDataDir, local.DefaultWorkspaceFile), []byte("test"), 0644); err != nil {
|
2017-02-16 18:29:19 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
delCmdView, delCmdDone := testView(t)
|
2017-05-30 18:06:13 -04:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2026-02-10 08:31:06 -05:00
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: delCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
},
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 11:28:19 -04:00
|
|
|
current, _ := delCmd.Workspace(t.Context())
|
2017-02-16 18:29:19 -05:00
|
|
|
if current != "test" {
|
2017-05-30 18:06:13 -04:00
|
|
|
t.Fatal("wrong workspace:", current)
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// we can't delete our current workspace
|
2017-02-23 13:13:28 -05:00
|
|
|
args := []string{"test"}
|
2026-03-03 05:44:56 -05:00
|
|
|
code := delCmd.Run(args)
|
|
|
|
|
delCmdOutput := delCmdDone(t)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatalf("expected error deleting current workspace: %s", delCmdOutput.All())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-28 13:13:03 -05:00
|
|
|
// change back to default
|
2017-05-30 20:13:43 -04:00
|
|
|
if err := delCmd.SetWorkspace(backend.DefaultStateName); err != nil {
|
2017-02-16 18:29:19 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-28 13:13:03 -05:00
|
|
|
// try the delete again
|
2026-03-03 05:44:56 -05:00
|
|
|
delCmdView, delCmdDone = testView(t)
|
|
|
|
|
delCmd.Meta.View = delCmdView
|
|
|
|
|
code = delCmd.Run(args)
|
|
|
|
|
delCmdOutput = delCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("error deleting workspace: %s", delCmdOutput.Stderr())
|
2017-02-28 13:13:03 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 11:28:19 -04:00
|
|
|
current, _ = delCmd.Workspace(t.Context())
|
2017-02-16 18:29:19 -05:00
|
|
|
if current != backend.DefaultStateName {
|
2017-05-30 18:06:13 -04:00
|
|
|
t.Fatalf("wrong workspace: %q", current)
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-18 10:03:30 -04:00
|
|
|
|
|
|
|
|
func TestWorkspace_deleteInvalid(t *testing.T) {
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2020-06-18 10:03:30 -04:00
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
delCmdView, delCmdDone := testView(t)
|
2020-06-18 10:03:30 -04:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2026-02-10 08:31:06 -05:00
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: delCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
},
|
2020-06-18 10:03:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delete the workspace
|
2026-03-03 05:44:56 -05:00
|
|
|
code := delCmd.Run([]string{workspace})
|
|
|
|
|
delCmdOutput := delCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("error deleting workspace: %s", delCmdOutput.Stderr())
|
2020-06-18 10:03:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 18:06:13 -04:00
|
|
|
func TestWorkspace_deleteWithState(t *testing.T) {
|
2022-04-08 12:34:16 -04:00
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2017-05-30 18:06:13 -04:00
|
|
|
// create the workspace directories
|
2017-05-30 20:13:43 -04:00
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultWorkspaceDir, "test"), 0755); err != nil {
|
2017-02-16 18:29:19 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 11:03:16 -04: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 18:29:19 -05:00
|
|
|
},
|
2026-03-19 11:03:16 -04:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
|
},
|
|
|
|
|
addrs.NoKey,
|
|
|
|
|
)
|
|
|
|
|
})
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2020-08-11 11:43:01 -04:00
|
|
|
f, err := os.Create(filepath.Join(local.DefaultWorkspaceDir, "test", "terraform.tfstate"))
|
2017-02-16 18:29:19 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-03-19 11:03:16 -04:00
|
|
|
err = statefile.Write(&statefile.File{
|
|
|
|
|
Serial: 0,
|
|
|
|
|
Lineage: "test-lineage",
|
|
|
|
|
State: originalState,
|
|
|
|
|
}, f, encryption.StateEncryptionDisabled())
|
|
|
|
|
if err != nil {
|
2020-08-11 11:43:01 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2025-09-02 15:07:35 -04:00
|
|
|
f.Close()
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
delCmdView, delCmdDone := testView(t)
|
2017-05-30 18:06:13 -04:00
|
|
|
delCmd := &WorkspaceDeleteCommand{
|
2026-02-10 08:31:06 -05:00
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: delCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
},
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
2017-02-23 13:13:28 -05:00
|
|
|
args := []string{"test"}
|
2026-03-03 05:44:56 -05:00
|
|
|
code := delCmd.Run(args)
|
|
|
|
|
delCmdOutput := delCmdDone(t)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatalf("expected failure without -force.\noutput: %s", delCmdOutput.All())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
gotStderr := delCmdOutput.Stderr()
|
2021-10-13 15:21:23 -04:00
|
|
|
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 18:29:19 -05:00
|
|
|
|
2026-03-03 05:44:56 -05:00
|
|
|
delCmdView, delCmdDone = testView(t)
|
|
|
|
|
delCmd.Meta.View = delCmdView
|
2017-02-16 18:29:19 -05:00
|
|
|
|
2017-02-23 13:13:28 -05:00
|
|
|
args = []string{"-force", "test"}
|
2026-03-03 05:44:56 -05:00
|
|
|
code = delCmd.Run(args)
|
|
|
|
|
delCmdOutput = delCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("failure: %s", delCmdOutput.Stderr())
|
2017-02-16 18:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 20:13:43 -04:00
|
|
|
if _, err := os.Stat(filepath.Join(local.DefaultWorkspaceDir, "test")); !os.IsNotExist(err) {
|
2017-02-16 18:29:19 -05:00
|
|
|
t.Fatal("env 'test' still exists!")
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-12 09:14:52 -04:00
|
|
|
|
|
|
|
|
func TestWorkspace_selectWithOrCreate(t *testing.T) {
|
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
|
td := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(td)
|
2022-08-12 09:14:52 -04:00
|
|
|
|
2026-02-10 08:31:06 -05:00
|
|
|
selectCmd := &WorkspaceSelectCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-08-12 09:14:52 -04:00
|
|
|
|
2025-04-30 11:28:19 -04:00
|
|
|
current, _ := selectCmd.Workspace(t.Context())
|
2022-08-12 09:14:52 -04:00
|
|
|
if current != backend.DefaultStateName {
|
|
|
|
|
t.Fatal("current workspace should be 'default'")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []string{"-or-create", "test"}
|
2026-03-03 05:44:56 -05:00
|
|
|
selectCmdView, selectCmdDone := testView(t)
|
2026-02-10 08:31:06 -05:00
|
|
|
selectCmd.Meta = Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
2026-03-03 05:44:56 -05:00
|
|
|
View: selectCmdView,
|
2026-02-10 08:31:06 -05:00
|
|
|
}
|
2026-03-03 05:44:56 -05:00
|
|
|
code := selectCmd.Run(args)
|
|
|
|
|
selectCmdOutput := selectCmdDone(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, selectCmdOutput.Stderr())
|
2022-08-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 11:28:19 -04:00
|
|
|
current, _ = selectCmd.Workspace(t.Context())
|
2022-08-12 09:14:52 -04:00
|
|
|
if current != "test" {
|
|
|
|
|
t.Fatalf("current workspace should be 'test', got %q", current)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|