2016-03-31 18:29:39 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-02-22 06:10:03 +01:00
|
|
|
"os"
|
2016-03-31 18:29:39 +02:00
|
|
|
"path/filepath"
|
2017-07-05 23:58:08 +02:00
|
|
|
"strings"
|
2016-03-31 18:29:39 +02: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 23:24:45 +02:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2017-07-27 21:31:13 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
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 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2016-03-31 18:29:39 +02: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 23:24:45 +02: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 18:29:39 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
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 23:24:45 +02: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2016-03-31 18:29:39 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2016-03-31 18:29:39 +02: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-16 03:51:26 +01: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
2019-03-16 03:51:26 +01: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
2019-03-16 03:51:26 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-13 21:32:58 +01:00
|
|
|
provider = provider["registry.terraform.io/-/test"]
|
2019-03-16 03:51:26 +01: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-02-13 21:32:58 +01:00
|
|
|
provider = provider["registry.terraform.io/-/test"]
|
2019-03-16 03:51:26 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
|
|
|
|
module.child:
|
|
|
|
test_instance.foo:
|
|
|
|
ID = foo
|
2020-02-13 21:32:58 +01:00
|
|
|
provider = provider["registry.terraform.io/-/test"]
|
2019-03-16 03:51:26 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:58:08 +02: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 23:24:45 +02: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 23:58:08 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
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 23:24:45 +02: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2017-07-05 23:58:08 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2017-07-05 23:58:08 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2018-10-25 15:41:00 +02:00
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Errorf("expected non-zero exit code, got: %d", code)
|
2017-07-05 23:58:08 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
if msg := ui.ErrorWriter.String(); !strings.Contains(msg, "At least one address") {
|
2017-07-05 23:58:08 +02:00
|
|
|
t.Errorf("not the error we were looking for:\n%s", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:38:06 +02: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
2018-10-15 17:38:06 +02: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
2018-10-15 17:38:06 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.baz", // doesn't exist in the state constructed above
|
|
|
|
}
|
2018-10-22 15:52:53 +02:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("expected exit status %d, got: %d", 0, code)
|
2018-10-15 17:38:06 +02:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:51:26 +01:00
|
|
|
if msg := ui.OutputWriter.String(); !strings.Contains(msg, "No matching resource instances found") {
|
2018-10-22 15:52:53 +02:00
|
|
|
t.Fatalf("unexpected output:\n%s", msg)
|
2018-10-15 17:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-22 06:10:03 +01: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 23:24:45 +02: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-22 06:10:03 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
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 23:24:45 +02: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2017-02-22 06:10:03 +01:00
|
|
|
statePath := testStateFile(t, state)
|
2018-10-25 15:41:00 +02:00
|
|
|
backupPath := statePath + ".backup.test"
|
2017-02-22 06:10:03 +01:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2017-02-22 06:10:03 +01: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 20:41:49 +02:00
|
|
|
// Test backup
|
2017-02-22 06:10:03 +01:00
|
|
|
testStateOutput(t, backupPath, testStateRmOutputOriginal)
|
|
|
|
}
|
|
|
|
|
2016-03-31 18:29:39 +02:00
|
|
|
func TestStateRm_noState(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2016-03-31 18:29:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
args := []string{"foo"}
|
2016-03-31 18:29:39 +02:00
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 21:31:13 +02:00
|
|
|
func TestStateRm_needsInit(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("backend-change"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"foo"}
|
|
|
|
if code := c.Run(args); code == 0 {
|
2018-10-22 15:52:53 +02:00
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
2017-07-27 21:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "Initialization") {
|
2018-10-22 15:52:53 +02:00
|
|
|
t.Fatalf("expected initialization error, got:\n%s", ui.ErrorWriter.String())
|
2017-07-27 21:31:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateRm_backendState(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("backend-unchanged"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-22 15:52:53 +02: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 21:31:13 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
2018-10-22 15:52:53 +02: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 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewLegacyProvider("test"),
|
|
|
|
Module: addrs.RootModuleInstance,
|
|
|
|
},
|
2018-10-22 15:52:53 +02:00
|
|
|
)
|
|
|
|
})
|
2017-07-27 21:31:13 +02:00
|
|
|
|
|
|
|
statePath := "local-state.tfstate"
|
|
|
|
backupPath := "local-state.backup"
|
|
|
|
|
|
|
|
f, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
2018-10-22 15:52:53 +02:00
|
|
|
t.Fatalf("failed to create state file %s: %s", statePath, err)
|
2017-07-27 21:31:13 +02:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-10-22 15:52:53 +02:00
|
|
|
err = writeStateForTesting(state, f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to write state to file %s: %s", statePath, err)
|
2017-07-27 21:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &StateRmCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 18:29:39 +02:00
|
|
|
const testStateRmOutputOriginal = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = foo
|
2020-02-13 21:32:58 +01:00
|
|
|
provider = provider["registry.terraform.io/-/test"]
|
2016-03-31 18:29:39 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-02-13 21:32:58 +01:00
|
|
|
provider = provider["registry.terraform.io/-/test"]
|
2016-03-31 18:29:39 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateRmOutput = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = foo
|
2020-02-13 21:32:58 +01:00
|
|
|
provider = provider["registry.terraform.io/-/test"]
|
2016-03-31 18:29:39 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|