2016-03-08 14:37:34 -06:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-13 14:14:19 -06:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-05-17 14:00:50 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 14:43:35 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2016-03-08 14:37:34 -06:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUntaint(t *testing.T) {
|
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
|
|
|
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,
|
2016-03-08 14:37:34 -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
|
|
|
)
|
|
|
|
})
|
2016-03-08 14:37:34 -06:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`)
|
|
|
|
testStateOutput(t, statePath, expected)
|
|
|
|
}
|
|
|
|
|
2017-02-03 15:13:42 -06:00
|
|
|
func TestUntaint_lockedState(t *testing.T) {
|
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
|
|
|
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,
|
2017-02-03 15:13:42 -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-03 15:13:42 -06:00
|
|
|
statePath := testStateFile(t, state)
|
2018-11-20 02:58:59 -06:00
|
|
|
unlock, err := testLockState(testDataDir, statePath)
|
2017-02-03 15:13:42 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-02-03 15:13:42 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2017-02-03 15:13:42 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
2017-02-15 13:01:18 -06:00
|
|
|
if !strings.Contains(output, "lock") {
|
2017-02-03 15:13:42 -06:00
|
|
|
t.Fatal("command output does not look like a lock error:", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 14:37:34 -06:00
|
|
|
func TestUntaint_backup(t *testing.T) {
|
|
|
|
// Get a temp cwd
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Write the temp state
|
2020-10-05 07:33:49 -05:00
|
|
|
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,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
2020-10-05 07:33:49 -05:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
testStateFileDefault(t, state)
|
2016-03-08 14:37:34 -06:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup is still tainted
|
2020-10-05 07:33:49 -05:00
|
|
|
testStateOutput(t, DefaultStateFilename+".backup", strings.TrimSpace(`
|
2016-04-21 14:59:10 -05:00
|
|
|
test_instance.foo: (tainted)
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`))
|
|
|
|
|
|
|
|
// State is untainted
|
2020-10-05 07:33:49 -05:00
|
|
|
testStateOutput(t, DefaultStateFilename, strings.TrimSpace(`
|
2016-03-08 14:37:34 -06:00
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUntaint_backupDisable(t *testing.T) {
|
|
|
|
// Get a temp cwd
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Write the temp state
|
2020-10-05 07:33:49 -05:00
|
|
|
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,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
2020-10-05 07:33:49 -05:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
testStateFileDefault(t, state)
|
2016-03-08 14:37:34 -06:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup", "-",
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:33:49 -05:00
|
|
|
if _, err := os.Stat(DefaultStateFilename + ".backup"); err == nil {
|
2016-03-08 14:37:34 -06:00
|
|
|
t.Fatal("backup path should not exist")
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:33:49 -05:00
|
|
|
testStateOutput(t, DefaultStateFilename, strings.TrimSpace(`
|
2016-03-08 14:37:34 -06:00
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUntaint_badState(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", "i-should-not-exist-ever",
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUntaint_defaultState(t *testing.T) {
|
|
|
|
// Get a temp cwd
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Write the temp state
|
2020-10-05 07:33:49 -05:00
|
|
|
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,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
2020-10-05 07:33:49 -05:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
testStateFileDefault(t, state)
|
2016-03-08 14:37:34 -06:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:33:49 -05:00
|
|
|
testStateOutput(t, DefaultStateFilename, strings.TrimSpace(`
|
2016-03-08 14:37:34 -06:00
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`))
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:09:37 -05:00
|
|
|
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{
|
2020-10-05 07:33:49 -05:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-09-28 12:09:37 -05:00
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
testWorkspace := "development"
|
|
|
|
path := testStateFileWorkspaceDefault(t, testWorkspace, state)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
|
|
|
meta := Meta{Ui: ui, View: view}
|
2020-09-28 12:09:37 -05:00
|
|
|
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
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2020-09-28 12:09:37 -05:00
|
|
|
`))
|
|
|
|
}
|
|
|
|
|
2016-03-08 14:37:34 -06:00
|
|
|
func TestUntaint_missing(t *testing.T) {
|
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
|
|
|
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,
|
2016-03-08 14:37:34 -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
|
|
|
)
|
|
|
|
})
|
2016-03-08 14:37:34 -06:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUntaint_missingAllow(t *testing.T) {
|
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
|
|
|
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,
|
2016-03-08 14:37:34 -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
|
|
|
)
|
|
|
|
})
|
2016-03-08 14:37:34 -06:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-allow-missing",
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2021-01-13 14:14:19 -06:00
|
|
|
|
|
|
|
// Check for the warning
|
|
|
|
actual := strings.TrimSpace(ui.ErrorWriter.String())
|
2021-01-26 12:13:32 -06:00
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
Warning: No such resource instance
|
|
|
|
|
|
|
|
Resource instance test_instance.bar was not found, but this is not an error
|
|
|
|
because -allow-missing was set.
|
|
|
|
|
2021-01-13 14:14:19 -06:00
|
|
|
`)
|
|
|
|
if diff := cmp.Diff(expected, actual); diff != "" {
|
|
|
|
t.Fatalf("wrong output\n%s", diff)
|
|
|
|
}
|
2016-03-08 14:37:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUntaint_stateOut(t *testing.T) {
|
|
|
|
// Get a temp cwd
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Write the temp state
|
2020-10-05 07:33:49 -05:00
|
|
|
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,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
2020-10-05 07:33:49 -05:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
testStateFileDefault(t, state)
|
2016-03-08 14:37:34 -06:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state-out", "foo",
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:33:49 -05:00
|
|
|
testStateOutput(t, DefaultStateFilename, strings.TrimSpace(`
|
2016-04-21 14:59:10 -05:00
|
|
|
test_instance.foo: (tainted)
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`))
|
|
|
|
testStateOutput(t, "foo", strings.TrimSpace(`
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUntaint_module(t *testing.T) {
|
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
|
|
|
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,
|
2016-03-08 14:37:34 -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
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "blah",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar"}`),
|
|
|
|
Status: states.ObjectTainted,
|
2016-03-08 14:37:34 -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
|
|
|
)
|
|
|
|
})
|
2016-03-08 14:37:34 -06:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-08 14:37:34 -06:00
|
|
|
c := &UntaintCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2016-03-08 14:37:34 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2018-10-09 18:58:41 -05:00
|
|
|
"module.child.test_instance.blah",
|
2016-03-08 14:37:34 -06:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2018-10-09 18:58:41 -05:00
|
|
|
t.Fatalf("command exited with status code %d; want 0\n\n%s", code, ui.ErrorWriter.String())
|
2016-03-08 14:37:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, strings.TrimSpace(`
|
2016-04-21 14:59:10 -05:00
|
|
|
test_instance.foo: (tainted)
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
|
|
|
|
module.child:
|
|
|
|
test_instance.blah:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-08 14:37:34 -06:00
|
|
|
`))
|
|
|
|
}
|