2017-01-19 05:50:45 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-03-01 22:10:48 +01:00
|
|
|
"bytes"
|
2017-01-19 05:50:45 +01:00
|
|
|
"os"
|
2018-11-20 09:58:59 +01:00
|
|
|
"strings"
|
2017-01-19 05:50:45 +01:00
|
|
|
"testing"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
|
|
|
"github.com/hashicorp/terraform/internal/backend/remote-state/inmem"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatePush_empty(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-push-good"), td)
|
2017-01-19 05:50:45 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-19 05:50:45 +01:00
|
|
|
c := &StatePushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"replace.tfstate"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testStateRead(t, "local-state.tfstate")
|
|
|
|
if !actual.Equal(expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 09:58:59 +01:00
|
|
|
func TestStatePush_lockedState(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-push-good"), td)
|
2018-11-20 09:58:59 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2018-11-20 09:58:59 +01:00
|
|
|
c := &StatePushCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2018-11-20 09:58:59 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock, err := testLockState(testDataDir, "local-state.tfstate")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
args := []string{"replace.tfstate"}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "Error acquiring the state lock") {
|
|
|
|
t.Fatalf("expected a lock error, got: %s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
func TestStatePush_replaceMatch(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-push-replace-match"), td)
|
2017-01-19 05:50:45 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-19 05:50:45 +01:00
|
|
|
c := &StatePushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"replace.tfstate"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testStateRead(t, "local-state.tfstate")
|
|
|
|
if !actual.Equal(expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 22:10:48 +01:00
|
|
|
func TestStatePush_replaceMatchStdin(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-push-replace-match"), td)
|
2017-03-01 22:10:48 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
|
2021-01-26 20:39:11 +01:00
|
|
|
// Set up the replacement to come from stdin
|
2017-03-01 22:10:48 +01:00
|
|
|
var buf bytes.Buffer
|
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
|
|
|
if err := writeStateForTesting(expected, &buf); err != nil {
|
2017-03-01 22:19:44 +01:00
|
|
|
t.Fatalf("err: %s", err)
|
2017-03-01 22:10:48 +01:00
|
|
|
}
|
|
|
|
defer testStdinPipe(t, &buf)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-03-01 22:10:48 +01:00
|
|
|
c := &StatePushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-03-01 22:10:48 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:41:05 +02:00
|
|
|
args := []string{"-force", "-"}
|
2017-03-01 22:10:48 +01:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testStateRead(t, "local-state.tfstate")
|
|
|
|
if !actual.Equal(expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
func TestStatePush_lineageMismatch(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-push-bad-lineage"), td)
|
2017-01-19 05:50:45 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
expected := testStateRead(t, "local-state.tfstate")
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-11-10 00:16:20 +01:00
|
|
|
ui := cli.NewMockUi()
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-19 05:50:45 +01:00
|
|
|
c := &StatePushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"replace.tfstate"}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testStateRead(t, "local-state.tfstate")
|
|
|
|
if !actual.Equal(expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatePush_serialNewer(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-push-serial-newer"), td)
|
2017-01-19 05:50:45 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
expected := testStateRead(t, "local-state.tfstate")
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-19 05:50:45 +01:00
|
|
|
c := &StatePushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"replace.tfstate"}
|
|
|
|
if code := c.Run(args); code != 1 {
|
2018-10-18 21:41:05 +02:00
|
|
|
t.Fatalf("bad: %d", code)
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := testStateRead(t, "local-state.tfstate")
|
|
|
|
if !actual.Equal(expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatePush_serialOlder(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-push-serial-older"), td)
|
2017-01-19 05:50:45 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-01-19 05:50:45 +01:00
|
|
|
c := &StatePushCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 13:19:22 +01:00
|
|
|
View: view,
|
2017-01-19 05:50:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"replace.tfstate"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testStateRead(t, "local-state.tfstate")
|
|
|
|
if !actual.Equal(expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 22:41:34 +02:00
|
|
|
|
|
|
|
func TestStatePush_forceRemoteState(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("inmem-backend"), td)
|
2017-08-01 22:41:34 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
defer inmem.Reset()
|
|
|
|
|
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 := states.NewState()
|
2017-08-01 22:41:34 +02:00
|
|
|
statePath := testStateFile(t, s)
|
|
|
|
|
|
|
|
// init the backend
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 13:19:22 +01:00
|
|
|
view, _ := testView(t)
|
2017-08-01 22:41:34 +02:00
|
|
|
initCmd := &InitCommand{
|
2021-02-16 13:19:22 +01:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-08-01 22:41:34 +02:00
|
|
|
}
|
|
|
|
if code := initCmd.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new workspace
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
newCmd := &WorkspaceNewCommand{
|
2021-02-16 13:19:22 +01:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-08-01 22:41:34 +02:00
|
|
|
}
|
|
|
|
if code := newCmd.Run([]string{"test"}); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// put a dummy state in place, so we have something to force
|
|
|
|
b := backend.TestBackendConfig(t, inmem.New(), nil)
|
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
|
|
|
sMgr, err := b.StateMgr("test")
|
2017-08-01 22:41:34 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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
|
|
|
if err := sMgr.WriteState(states.NewState()); err != nil {
|
2017-08-01 22:41:34 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := sMgr.PersistState(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// push our local state to that new workspace
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c := &StatePushCommand{
|
2021-02-16 13:19:22 +01:00
|
|
|
Meta: Meta{Ui: ui, View: view},
|
2017-08-01 22:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-force", statePath}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|