2016-04-12 20:44:12 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
"fmt"
|
2017-02-22 06:10:03 +01:00
|
|
|
"os"
|
2016-04-12 20:44:12 +02:00
|
|
|
"path/filepath"
|
2021-01-27 18:48:36 +01:00
|
|
|
"strings"
|
2016-04-12 20:44:12 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-01-12 18:59:28 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
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"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2016-04-12 20:44:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateMv(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-04-12 20:44:12 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
2020-01-04 14:29:25 +01:00
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2020-03-23 20:26:18 +01:00
|
|
|
Dependencies: []addrs.ConfigResource{mustResourceAddr("test_instance.foo")},
|
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
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2016-04-12 20:44:12 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2016-04-12 20:44:12 +02:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2016-04-12 20:44:12 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2020-03-02 20:45:03 +01:00
|
|
|
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
|
2016-04-12 20:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, statePath, testStateMvOutput)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvOutputOriginal)
|
2020-03-02 20:45:03 +01:00
|
|
|
|
|
|
|
// Change the single instance to a counted instance
|
|
|
|
args = []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.bar",
|
|
|
|
"test_instance.bar[0]",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract the resource and verify the mode
|
|
|
|
s := testStateRead(t, statePath)
|
|
|
|
addr, diags := addrs.ParseAbsResourceStr("test_instance.bar")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Err())
|
|
|
|
}
|
2020-04-25 03:52:05 +02:00
|
|
|
for key := range s.Resource(addr).Instances {
|
|
|
|
if _, ok := key.(addrs.IntKey); !ok {
|
|
|
|
t.Fatalf("expected each mode List, got key %q", key)
|
|
|
|
}
|
2020-03-02 20:45:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// change from list to map
|
|
|
|
args = []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.bar[0]",
|
|
|
|
"test_instance.bar[\"baz\"]",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract the resource and verify the mode
|
|
|
|
s = testStateRead(t, statePath)
|
|
|
|
addr, diags = addrs.ParseAbsResourceStr("test_instance.bar")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Err())
|
|
|
|
}
|
2020-04-25 03:52:05 +02:00
|
|
|
for key := range s.Resource(addr).Instances {
|
|
|
|
if _, ok := key.(addrs.StringKey); !ok {
|
|
|
|
t.Fatalf("expected each mode map, found key %q", key)
|
|
|
|
}
|
2020-03-02 20:45:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// change from from map back to single
|
|
|
|
args = []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.bar[\"baz\"]",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract the resource and verify the mode
|
|
|
|
s = testStateRead(t, statePath)
|
|
|
|
addr, diags = addrs.ParseAbsResourceStr("test_instance.bar")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Err())
|
|
|
|
}
|
2020-04-25 03:52:05 +02:00
|
|
|
for key := range s.Resource(addr).Instances {
|
|
|
|
if key != addrs.NoKey {
|
|
|
|
t.Fatalf("expected no each mode, found key %q", key)
|
|
|
|
}
|
2020-03-02 20:45:03 +01:00
|
|
|
}
|
|
|
|
|
2016-04-12 20:44:12 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 21:53:18 +02:00
|
|
|
func TestStateMv_backupAndBackupOutOptionsWithNonLocalBackend(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,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("backup option specified", func(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("init-backend-http"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
backupPath := filepath.Join(td, "backup")
|
|
|
|
|
|
|
|
// Set up our backend state using mock state
|
|
|
|
dataState, srv := testBackendState(t, state, 200)
|
|
|
|
defer srv.Close()
|
|
|
|
testStateFileRemote(t, dataState)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
view, _ := testView(t)
|
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup", backupPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
gotErr := ui.ErrorWriter.String()
|
|
|
|
wantErr := `
|
|
|
|
Error: Invalid command line options: -backup
|
|
|
|
|
|
|
|
Command line options -backup and -backup-out are legacy options that operate
|
|
|
|
on a local state file only. You must specify a local state file with the
|
|
|
|
-state option or switch to the local backend.
|
|
|
|
|
|
|
|
`
|
|
|
|
if gotErr != wantErr {
|
|
|
|
t.Fatalf("expected error\ngot:%s\n\nwant:%s", gotErr, wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("backup-out option specified", func(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("init-backend-http"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
backupOutPath := filepath.Join(td, "backup-out")
|
|
|
|
|
|
|
|
// Set up our backend state using mock state
|
|
|
|
dataState, srv := testBackendState(t, state, 200)
|
|
|
|
defer srv.Close()
|
|
|
|
testStateFileRemote(t, dataState)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
view, _ := testView(t)
|
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup-out", backupOutPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
gotErr := ui.ErrorWriter.String()
|
|
|
|
wantErr := `
|
|
|
|
Error: Invalid command line options: -backup-out
|
|
|
|
|
|
|
|
Command line options -backup and -backup-out are legacy options that operate
|
|
|
|
on a local state file only. You must specify a local state file with the
|
|
|
|
-state option or switch to the local backend.
|
|
|
|
|
|
|
|
`
|
|
|
|
if gotErr != wantErr {
|
|
|
|
t.Fatalf("expected error\ngot:%s\n\nwant:%s", gotErr, wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("backup and backup-out options specified", func(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("init-backend-http"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
backupPath := filepath.Join(td, "backup")
|
|
|
|
backupOutPath := filepath.Join(td, "backup-out")
|
|
|
|
|
|
|
|
// Set up our backend state using mock state
|
|
|
|
dataState, srv := testBackendState(t, state, 200)
|
|
|
|
defer srv.Close()
|
|
|
|
testStateFileRemote(t, dataState)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
view, _ := testView(t)
|
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup", backupPath,
|
|
|
|
"-backup-out", backupOutPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
gotErr := ui.ErrorWriter.String()
|
|
|
|
wantErr := `
|
|
|
|
Error: Invalid command line options: -backup, -backup-out
|
|
|
|
|
|
|
|
Command line options -backup and -backup-out are legacy options that operate
|
|
|
|
on a local state file only. You must specify a local state file with the
|
|
|
|
-state option or switch to the local backend.
|
|
|
|
|
|
|
|
`
|
|
|
|
if gotErr != wantErr {
|
|
|
|
t.Fatalf("expected error\ngot:%s\n\nwant:%s", gotErr, wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("backup option specified with state option", func(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("init-backend-http"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
backupPath := filepath.Join(td, "backup")
|
|
|
|
|
|
|
|
// Set up our backend state using mock state
|
|
|
|
dataState, srv := testBackendState(t, state, 200)
|
|
|
|
defer srv.Close()
|
|
|
|
testStateFileRemote(t, dataState)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
view, _ := testView(t)
|
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-backup", backupPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
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, testStateMvBackupAndBackupOutOptionsWithNonLocalBackendOutput)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("backup-out option specified with state option", func(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("init-backend-http"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
backupOutPath := filepath.Join(td, "backup-out")
|
|
|
|
|
|
|
|
// Set up our backend state using mock state
|
|
|
|
dataState, srv := testBackendState(t, state, 200)
|
|
|
|
defer srv.Close()
|
|
|
|
testStateFileRemote(t, dataState)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
view, _ := testView(t)
|
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-backup-out", backupOutPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
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, testStateMvBackupAndBackupOutOptionsWithNonLocalBackendOutput)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:51:26 +01:00
|
|
|
func TestStateMv_resourceToInstance(t *testing.T) {
|
2021-01-12 18:59:28 +01:00
|
|
|
// A single resource (no count defined)
|
2019-03-16 03:51:26 +01: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,
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-03-16 03:51:26 +01:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
2020-01-04 14:29:25 +01:00
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2020-03-23 20:26:18 +01:00
|
|
|
Dependencies: []addrs.ConfigResource{mustResourceAddr("test_instance.foo")},
|
2019-03-16 03:51:26 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-03-16 03:51:26 +01:00
|
|
|
)
|
2020-04-25 03:52:05 +02:00
|
|
|
s.SetResourceProvider(
|
2019-03-16 03:51:26 +01:00
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "bar",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-03-16 03:51:26 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2019-03-16 03:51:26 +01:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2019-03-16 03:51:26 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar[0]",
|
|
|
|
}
|
|
|
|
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, `
|
|
|
|
test_instance.bar.0:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-03-16 03:51:26 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.baz:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/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], testStateMvOutputOriginal)
|
|
|
|
}
|
|
|
|
|
2021-01-12 18:59:28 +01:00
|
|
|
func TestStateMv_resourceToInstanceErr(t *testing.T) {
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
s.SetResourceProvider(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "bar",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-01-26 19:13:32 +01:00
|
|
|
ui := cli.NewMockUi()
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2021-01-26 19:13:32 +01:00
|
|
|
|
2021-01-12 18:59:28 +01:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2021-01-12 18:59:28 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar[0]",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:13:32 +01:00
|
|
|
expectedErr := `
|
|
|
|
Error: Invalid target address
|
|
|
|
|
|
|
|
Cannot move test_instance.foo to test_instance.bar[0]: the source is a whole
|
|
|
|
resource (not a resource instance) so the target must also be a whole
|
|
|
|
resource.
|
|
|
|
|
2021-01-12 18:59:28 +01:00
|
|
|
`
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if errOutput != expectedErr {
|
2020-12-19 03:01:19 +01:00
|
|
|
t.Errorf("wrong output\n%s", cmp.Diff(errOutput, expectedErr))
|
2021-01-12 18:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 19:13:32 +01:00
|
|
|
|
|
|
|
func TestStateMv_resourceToInstanceErrInAutomation(t *testing.T) {
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
s.SetResourceProvider(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "bar",
|
|
|
|
}.Absolute(addrs.RootModuleInstance),
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2021-01-26 19:13:32 +01:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2021-01-26 19:13:32 +01:00
|
|
|
RunningInAutomation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar[0]",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedErr := `
|
|
|
|
Error: Invalid target address
|
|
|
|
|
|
|
|
Cannot move test_instance.foo to test_instance.bar[0]: the source is a whole
|
|
|
|
resource (not a resource instance) so the target must also be a whole
|
|
|
|
resource.
|
|
|
|
|
|
|
|
`
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if errOutput != expectedErr {
|
|
|
|
t.Errorf("Unexpected diff.\ngot:\n%s\nwant:\n%s\n", errOutput, expectedErr)
|
|
|
|
t.Errorf("%s", cmp.Diff(errOutput, expectedErr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:51:26 +01:00
|
|
|
func TestStateMv_instanceToResource(t *testing.T) {
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(0)).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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-03-16 03:51:26 +01:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "baz",
|
|
|
|
}.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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-03-16 03:51:26 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2019-03-16 03:51:26 +01:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2019-03-16 03:51:26 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo[0]",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
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, `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-03-16 03:51:26 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.baz:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/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.baz:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-03-16 03:51:26 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.0:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-03-16 03:51:26 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:06:25 +01:00
|
|
|
func TestStateMv_instanceToNewResource(t *testing.T) {
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(0)).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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-12-05 22:06:25 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2019-12-05 22:06:25 +01:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2019-12-05 22:06:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo[0]",
|
|
|
|
"test_instance.bar[\"new\"]",
|
|
|
|
}
|
|
|
|
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, `
|
|
|
|
test_instance.bar["new"]:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-12-05 22:06:25 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`)
|
|
|
|
|
|
|
|
// now move the instance to a new resource in a new module
|
|
|
|
args = []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.bar[\"new\"]",
|
|
|
|
"module.test.test_instance.baz[\"new\"]",
|
|
|
|
}
|
|
|
|
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.test:
|
|
|
|
test_instance.baz["new"]:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-12-05 22:06:25 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
func TestStateMv_differentResourceTypes(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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-10-25 15:41:00 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2018-10-25 15:41:00 +02:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2018-10-25 15:41:00 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_network.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:13:32 +01:00
|
|
|
gotErr := ui.ErrorWriter.String()
|
|
|
|
wantErr := `
|
|
|
|
Error: Invalid state move request
|
|
|
|
|
|
|
|
Cannot move test_instance.foo to test_network.bar: resource types don't
|
|
|
|
match.
|
|
|
|
|
|
|
|
`
|
2020-12-19 03:01:19 +01:00
|
|
|
if gotErr != wantErr {
|
|
|
|
t.Fatalf("expected initialization error\ngot:\n%s\n\nwant:%s", gotErr, wantErr)
|
2018-10-25 15:41:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 20:39:37 +02:00
|
|
|
// don't modify backend state is we supply a -state flag
|
|
|
|
func TestStateMv_explicitWithBackend(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("init-backend"), td)
|
2017-06-23 20:39:37 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
backupPath := filepath.Join(td, "backup")
|
|
|
|
|
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-06-23 20:39:37 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "baz",
|
|
|
|
}.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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2017-06-23 20:39:37 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// init our backend
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-06-23 20:39:37 +02:00
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-06-23 20:39:37 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := ic.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// only modify statePath
|
|
|
|
p := testProvider()
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2017-06-23 20:39:37 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args = []string{
|
|
|
|
"-backup", backupPath,
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
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, testStateMvOutput)
|
|
|
|
}
|
|
|
|
|
2017-02-22 06:10:03 +01:00
|
|
|
func TestStateMv_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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
2020-01-04 14:29:25 +01:00
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2020-03-23 20:26:18 +01:00
|
|
|
Dependencies: []addrs.ConfigResource{mustResourceAddr("test_instance.foo")},
|
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
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 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)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-02-22 06:10:03 +01:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2017-02-22 06:10:03 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup", backupPath,
|
|
|
|
"-state", statePath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
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, testStateMvOutput)
|
|
|
|
|
2017-06-23 20:41:49 +02:00
|
|
|
// Test backup
|
2017-02-22 06:10:03 +01:00
|
|
|
testStateOutput(t, backupPath, testStateMvOutputOriginal)
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
func TestStateMv_stateOutNew(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-04-12 23:17:59 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2016-04-12 23:17:59 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
stateOutPath := statePath + ".out"
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2016-04-12 23:17:59 +02:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2016-04-12 23:17:59 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", stateOutPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, stateOutPath, testStateMvOutput_stateOut)
|
|
|
|
testStateOutput(t, statePath, testStateMvOutput_stateOutSrc)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvOutput_stateOutOriginal)
|
|
|
|
}
|
|
|
|
|
2016-04-13 00:02:18 +02:00
|
|
|
func TestStateMv_stateOutExisting(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
|
|
|
stateSrc := 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-04-13 00:02:18 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2016-04-13 00:02:18 +02:00
|
|
|
statePath := testStateFile(t, stateSrc)
|
|
|
|
|
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
|
|
|
stateDst := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "qux",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar"}`),
|
|
|
|
Status: states.ObjectReady,
|
2016-04-13 00:02:18 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2016-04-13 00:02:18 +02:00
|
|
|
stateOutPath := testStateFile(t, stateDst)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2016-04-13 00:02:18 +02:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2016-04-13 00:02:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", stateOutPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, stateOutPath, testStateMvExisting_stateDst)
|
|
|
|
testStateOutput(t, statePath, testStateMvExisting_stateSrc)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvExisting_stateSrcOriginal)
|
|
|
|
|
|
|
|
backups = testStateBackups(t, filepath.Dir(stateOutPath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvExisting_stateDstOriginal)
|
|
|
|
}
|
|
|
|
|
2016-04-12 20:44:12 +02:00
|
|
|
func TestStateMv_noState(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2016-04-12 20:44:12 +02:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2016-04-12 20:44:12 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"from", "to"}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:05:20 +02:00
|
|
|
func TestStateMv_stateOutNew_count(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.IntKey(0)).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2016-08-19 18:05:20 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(1)).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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 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":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2016-08-19 18:05:20 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
stateOutPath := statePath + ".out"
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2016-08-19 18:05:20 +02:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2016-08-19 18:05:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", stateOutPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, stateOutPath, testStateMvCount_stateOut)
|
|
|
|
testStateOutput(t, statePath, testStateMvCount_stateOutSrc)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvCount_stateOutOriginal)
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:27:16 +01:00
|
|
|
// Modules with more than 10 resources were sorted lexically, causing the
|
|
|
|
// indexes in the new location to change.
|
|
|
|
func TestStateMv_stateOutNew_largeCount(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) {
|
|
|
|
// test_instance.foo has 11 instances, all the same except for their ids
|
|
|
|
for i := 0; i < 11; i++ {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(i)).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(fmt.Sprintf(`{"id":"foo%d","foo":"value","bar":"value"}`, i)),
|
|
|
|
Status: states.ObjectReady,
|
2017-02-24 00:27:16 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 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":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-02-24 00:27:16 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2017-02-24 00:27:16 +01:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
stateOutPath := statePath + ".out"
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-02-24 00:27:16 +01:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2017-02-24 00:27:16 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", stateOutPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, stateOutPath, testStateMvLargeCount_stateOut)
|
|
|
|
testStateOutput(t, statePath, testStateMvLargeCount_stateOutSrc)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvLargeCount_stateOutOriginal)
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:05:42 +02:00
|
|
|
func TestStateMv_stateOutNew_nestedModule(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.Child("foo", addrs.NoKey).Child("child1", addrs.NoKey)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2016-08-18 21:05:42 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("foo", addrs.NoKey).Child("child2", addrs.NoKey)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2016-08-18 21:05:42 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01: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 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2016-08-18 21:05:42 +02:00
|
|
|
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
stateOutPath := statePath + ".out"
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2016-08-18 21:05:42 +02:00
|
|
|
c := &StateMvCommand{
|
2017-07-26 19:08:09 +02:00
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-26 19:08:09 +02:00
|
|
|
},
|
2016-08-18 21:05:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", stateOutPath,
|
|
|
|
"module.foo",
|
|
|
|
"module.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, stateOutPath, testStateMvNestedModule_stateOut)
|
|
|
|
testStateOutput(t, statePath, testStateMvNestedModule_stateOutSrc)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvNestedModule_stateOutOriginal)
|
|
|
|
}
|
|
|
|
|
2019-08-02 00:54:09 +02:00
|
|
|
func TestStateMv_toNewModule(t *testing.T) {
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "bar",
|
|
|
|
}.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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2019-08-02 00:54:09 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
stateOutPath1 := statePath + ".out1"
|
|
|
|
stateOutPath2 := statePath + ".out2"
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2019-08-02 00:54:09 +02:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2019-08-02 00:54:09 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", stateOutPath1,
|
|
|
|
"test_instance.bar",
|
|
|
|
"module.bar.test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is correct
|
|
|
|
testStateOutput(t, stateOutPath1, testStateMvNewModule_stateOut)
|
|
|
|
testStateOutput(t, statePath, testStateMvNestedModule_stateOutSrc)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvNewModule_stateOutOriginal)
|
|
|
|
|
|
|
|
// now verify we can move the module itself
|
|
|
|
args = []string{
|
|
|
|
"-state", stateOutPath1,
|
|
|
|
"-state-out", stateOutPath2,
|
|
|
|
"module.bar",
|
|
|
|
"module.foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
testStateOutput(t, stateOutPath2, testStateMvModuleNewModule_stateOut)
|
|
|
|
}
|
2020-01-04 14:29:25 +01:00
|
|
|
|
2017-07-27 21:31:13 +02:00
|
|
|
func TestStateMv_withinBackend(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("backend-unchanged"), td)
|
2017-07-27 21:31:13 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-01-04 14:29:25 +01: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{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2020-01-04 14:29:25 +01:00
|
|
|
)
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "baz",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2020-03-23 20:26:18 +01:00
|
|
|
Dependencies: []addrs.ConfigResource{mustResourceAddr("test_instance.foo")},
|
2020-01-04 14:29:25 +01:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2020-01-04 14:29:25 +01:00
|
|
|
)
|
|
|
|
})
|
2017-07-27 21:31:13 +02:00
|
|
|
|
|
|
|
// the local backend state file is "foo"
|
|
|
|
statePath := "local-state.tfstate"
|
|
|
|
backupPath := "local-state.backup"
|
|
|
|
|
|
|
|
f, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2020-01-04 14:29:25 +01:00
|
|
|
if err := writeStateForTesting(state, f); err != nil {
|
2017-07-27 21:31:13 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-07-27 21:31:13 +02:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-27 21:31:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-backup", backupPath,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePath, testStateMvOutput)
|
|
|
|
testStateOutput(t, backupPath, testStateMvOutputOriginal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateMv_fromBackendToLocal(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("backend-unchanged"), td)
|
2017-07-27 21:31:13 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-09-30 22:07:54 +02:00
|
|
|
state := states.NewState()
|
|
|
|
state.Module(addrs.RootModuleInstance).SetResourceInstanceCurrent(
|
|
|
|
mustResourceAddr("test_instance.foo").Resource.Instance(addrs.NoKey),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
2017-07-27 21:31:13 +02:00
|
|
|
},
|
2020-09-30 22:07:54 +02:00
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-09-30 22:07:54 +02:00
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
state.Module(addrs.RootModuleInstance).SetResourceInstanceCurrent(
|
|
|
|
mustResourceAddr("test_instance.baz").Resource.Instance(addrs.NoKey),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-09-30 22:07:54 +02:00
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
2017-07-27 21:31:13 +02:00
|
|
|
|
|
|
|
// the local backend state file is "foo"
|
|
|
|
statePath := "local-state.tfstate"
|
|
|
|
|
|
|
|
// real "local" state file
|
|
|
|
statePathOut := "real-local.tfstate"
|
|
|
|
|
|
|
|
f, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2020-09-30 22:07:54 +02:00
|
|
|
if err := writeStateForTesting(state, f); err != nil {
|
2017-07-27 21:31:13 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-07-27 21:31:13 +02:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-07-27 21:31:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state-out", statePathOut,
|
|
|
|
"test_instance.foo",
|
|
|
|
"test_instance.bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
testStateOutput(t, statePathOut, testStateMvCount_stateOutSrc)
|
|
|
|
|
|
|
|
// the backend state should be left with only baz
|
|
|
|
testStateOutput(t, statePath, testStateMvOriginal_backend)
|
|
|
|
}
|
|
|
|
|
2020-07-08 23:09:58 +02:00
|
|
|
// This test covers moving the only resource in a module to a new address in
|
|
|
|
// that module, which triggers the maybePruneModule functionality. This caused
|
|
|
|
// a panic report: https://github.com/hashicorp/terraform/issues/25520
|
|
|
|
func TestStateMv_onlyResourceInModule(t *testing.T) {
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
|
|
s.SetResourceInstanceCurrent(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_instance",
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance.Child("foo", addrs.NoKey)),
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
},
|
|
|
|
addrs.AbsProviderConfig{
|
2020-10-05 14:33:49 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-07-08 23:09:58 +02:00
|
|
|
Module: addrs.RootModule,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
testStateOutput(t, statePath, testStateMvOnlyResourceInModule_original)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2020-07-08 23:09:58 +02:00
|
|
|
c := &StateMvCommand{
|
|
|
|
StateMeta{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2020-07-08 23:09:58 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"module.foo.test_instance.foo",
|
|
|
|
"module.foo.test_instance.bar",
|
|
|
|
}
|
|
|
|
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, testStateMvOnlyResourceInModule_output)
|
|
|
|
|
|
|
|
// Test we have backups
|
|
|
|
backups := testStateBackups(t, filepath.Dir(statePath))
|
|
|
|
if len(backups) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", backups)
|
|
|
|
}
|
|
|
|
testStateOutput(t, backups[0], testStateMvOnlyResourceInModule_original)
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:24:28 +01:00
|
|
|
func TestStateMvHelp(t *testing.T) {
|
|
|
|
c := &StateMvCommand{}
|
|
|
|
if strings.ContainsRune(c.Help(), '\t') {
|
|
|
|
t.Fatal("help text contains tab character, which will result in poor formatting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 20:44:12 +02:00
|
|
|
const testStateMvOutputOriginal = `
|
|
|
|
test_instance.baz:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-12 20:44:12 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
2020-01-04 14:29:25 +01:00
|
|
|
|
|
|
|
Dependencies:
|
|
|
|
test_instance.foo
|
2016-04-12 20:44:12 +02:00
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-12 20:44:12 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvOutput = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-12 20:44:12 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.baz:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-12 20:44:12 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
2016-04-12 23:17:59 +02:00
|
|
|
|
2021-10-29 21:53:18 +02:00
|
|
|
const testStateMvBackupAndBackupOutOptionsWithNonLocalBackendOutput = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
2016-08-19 18:05:20 +02:00
|
|
|
const testStateMvCount_stateOut = `
|
|
|
|
test_instance.bar.0:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-19 18:05:20 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.1:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-19 18:05:20 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvCount_stateOutSrc = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-19 18:05:20 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvCount_stateOutOriginal = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-19 18:05:20 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.0:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-19 18:05:20 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.1:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-19 18:05:20 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
2017-02-24 00:27:16 +01:00
|
|
|
const testStateMvLargeCount_stateOut = `
|
|
|
|
test_instance.bar.0:
|
|
|
|
ID = foo0
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.1:
|
|
|
|
ID = foo1
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.2:
|
|
|
|
ID = foo2
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.3:
|
|
|
|
ID = foo3
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.4:
|
|
|
|
ID = foo4
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.5:
|
|
|
|
ID = foo5
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.6:
|
|
|
|
ID = foo6
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.7:
|
|
|
|
ID = foo7
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.8:
|
|
|
|
ID = foo8
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.9:
|
|
|
|
ID = foo9
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.bar.10:
|
|
|
|
ID = foo10
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvLargeCount_stateOutSrc = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvLargeCount_stateOutOriginal = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.0:
|
|
|
|
ID = foo0
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.1:
|
|
|
|
ID = foo1
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.2:
|
|
|
|
ID = foo2
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.3:
|
|
|
|
ID = foo3
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.4:
|
|
|
|
ID = foo4
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.5:
|
|
|
|
ID = foo5
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.6:
|
|
|
|
ID = foo6
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.7:
|
|
|
|
ID = foo7
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.8:
|
|
|
|
ID = foo8
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.9:
|
|
|
|
ID = foo9
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.foo.10:
|
|
|
|
ID = foo10
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-02-24 00:27:16 +01:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
2016-08-18 21:05:42 +02:00
|
|
|
const testStateMvNestedModule_stateOut = `
|
2016-08-18 23:39:07 +02:00
|
|
|
<no state>
|
2016-08-18 21:05:42 +02:00
|
|
|
module.bar.child1:
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-18 21:05:42 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
module.bar.child2:
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-18 21:05:42 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
2019-08-02 00:54:09 +02:00
|
|
|
const testStateMvNewModule_stateOut = `
|
|
|
|
<no state>
|
|
|
|
module.bar:
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-08-02 00:54:09 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvModuleNewModule_stateOut = `
|
|
|
|
<no state>
|
|
|
|
module.foo:
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-08-02 00:54:09 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvNewModule_stateOutOriginal = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2019-08-02 00:54:09 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
2016-08-18 21:05:42 +02:00
|
|
|
const testStateMvNestedModule_stateOutSrc = `
|
|
|
|
<no state>
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvNestedModule_stateOutOriginal = `
|
|
|
|
<no state>
|
|
|
|
module.foo.child1:
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-18 21:05:42 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
module.foo.child2:
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-08-18 21:05:42 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
const testStateMvOutput_stateOut = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-12 23:17:59 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvOutput_stateOutSrc = `
|
|
|
|
<no state>
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvOutput_stateOutOriginal = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-12 23:17:59 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
2016-04-13 00:02:18 +02:00
|
|
|
|
|
|
|
const testStateMvExisting_stateSrc = `
|
|
|
|
<no state>
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvExisting_stateDst = `
|
|
|
|
test_instance.bar:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-13 00:02:18 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
test_instance.qux:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-13 00:02:18 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvExisting_stateSrcOriginal = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-13 00:02:18 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvExisting_stateDstOriginal = `
|
|
|
|
test_instance.qux:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2016-04-13 00:02:18 +02:00
|
|
|
`
|
2017-07-27 21:31:13 +02:00
|
|
|
|
|
|
|
const testStateMvOriginal_backend = `
|
|
|
|
test_instance.baz:
|
|
|
|
ID = foo
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2017-07-27 21:31:13 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
2020-07-08 23:09:58 +02:00
|
|
|
|
|
|
|
const testStateMvOnlyResourceInModule_original = `
|
|
|
|
<no state>
|
|
|
|
module.foo:
|
|
|
|
test_instance.foo.0:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2020-07-08 23:09:58 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|
|
|
|
|
|
|
|
const testStateMvOnlyResourceInModule_output = `
|
|
|
|
<no state>
|
|
|
|
module.foo:
|
|
|
|
test_instance.bar.0:
|
|
|
|
ID = bar
|
2020-10-05 14:33:49 +02:00
|
|
|
provider = provider["registry.terraform.io/hashicorp/test"]
|
2020-07-08 23:09:58 +02:00
|
|
|
bar = value
|
|
|
|
foo = value
|
|
|
|
`
|