2017-02-06 12:45:30 -06:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-17 10:42:17 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/backend/remote-state/inmem"
|
2017-02-06 12:45:30 -06:00
|
|
|
"github.com/mitchellh/cli"
|
2020-11-18 12:41:33 -06:00
|
|
|
|
|
|
|
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
|
2017-02-06 12:45:30 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2020-11-18 12:41:33 -06:00
|
|
|
err = legacy.WriteState(legacy.NewState(), f)
|
2017-02-06 12:45:30 -06:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-02-06 12:45:30 -06:00
|
|
|
c := &UnlockCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2021-02-16 06:19:22 -06:00
|
|
|
View: view,
|
2017-02-06 12:45:30 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-15 10:53:19 -06:00
|
|
|
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())
|
2017-02-06 12:45:30 -06:00
|
|
|
}
|
2019-03-12 16:57:26 -05:00
|
|
|
|
|
|
|
// make sure we don't crash with arguments in the wrong order
|
|
|
|
args = []string{
|
|
|
|
"LOCK_ID",
|
|
|
|
"-force",
|
|
|
|
}
|
|
|
|
|
2021-02-02 09:35:45 -06:00
|
|
|
if code := c.Run(args); code != cli.RunResultHelp {
|
2019-03-12 16:57:26 -05:00
|
|
|
t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String())
|
|
|
|
}
|
2017-02-06 12:45:30 -06:00
|
|
|
}
|
2017-02-17 19:28:00 -06:00
|
|
|
|
|
|
|
// Newly configured backend
|
|
|
|
func TestUnlock_inmemBackend(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 11:48:25 -05:00
|
|
|
testCopyDir(t, testFixturePath("backend-inmem-locked"), td)
|
2017-02-17 19:28:00 -06:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-08-01 15:41:34 -05:00
|
|
|
defer inmem.Reset()
|
2017-02-17 19:28:00 -06:00
|
|
|
|
|
|
|
// init backend
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-16 06:19:22 -06:00
|
|
|
view, _ := testView(t)
|
2017-02-17 19:28:00 -06:00
|
|
|
ci := &InitCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2017-02-17 19:28:00 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if code := ci.Run(nil); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
c := &UnlockCommand{
|
|
|
|
Meta: Meta{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2017-02-17 19:28:00 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2021-02-16 06:19:22 -06:00
|
|
|
Ui: ui,
|
|
|
|
View: view,
|
2017-02-17 19:28:00 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|