Fix taint and untaint commands when in a workspace (#22467)
* Fix taint and untaint commands when in a workspace Fixes #22157. Removes DefaultStateFilepath as the default for the -state flag, allowing workspaces to be used properly. * update test with modern state types Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
This commit is contained in:
parent
c05127c289
commit
529ee04269
|
@ -42,6 +42,7 @@ import (
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
|
||||||
backendInit "github.com/hashicorp/terraform/backend/init"
|
backendInit "github.com/hashicorp/terraform/backend/init"
|
||||||
|
backendLocal "github.com/hashicorp/terraform/backend/local"
|
||||||
)
|
)
|
||||||
|
|
||||||
// These are the directories for our test data and fixtures.
|
// These are the directories for our test data and fixtures.
|
||||||
|
@ -388,6 +389,31 @@ func testStateFileDefault(t *testing.T, s *terraform.State) string {
|
||||||
return DefaultStateFilename
|
return DefaultStateFilename
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// testStateFileWorkspaceDefault writes the state out to the default statefile
|
||||||
|
// for the given workspace in the cwd. Use `testCwd` to change into a temp cwd.
|
||||||
|
func testStateFileWorkspaceDefault(t *testing.T, workspace string, s *states.State) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
workspaceDir := filepath.Join(backendLocal.DefaultWorkspaceDir, workspace)
|
||||||
|
err := os.MkdirAll(workspaceDir, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := filepath.Join(workspaceDir, DefaultStateFilename)
|
||||||
|
f, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if err := writeStateForTesting(s, f); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
// testStateFileRemote writes the state out to the remote statefile
|
// testStateFileRemote writes the state out to the remote statefile
|
||||||
// in the cwd. Use `testCwd` to change into a temp cwd.
|
// in the cwd. Use `testCwd` to change into a temp cwd.
|
||||||
func testStateFileRemote(t *testing.T, s *terraform.State) string {
|
func testStateFileRemote(t *testing.T, s *terraform.State) string {
|
||||||
|
|
|
@ -29,7 +29,7 @@ func (c *TaintCommand) Run(args []string) int {
|
||||||
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
||||||
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
||||||
cmdFlags.StringVar(&module, "module", "", "module")
|
cmdFlags.StringVar(&module, "module", "", "module")
|
||||||
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
|
||||||
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := cmdFlags.Parse(args); err != nil {
|
||||||
|
|
|
@ -240,6 +240,48 @@ func TestTaint_defaultState(t *testing.T) {
|
||||||
testStateOutput(t, path, testTaintStr)
|
testStateOutput(t, path, testTaintStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTaint_defaultWorkspaceState(t *testing.T) {
|
||||||
|
// Get a temp cwd
|
||||||
|
tmp, cwd := testCwd(t)
|
||||||
|
defer testFixCwd(t, tmp, cwd)
|
||||||
|
|
||||||
|
state := 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,
|
||||||
|
},
|
||||||
|
addrs.AbsProviderConfig{
|
||||||
|
Provider: addrs.NewLegacyProvider("test"),
|
||||||
|
Module: addrs.RootModule,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
testWorkspace := "development"
|
||||||
|
path := testStateFileWorkspaceDefault(t, testWorkspace, state)
|
||||||
|
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
meta := Meta{Ui: ui}
|
||||||
|
meta.SetWorkspace(testWorkspace)
|
||||||
|
c := &TaintCommand{
|
||||||
|
Meta: meta,
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"test_instance.foo",
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
testStateOutput(t, path, testTaintStr)
|
||||||
|
}
|
||||||
|
|
||||||
func TestTaint_missing(t *testing.T) {
|
func TestTaint_missing(t *testing.T) {
|
||||||
state := states.BuildState(func(s *states.SyncState) {
|
state := states.BuildState(func(s *states.SyncState) {
|
||||||
s.SetResourceInstanceCurrent(
|
s.SetResourceInstanceCurrent(
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (c *UntaintCommand) Run(args []string) int {
|
||||||
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
||||||
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
||||||
cmdFlags.StringVar(&module, "module", "", "module")
|
cmdFlags.StringVar(&module, "module", "", "module")
|
||||||
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
|
||||||
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := cmdFlags.Parse(args); err != nil {
|
||||||
|
|
|
@ -265,6 +265,53 @@ test_instance.foo:
|
||||||
`))
|
`))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUntaint_defaultWorkspaceState(t *testing.T) {
|
||||||
|
// Get a temp cwd
|
||||||
|
tmp, cwd := testCwd(t)
|
||||||
|
defer testFixCwd(t, tmp, cwd)
|
||||||
|
|
||||||
|
// Write the temp state
|
||||||
|
state := 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.ObjectTainted,
|
||||||
|
},
|
||||||
|
addrs.AbsProviderConfig{
|
||||||
|
Provider: addrs.NewLegacyProvider("test"),
|
||||||
|
Module: addrs.RootModule,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
testWorkspace := "development"
|
||||||
|
path := testStateFileWorkspaceDefault(t, testWorkspace, state)
|
||||||
|
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
meta := Meta{Ui: ui}
|
||||||
|
meta.SetWorkspace(testWorkspace)
|
||||||
|
c := &UntaintCommand{
|
||||||
|
Meta: meta,
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"test_instance.foo",
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
testStateOutput(t, path, strings.TrimSpace(`
|
||||||
|
test_instance.foo:
|
||||||
|
ID = bar
|
||||||
|
provider = provider["registry.terraform.io/-/test"]
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
|
||||||
func TestUntaint_missing(t *testing.T) {
|
func TestUntaint_missing(t *testing.T) {
|
||||||
state := states.BuildState(func(s *states.SyncState) {
|
state := states.BuildState(func(s *states.SyncState) {
|
||||||
s.SetResourceInstanceCurrent(
|
s.SetResourceInstanceCurrent(
|
||||||
|
|
Loading…
Reference in New Issue