github.com/haklop/terraform@v0.3.6/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  // This is the directory where our test fixtures are.
    10  const fixtureDir = "./test-fixtures"
    11  
    12  func TestConfigCount(t *testing.T) {
    13  	c := testConfig(t, "count-int")
    14  	actual, err := c.Resources[0].Count()
    15  	if err != nil {
    16  		t.Fatalf("err: %s", err)
    17  	}
    18  	if actual != 5 {
    19  		t.Fatalf("bad: %#v", actual)
    20  	}
    21  }
    22  
    23  func TestConfigCount_string(t *testing.T) {
    24  	c := testConfig(t, "count-string")
    25  	actual, err := c.Resources[0].Count()
    26  	if err != nil {
    27  		t.Fatalf("err: %s", err)
    28  	}
    29  	if actual != 5 {
    30  		t.Fatalf("bad: %#v", actual)
    31  	}
    32  }
    33  
    34  func TestConfigCount_var(t *testing.T) {
    35  	c := testConfig(t, "count-var")
    36  	_, err := c.Resources[0].Count()
    37  	if err == nil {
    38  		t.Fatalf("should error")
    39  	}
    40  }
    41  
    42  func TestConfigValidate(t *testing.T) {
    43  	c := testConfig(t, "validate-good")
    44  	if err := c.Validate(); err != nil {
    45  		t.Fatalf("err: %s", err)
    46  	}
    47  }
    48  
    49  func TestConfigValidate_badDependsOn(t *testing.T) {
    50  	c := testConfig(t, "validate-bad-depends-on")
    51  	if err := c.Validate(); err == nil {
    52  		t.Fatal("should not be valid")
    53  	}
    54  }
    55  
    56  func TestConfigValidate_countInt(t *testing.T) {
    57  	c := testConfig(t, "validate-count-int")
    58  	if err := c.Validate(); err != nil {
    59  		t.Fatalf("err: %s", err)
    60  	}
    61  }
    62  
    63  func TestConfigValidate_countCountVar(t *testing.T) {
    64  	c := testConfig(t, "validate-count-count-var")
    65  	if err := c.Validate(); err == nil {
    66  		t.Fatal("should not be valid")
    67  	}
    68  }
    69  
    70  func TestConfigValidate_countModuleVar(t *testing.T) {
    71  	c := testConfig(t, "validate-count-module-var")
    72  	if err := c.Validate(); err == nil {
    73  		t.Fatal("should not be valid")
    74  	}
    75  }
    76  
    77  func TestConfigValidate_countNotInt(t *testing.T) {
    78  	c := testConfig(t, "validate-count-not-int")
    79  	if err := c.Validate(); err == nil {
    80  		t.Fatal("should not be valid")
    81  	}
    82  }
    83  
    84  func TestConfigValidate_countResourceVar(t *testing.T) {
    85  	c := testConfig(t, "validate-count-resource-var")
    86  	if err := c.Validate(); err == nil {
    87  		t.Fatal("should not be valid")
    88  	}
    89  }
    90  
    91  func TestConfigValidate_countUserVar(t *testing.T) {
    92  	c := testConfig(t, "validate-count-user-var")
    93  	if err := c.Validate(); err != nil {
    94  		t.Fatalf("err: %s", err)
    95  	}
    96  }
    97  
    98  func TestConfigValidate_countVar(t *testing.T) {
    99  	c := testConfig(t, "validate-count-var")
   100  	if err := c.Validate(); err != nil {
   101  		t.Fatalf("err: %s", err)
   102  	}
   103  }
   104  
   105  func TestConfigValidate_countVarInvalid(t *testing.T) {
   106  	c := testConfig(t, "validate-count-var-invalid")
   107  	if err := c.Validate(); err == nil {
   108  		t.Fatal("should not be valid")
   109  	}
   110  }
   111  
   112  func TestConfigValidate_dupModule(t *testing.T) {
   113  	c := testConfig(t, "validate-dup-module")
   114  	if err := c.Validate(); err == nil {
   115  		t.Fatal("should not be valid")
   116  	}
   117  }
   118  
   119  func TestConfigValidate_dupResource(t *testing.T) {
   120  	c := testConfig(t, "validate-dup-resource")
   121  	if err := c.Validate(); err == nil {
   122  		t.Fatal("should not be valid")
   123  	}
   124  }
   125  
   126  func TestConfigValidate_moduleNameBad(t *testing.T) {
   127  	c := testConfig(t, "validate-module-name-bad")
   128  	if err := c.Validate(); err == nil {
   129  		t.Fatal("should not be valid")
   130  	}
   131  }
   132  
   133  func TestConfigValidate_moduleSourceVar(t *testing.T) {
   134  	c := testConfig(t, "validate-module-source-var")
   135  	if err := c.Validate(); err == nil {
   136  		t.Fatal("should not be valid")
   137  	}
   138  }
   139  
   140  func TestConfigValidate_moduleVarInt(t *testing.T) {
   141  	c := testConfig(t, "validate-module-var-int")
   142  	if err := c.Validate(); err != nil {
   143  		t.Fatalf("should be valid: %s", err)
   144  	}
   145  }
   146  
   147  func TestConfigValidate_moduleVarMap(t *testing.T) {
   148  	c := testConfig(t, "validate-module-var-map")
   149  	if err := c.Validate(); err == nil {
   150  		t.Fatal("should be invalid")
   151  	}
   152  }
   153  
   154  func TestConfigValidate_nil(t *testing.T) {
   155  	var c Config
   156  	if err := c.Validate(); err != nil {
   157  		t.Fatalf("err: %s", err)
   158  	}
   159  }
   160  
   161  func TestConfigValidate_outputBadField(t *testing.T) {
   162  	c := testConfig(t, "validate-output-bad-field")
   163  	if err := c.Validate(); err == nil {
   164  		t.Fatal("should not be valid")
   165  	}
   166  }
   167  
   168  func TestConfigValidate_pathVar(t *testing.T) {
   169  	c := testConfig(t, "validate-path-var")
   170  	if err := c.Validate(); err != nil {
   171  		t.Fatalf("err: %s", err)
   172  	}
   173  }
   174  
   175  func TestConfigValidate_pathVarInvalid(t *testing.T) {
   176  	c := testConfig(t, "validate-path-var-invalid")
   177  	if err := c.Validate(); err == nil {
   178  		t.Fatal("should not be valid")
   179  	}
   180  }
   181  
   182  func TestConfigValidate_unknownThing(t *testing.T) {
   183  	c := testConfig(t, "validate-unknownthing")
   184  	if err := c.Validate(); err == nil {
   185  		t.Fatal("should not be valid")
   186  	}
   187  }
   188  
   189  func TestConfigValidate_unknownResourceVar(t *testing.T) {
   190  	c := testConfig(t, "validate-unknown-resource-var")
   191  	if err := c.Validate(); err == nil {
   192  		t.Fatal("should not be valid")
   193  	}
   194  }
   195  
   196  func TestConfigValidate_unknownResourceVar_output(t *testing.T) {
   197  	c := testConfig(t, "validate-unknown-resource-var-output")
   198  	if err := c.Validate(); err == nil {
   199  		t.Fatal("should not be valid")
   200  	}
   201  }
   202  
   203  func TestConfigValidate_unknownVar(t *testing.T) {
   204  	c := testConfig(t, "validate-unknownvar")
   205  	if err := c.Validate(); err == nil {
   206  		t.Fatal("should not be valid")
   207  	}
   208  }
   209  
   210  func TestConfigValidate_unknownVarCount(t *testing.T) {
   211  	c := testConfig(t, "validate-unknownvar-count")
   212  	if err := c.Validate(); err == nil {
   213  		t.Fatal("should not be valid")
   214  	}
   215  }
   216  
   217  func TestConfigValidate_varDefault(t *testing.T) {
   218  	c := testConfig(t, "validate-var-default")
   219  	if err := c.Validate(); err != nil {
   220  		t.Fatalf("should be valid: %s", err)
   221  	}
   222  }
   223  
   224  func TestConfigValidate_varDefaultBadType(t *testing.T) {
   225  	c := testConfig(t, "validate-var-default-bad-type")
   226  	if err := c.Validate(); err == nil {
   227  		t.Fatal("should not be valid")
   228  	}
   229  }
   230  
   231  func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
   232  	c := testConfig(t, "validate-var-default-interpolate")
   233  	if err := c.Validate(); err == nil {
   234  		t.Fatal("should not be valid")
   235  	}
   236  }
   237  
   238  func TestConfigValidate_varMultiExactNonSlice(t *testing.T) {
   239  	c := testConfig(t, "validate-var-multi-exact-non-slice")
   240  	if err := c.Validate(); err != nil {
   241  		t.Fatalf("should be valid: %s", err)
   242  	}
   243  }
   244  
   245  func TestConfigValidate_varMultiNonSlice(t *testing.T) {
   246  	c := testConfig(t, "validate-var-multi-non-slice")
   247  	if err := c.Validate(); err == nil {
   248  		t.Fatal("should not be valid")
   249  	}
   250  }
   251  
   252  func TestConfigValidate_varModule(t *testing.T) {
   253  	c := testConfig(t, "validate-var-module")
   254  	if err := c.Validate(); err != nil {
   255  		t.Fatalf("err: %s", err)
   256  	}
   257  }
   258  
   259  func TestConfigValidate_varModuleInvalid(t *testing.T) {
   260  	c := testConfig(t, "validate-var-module-invalid")
   261  	if err := c.Validate(); err == nil {
   262  		t.Fatal("should not be valid")
   263  	}
   264  }
   265  
   266  func TestNameRegexp(t *testing.T) {
   267  	cases := []struct {
   268  		Input string
   269  		Match bool
   270  	}{
   271  		{"hello", true},
   272  		{"foo-bar", true},
   273  		{"foo_bar", true},
   274  		{"_hello", true},
   275  		{"foo bar", false},
   276  		{"foo.bar", false},
   277  	}
   278  
   279  	for _, tc := range cases {
   280  		if NameRegexp.Match([]byte(tc.Input)) != tc.Match {
   281  			t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match)
   282  		}
   283  	}
   284  }
   285  
   286  func TestProviderConfigName(t *testing.T) {
   287  	pcs := []*ProviderConfig{
   288  		&ProviderConfig{Name: "aw"},
   289  		&ProviderConfig{Name: "aws"},
   290  		&ProviderConfig{Name: "a"},
   291  		&ProviderConfig{Name: "gce_"},
   292  	}
   293  
   294  	n := ProviderConfigName("aws_instance", pcs)
   295  	if n != "aws" {
   296  		t.Fatalf("bad: %s", n)
   297  	}
   298  }
   299  
   300  func TestVariableDefaultsMap(t *testing.T) {
   301  	cases := []struct {
   302  		Default interface{}
   303  		Output  map[string]string
   304  	}{
   305  		{
   306  			nil,
   307  			nil,
   308  		},
   309  
   310  		{
   311  			"foo",
   312  			map[string]string{"var.foo": "foo"},
   313  		},
   314  
   315  		{
   316  			map[interface{}]interface{}{
   317  				"foo": "bar",
   318  				"bar": "baz",
   319  			},
   320  			map[string]string{
   321  				"var.foo":     "foo",
   322  				"var.foo.foo": "bar",
   323  				"var.foo.bar": "baz",
   324  			},
   325  		},
   326  	}
   327  
   328  	for i, tc := range cases {
   329  		v := &Variable{Name: "foo", Default: tc.Default}
   330  		actual := v.DefaultsMap()
   331  		if !reflect.DeepEqual(actual, tc.Output) {
   332  			t.Fatalf("%d: bad: %#v", i, actual)
   333  		}
   334  	}
   335  }
   336  
   337  func testConfig(t *testing.T, name string) *Config {
   338  	c, err := Load(filepath.Join(fixtureDir, name, "main.tf"))
   339  	if err != nil {
   340  		t.Fatalf("file: %s\n\nerr: %s", name, err)
   341  	}
   342  
   343  	return c
   344  }