github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/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 TestConfigCount(t *testing.T) {
    14  	c := testConfig(t, "count-int")
    15  	actual, err := c.Resources[0].Count()
    16  	if err != nil {
    17  		t.Fatalf("err: %s", err)
    18  	}
    19  	if actual != 5 {
    20  		t.Fatalf("bad: %#v", actual)
    21  	}
    22  }
    23  
    24  func TestConfigCount_string(t *testing.T) {
    25  	c := testConfig(t, "count-string")
    26  	actual, err := c.Resources[0].Count()
    27  	if err != nil {
    28  		t.Fatalf("err: %s", err)
    29  	}
    30  	if actual != 5 {
    31  		t.Fatalf("bad: %#v", actual)
    32  	}
    33  }
    34  
    35  func TestConfigCount_var(t *testing.T) {
    36  	c := testConfig(t, "count-var")
    37  	_, err := c.Resources[0].Count()
    38  	if err == nil {
    39  		t.Fatalf("should error")
    40  	}
    41  }
    42  
    43  func TestConfigValidate(t *testing.T) {
    44  	c := testConfig(t, "validate-good")
    45  	if err := c.Validate(); err != nil {
    46  		t.Fatalf("err: %s", err)
    47  	}
    48  }
    49  
    50  func TestConfigValidate_badDependsOn(t *testing.T) {
    51  	c := testConfig(t, "validate-bad-depends-on")
    52  	if err := c.Validate(); err == nil {
    53  		t.Fatal("should not be valid")
    54  	}
    55  }
    56  
    57  func TestConfigValidate_countInt(t *testing.T) {
    58  	c := testConfig(t, "validate-count-int")
    59  	if err := c.Validate(); err != nil {
    60  		t.Fatalf("err: %s", err)
    61  	}
    62  }
    63  
    64  func TestConfigValidate_countBadContext(t *testing.T) {
    65  	c := testConfig(t, "validate-count-bad-context")
    66  
    67  	err := c.Validate()
    68  
    69  	expected := []string{
    70  		"no_count_in_output: count variables are only valid within resources",
    71  		"no_count_in_module: count variables are only valid within resources",
    72  	}
    73  	for _, exp := range expected {
    74  		if !strings.Contains(err.Error(), exp) {
    75  			t.Fatalf("expected: %q,\nto contain: %q", err, exp)
    76  		}
    77  	}
    78  }
    79  
    80  func TestConfigValidate_countCountVar(t *testing.T) {
    81  	c := testConfig(t, "validate-count-count-var")
    82  	if err := c.Validate(); err == nil {
    83  		t.Fatal("should not be valid")
    84  	}
    85  }
    86  
    87  func TestConfigValidate_countModuleVar(t *testing.T) {
    88  	c := testConfig(t, "validate-count-module-var")
    89  	if err := c.Validate(); err == nil {
    90  		t.Fatal("should not be valid")
    91  	}
    92  }
    93  
    94  func TestConfigValidate_countNotInt(t *testing.T) {
    95  	c := testConfig(t, "validate-count-not-int")
    96  	if err := c.Validate(); err == nil {
    97  		t.Fatal("should not be valid")
    98  	}
    99  }
   100  
   101  func TestConfigValidate_countResourceVar(t *testing.T) {
   102  	c := testConfig(t, "validate-count-resource-var")
   103  	if err := c.Validate(); err == nil {
   104  		t.Fatal("should not be valid")
   105  	}
   106  }
   107  
   108  func TestConfigValidate_countUserVar(t *testing.T) {
   109  	c := testConfig(t, "validate-count-user-var")
   110  	if err := c.Validate(); err != nil {
   111  		t.Fatalf("err: %s", err)
   112  	}
   113  }
   114  
   115  func TestConfigValidate_countVar(t *testing.T) {
   116  	c := testConfig(t, "validate-count-var")
   117  	if err := c.Validate(); err != nil {
   118  		t.Fatalf("err: %s", err)
   119  	}
   120  }
   121  
   122  func TestConfigValidate_countVarInvalid(t *testing.T) {
   123  	c := testConfig(t, "validate-count-var-invalid")
   124  	if err := c.Validate(); err == nil {
   125  		t.Fatal("should not be valid")
   126  	}
   127  }
   128  
   129  func TestConfigValidate_dependsOnVar(t *testing.T) {
   130  	c := testConfig(t, "validate-depends-on-var")
   131  	if err := c.Validate(); err == nil {
   132  		t.Fatal("should not be valid")
   133  	}
   134  }
   135  
   136  func TestConfigValidate_dupModule(t *testing.T) {
   137  	c := testConfig(t, "validate-dup-module")
   138  	if err := c.Validate(); err == nil {
   139  		t.Fatal("should not be valid")
   140  	}
   141  }
   142  
   143  func TestConfigValidate_dupResource(t *testing.T) {
   144  	c := testConfig(t, "validate-dup-resource")
   145  	if err := c.Validate(); err == nil {
   146  		t.Fatal("should not be valid")
   147  	}
   148  }
   149  
   150  func TestConfigValidate_moduleNameBad(t *testing.T) {
   151  	c := testConfig(t, "validate-module-name-bad")
   152  	if err := c.Validate(); err == nil {
   153  		t.Fatal("should not be valid")
   154  	}
   155  }
   156  
   157  func TestConfigValidate_moduleSourceVar(t *testing.T) {
   158  	c := testConfig(t, "validate-module-source-var")
   159  	if err := c.Validate(); err == nil {
   160  		t.Fatal("should not be valid")
   161  	}
   162  }
   163  
   164  func TestConfigValidate_moduleVarInt(t *testing.T) {
   165  	c := testConfig(t, "validate-module-var-int")
   166  	if err := c.Validate(); err != nil {
   167  		t.Fatalf("should be valid: %s", err)
   168  	}
   169  }
   170  
   171  func TestConfigValidate_moduleVarMap(t *testing.T) {
   172  	c := testConfig(t, "validate-module-var-map")
   173  	if err := c.Validate(); err == nil {
   174  		t.Fatal("should be invalid")
   175  	}
   176  }
   177  
   178  func TestConfigValidate_nil(t *testing.T) {
   179  	var c Config
   180  	if err := c.Validate(); err != nil {
   181  		t.Fatalf("err: %s", err)
   182  	}
   183  }
   184  
   185  func TestConfigValidate_outputBadField(t *testing.T) {
   186  	c := testConfig(t, "validate-output-bad-field")
   187  	if err := c.Validate(); err == nil {
   188  		t.Fatal("should not be valid")
   189  	}
   190  }
   191  
   192  func TestConfigValidate_pathVar(t *testing.T) {
   193  	c := testConfig(t, "validate-path-var")
   194  	if err := c.Validate(); err != nil {
   195  		t.Fatalf("err: %s", err)
   196  	}
   197  }
   198  
   199  func TestConfigValidate_pathVarInvalid(t *testing.T) {
   200  	c := testConfig(t, "validate-path-var-invalid")
   201  	if err := c.Validate(); err == nil {
   202  		t.Fatal("should not be valid")
   203  	}
   204  }
   205  
   206  func TestConfigValidate_provConnSplatOther(t *testing.T) {
   207  	c := testConfig(t, "validate-prov-conn-splat-other")
   208  	if err := c.Validate(); err != nil {
   209  		t.Fatalf("should be valid: %s", err)
   210  	}
   211  }
   212  
   213  func TestConfigValidate_provConnSplatSelf(t *testing.T) {
   214  	c := testConfig(t, "validate-prov-conn-splat-self")
   215  	if err := c.Validate(); err == nil {
   216  		t.Fatal("should not be valid")
   217  	}
   218  }
   219  
   220  func TestConfigValidate_provSplatOther(t *testing.T) {
   221  	c := testConfig(t, "validate-prov-splat-other")
   222  	if err := c.Validate(); err != nil {
   223  		t.Fatalf("should be valid: %s", err)
   224  	}
   225  }
   226  
   227  func TestConfigValidate_provSplatSelf(t *testing.T) {
   228  	c := testConfig(t, "validate-prov-splat-self")
   229  	if err := c.Validate(); err == nil {
   230  		t.Fatal("should not be valid")
   231  	}
   232  }
   233  
   234  func TestConfigValidate_resourceProvVarSelf(t *testing.T) {
   235  	c := testConfig(t, "validate-resource-prov-self")
   236  	if err := c.Validate(); err != nil {
   237  		t.Fatalf("should be valid: %s", err)
   238  	}
   239  }
   240  
   241  func TestConfigValidate_resourceVarSelf(t *testing.T) {
   242  	c := testConfig(t, "validate-resource-self")
   243  	if err := c.Validate(); err == nil {
   244  		t.Fatal("should not be valid")
   245  	}
   246  }
   247  
   248  func TestConfigValidate_unknownThing(t *testing.T) {
   249  	c := testConfig(t, "validate-unknownthing")
   250  	if err := c.Validate(); err == nil {
   251  		t.Fatal("should not be valid")
   252  	}
   253  }
   254  
   255  func TestConfigValidate_unknownResourceVar(t *testing.T) {
   256  	c := testConfig(t, "validate-unknown-resource-var")
   257  	if err := c.Validate(); err == nil {
   258  		t.Fatal("should not be valid")
   259  	}
   260  }
   261  
   262  func TestConfigValidate_unknownResourceVar_output(t *testing.T) {
   263  	c := testConfig(t, "validate-unknown-resource-var-output")
   264  	if err := c.Validate(); err == nil {
   265  		t.Fatal("should not be valid")
   266  	}
   267  }
   268  
   269  func TestConfigValidate_unknownVar(t *testing.T) {
   270  	c := testConfig(t, "validate-unknownvar")
   271  	if err := c.Validate(); err == nil {
   272  		t.Fatal("should not be valid")
   273  	}
   274  }
   275  
   276  func TestConfigValidate_unknownVarCount(t *testing.T) {
   277  	c := testConfig(t, "validate-unknownvar-count")
   278  	if err := c.Validate(); err == nil {
   279  		t.Fatal("should not be valid")
   280  	}
   281  }
   282  
   283  func TestConfigValidate_varDefault(t *testing.T) {
   284  	c := testConfig(t, "validate-var-default")
   285  	if err := c.Validate(); err != nil {
   286  		t.Fatalf("should be valid: %s", err)
   287  	}
   288  }
   289  
   290  func TestConfigValidate_varDefaultBadType(t *testing.T) {
   291  	c := testConfig(t, "validate-var-default-bad-type")
   292  	if err := c.Validate(); err == nil {
   293  		t.Fatal("should not be valid")
   294  	}
   295  }
   296  
   297  func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
   298  	c := testConfig(t, "validate-var-default-interpolate")
   299  	if err := c.Validate(); err == nil {
   300  		t.Fatal("should not be valid")
   301  	}
   302  }
   303  
   304  func TestConfigValidate_varMultiExactNonSlice(t *testing.T) {
   305  	c := testConfig(t, "validate-var-multi-exact-non-slice")
   306  	if err := c.Validate(); err != nil {
   307  		t.Fatalf("should be valid: %s", err)
   308  	}
   309  }
   310  
   311  func TestConfigValidate_varMultiNonSlice(t *testing.T) {
   312  	c := testConfig(t, "validate-var-multi-non-slice")
   313  	if err := c.Validate(); err == nil {
   314  		t.Fatal("should not be valid")
   315  	}
   316  }
   317  
   318  func TestConfigValidate_varMultiNonSliceProvisioner(t *testing.T) {
   319  	c := testConfig(t, "validate-var-multi-non-slice-provisioner")
   320  	if err := c.Validate(); err == nil {
   321  		t.Fatal("should not be valid")
   322  	}
   323  }
   324  
   325  func TestConfigValidate_varMultiFunctionCall(t *testing.T) {
   326  	c := testConfig(t, "validate-var-multi-func")
   327  	if err := c.Validate(); err != nil {
   328  		t.Fatalf("should be valid: %s", err)
   329  	}
   330  }
   331  
   332  func TestConfigValidate_varModule(t *testing.T) {
   333  	c := testConfig(t, "validate-var-module")
   334  	if err := c.Validate(); err != nil {
   335  		t.Fatalf("err: %s", err)
   336  	}
   337  }
   338  
   339  func TestConfigValidate_varModuleInvalid(t *testing.T) {
   340  	c := testConfig(t, "validate-var-module-invalid")
   341  	if err := c.Validate(); err == nil {
   342  		t.Fatal("should not be valid")
   343  	}
   344  }
   345  
   346  func TestNameRegexp(t *testing.T) {
   347  	cases := []struct {
   348  		Input string
   349  		Match bool
   350  	}{
   351  		{"hello", true},
   352  		{"foo-bar", true},
   353  		{"foo_bar", true},
   354  		{"_hello", true},
   355  		{"foo bar", false},
   356  		{"foo.bar", false},
   357  	}
   358  
   359  	for _, tc := range cases {
   360  		if NameRegexp.Match([]byte(tc.Input)) != tc.Match {
   361  			t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match)
   362  		}
   363  	}
   364  }
   365  
   366  func TestProviderConfigName(t *testing.T) {
   367  	pcs := []*ProviderConfig{
   368  		&ProviderConfig{Name: "aw"},
   369  		&ProviderConfig{Name: "aws"},
   370  		&ProviderConfig{Name: "a"},
   371  		&ProviderConfig{Name: "gce_"},
   372  	}
   373  
   374  	n := ProviderConfigName("aws_instance", pcs)
   375  	if n != "aws" {
   376  		t.Fatalf("bad: %s", n)
   377  	}
   378  }
   379  
   380  func TestVariableDefaultsMap(t *testing.T) {
   381  	cases := []struct {
   382  		Default interface{}
   383  		Output  map[string]string
   384  	}{
   385  		{
   386  			nil,
   387  			nil,
   388  		},
   389  
   390  		{
   391  			"foo",
   392  			map[string]string{"var.foo": "foo"},
   393  		},
   394  
   395  		{
   396  			map[interface{}]interface{}{
   397  				"foo": "bar",
   398  				"bar": "baz",
   399  			},
   400  			map[string]string{
   401  				"var.foo":     "foo",
   402  				"var.foo.foo": "bar",
   403  				"var.foo.bar": "baz",
   404  			},
   405  		},
   406  	}
   407  
   408  	for i, tc := range cases {
   409  		v := &Variable{Name: "foo", Default: tc.Default}
   410  		actual := v.DefaultsMap()
   411  		if !reflect.DeepEqual(actual, tc.Output) {
   412  			t.Fatalf("%d: bad: %#v", i, actual)
   413  		}
   414  	}
   415  }
   416  
   417  func testConfig(t *testing.T, name string) *Config {
   418  	c, err := Load(filepath.Join(fixtureDir, name, "main.tf"))
   419  	if err != nil {
   420  		t.Fatalf("file: %s\n\nerr: %s", name, err)
   421  	}
   422  
   423  	return c
   424  }