mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
The clistate package includes a Locker interface which provides a simple way for the local backend to lock and unlock state, while providing feedback to the user if there is a delay while waiting for the lock. Prior to this commit, the backend was responsible for initializing the Locker, passing through direct access to the cli.Ui instance. This structure prevented commands from implementing different implementations of the state locker UI. In this commit, we: - Move the responsibility of creating the appropriate Locker to the source of the Operation; - Add the ability to set the context for a Locker via a WithContext method; - Replace the Locker's cli.Ui and Colorize members with a StateLocker view; - Implement views.StateLocker for human-readable UI; - Update the Locker interface to return detailed diagnostics instead of errors, reducing its direct interactions with UI; - Add a Timeout() method on Locker to allow the remote backend to continue to misuse the -lock-timeout flag to cancel pending runs. When an Operation is created, the StateLocker field must now be populated with an implementation of Locker. For situations where locking is disabled, this can be a no-op locker. This change has no significant effect on the operation of Terraform, with the exception of slightly different formatting of errors when state locking or unlocking fails.
121 lines
2.4 KiB
Go
121 lines
2.4 KiB
Go
package command
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/backend/remote-state/inmem"
|
|
"github.com/mitchellh/cli"
|
|
|
|
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
|
|
)
|
|
|
|
// 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 = legacy.WriteState(legacy.NewState(), f)
|
|
f.Close()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
}
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
view, _ := testView(t)
|
|
c := &UnlockCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
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 != cli.RunResultHelp {
|
|
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)
|
|
view, _ := testView(t)
|
|
ci := &InitCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
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,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
// 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,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
// 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())
|
|
}
|
|
|
|
}
|