2017-02-06 19:45:30 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-01 22:41:34 +02:00
|
|
|
"github.com/hashicorp/terraform/backend/remote-state/inmem"
|
2017-02-18 02:28:00 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2017-02-06 19:45:30 +01:00
|
|
|
"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)
|
|
|
|
}
|
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
|
|
|
err = terraform.WriteState(terraform.NewState(), f)
|
2017-02-06 19:45:30 +01:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &UnlockCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-02-06 19:45:30 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-15 17:53:19 +01: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 19:45:30 +01:00
|
|
|
}
|
2019-03-12 22:57:26 +01:00
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
2017-02-06 19:45:30 +01:00
|
|
|
}
|
2017-02-18 02:28:00 +01:00
|
|
|
|
|
|
|
// Newly configured backend
|
|
|
|
func TestUnlock_inmemBackend(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("backend-inmem-locked"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2017-08-01 22:41:34 +02:00
|
|
|
defer inmem.Reset()
|
2017-02-18 02:28:00 +01:00
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|