github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/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_countUserVar(t *testing.T) { 199 c := testConfig(t, "validate-count-user-var") 200 if err := c.Validate(); err != nil { 201 t.Fatalf("err: %s", err) 202 } 203 } 204 205 func TestConfigValidate_countVar(t *testing.T) { 206 c := testConfig(t, "validate-count-var") 207 if err := c.Validate(); err != nil { 208 t.Fatalf("err: %s", err) 209 } 210 } 211 212 func TestConfigValidate_countVarInvalid(t *testing.T) { 213 c := testConfig(t, "validate-count-var-invalid") 214 if err := c.Validate(); err == nil { 215 t.Fatal("should not be valid") 216 } 217 } 218 219 func TestConfigValidate_countVarUnknown(t *testing.T) { 220 c := testConfig(t, "validate-count-var-unknown") 221 if err := c.Validate(); err == nil { 222 t.Fatal("should not be valid") 223 } 224 } 225 226 func TestConfigValidate_dependsOnVar(t *testing.T) { 227 c := testConfig(t, "validate-depends-on-var") 228 if err := c.Validate(); err == nil { 229 t.Fatal("should not be valid") 230 } 231 } 232 233 func TestConfigValidate_dupModule(t *testing.T) { 234 c := testConfig(t, "validate-dup-module") 235 if err := c.Validate(); err == nil { 236 t.Fatal("should not be valid") 237 } 238 } 239 240 func TestConfigValidate_dupResource(t *testing.T) { 241 c := testConfig(t, "validate-dup-resource") 242 if err := c.Validate(); err == nil { 243 t.Fatal("should not be valid") 244 } 245 } 246 247 func TestConfigValidate_moduleNameBad(t *testing.T) { 248 c := testConfig(t, "validate-module-name-bad") 249 if err := c.Validate(); err == nil { 250 t.Fatal("should not be valid") 251 } 252 } 253 254 func TestConfigValidate_moduleSourceVar(t *testing.T) { 255 c := testConfig(t, "validate-module-source-var") 256 if err := c.Validate(); err == nil { 257 t.Fatal("should not be valid") 258 } 259 } 260 261 func TestConfigValidate_moduleVarInt(t *testing.T) { 262 c := testConfig(t, "validate-module-var-int") 263 if err := c.Validate(); err != nil { 264 t.Fatalf("should be valid: %s", err) 265 } 266 } 267 268 func TestConfigValidate_moduleVarMap(t *testing.T) { 269 c := testConfig(t, "validate-module-var-map") 270 if err := c.Validate(); err != nil { 271 t.Fatalf("should be valid: %s", err) 272 } 273 } 274 275 func TestConfigValidate_moduleVarList(t *testing.T) { 276 c := testConfig(t, "validate-module-var-list") 277 if err := c.Validate(); err != nil { 278 t.Fatalf("should be valid: %s", err) 279 } 280 } 281 282 func TestConfigValidate_moduleVarSelf(t *testing.T) { 283 c := testConfig(t, "validate-module-var-self") 284 if err := c.Validate(); err == nil { 285 t.Fatal("should be invalid") 286 } 287 } 288 289 func TestConfigValidate_nil(t *testing.T) { 290 var c Config 291 if err := c.Validate(); err != nil { 292 t.Fatalf("err: %s", err) 293 } 294 } 295 296 func TestConfigValidate_outputBadField(t *testing.T) { 297 c := testConfig(t, "validate-output-bad-field") 298 if err := c.Validate(); err == nil { 299 t.Fatal("should not be valid") 300 } 301 } 302 303 func TestConfigValidate_outputDuplicate(t *testing.T) { 304 c := testConfig(t, "validate-output-dup") 305 if err := c.Validate(); err == nil { 306 t.Fatal("should not be valid") 307 } 308 } 309 310 func TestConfigValidate_pathVar(t *testing.T) { 311 c := testConfig(t, "validate-path-var") 312 if err := c.Validate(); err != nil { 313 t.Fatalf("err: %s", err) 314 } 315 } 316 317 func TestConfigValidate_pathVarInvalid(t *testing.T) { 318 c := testConfig(t, "validate-path-var-invalid") 319 if err := c.Validate(); err == nil { 320 t.Fatal("should not be valid") 321 } 322 } 323 324 func TestConfigValidate_providerMulti(t *testing.T) { 325 c := testConfig(t, "validate-provider-multi") 326 if err := c.Validate(); err == nil { 327 t.Fatal("should not be valid") 328 } 329 } 330 331 func TestConfigValidate_providerMultiGood(t *testing.T) { 332 c := testConfig(t, "validate-provider-multi-good") 333 if err := c.Validate(); err != nil { 334 t.Fatalf("should be valid: %s", err) 335 } 336 } 337 338 func TestConfigValidate_providerMultiRefGood(t *testing.T) { 339 c := testConfig(t, "validate-provider-multi-ref-good") 340 if err := c.Validate(); err != nil { 341 t.Fatalf("should be valid: %s", err) 342 } 343 } 344 345 func TestConfigValidate_providerMultiRefBad(t *testing.T) { 346 c := testConfig(t, "validate-provider-multi-ref-bad") 347 if err := c.Validate(); err == nil { 348 t.Fatal("should not be valid") 349 } 350 } 351 352 func TestConfigValidate_provConnSplatOther(t *testing.T) { 353 c := testConfig(t, "validate-prov-conn-splat-other") 354 if err := c.Validate(); err != nil { 355 t.Fatalf("should be valid: %s", err) 356 } 357 } 358 359 func TestConfigValidate_provConnSplatSelf(t *testing.T) { 360 c := testConfig(t, "validate-prov-conn-splat-self") 361 if err := c.Validate(); err == nil { 362 t.Fatal("should not be valid") 363 } 364 } 365 366 func TestConfigValidate_provSplatOther(t *testing.T) { 367 c := testConfig(t, "validate-prov-splat-other") 368 if err := c.Validate(); err != nil { 369 t.Fatalf("should be valid: %s", err) 370 } 371 } 372 373 func TestConfigValidate_provSplatSelf(t *testing.T) { 374 c := testConfig(t, "validate-prov-splat-self") 375 if err := c.Validate(); err == nil { 376 t.Fatal("should not be valid") 377 } 378 } 379 380 func TestConfigValidate_resourceProvVarSelf(t *testing.T) { 381 c := testConfig(t, "validate-resource-prov-self") 382 if err := c.Validate(); err != nil { 383 t.Fatalf("should be valid: %s", err) 384 } 385 } 386 387 func TestConfigValidate_resourceVarSelf(t *testing.T) { 388 c := testConfig(t, "validate-resource-self") 389 if err := c.Validate(); err == nil { 390 t.Fatal("should not be valid") 391 } 392 } 393 394 func TestConfigValidate_unknownThing(t *testing.T) { 395 c := testConfig(t, "validate-unknownthing") 396 if err := c.Validate(); err == nil { 397 t.Fatal("should not be valid") 398 } 399 } 400 401 func TestConfigValidate_unknownResourceVar(t *testing.T) { 402 c := testConfig(t, "validate-unknown-resource-var") 403 if err := c.Validate(); err == nil { 404 t.Fatal("should not be valid") 405 } 406 } 407 408 func TestConfigValidate_unknownResourceVar_output(t *testing.T) { 409 c := testConfig(t, "validate-unknown-resource-var-output") 410 if err := c.Validate(); err == nil { 411 t.Fatal("should not be valid") 412 } 413 } 414 415 func TestConfigValidate_unknownVar(t *testing.T) { 416 c := testConfig(t, "validate-unknownvar") 417 if err := c.Validate(); err == nil { 418 t.Fatal("should not be valid") 419 } 420 } 421 422 func TestConfigValidate_unknownVarCount(t *testing.T) { 423 c := testConfig(t, "validate-unknownvar-count") 424 if err := c.Validate(); err == nil { 425 t.Fatal("should not be valid") 426 } 427 } 428 429 func TestConfigValidate_varDefault(t *testing.T) { 430 c := testConfig(t, "validate-var-default") 431 if err := c.Validate(); err != nil { 432 t.Fatalf("should be valid: %s", err) 433 } 434 } 435 436 func TestConfigValidate_varDefaultListType(t *testing.T) { 437 c := testConfig(t, "validate-var-default-list-type") 438 if err := c.Validate(); err != nil { 439 t.Fatalf("should be valid: %s", err) 440 } 441 } 442 443 func TestConfigValidate_varDefaultInterpolate(t *testing.T) { 444 c := testConfig(t, "validate-var-default-interpolate") 445 if err := c.Validate(); err == nil { 446 t.Fatal("should not be valid") 447 } 448 } 449 450 func TestConfigValidate_varDup(t *testing.T) { 451 c := testConfig(t, "validate-var-dup") 452 if err := c.Validate(); err == nil { 453 t.Fatal("should not be valid") 454 } 455 } 456 457 func TestConfigValidate_varMultiExactNonSlice(t *testing.T) { 458 c := testConfig(t, "validate-var-multi-exact-non-slice") 459 if err := c.Validate(); err != nil { 460 t.Fatalf("should be valid: %s", err) 461 } 462 } 463 464 func TestConfigValidate_varMultiNonSlice(t *testing.T) { 465 c := testConfig(t, "validate-var-multi-non-slice") 466 if err := c.Validate(); err == nil { 467 t.Fatal("should not be valid") 468 } 469 } 470 471 func TestConfigValidate_varMultiNonSliceProvisioner(t *testing.T) { 472 c := testConfig(t, "validate-var-multi-non-slice-provisioner") 473 if err := c.Validate(); err == nil { 474 t.Fatal("should not be valid") 475 } 476 } 477 478 func TestConfigValidate_varMultiFunctionCall(t *testing.T) { 479 c := testConfig(t, "validate-var-multi-func") 480 if err := c.Validate(); err != nil { 481 t.Fatalf("should be valid: %s", err) 482 } 483 } 484 485 func TestConfigValidate_varModule(t *testing.T) { 486 c := testConfig(t, "validate-var-module") 487 if err := c.Validate(); err != nil { 488 t.Fatalf("err: %s", err) 489 } 490 } 491 492 func TestConfigValidate_varModuleInvalid(t *testing.T) { 493 c := testConfig(t, "validate-var-module-invalid") 494 if err := c.Validate(); err == nil { 495 t.Fatal("should not be valid") 496 } 497 } 498 499 func TestNameRegexp(t *testing.T) { 500 cases := []struct { 501 Input string 502 Match bool 503 }{ 504 {"hello", true}, 505 {"foo-bar", true}, 506 {"foo_bar", true}, 507 {"_hello", true}, 508 {"foo bar", false}, 509 {"foo.bar", false}, 510 } 511 512 for _, tc := range cases { 513 if NameRegexp.Match([]byte(tc.Input)) != tc.Match { 514 t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match) 515 } 516 } 517 } 518 519 func TestProviderConfigName(t *testing.T) { 520 pcs := []*ProviderConfig{ 521 &ProviderConfig{Name: "aw"}, 522 &ProviderConfig{Name: "aws"}, 523 &ProviderConfig{Name: "a"}, 524 &ProviderConfig{Name: "gce_"}, 525 } 526 527 n := ProviderConfigName("aws_instance", pcs) 528 if n != "aws" { 529 t.Fatalf("bad: %s", n) 530 } 531 } 532 533 func testConfig(t *testing.T, name string) *Config { 534 c, err := LoadFile(filepath.Join(fixtureDir, name, "main.tf")) 535 if err != nil { 536 t.Fatalf("file: %s\n\nerr: %s", name, err) 537 } 538 539 return c 540 }