Add complete unlock test

Test actual unlock failure and success through the the unlock command.
This commit is contained in:
James Bardin 2017-02-17 20:28:00 -05:00
parent 2392455a67
commit 5095d7c6a7
3 changed files with 59 additions and 1 deletions

View File

@ -25,6 +25,7 @@ import (
backendlegacy "github.com/hashicorp/terraform/backend/legacy" backendlegacy "github.com/hashicorp/terraform/backend/legacy"
backendlocal "github.com/hashicorp/terraform/backend/local" backendlocal "github.com/hashicorp/terraform/backend/local"
backendconsul "github.com/hashicorp/terraform/backend/remote-state/consul" backendconsul "github.com/hashicorp/terraform/backend/remote-state/consul"
backendinmem "github.com/hashicorp/terraform/backend/remote-state/inmem"
) )
// BackendOpts are the options used to initialize a backend.Backend. // BackendOpts are the options used to initialize a backend.Backend.
@ -1409,6 +1410,7 @@ func init() {
Backends = map[string]func() backend.Backend{ Backends = map[string]func() backend.Backend{
"local": func() backend.Backend { return &backendlocal.Local{} }, "local": func() backend.Backend { return &backendlocal.Local{} },
"consul": func() backend.Backend { return backendconsul.New() }, "consul": func() backend.Backend { return backendconsul.New() },
"inmem": func() backend.Backend { return backendinmem.New() },
} }
// Add the legacy remote backends that haven't yet been convertd to // Add the legacy remote backends that haven't yet been convertd to

View File

@ -0,0 +1,5 @@
terraform {
backend "inmem" {
lock_id = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"
}
}

View File

@ -4,13 +4,13 @@ import (
"os" "os"
"testing" "testing"
"github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
// Since we can't unlock a local state file, just test that calling unlock // Since we can't unlock a local state file, just test that calling unlock
// doesn't fail. // doesn't fail.
// TODO: mock remote state for UI testing
func TestUnlock(t *testing.T) { func TestUnlock(t *testing.T) {
td := tempDir(t) td := tempDir(t)
os.MkdirAll(td, 0755) os.MkdirAll(td, 0755)
@ -49,3 +49,54 @@ func TestUnlock(t *testing.T) {
t.Fatalf("bad: %d\n%s\n%s", code, ui.OutputWriter.String(), ui.ErrorWriter.String()) 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)
copy.CopyDir(testFixturePath("backend-inmem-locked"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
// 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())
}
}