mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
04be220f5f
helper/copy CopyDir was used heavily in tests. It differes from internal/copydir in a few ways, the main one being that it creates the dst directory while the internal version expected the dst to exist (there are other differences, which is why I did not just switch tests to using internal's CopyDir). I moved the CopyDir func from helper/copy into command_test.go; I could also have moved it into internal/copy and named it something like CreateDirAndCopy so if that seems like a better option please let me know. helper/copy/CopyFile was used in a couple of spots so I moved it into internal, at which point I thought it made more sense to rename the package copy (instead of copydir). There's also a `go mod tidy` included.
279 lines
6.5 KiB
Go
279 lines
6.5 KiB
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
"github.com/hashicorp/terraform/backend/remote-state/inmem"
|
|
"github.com/hashicorp/terraform/states"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestStatePush_empty(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("state-push-good"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StatePushCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestStatePush_lockedState(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("state-push-good"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StatePushCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
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())
|
|
}
|
|
}
|
|
|
|
func TestStatePush_replaceMatch(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("state-push-replace-match"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StatePushCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestStatePush_replaceMatchStdin(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("state-push-replace-match"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
// Setup the replacement to come from stdin
|
|
var buf bytes.Buffer
|
|
if err := writeStateForTesting(expected, &buf); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer testStdinPipe(t, &buf)()
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StatePushCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{"-force", "-"}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestStatePush_lineageMismatch(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("state-push-bad-lineage"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
expected := testStateRead(t, "local-state.tfstate")
|
|
|
|
p := testProvider()
|
|
ui := cli.NewMockUi()
|
|
c := &StatePushCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
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)
|
|
testCopyDir(t, testFixturePath("state-push-serial-newer"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
expected := testStateRead(t, "local-state.tfstate")
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StatePushCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{"replace.tfstate"}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: %d", code)
|
|
}
|
|
|
|
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)
|
|
testCopyDir(t, testFixturePath("state-push-serial-older"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
expected := testStateRead(t, "replace.tfstate")
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StatePushCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestStatePush_forceRemoteState(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("inmem-backend"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
defer inmem.Reset()
|
|
|
|
s := states.NewState()
|
|
statePath := testStateFile(t, s)
|
|
|
|
// init the backend
|
|
ui := new(cli.MockUi)
|
|
initCmd := &InitCommand{
|
|
Meta: Meta{Ui: ui},
|
|
}
|
|
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{
|
|
Meta: Meta{Ui: ui},
|
|
}
|
|
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)
|
|
sMgr, err := b.StateMgr("test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := sMgr.WriteState(states.NewState()); err != nil {
|
|
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{
|
|
Meta: Meta{Ui: ui},
|
|
}
|
|
|
|
args := []string{"-force", statePath}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
}
|