github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/meta_test.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/hashicorp/terraform/backend"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestMetaColorize(t *testing.T) {
    16  	var m *Meta
    17  	var args, args2 []string
    18  
    19  	// Test basic, color
    20  	m = new(Meta)
    21  	m.Color = true
    22  	args = []string{"foo", "bar"}
    23  	args2 = []string{"foo", "bar"}
    24  	args, err := m.process(args, false)
    25  	if err != nil {
    26  		t.Fatalf("err: %s", err)
    27  	}
    28  	if !reflect.DeepEqual(args, args2) {
    29  		t.Fatalf("bad: %#v", args)
    30  	}
    31  	if m.Colorize().Disable {
    32  		t.Fatal("should not be disabled")
    33  	}
    34  
    35  	// Test basic, no change
    36  	m = new(Meta)
    37  	args = []string{"foo", "bar"}
    38  	args2 = []string{"foo", "bar"}
    39  	args, err = m.process(args, false)
    40  	if err != nil {
    41  		t.Fatalf("err: %s", err)
    42  	}
    43  	if !reflect.DeepEqual(args, args2) {
    44  		t.Fatalf("bad: %#v", args)
    45  	}
    46  	if !m.Colorize().Disable {
    47  		t.Fatal("should be disabled")
    48  	}
    49  
    50  	// Test disable #1
    51  	m = new(Meta)
    52  	m.Color = true
    53  	args = []string{"foo", "-no-color", "bar"}
    54  	args2 = []string{"foo", "bar"}
    55  	args, err = m.process(args, false)
    56  	if err != nil {
    57  		t.Fatalf("err: %s", err)
    58  	}
    59  	if !reflect.DeepEqual(args, args2) {
    60  		t.Fatalf("bad: %#v", args)
    61  	}
    62  	if !m.Colorize().Disable {
    63  		t.Fatal("should be disabled")
    64  	}
    65  }
    66  
    67  func TestMetaInputMode(t *testing.T) {
    68  	test = false
    69  	defer func() { test = true }()
    70  
    71  	m := new(Meta)
    72  	args := []string{}
    73  
    74  	fs := m.flagSet("foo")
    75  	if err := fs.Parse(args); err != nil {
    76  		t.Fatalf("err: %s", err)
    77  	}
    78  
    79  	if m.InputMode() != terraform.InputModeStd|terraform.InputModeVarUnset {
    80  		t.Fatalf("bad: %#v", m.InputMode())
    81  	}
    82  }
    83  
    84  func TestMetaInputMode_envVar(t *testing.T) {
    85  	test = false
    86  	defer func() { test = true }()
    87  	old := os.Getenv(InputModeEnvVar)
    88  	defer os.Setenv(InputModeEnvVar, old)
    89  
    90  	m := new(Meta)
    91  	args := []string{}
    92  
    93  	fs := m.flagSet("foo")
    94  	if err := fs.Parse(args); err != nil {
    95  		t.Fatalf("err: %s", err)
    96  	}
    97  
    98  	off := terraform.InputMode(0)
    99  	on := terraform.InputModeStd | terraform.InputModeVarUnset
   100  	cases := []struct {
   101  		EnvVar   string
   102  		Expected terraform.InputMode
   103  	}{
   104  		{"false", off},
   105  		{"0", off},
   106  		{"true", on},
   107  		{"1", on},
   108  	}
   109  
   110  	for _, tc := range cases {
   111  		os.Setenv(InputModeEnvVar, tc.EnvVar)
   112  		if m.InputMode() != tc.Expected {
   113  			t.Fatalf("expected InputMode: %#v, got: %#v", tc.Expected, m.InputMode())
   114  		}
   115  	}
   116  }
   117  
   118  func TestMetaInputMode_disable(t *testing.T) {
   119  	test = false
   120  	defer func() { test = true }()
   121  
   122  	m := new(Meta)
   123  	args := []string{"-input=false"}
   124  
   125  	fs := m.flagSet("foo")
   126  	if err := fs.Parse(args); err != nil {
   127  		t.Fatalf("err: %s", err)
   128  	}
   129  
   130  	if m.InputMode() > 0 {
   131  		t.Fatalf("bad: %#v", m.InputMode())
   132  	}
   133  }
   134  
   135  func TestMetaInputMode_defaultVars(t *testing.T) {
   136  	test = false
   137  	defer func() { test = true }()
   138  
   139  	// Create a temporary directory for our cwd
   140  	d := tempDir(t)
   141  	os.MkdirAll(d, 0755)
   142  	defer os.RemoveAll(d)
   143  	defer testChdir(t, d)()
   144  
   145  	// Create the default vars file
   146  	err := ioutil.WriteFile(
   147  		filepath.Join(d, DefaultVarsFilename),
   148  		[]byte(""),
   149  		0644)
   150  	if err != nil {
   151  		t.Fatalf("err: %s", err)
   152  	}
   153  
   154  	m := new(Meta)
   155  	args := []string{}
   156  	args, err = m.process(args, false)
   157  	if err != nil {
   158  		t.Fatalf("err: %s", err)
   159  	}
   160  
   161  	fs := m.flagSet("foo")
   162  	if err := fs.Parse(args); err != nil {
   163  		t.Fatalf("err: %s", err)
   164  	}
   165  
   166  	if m.InputMode()&terraform.InputModeVar == 0 {
   167  		t.Fatalf("bad: %#v", m.InputMode())
   168  	}
   169  }
   170  
   171  func TestMetaInputMode_vars(t *testing.T) {
   172  	test = false
   173  	defer func() { test = true }()
   174  
   175  	m := new(Meta)
   176  	args := []string{"-var", "foo=bar"}
   177  
   178  	fs := m.flagSet("foo")
   179  	if err := fs.Parse(args); err != nil {
   180  		t.Fatalf("err: %s", err)
   181  	}
   182  
   183  	if m.InputMode()&terraform.InputModeVar == 0 {
   184  		t.Fatalf("bad: %#v", m.InputMode())
   185  	}
   186  
   187  	if m.InputMode()&terraform.InputModeVarUnset == 0 {
   188  		t.Fatalf("bad: %#v", m.InputMode())
   189  	}
   190  }
   191  
   192  func TestMeta_initStatePaths(t *testing.T) {
   193  	m := new(Meta)
   194  	m.initStatePaths()
   195  
   196  	if m.statePath != DefaultStateFilename {
   197  		t.Fatalf("bad: %#v", m)
   198  	}
   199  	if m.stateOutPath != DefaultStateFilename {
   200  		t.Fatalf("bad: %#v", m)
   201  	}
   202  	if m.backupPath != DefaultStateFilename+DefaultBackupExtension {
   203  		t.Fatalf("bad: %#v", m)
   204  	}
   205  
   206  	m = new(Meta)
   207  	m.statePath = "foo"
   208  	m.initStatePaths()
   209  
   210  	if m.stateOutPath != "foo" {
   211  		t.Fatalf("bad: %#v", m)
   212  	}
   213  	if m.backupPath != "foo"+DefaultBackupExtension {
   214  		t.Fatalf("bad: %#v", m)
   215  	}
   216  
   217  	m = new(Meta)
   218  	m.stateOutPath = "foo"
   219  	m.initStatePaths()
   220  
   221  	if m.statePath != DefaultStateFilename {
   222  		t.Fatalf("bad: %#v", m)
   223  	}
   224  	if m.backupPath != "foo"+DefaultBackupExtension {
   225  		t.Fatalf("bad: %#v", m)
   226  	}
   227  }
   228  
   229  func TestMeta_addModuleDepthFlag(t *testing.T) {
   230  	old := os.Getenv(ModuleDepthEnvVar)
   231  	defer os.Setenv(ModuleDepthEnvVar, old)
   232  
   233  	cases := map[string]struct {
   234  		EnvVar   string
   235  		Args     []string
   236  		Expected int
   237  	}{
   238  		"env var sets value when no flag present": {
   239  			EnvVar:   "4",
   240  			Args:     []string{},
   241  			Expected: 4,
   242  		},
   243  		"flag overrides envvar": {
   244  			EnvVar:   "4",
   245  			Args:     []string{"-module-depth=-1"},
   246  			Expected: -1,
   247  		},
   248  		"negative envvar works": {
   249  			EnvVar:   "-1",
   250  			Args:     []string{},
   251  			Expected: -1,
   252  		},
   253  		"invalid envvar is ignored": {
   254  			EnvVar:   "-#",
   255  			Args:     []string{},
   256  			Expected: ModuleDepthDefault,
   257  		},
   258  		"empty envvar is okay too": {
   259  			EnvVar:   "",
   260  			Args:     []string{},
   261  			Expected: ModuleDepthDefault,
   262  		},
   263  	}
   264  
   265  	for tn, tc := range cases {
   266  		m := new(Meta)
   267  		var moduleDepth int
   268  		flags := flag.NewFlagSet("test", flag.ContinueOnError)
   269  		os.Setenv(ModuleDepthEnvVar, tc.EnvVar)
   270  		m.addModuleDepthFlag(flags, &moduleDepth)
   271  		err := flags.Parse(tc.Args)
   272  		if err != nil {
   273  			t.Fatalf("%s: err: %#v", tn, err)
   274  		}
   275  		if moduleDepth != tc.Expected {
   276  			t.Fatalf("%s: expected: %#v, got: %#v", tn, tc.Expected, moduleDepth)
   277  		}
   278  	}
   279  }
   280  
   281  func TestMeta_Env(t *testing.T) {
   282  	td := tempDir(t)
   283  	os.MkdirAll(td, 0755)
   284  	defer os.RemoveAll(td)
   285  	defer testChdir(t, td)()
   286  
   287  	m := new(Meta)
   288  
   289  	env := m.Workspace()
   290  
   291  	if env != backend.DefaultStateName {
   292  		t.Fatalf("expected env %q, got env %q", backend.DefaultStateName, env)
   293  	}
   294  
   295  	testEnv := "test_env"
   296  	if err := m.SetWorkspace(testEnv); err != nil {
   297  		t.Fatal("error setting env:", err)
   298  	}
   299  
   300  	env = m.Workspace()
   301  	if env != testEnv {
   302  		t.Fatalf("expected env %q, got env %q", testEnv, env)
   303  	}
   304  
   305  	if err := m.SetWorkspace(backend.DefaultStateName); err != nil {
   306  		t.Fatal("error setting env:", err)
   307  	}
   308  
   309  	env = m.Workspace()
   310  	if env != backend.DefaultStateName {
   311  		t.Fatalf("expected env %q, got env %q", backend.DefaultStateName, env)
   312  	}
   313  }
   314  
   315  func TestMeta_process(t *testing.T) {
   316  	test = false
   317  	defer func() { test = true }()
   318  
   319  	// Create a temporary directory for our cwd
   320  	d := tempDir(t)
   321  	os.MkdirAll(d, 0755)
   322  	defer os.RemoveAll(d)
   323  	defer testChdir(t, d)()
   324  
   325  	// Create two vars files
   326  	defaultVarsfile := "terraform.tfvars"
   327  	err := ioutil.WriteFile(
   328  		filepath.Join(d, defaultVarsfile),
   329  		[]byte(""),
   330  		0644)
   331  	if err != nil {
   332  		t.Fatalf("err: %s", err)
   333  	}
   334  	fileFirstAlphabetical := "a-file.auto.tfvars"
   335  	err = ioutil.WriteFile(
   336  		filepath.Join(d, fileFirstAlphabetical),
   337  		[]byte(""),
   338  		0644)
   339  	if err != nil {
   340  		t.Fatalf("err: %s", err)
   341  	}
   342  	fileLastAlphabetical := "z-file.auto.tfvars"
   343  	err = ioutil.WriteFile(
   344  		filepath.Join(d, fileLastAlphabetical),
   345  		[]byte(""),
   346  		0644)
   347  	if err != nil {
   348  		t.Fatalf("err: %s", err)
   349  	}
   350  	// Regular tfvars files will not be autoloaded
   351  	fileIgnored := "ignored.tfvars"
   352  	err = ioutil.WriteFile(
   353  		filepath.Join(d, fileIgnored),
   354  		[]byte(""),
   355  		0644)
   356  	if err != nil {
   357  		t.Fatalf("err: %s", err)
   358  	}
   359  
   360  	m := new(Meta)
   361  	args := []string{}
   362  	args, err = m.process(args, true)
   363  	if err != nil {
   364  		t.Fatalf("err: %s", err)
   365  	}
   366  
   367  	if len(args) != 6 {
   368  		t.Fatalf("expected 6 args, got %v", args)
   369  	}
   370  
   371  	if args[0] != "-var-file-default" {
   372  		t.Fatalf("expected %q, got %q", "-var-file-default", args[0])
   373  	}
   374  	if args[1] != defaultVarsfile {
   375  		t.Fatalf("expected %q, got %q", defaultVarsfile, args[1])
   376  	}
   377  	if args[2] != "-var-file-default" {
   378  		t.Fatalf("expected %q, got %q", "-var-file-default", args[2])
   379  	}
   380  	if args[3] != fileFirstAlphabetical {
   381  		t.Fatalf("expected %q, got %q", fileFirstAlphabetical, args[3])
   382  	}
   383  	if args[4] != "-var-file-default" {
   384  		t.Fatalf("expected %q, got %q", "-var-file-default", args[4])
   385  	}
   386  	if args[5] != fileLastAlphabetical {
   387  		t.Fatalf("expected %q, got %q", fileLastAlphabetical, args[5])
   388  	}
   389  }