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