2016-11-14 07:18:18 +01:00
|
|
|
package command
|
|
|
|
|
2016-11-30 20:36:54 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2020-04-30 15:21:42 +02:00
|
|
|
"os"
|
2016-11-30 20:36:54 +01:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-07 20:04:40 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2021-01-12 22:13:10 +01:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2016-11-30 20:36:54 +01:00
|
|
|
"github.com/mitchellh/cli"
|
2020-08-07 20:04:40 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2016-11-30 20:36:54 +01:00
|
|
|
)
|
|
|
|
|
2016-11-14 07:18:18 +01:00
|
|
|
// 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"
|
2016-11-30 20:36:54 +01:00
|
|
|
//
|
|
|
|
// 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()
|
2020-09-09 20:13:53 +02:00
|
|
|
ui := cli.NewMockUi()
|
2016-11-30 20:36:54 +01:00
|
|
|
c := &ConsoleCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-11-30 20:36:54 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
// Write a terraform.tvars
|
|
|
|
varFilePath := filepath.Join(tmp, "terraform.tfvars")
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-01-12 22:13:10 +01:00
|
|
|
p.GetSchemaResponse = &providers.GetSchemaResponse{
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2020-08-07 20:04:40 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2020-08-07 20:04:40 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-09-09 20:13:53 +02:00
|
|
|
ui := cli.NewMockUi()
|
2016-11-30 20:36:54 +01:00
|
|
|
c := &ConsoleCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-11-30 20:36:54 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
defer testStdinPipe(t, strings.NewReader("var.foo\n"))()
|
|
|
|
outCloser := testStdoutCapture(t, &output)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
testFixturePath("apply-vars"),
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
outCloser()
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := output.String()
|
2020-09-09 20:13:53 +02:00
|
|
|
if actual != "\"bar\"\n" {
|
2016-11-30 20:36:54 +01:00
|
|
|
t.Fatalf("bad: %q", actual)
|
|
|
|
}
|
|
|
|
}
|
2019-10-09 23:29:40 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-01-12 22:13:10 +01:00
|
|
|
p.GetSchemaResponse = &providers.GetSchemaResponse{
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2020-08-07 20:04:40 +02:00
|
|
|
"test_instance": {
|
2021-01-12 22:13:10 +01:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2020-08-07 20:04:40 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-09-09 20:13:53 +02:00
|
|
|
ui := cli.NewMockUi()
|
2019-10-09 23:29:40 +02:00
|
|
|
c := &ConsoleCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
defer testStdinPipe(t, strings.NewReader("var.foo\n"))()
|
|
|
|
outCloser := testStdoutCapture(t, &output)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
// This test fixture includes variable "foo" {}, which we are
|
|
|
|
// intentionally not setting here.
|
|
|
|
testFixturePath("apply-vars"),
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
outCloser()
|
|
|
|
|
2020-09-09 20:13:53 +02:00
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
2019-10-09 23:29:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 20:13:53 +02:00
|
|
|
if got, want := output.String(), "(known after apply)\n"; got != want {
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
2019-10-09 23:29:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 15:21:42 +02:00
|
|
|
|
2020-10-05 18:37:36 +02:00
|
|
|
func TestConsole_variables(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := &ConsoleCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
testFixturePath("variables"),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:21:42 +02:00
|
|
|
func TestConsole_modules(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("modules"), td)
|
2020-04-30 15:21:42 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2020-08-07 20:04:40 +02:00
|
|
|
p := applyFixtureProvider()
|
2020-09-09 20:13:53 +02:00
|
|
|
ui := cli.NewMockUi()
|
2020-04-30 15:21:42 +02:00
|
|
|
|
|
|
|
c := &ConsoleCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
commands := map[string]string{
|
2020-09-09 20:13:53 +02:00
|
|
|
"module.child.myoutput\n": "\"bar\"\n",
|
|
|
|
"module.count_child[0].myoutput\n": "\"bar\"\n",
|
2020-04-30 15:21:42 +02:00
|
|
|
"local.foo\n": "3\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
testFixturePath("modules"),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|