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