2014-07-13 05:21:46 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-11-08 00:45:06 +01:00
|
|
|
"fmt"
|
2014-10-01 17:37:57 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-07-13 05:21:46 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-10-08 19:29:54 +02:00
|
|
|
|
2018-11-08 00:45:06 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2014-10-08 19:29:54 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-07-13 05:21:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetaColorize(t *testing.T) {
|
|
|
|
var m *Meta
|
|
|
|
var args, args2 []string
|
|
|
|
|
2014-07-17 18:34:32 +02:00
|
|
|
// Test basic, color
|
2014-07-13 05:21:46 +02:00
|
|
|
m = new(Meta)
|
2014-07-17 18:34:32 +02:00
|
|
|
m.Color = true
|
2014-07-13 05:21:46 +02:00
|
|
|
args = []string{"foo", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err := m.process(args, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-07-13 05:21:46 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if m.Colorize().Disable {
|
|
|
|
t.Fatal("should not be disabled")
|
|
|
|
}
|
|
|
|
|
2014-07-17 18:34:32 +02:00
|
|
|
// Test basic, no change
|
|
|
|
m = new(Meta)
|
|
|
|
args = []string{"foo", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err = m.process(args, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-07-17 18:34:32 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if !m.Colorize().Disable {
|
|
|
|
t.Fatal("should be disabled")
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
// Test disable #1
|
|
|
|
m = new(Meta)
|
2014-09-09 05:41:10 +02:00
|
|
|
m.Color = true
|
2014-07-13 05:21:46 +02:00
|
|
|
args = []string{"foo", "-no-color", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err = m.process(args, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-07-13 05:21:46 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if !m.Colorize().Disable {
|
|
|
|
t.Fatal("should be disabled")
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 20:11:35 +02:00
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:07:08 +01:00
|
|
|
if m.InputMode() != terraform.InputModeStd|terraform.InputModeVarUnset {
|
2014-10-08 19:29:54 +02:00
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 16:59:14 +02:00
|
|
|
func TestMetaInputMode_envVar(t *testing.T) {
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
old := os.Getenv(InputModeEnvVar)
|
|
|
|
defer os.Setenv(InputModeEnvVar, old)
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
args := []string{}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
off := terraform.InputMode(0)
|
|
|
|
on := terraform.InputModeStd | terraform.InputModeVarUnset
|
|
|
|
cases := []struct {
|
|
|
|
EnvVar string
|
|
|
|
Expected terraform.InputMode
|
|
|
|
}{
|
|
|
|
{"false", off},
|
|
|
|
{"0", off},
|
|
|
|
{"true", on},
|
|
|
|
{"1", on},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
os.Setenv(InputModeEnvVar, tc.EnvVar)
|
|
|
|
if m.InputMode() != tc.Expected {
|
|
|
|
t.Fatalf("expected InputMode: %#v, got: %#v", tc.Expected, m.InputMode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode_disable(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{"-input=false"}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode() > 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode_defaultVars(t *testing.T) {
|
2014-10-01 17:37:57 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
// Create a temporary directory for our cwd
|
|
|
|
d := tempDir(t)
|
2017-07-06 17:33:32 +02:00
|
|
|
os.MkdirAll(d, 0755)
|
|
|
|
defer os.RemoveAll(d)
|
|
|
|
defer testChdir(t, d)()
|
2014-10-01 17:37:57 +02:00
|
|
|
|
|
|
|
// Create the default vars file
|
2017-07-06 17:33:32 +02:00
|
|
|
err := ioutil.WriteFile(
|
2014-10-01 17:37:57 +02:00
|
|
|
filepath.Join(d, DefaultVarsFilename),
|
|
|
|
[]byte(""),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
args := []string{}
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err = m.process(args, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-10-01 17:37:57 +02:00
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-26 00:29:46 +02:00
|
|
|
if m.InputMode()&terraform.InputModeVar == 0 {
|
2014-10-08 19:29:54 +02:00
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-10-01 17:37:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 19:29:54 +02:00
|
|
|
|
|
|
|
func TestMetaInputMode_vars(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{"-var", "foo=bar"}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-11-02 03:16:43 +01:00
|
|
|
if m.InputMode()&terraform.InputModeVar == 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.InputMode()&terraform.InputModeVarUnset == 0 {
|
2014-10-08 19:29:54 +02:00
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-12 03:05:23 +02:00
|
|
|
|
|
|
|
func TestMeta_initStatePaths(t *testing.T) {
|
|
|
|
m := new(Meta)
|
|
|
|
m.initStatePaths()
|
|
|
|
|
|
|
|
if m.statePath != DefaultStateFilename {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
if m.stateOutPath != DefaultStateFilename {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
2015-09-11 20:56:20 +02:00
|
|
|
if m.backupPath != DefaultStateFilename+DefaultBackupExtension {
|
2014-10-12 03:05:23 +02:00
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = new(Meta)
|
|
|
|
m.statePath = "foo"
|
|
|
|
m.initStatePaths()
|
|
|
|
|
|
|
|
if m.stateOutPath != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
2015-09-11 20:56:20 +02:00
|
|
|
if m.backupPath != "foo"+DefaultBackupExtension {
|
2014-10-12 03:05:23 +02:00
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = new(Meta)
|
|
|
|
m.stateOutPath = "foo"
|
|
|
|
m.initStatePaths()
|
|
|
|
|
|
|
|
if m.statePath != DefaultStateFilename {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
2015-09-11 20:56:20 +02:00
|
|
|
if m.backupPath != "foo"+DefaultBackupExtension {
|
2014-10-12 03:05:23 +02:00
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 21:05:39 +02:00
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
func TestMeta_Env(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
env := m.Workspace()
|
2017-02-28 19:13:03 +01:00
|
|
|
|
|
|
|
if env != backend.DefaultStateName {
|
|
|
|
t.Fatalf("expected env %q, got env %q", backend.DefaultStateName, env)
|
|
|
|
}
|
|
|
|
|
|
|
|
testEnv := "test_env"
|
2017-05-31 02:13:43 +02:00
|
|
|
if err := m.SetWorkspace(testEnv); err != nil {
|
2017-02-28 19:13:03 +01:00
|
|
|
t.Fatal("error setting env:", err)
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
env = m.Workspace()
|
2017-02-28 19:13:03 +01:00
|
|
|
if env != testEnv {
|
|
|
|
t.Fatalf("expected env %q, got env %q", testEnv, env)
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
if err := m.SetWorkspace(backend.DefaultStateName); err != nil {
|
2017-02-28 19:13:03 +01:00
|
|
|
t.Fatal("error setting env:", err)
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
env = m.Workspace()
|
2017-02-28 19:13:03 +01:00
|
|
|
if env != backend.DefaultStateName {
|
|
|
|
t.Fatalf("expected env %q, got env %q", backend.DefaultStateName, env)
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 05:09:48 +01:00
|
|
|
|
|
|
|
func TestMeta_process(t *testing.T) {
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
// Create a temporary directory for our cwd
|
|
|
|
d := tempDir(t)
|
2017-07-06 17:33:32 +02:00
|
|
|
os.MkdirAll(d, 0755)
|
|
|
|
defer os.RemoveAll(d)
|
|
|
|
defer testChdir(t, d)()
|
2017-03-08 05:09:48 +01:00
|
|
|
|
2018-11-08 00:45:06 +01:00
|
|
|
// At one point it was the responsibility of this process function to
|
|
|
|
// insert fake additional -var-file options into the command line
|
|
|
|
// if the automatic tfvars files were present. This is no longer the
|
|
|
|
// responsibility of process (it happens in collectVariableValues instead)
|
|
|
|
// but we're still testing with these files in place to verify that
|
|
|
|
// they _aren't_ being interpreted by process, since that could otherwise
|
|
|
|
// cause them to be added more than once and mess up the precedence order.
|
2017-06-22 03:22:07 +02:00
|
|
|
defaultVarsfile := "terraform.tfvars"
|
2017-07-06 17:33:32 +02:00
|
|
|
err := ioutil.WriteFile(
|
2017-06-22 03:22:07 +02:00
|
|
|
filepath.Join(d, defaultVarsfile),
|
2017-03-08 05:09:48 +01:00
|
|
|
[]byte(""),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-06-22 03:22:07 +02:00
|
|
|
fileFirstAlphabetical := "a-file.auto.tfvars"
|
2017-03-08 05:09:48 +01:00
|
|
|
err = ioutil.WriteFile(
|
2017-06-22 03:22:07 +02:00
|
|
|
filepath.Join(d, fileFirstAlphabetical),
|
|
|
|
[]byte(""),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
fileLastAlphabetical := "z-file.auto.tfvars"
|
|
|
|
err = ioutil.WriteFile(
|
|
|
|
filepath.Join(d, fileLastAlphabetical),
|
2017-03-08 05:09:48 +01:00
|
|
|
[]byte(""),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-06-22 04:05:05 +02:00
|
|
|
// Regular tfvars files will not be autoloaded
|
|
|
|
fileIgnored := "ignored.tfvars"
|
|
|
|
err = ioutil.WriteFile(
|
|
|
|
filepath.Join(d, fileIgnored),
|
|
|
|
[]byte(""),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-03-08 05:09:48 +01:00
|
|
|
|
2018-11-08 00:45:06 +01:00
|
|
|
tests := []struct {
|
|
|
|
GivenArgs []string
|
|
|
|
FilteredArgs []string
|
|
|
|
ExtraCheck func(*testing.T, *Meta)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{},
|
|
|
|
[]string{},
|
|
|
|
func(t *testing.T, m *Meta) {
|
|
|
|
if got, want := m.color, true; got != want {
|
|
|
|
t.Errorf("wrong m.color value %#v; want %#v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := m.Color, true; got != want {
|
|
|
|
t.Errorf("wrong m.Color value %#v; want %#v", got, want)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-no-color"},
|
|
|
|
[]string{},
|
|
|
|
func(t *testing.T, m *Meta) {
|
|
|
|
if got, want := m.color, false; got != want {
|
|
|
|
t.Errorf("wrong m.color value %#v; want %#v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := m.Color, false; got != want {
|
|
|
|
t.Errorf("wrong m.Color value %#v; want %#v", got, want)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s", test.GivenArgs), func(t *testing.T) {
|
|
|
|
m := new(Meta)
|
|
|
|
m.Color = true // this is the default also for normal use, overridden by -no-color
|
|
|
|
args := test.GivenArgs
|
|
|
|
args, err = m.process(args, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmp.Equal(test.FilteredArgs, args) {
|
|
|
|
t.Errorf("wrong filtered arguments\n%s", cmp.Diff(test.FilteredArgs, args))
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.ExtraCheck != nil {
|
|
|
|
test.ExtraCheck(t, m)
|
|
|
|
}
|
|
|
|
})
|
2017-03-08 05:09:48 +01:00
|
|
|
}
|
|
|
|
}
|