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.
114 lines
2.2 KiB
Go
114 lines
2.2 KiB
Go
package command
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/backend/remote-state/inmem"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
// Since we can't unlock a local state file, just test that calling unlock
|
|
// doesn't fail.
|
|
func TestUnlock(t *testing.T) {
|
|
td := tempDir(t)
|
|
os.MkdirAll(td, 0755)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
// Write the legacy state
|
|
statePath := DefaultStateFilename
|
|
{
|
|
f, err := os.Create(statePath)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
err = terraform.WriteState(terraform.NewState(), f)
|
|
f.Close()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &UnlockCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-force",
|
|
"LOCK_ID",
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
|
|
}
|
|
|
|
// make sure we don't crash with arguments in the wrong order
|
|
args = []string{
|
|
"LOCK_ID",
|
|
"-force",
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
// Newly configured backend
|
|
func TestUnlock_inmemBackend(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("backend-inmem-locked"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
defer inmem.Reset()
|
|
|
|
// init backend
|
|
ui := new(cli.MockUi)
|
|
ci := &InitCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
if code := ci.Run(nil); code != 0 {
|
|
t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter)
|
|
}
|
|
|
|
ui = new(cli.MockUi)
|
|
c := &UnlockCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
// run with the incorrect lock ID
|
|
args := []string{
|
|
"-force",
|
|
"LOCK_ID",
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
|
|
}
|
|
|
|
ui = new(cli.MockUi)
|
|
c = &UnlockCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
// lockID set in the test fixture
|
|
args[1] = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
|
|
}
|
|
|
|
}
|