github.com/biggiemac/terraform@v0.6.12-0.20160217180759-34b7665af0d6/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  // This is the directory where our test fixtures are.
    11  const fixtureDir = "./test-fixtures"
    12  
    13  func TestConfigCopy(t *testing.T) {
    14  	c := testConfig(t, "copy-basic")
    15  	rOrig := c.Resources[0]
    16  	rCopy := rOrig.Copy()
    17  
    18  	if rCopy.Name != rOrig.Name {
    19  		t.Fatalf("Expected names to equal: %q <=> %q", rCopy.Name, rOrig.Name)
    20  	}
    21  
    22  	if rCopy.Type != rOrig.Type {
    23  		t.Fatalf("Expected types to equal: %q <=> %q", rCopy.Type, rOrig.Type)
    24  	}
    25  
    26  	origCount := rOrig.RawCount.Config()["count"]
    27  	rCopy.RawCount.Config()["count"] = "5"
    28  	if rOrig.RawCount.Config()["count"] != origCount {
    29  		t.Fatalf("Expected RawCount to be copied, but it behaves like a ref!")
    30  	}
    31  
    32  	rCopy.RawConfig.Config()["newfield"] = "hello"
    33  	if rOrig.RawConfig.Config()["newfield"] == "hello" {
    34  		t.Fatalf("Expected RawConfig to be copied, but it behaves like a ref!")
    35  	}
    36  
    37  	rCopy.Provisioners = append(rCopy.Provisioners, &Provisioner{})
    38  	if len(rOrig.Provisioners) == len(rCopy.Provisioners) {
    39  		t.Fatalf("Expected Provisioners to be copied, but it behaves like a ref!")
    40  	}
    41  
    42  	if rCopy.Provider != rOrig.Provider {
    43  		t.Fatalf("Expected providers to equal: %q <=> %q",
    44  			rCopy.Provider, rOrig.Provider)
    45  	}
    46  
    47  	rCopy.DependsOn[0] = "gotchya"
    48  	if rOrig.DependsOn[0] == rCopy.DependsOn[0] {
    49  		t.Fatalf("Expected DependsOn to be copied, but it behaves like a ref!")
    50  	}
    51  
    52  	rCopy.Lifecycle.IgnoreChanges[0] = "gotchya"
    53  	if rOrig.Lifecycle.IgnoreChanges[0] == rCopy.Lifecycle.IgnoreChanges[0] {
    54  		t.Fatalf("Expected Lifecycle to be copied, but it behaves like a ref!")
    55  	}
    56  
    57  }
    58  
    59  func TestConfigCount(t *testing.T) {
    60  	c := testConfig(t, "count-int")
    61  	actual, err := c.Resources[0].Count()
    62  	if err != nil {
    63  		t.Fatalf("err: %s", err)
    64  	}
    65  	if actual != 5 {
    66  		t.Fatalf("bad: %#v", actual)
    67  	}
    68  }
    69  
    70  func TestConfigCount_string(t *testing.T) {
    71  	c := testConfig(t, "count-string")
    72  	actual, err := c.Resources[0].Count()
    73  	if err != nil {
    74  		t.Fatalf("err: %s", err)
    75  	}
    76  	if actual != 5 {
    77  		t.Fatalf("bad: %#v", actual)
    78  	}
    79  }
    80  
    81  func TestConfigCount_var(t *testing.T) {
    82  	c := testConfig(t, "count-var")
    83  	_, err := c.Resources[0].Count()
    84  	if err == nil {
    85  		t.Fatalf("should error")
    86  	}
    87  }
    88  
    89  func TestConfigValidate(t *testing.T) {
    90  	c := testConfig(t, "validate-good")
    91  	if err := c.Validate(); err != nil {
    92  		t.Fatalf("err: %s", err)
    93  	}
    94  }
    95  
    96  func TestConfigValidate_badDependsOn(t *testing.T) {
    97  	c := testConfig(t, "validate-bad-depends-on")
    98  	if err := c.Validate(); err == nil {
    99  		t.Fatal("should not be valid")
   100  	}
   101  }
   102  
   103  func TestConfigValidate_countInt(t *testing.T) {
   104  	c := testConfig(t, "validate-count-int")
   105  	if err := c.Validate(); err != nil {
   106  		t.Fatalf("err: %s", err)
   107  	}
   108  }
   109  
   110  func TestConfigValidate_countBadContext(t *testing.T) {
   111  	c := testConfig(t, "validate-count-bad-context")
   112  
   113  	err := c.Validate()
   114  
   115  	expected := []string{
   116  		"no_count_in_output: count variables are only valid within resources",
   117  		"no_count_in_module: count variables are only valid within resources",
   118  	}
   119  	for _, exp := range expected {
   120  		if !strings.Contains(err.Error(), exp) {
   121  			t.Fatalf("expected: %q,\nto contain: %q", err, exp)
   122  		}
   123  	}
   124  }
   125  
   126  func TestConfigValidate_countCountVar(t *testing.T) {
   127  	c := testConfig(t, "validate-count-count-var")
   128  	if err := c.Validate(); err == nil {
   129  		t.Fatal("should not be valid")
   130  	}
   131  }
   132  
   133  func TestConfigValidate_countModuleVar(t *testing.T) {
   134  	c := testConfig(t, "validate-count-module-var")
   135  	if err := c.Validate(); err == nil {
   136  		t.Fatal("should not be valid")
   137  	}
   138  }
   139  
   140  func TestConfigValidate_countNotInt(t *testing.T) {
   141  	c := testConfig(t, "validate-count-not-int")
   142  	if err := c.Validate(); err == nil {
   143  		t.Fatal("should not be valid")
   144  	}
   145  }
   146  
   147  func TestConfigValidate_countResourceVar(t *testing.T) {
   148  	c := testConfig(t, "validate-count-resource-var")
   149  	if err := c.Validate(); err == nil {
   150  		t.Fatal("should not be valid")
   151  	}
   152  }
   153  
   154  func TestConfigValidate_countUserVar(t *testing.T) {
   155  	c := testConfig(t, "validate-count-user-var")
   156  	if err := c.Validate(); err != nil {
   157  		t.Fatalf("err: %s", err)
   158  	}
   159  }
   160  
   161  func TestConfigValidate_countVar(t *testing.T) {
   162  	c := testConfig(t, "validate-count-var")
   163  	if err := c.Validate(); err != nil {
   164  		t.Fatalf("err: %s", err)
   165  	}
   166  }
   167  
   168  func TestConfigValidate_countVarInvalid(t *testing.T) {
   169  	c := testConfig(t, "validate-count-var-invalid")
   170  	if err := c.Validate(); err == nil {
   171  		t.Fatal("should not be valid")
   172  	}
   173  }
   174  
   175  func TestConfigValidate_dependsOnVar(t *testing.T) {
   176  	c := testConfig(t, "validate-depends-on-var")
   177  	if err := c.Validate(); err == nil {
   178  		t.Fatal("should not be valid")
   179  	}
   180  }
   181  
   182  func TestConfigValidate_dupModule(t *testing.T) {
   183  	c := testConfig(t, "validate-dup-module")
   184  	if err := c.Validate(); err == nil {
   185  		t.Fatal("should not be valid")
   186  	}
   187  }
   188  
   189  func TestConfigValidate_dupResource(t *testing.T) {
   190  	c := testConfig(t, "validate-dup-resource")
   191  	if err := c.Validate(); err == nil {
   192  		t.Fatal("should not be valid")
   193  	}
   194  }
   195  
   196  func TestConfigValidate_moduleNameBad(t *testing.T) {
   197  	c := testConfig(t, "validate-module-name-bad")
   198  	if err := c.Validate(); err == nil {
   199  		t.Fatal("should not be valid")
   200  	}
   201  }
   202  
   203  func TestConfigValidate_moduleSourceVar(t *testing.T) {
   204  	c := testConfig(t, "validate-module-source-var")
   205  	if err := c.Validate(); err == nil {
   206  		t.Fatal("should not be valid")
   207  	}
   208  }
   209  
   210  func TestConfigValidate_moduleVarInt(t *testing.T) {
   211  	c := testConfig(t, "validate-module-var-int")
   212  	if err := c.Validate(); err != nil {
   213  		t.Fatalf("should be valid: %s", err)
   214  	}
   215  }
   216  
   217  func TestConfigValidate_moduleVarMap(t *testing.T) {
   218  	c := testConfig(t, "validate-module-var-map")
   219  	if err := c.Validate(); err == nil {
   220  		t.Fatal("should be invalid")
   221  	}
   222  }
   223  
   224  func TestConfigValidate_moduleVarSelf(t *testing.T) {
   225  	c := testConfig(t, "validate-module-var-self")
   226  	if err := c.Validate(); err == nil {
   227  		t.Fatal("should be invalid")
   228  	}
   229  }
   230  
   231  func TestConfigValidate_nil(t *testing.T) {
   232  	var c Config
   233  	if err := c.Validate(); err != nil {
   234  		t.Fatalf("err: %s", err)
   235  	}
   236  }
   237  
   238  func TestConfigValidate_outputBadField(t *testing.T) {
   239  	c := testConfig(t, "validate-output-bad-field")
   240  	if err := c.Validate(); err == nil {
   241  		t.Fatal("should not be valid")
   242  	}
   243  }
   244  
   245  func TestConfigValidate_outputMissingEquals(t *testing.T) {
   246  	c := testConfig(t, "validate-output-missing-equals")
   247  	if err := c.Validate(); err == nil {
   248  		t.Fatal("should not be valid")
   249  	}
   250  }
   251  
   252  func TestConfigValidate_pathVar(t *testing.T) {
   253  	c := testConfig(t, "validate-path-var")
   254  	if err := c.Validate(); err != nil {
   255  		t.Fatalf("err: %s", err)
   256  	}
   257  }
   258  
   259  func TestConfigValidate_pathVarInvalid(t *testing.T) {
   260  	c := testConfig(t, "validate-path-var-invalid")
   261  	if err := c.Validate(); err == nil {
   262  		t.Fatal("should not be valid")
   263  	}
   264  }
   265  
   266  func TestConfigValidate_providerMulti(t *testing.T) {
   267  	c := testConfig(t, "validate-provider-multi")
   268  	if err := c.Validate(); err == nil {
   269  		t.Fatal("should not be valid")
   270  	}
   271  }
   272  
   273  func TestConfigValidate_providerMultiGood(t *testing.T) {
   274  	c := testConfig(t, "validate-provider-multi-good")
   275  	if err := c.Validate(); err != nil {
   276  		t.Fatalf("should be valid: %s", err)
   277  	}
   278  }
   279  
   280  func TestConfigValidate_providerMultiRefGood(t *testing.T) {
   281  	c := testConfig(t, "validate-provider-multi-ref-good")
   282  	if err := c.Validate(); err != nil {
   283  		t.Fatalf("should be valid: %s", err)
   284  	}
   285  }
   286  
   287  func TestConfigValidate_providerMultiRefBad(t *testing.T) {
   288  	c := testConfig(t, "validate-provider-multi-ref-bad")
   289  	if err := c.Validate(); err == nil {
   290  		t.Fatal("should not be valid")
   291  	}
   292  }
   293  
   294  func TestConfigValidate_provConnSplatOther(t *testing.T) {
   295  	c := testConfig(t, "validate-prov-conn-splat-other")
   296  	if err := c.Validate(); err != nil {
   297  		t.Fatalf("should be valid: %s", err)
   298  	}
   299  }
   300  
   301  func TestConfigValidate_provConnSplatSelf(t *testing.T) {
   302  	c := testConfig(t, "validate-prov-conn-splat-self")
   303  	if err := c.Validate(); err == nil {
   304  		t.Fatal("should not be valid")
   305  	}
   306  }
   307  
   308  func TestConfigValidate_provSplatOther(t *testing.T) {
   309  	c := testConfig(t, "validate-prov-splat-other")
   310  	if err := c.Validate(); err != nil {
   311  		t.Fatalf("should be valid: %s", err)
   312  	}
   313  }
   314  
   315  func TestConfigValidate_provSplatSelf(t *testing.T) {
   316  	c := testConfig(t, "validate-prov-splat-self")
   317  	if err := c.Validate(); err == nil {
   318  		t.Fatal("should not be valid")
   319  	}
   320  }
   321  
   322  func TestConfigValidate_resourceProvVarSelf(t *testing.T) {
   323  	c := testConfig(t, "validate-resource-prov-self")
   324  	if err := c.Validate(); err != nil {
   325  		t.Fatalf("should be valid: %s", err)
   326  	}
   327  }
   328  
   329  func TestConfigValidate_resourceVarSelf(t *testing.T) {
   330  	c := testConfig(t, "validate-resource-self")
   331  	if err := c.Validate(); err == nil {
   332  		t.Fatal("should not be valid")
   333  	}
   334  }
   335  
   336  func TestConfigValidate_unknownThing(t *testing.T) {
   337  	c := testConfig(t, "validate-unknownthing")
   338  	if err := c.Validate(); err == nil {
   339  		t.Fatal("should not be valid")
   340  	}
   341  }
   342  
   343  func TestConfigValidate_unknownResourceVar(t *testing.T) {
   344  	c := testConfig(t, "validate-unknown-resource-var")
   345  	if err := c.Validate(); err == nil {
   346  		t.Fatal("should not be valid")
   347  	}
   348  }
   349  
   350  func TestConfigValidate_unknownResourceVar_output(t *testing.T) {
   351  	c := testConfig(t, "validate-unknown-resource-var-output")
   352  	if err := c.Validate(); err == nil {
   353  		t.Fatal("should not be valid")
   354  	}
   355  }
   356  
   357  func TestConfigValidate_unknownVar(t *testing.T) {
   358  	c := testConfig(t, "validate-unknownvar")
   359  	if err := c.Validate(); err == nil {
   360  		t.Fatal("should not be valid")
   361  	}
   362  }
   363  
   364  func TestConfigValidate_unknownVarCount(t *testing.T) {
   365  	c := testConfig(t, "validate-unknownvar-count")
   366  	if err := c.Validate(); err == nil {
   367  		t.Fatal("should not be valid")
   368  	}
   369  }
   370  
   371  func TestConfigValidate_varDefault(t *testing.T) {
   372  	c := testConfig(t, "validate-var-default")
   373  	if err := c.Validate(); err != nil {
   374  		t.Fatalf("should be valid: %s", err)
   375  	}
   376  }
   377  
   378  func TestConfigValidate_varDefaultBadType(t *testing.T) {
   379  	c := testConfig(t, "validate-var-default-bad-type")
   380  	if err := c.Validate(); err == nil {
   381  		t.Fatal("should not be valid")
   382  	}
   383  }
   384  
   385  func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
   386  	c := testConfig(t, "validate-var-default-interpolate")
   387  	if err := c.Validate(); err == nil {
   388  		t.Fatal("should not be valid")
   389  	}
   390  }
   391  
   392  func TestConfigValidate_varMultiExactNonSlice(t *testing.T) {
   393  	c := testConfig(t, "validate-var-multi-exact-non-slice")
   394  	if err := c.Validate(); err != nil {
   395  		t.Fatalf("should be valid: %s", err)
   396  	}
   397  }
   398  
   399  func TestConfigValidate_varMultiNonSlice(t *testing.T) {
   400  	c := testConfig(t, "validate-var-multi-non-slice")
   401  	if err := c.Validate(); err == nil {
   402  		t.Fatal("should not be valid")
   403  	}
   404  }
   405  
   406  func TestConfigValidate_varMultiNonSliceProvisioner(t *testing.T) {
   407  	c := testConfig(t, "validate-var-multi-non-slice-provisioner")
   408  	if err := c.Validate(); err == nil {
   409  		t.Fatal("should not be valid")
   410  	}
   411  }
   412  
   413  func TestConfigValidate_varMultiFunctionCall(t *testing.T) {
   414  	c := testConfig(t, "validate-var-multi-func")
   415  	if err := c.Validate(); err != nil {
   416  		t.Fatalf("should be valid: %s", err)
   417  	}
   418  }
   419  
   420  func TestConfigValidate_varModule(t *testing.T) {
   421  	c := testConfig(t, "validate-var-module")
   422  	if err := c.Validate(); err != nil {
   423  		t.Fatalf("err: %s", err)
   424  	}
   425  }
   426  
   427  func TestConfigValidate_varModuleInvalid(t *testing.T) {
   428  	c := testConfig(t, "validate-var-module-invalid")
   429  	if err := c.Validate(); err == nil {
   430  		t.Fatal("should not be valid")
   431  	}
   432  }
   433  
   434  func TestNameRegexp(t *testing.T) {
   435  	cases := []struct {
   436  		Input string
   437  		Match bool
   438  	}{
   439  		{"hello", true},
   440  		{"foo-bar", true},
   441  		{"foo_bar", true},
   442  		{"_hello", true},
   443  		{"foo bar", false},
   444  		{"foo.bar", false},
   445  	}
   446  
   447  	for _, tc := range cases {
   448  		if NameRegexp.Match([]byte(tc.Input)) != tc.Match {
   449  			t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match)
   450  		}
   451  	}
   452  }
   453  
   454  func TestProviderConfigName(t *testing.T) {
   455  	pcs := []*ProviderConfig{
   456  		&ProviderConfig{Name: "aw"},
   457  		&ProviderConfig{Name: "aws"},
   458  		&ProviderConfig{Name: "a"},
   459  		&ProviderConfig{Name: "gce_"},
   460  	}
   461  
   462  	n := ProviderConfigName("aws_instance", pcs)
   463  	if n != "aws" {
   464  		t.Fatalf("bad: %s", n)
   465  	}
   466  }
   467  
   468  func TestVariableDefaultsMap(t *testing.T) {
   469  	cases := []struct {
   470  		Default interface{}
   471  		Output  map[string]string
   472  	}{
   473  		{
   474  			nil,
   475  			nil,
   476  		},
   477  
   478  		{
   479  			"foo",
   480  			map[string]string{"var.foo": "foo"},
   481  		},
   482  
   483  		{
   484  			map[interface{}]interface{}{
   485  				"foo": "bar",
   486  				"bar": "baz",
   487  			},
   488  			map[string]string{
   489  				"var.foo":     "foo",
   490  				"var.foo.foo": "bar",
   491  				"var.foo.bar": "baz",
   492  			},
   493  		},
   494  	}
   495  
   496  	for i, tc := range cases {
   497  		v := &Variable{Name: "foo", Default: tc.Default}
   498  		actual := v.DefaultsMap()
   499  		if !reflect.DeepEqual(actual, tc.Output) {
   500  			t.Fatalf("%d: bad: %#v", i, actual)
   501  		}
   502  	}
   503  }
   504  
   505  func testConfig(t *testing.T, name string) *Config {
   506  	c, err := LoadFile(filepath.Join(fixtureDir, name, "main.tf"))
   507  	if err != nil {
   508  		t.Fatalf("file: %s\n\nerr: %s", name, err)
   509  	}
   510  
   511  	return c
   512  }