opentofu/command/unlock_test.go
Martin Atkins 8364383c35 Push plugin discovery down into command package
Previously we did plugin discovery in the main package, but as we move
towards versioned plugins we need more information available in order to
resolve plugins, so we move this responsibility into the command package
itself.

For the moment this is just preserving the existing behavior as long as
there are only internal and unversioned plugins present. This is the
final state for provisioners in 0.10, since we don't want to support
versioned provisioners yet. For providers this is just a checkpoint along
the way, since further work is required to apply version constraints from
configuration and support additional plugin search directories.

The automatic plugin discovery behavior is not desirable for tests because
we want to mock the plugins there, so we add a new backdoor for the tests
to use to skip the plugin discovery and just provide their own mock
implementations. Most of this diff is thus noisy rework of the tests to
use this new mechanism.
2017-06-09 14:03:59 -07:00

103 lines
2.0 KiB
Go

package command
import (
"os"
"testing"
"github.com/hashicorp/terraform/helper/copy"
"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(testState(), 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())
}
}
// 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())
}
}