github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/config/config_test.go (about)

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