2016-03-31 11:29:39 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-02-21 23:10:03 -06:00
|
|
|
"os"
|
2016-03-31 11:29:39 -05:00
|
|
|
"path/filepath"
|
2017-07-05 16:58:08 -05:00
|
|
|
"strings"
|
2016-03-31 11:29:39 -05:00
|
|
|
"testing"
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
|
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-31 11:29:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateRm(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","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2016-03-31 11:29:39 -05: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: "bar",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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-31 11:29:39 -05:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-31 11:29:39 -05:00
|
|
|
c := &StateRmCommand{
|
2017-07-26 12:08:09 -05:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2017-07-26 12:08:09 -05:00
|
|
|
},
|
2016-03-31 11:29:39 -05: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())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, statePath, testStateRmOutput)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateRmOutputOriginal)
|
|
|
|
}
|
|
|
|
|
2019-03-15 21:51:26 -05:00
|
|
|
func TestStateRmNotChildModule(t *testing.T) {
|
|
|
|
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","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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
|
|
|
},
|
2019-03-15 21:51:26 -05:00
|
|
|
)
|
|
|
|
// This second instance has the same local address as the first but
|
|
|
|
// is in a child module. Older versions of Terraform would incorrectly
|
|
|
|
// remove this one too, since they failed to check the module address.
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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
|
|
|
},
|
2019-03-15 21:51:26 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2019-03-15 21:51:26 -05:00
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2019-03-15 21:51:26 -05: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())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, statePath, `
|
|
|
|
<no state>
|
|
|
|
module.child:
|
|
|
|
test_instance.foo:
|
|
|
|
ID = foo
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-03-15 21:51:26 -05:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-03-15 21:51:26 -05:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
|
|
|
|
module.child:
|
|
|
|
test_instance.foo:
|
|
|
|
ID = foo
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-03-15 21:51:26 -05:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2017-07-05 16:58:08 -05:00
|
|
|
func TestStateRmNoArgs(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","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-07-05 16:58:08 -05: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: "bar",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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-07-05 16:58:08 -05:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-07-05 16:58:08 -05:00
|
|
|
c := &StateRmCommand{
|
2017-07-26 12:08:09 -05:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2017-07-26 12:08:09 -05:00
|
|
|
},
|
2017-07-05 16:58:08 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2018-10-25 08:41:00 -05:00
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Errorf("expected non-zero exit code, got: %d", code)
|
2017-07-05 16:58:08 -05:00
|
|
|
}
|
|
|
|
|
2018-10-25 08:41:00 -05:00
|
|
|
if msg := ui.ErrorWriter.String(); !strings.Contains(msg, "At least one address") {
|
2017-07-05 16:58:08 -05:00
|
|
|
t.Errorf("not the error we were looking for:\n%s", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-15 10:38:06 -05:00
|
|
|
func TestStateRmNonExist(t *testing.T) {
|
|
|
|
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","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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
|
|
|
},
|
2018-10-15 10:38:06 -05:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "bar",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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
|
|
|
},
|
2018-10-15 10:38:06 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2018-10-15 10:38:06 -05:00
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2018-10-15 10:38:06 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.baz", // doesn't exist in the state constructed above
|
|
|
|
}
|
2020-06-24 13:03:23 -05:00
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("expected exit status %d, got: %d", 1, code)
|
2018-10-15 10:38:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 23:10:03 -06:00
|
|
|
func TestStateRm_backupExplicit(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","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-02-21 23:10:03 -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: "bar",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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-21 23:10:03 -06:00
|
|
|
statePath := testStateFile(t, state)
|
2018-10-25 08:41:00 -05:00
|
|
|
backupPath := statePath + ".backup.test"
|
2017-02-21 23:10:03 -06:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-02-21 23:10:03 -06:00
|
|
|
c := &StateRmCommand{
|
2017-07-26 12:08:09 -05:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2017-07-26 12:08:09 -05:00
|
|
|
},
|
2017-02-21 23:10:03 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup", backupPath,
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, statePath, testStateRmOutput)
|
|
|
|
|
2017-06-23 13:41:49 -05:00
|
|
|
// Test backup
|
2017-02-21 23:10:03 -06:00
|
|
|
testStateOutput(t, backupPath, testStateRmOutputOriginal)
|
|
|
|
}
|
|
|
|
|
2016-03-31 11:29:39 -05:00
|
|
|
func TestStateRm_noState(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2016-03-31 11:29:39 -05:00
|
|
|
c := &StateRmCommand{
|
2017-07-26 12:08:09 -05:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2017-07-26 12:08:09 -05:00
|
|
|
},
|
2016-03-31 11:29:39 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:41:00 -05:00
|
|
|
args := []string{"foo"}
|
2016-03-31 11:29:39 -05:00
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:31:13 -05:00
|
|
|
func TestStateRm_needsInit(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 11:48:25 -05:00
|
|
|
testCopyDir(t, testFixturePath("backend-change"), td)
|
2017-07-27 14:31:13 -05:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-07-27 14:31:13 -05:00
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2017-07-27 14:31:13 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"foo"}
|
|
|
|
if code := c.Run(args); code == 0 {
|
2018-10-22 08:52:53 -05:00
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
2017-07-27 14:31:13 -05:00
|
|
|
}
|
|
|
|
|
2021-05-14 16:36:54 -05:00
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "Backend initialization") {
|
2018-10-22 08:52:53 -05:00
|
|
|
t.Fatalf("expected initialization error, got:\n%s", ui.ErrorWriter.String())
|
2017-07-27 14:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateRm_backendState(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 11:48:25 -05:00
|
|
|
testCopyDir(t, testFixturePath("backend-unchanged"), td)
|
2017-07-27 14:31:13 -05:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-22 08:52:53 -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","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-07-27 14:31:13 -05: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
|
|
|
},
|
2018-10-22 08:52:53 -05:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "bar",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
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
|
|
|
},
|
2018-10-22 08:52:53 -05:00
|
|
|
)
|
|
|
|
})
|
2017-07-27 14:31:13 -05:00
|
|
|
|
|
|
|
statePath := "local-state.tfstate"
|
|
|
|
backupPath := "local-state.backup"
|
|
|
|
|
|
|
|
f, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
2018-10-22 08:52:53 -05:00
|
|
|
t.Fatalf("failed to create state file %s: %s", statePath, err)
|
2017-07-27 14:31:13 -05:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
err = writeStateForTesting(state, f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to write state to file %s: %s", statePath, err)
|
2017-07-27 14:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-07-27 14:31:13 -05:00
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2017-07-27 14:31:13 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup", backupPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, statePath, testStateRmOutput)
|
|
|
|
|
|
|
|
// Test backup
|
|
|
|
testStateOutput(t, backupPath, testStateRmOutputOriginal)
|
|
|
|
}
|
|
|
|
|
2016-03-31 11:29:39 -05:00
|
|
|
const testStateRmOutputOriginal = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = foo
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-31 11:29:39 -05:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-31 11:29:39 -05:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateRmOutput = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = foo
|
2020-10-05 07:33:49 -05:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-03-31 11:29:39 -05:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|