github.com/franklinhu/terraform@v0.6.9-0.20151202232446-81f7fb1e6f9e/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_moduleVarSelf(t *testing.T) { 179 c := testConfig(t, "validate-module-var-self") 180 if err := c.Validate(); err == nil { 181 t.Fatal("should be invalid") 182 } 183 } 184 185 func TestConfigValidate_nil(t *testing.T) { 186 var c Config 187 if err := c.Validate(); err != nil { 188 t.Fatalf("err: %s", err) 189 } 190 } 191 192 func TestConfigValidate_outputBadField(t *testing.T) { 193 c := testConfig(t, "validate-output-bad-field") 194 if err := c.Validate(); err == nil { 195 t.Fatal("should not be valid") 196 } 197 } 198 199 func TestConfigValidate_pathVar(t *testing.T) { 200 c := testConfig(t, "validate-path-var") 201 if err := c.Validate(); err != nil { 202 t.Fatalf("err: %s", err) 203 } 204 } 205 206 func TestConfigValidate_pathVarInvalid(t *testing.T) { 207 c := testConfig(t, "validate-path-var-invalid") 208 if err := c.Validate(); err == nil { 209 t.Fatal("should not be valid") 210 } 211 } 212 213 func TestConfigValidate_providerMulti(t *testing.T) { 214 c := testConfig(t, "validate-provider-multi") 215 if err := c.Validate(); err == nil { 216 t.Fatal("should not be valid") 217 } 218 } 219 220 func TestConfigValidate_providerMultiGood(t *testing.T) { 221 c := testConfig(t, "validate-provider-multi-good") 222 if err := c.Validate(); err != nil { 223 t.Fatalf("should be valid: %s", err) 224 } 225 } 226 227 func TestConfigValidate_providerMultiRefGood(t *testing.T) { 228 c := testConfig(t, "validate-provider-multi-ref-good") 229 if err := c.Validate(); err != nil { 230 t.Fatalf("should be valid: %s", err) 231 } 232 } 233 234 func TestConfigValidate_providerMultiRefBad(t *testing.T) { 235 c := testConfig(t, "validate-provider-multi-ref-bad") 236 if err := c.Validate(); err == nil { 237 t.Fatal("should not be valid") 238 } 239 } 240 241 func TestConfigValidate_provConnSplatOther(t *testing.T) { 242 c := testConfig(t, "validate-prov-conn-splat-other") 243 if err := c.Validate(); err != nil { 244 t.Fatalf("should be valid: %s", err) 245 } 246 } 247 248 func TestConfigValidate_provConnSplatSelf(t *testing.T) { 249 c := testConfig(t, "validate-prov-conn-splat-self") 250 if err := c.Validate(); err == nil { 251 t.Fatal("should not be valid") 252 } 253 } 254 255 func TestConfigValidate_provSplatOther(t *testing.T) { 256 c := testConfig(t, "validate-prov-splat-other") 257 if err := c.Validate(); err != nil { 258 t.Fatalf("should be valid: %s", err) 259 } 260 } 261 262 func TestConfigValidate_provSplatSelf(t *testing.T) { 263 c := testConfig(t, "validate-prov-splat-self") 264 if err := c.Validate(); err == nil { 265 t.Fatal("should not be valid") 266 } 267 } 268 269 func TestConfigValidate_resourceProvVarSelf(t *testing.T) { 270 c := testConfig(t, "validate-resource-prov-self") 271 if err := c.Validate(); err != nil { 272 t.Fatalf("should be valid: %s", err) 273 } 274 } 275 276 func TestConfigValidate_resourceVarSelf(t *testing.T) { 277 c := testConfig(t, "validate-resource-self") 278 if err := c.Validate(); err == nil { 279 t.Fatal("should not be valid") 280 } 281 } 282 283 func TestConfigValidate_unknownThing(t *testing.T) { 284 c := testConfig(t, "validate-unknownthing") 285 if err := c.Validate(); err == nil { 286 t.Fatal("should not be valid") 287 } 288 } 289 290 func TestConfigValidate_unknownResourceVar(t *testing.T) { 291 c := testConfig(t, "validate-unknown-resource-var") 292 if err := c.Validate(); err == nil { 293 t.Fatal("should not be valid") 294 } 295 } 296 297 func TestConfigValidate_unknownResourceVar_output(t *testing.T) { 298 c := testConfig(t, "validate-unknown-resource-var-output") 299 if err := c.Validate(); err == nil { 300 t.Fatal("should not be valid") 301 } 302 } 303 304 func TestConfigValidate_unknownVar(t *testing.T) { 305 c := testConfig(t, "validate-unknownvar") 306 if err := c.Validate(); err == nil { 307 t.Fatal("should not be valid") 308 } 309 } 310 311 func TestConfigValidate_unknownVarCount(t *testing.T) { 312 c := testConfig(t, "validate-unknownvar-count") 313 if err := c.Validate(); err == nil { 314 t.Fatal("should not be valid") 315 } 316 } 317 318 func TestConfigValidate_varDefault(t *testing.T) { 319 c := testConfig(t, "validate-var-default") 320 if err := c.Validate(); err != nil { 321 t.Fatalf("should be valid: %s", err) 322 } 323 } 324 325 func TestConfigValidate_varDefaultBadType(t *testing.T) { 326 c := testConfig(t, "validate-var-default-bad-type") 327 if err := c.Validate(); err == nil { 328 t.Fatal("should not be valid") 329 } 330 } 331 332 func TestConfigValidate_varDefaultInterpolate(t *testing.T) { 333 c := testConfig(t, "validate-var-default-interpolate") 334 if err := c.Validate(); err == nil { 335 t.Fatal("should not be valid") 336 } 337 } 338 339 func TestConfigValidate_varMultiExactNonSlice(t *testing.T) { 340 c := testConfig(t, "validate-var-multi-exact-non-slice") 341 if err := c.Validate(); err != nil { 342 t.Fatalf("should be valid: %s", err) 343 } 344 } 345 346 func TestConfigValidate_varMultiNonSlice(t *testing.T) { 347 c := testConfig(t, "validate-var-multi-non-slice") 348 if err := c.Validate(); err == nil { 349 t.Fatal("should not be valid") 350 } 351 } 352 353 func TestConfigValidate_varMultiNonSliceProvisioner(t *testing.T) { 354 c := testConfig(t, "validate-var-multi-non-slice-provisioner") 355 if err := c.Validate(); err == nil { 356 t.Fatal("should not be valid") 357 } 358 } 359 360 func TestConfigValidate_varMultiFunctionCall(t *testing.T) { 361 c := testConfig(t, "validate-var-multi-func") 362 if err := c.Validate(); err != nil { 363 t.Fatalf("should be valid: %s", err) 364 } 365 } 366 367 func TestConfigValidate_varModule(t *testing.T) { 368 c := testConfig(t, "validate-var-module") 369 if err := c.Validate(); err != nil { 370 t.Fatalf("err: %s", err) 371 } 372 } 373 374 func TestConfigValidate_varModuleInvalid(t *testing.T) { 375 c := testConfig(t, "validate-var-module-invalid") 376 if err := c.Validate(); err == nil { 377 t.Fatal("should not be valid") 378 } 379 } 380 381 func TestNameRegexp(t *testing.T) { 382 cases := []struct { 383 Input string 384 Match bool 385 }{ 386 {"hello", true}, 387 {"foo-bar", true}, 388 {"foo_bar", true}, 389 {"_hello", true}, 390 {"foo bar", false}, 391 {"foo.bar", false}, 392 } 393 394 for _, tc := range cases { 395 if NameRegexp.Match([]byte(tc.Input)) != tc.Match { 396 t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match) 397 } 398 } 399 } 400 401 func TestProviderConfigName(t *testing.T) { 402 pcs := []*ProviderConfig{ 403 &ProviderConfig{Name: "aw"}, 404 &ProviderConfig{Name: "aws"}, 405 &ProviderConfig{Name: "a"}, 406 &ProviderConfig{Name: "gce_"}, 407 } 408 409 n := ProviderConfigName("aws_instance", pcs) 410 if n != "aws" { 411 t.Fatalf("bad: %s", n) 412 } 413 } 414 415 func TestVariableDefaultsMap(t *testing.T) { 416 cases := []struct { 417 Default interface{} 418 Output map[string]string 419 }{ 420 { 421 nil, 422 nil, 423 }, 424 425 { 426 "foo", 427 map[string]string{"var.foo": "foo"}, 428 }, 429 430 { 431 map[interface{}]interface{}{ 432 "foo": "bar", 433 "bar": "baz", 434 }, 435 map[string]string{ 436 "var.foo": "foo", 437 "var.foo.foo": "bar", 438 "var.foo.bar": "baz", 439 }, 440 }, 441 } 442 443 for i, tc := range cases { 444 v := &Variable{Name: "foo", Default: tc.Default} 445 actual := v.DefaultsMap() 446 if !reflect.DeepEqual(actual, tc.Output) { 447 t.Fatalf("%d: bad: %#v", i, actual) 448 } 449 } 450 } 451 452 func testConfig(t *testing.T, name string) *Config { 453 c, err := LoadFile(filepath.Join(fixtureDir, name, "main.tf")) 454 if err != nil { 455 t.Fatalf("file: %s\n\nerr: %s", name, err) 456 } 457 458 return c 459 }