mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-05-13 06:00:33 -04: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.
245 lines
5.8 KiB
Go
245 lines
5.8 KiB
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
"github.com/hashicorp/terraform/providers"
|
|
"github.com/mitchellh/cli"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
// ConsoleCommand is tested primarily with tests in the "repl" package.
|
|
// It is not tested here because the Console uses a readline-like library
|
|
// that takes over stdin/stdout. It is difficult to test directly. The
|
|
// core logic is tested in "repl"
|
|
//
|
|
// This file still contains some tests using the stdin-based input.
|
|
|
|
func TestConsole_basic(t *testing.T) {
|
|
tmp, cwd := testCwd(t)
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
p := testProvider()
|
|
ui := cli.NewMockUi()
|
|
view, _ := testView(t)
|
|
c := &ConsoleCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
defer testStdinPipe(t, strings.NewReader("1+5\n"))()
|
|
outCloser := testStdoutCapture(t, &output)
|
|
|
|
args := []string{}
|
|
code := c.Run(args)
|
|
outCloser()
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := output.String()
|
|
if actual != "6\n" {
|
|
t.Fatalf("bad: %q", actual)
|
|
}
|
|
}
|
|
|
|
func TestConsole_tfvars(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("apply-vars"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
// Write a terraform.tvars
|
|
varFilePath := filepath.Join(td, "terraform.tfvars")
|
|
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
p := testProvider()
|
|
p.GetSchemaResponse = &providers.GetSchemaResponse{
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"value": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
ui := cli.NewMockUi()
|
|
view, _ := testView(t)
|
|
c := &ConsoleCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
defer testStdinPipe(t, strings.NewReader("var.foo\n"))()
|
|
outCloser := testStdoutCapture(t, &output)
|
|
|
|
args := []string{}
|
|
code := c.Run(args)
|
|
outCloser()
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := output.String()
|
|
if actual != "\"bar\"\n" {
|
|
t.Fatalf("bad: %q", actual)
|
|
}
|
|
}
|
|
|
|
func TestConsole_unsetRequiredVars(t *testing.T) {
|
|
// This test is verifying that it's possible to run "terraform console"
|
|
// without providing values for all required variables, without
|
|
// "terraform console" producing an interactive prompt for those variables
|
|
// or producing errors. Instead, it should allow evaluation in that
|
|
// partial context but see the unset variables values as being unknown.
|
|
//
|
|
// This test fixture includes variable "foo" {}, which we are
|
|
// intentionally not setting here.
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("apply-vars"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
p := testProvider()
|
|
p.GetSchemaResponse = &providers.GetSchemaResponse{
|
|
ResourceTypes: map[string]providers.Schema{
|
|
"test_instance": {
|
|
Block: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"value": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
ui := cli.NewMockUi()
|
|
view, _ := testView(t)
|
|
c := &ConsoleCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
defer testStdinPipe(t, strings.NewReader("var.foo\n"))()
|
|
outCloser := testStdoutCapture(t, &output)
|
|
|
|
args := []string{}
|
|
code := c.Run(args)
|
|
outCloser()
|
|
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
if got, want := output.String(), "(known after apply)\n"; got != want {
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestConsole_variables(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("variables"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
p := testProvider()
|
|
ui := cli.NewMockUi()
|
|
view, _ := testView(t)
|
|
c := &ConsoleCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
commands := map[string]string{
|
|
"var.foo\n": "\"bar\"\n",
|
|
"var.snack\n": "\"popcorn\"\n",
|
|
"var.secret_snack\n": "(sensitive)\n",
|
|
"local.snack_bar\n": "[\n \"popcorn\",\n (sensitive),\n]\n",
|
|
}
|
|
|
|
args := []string{}
|
|
|
|
for cmd, val := range commands {
|
|
var output bytes.Buffer
|
|
defer testStdinPipe(t, strings.NewReader(cmd))()
|
|
outCloser := testStdoutCapture(t, &output)
|
|
code := c.Run(args)
|
|
outCloser()
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := output.String()
|
|
if output.String() != val {
|
|
t.Fatalf("bad: %q, expected %q", actual, val)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestConsole_modules(t *testing.T) {
|
|
td := tempDir(t)
|
|
testCopyDir(t, testFixturePath("modules"), td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
p := applyFixtureProvider()
|
|
ui := cli.NewMockUi()
|
|
view, _ := testView(t)
|
|
|
|
c := &ConsoleCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
View: view,
|
|
},
|
|
}
|
|
|
|
commands := map[string]string{
|
|
"module.child.myoutput\n": "\"bar\"\n",
|
|
"module.count_child[0].myoutput\n": "\"bar\"\n",
|
|
"local.foo\n": "3\n",
|
|
}
|
|
|
|
args := []string{}
|
|
|
|
for cmd, val := range commands {
|
|
var output bytes.Buffer
|
|
defer testStdinPipe(t, strings.NewReader(cmd))()
|
|
outCloser := testStdoutCapture(t, &output)
|
|
code := c.Run(args)
|
|
outCloser()
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := output.String()
|
|
if output.String() != val {
|
|
t.Fatalf("bad: %q, expected %q", actual, val)
|
|
}
|
|
}
|
|
}
|