github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/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_dependsOnVar(t *testing.T) { 113 c := testConfig(t, "validate-depends-on-var") 114 if err := c.Validate(); err == nil { 115 t.Fatal("should not be valid") 116 } 117 } 118 119 func TestConfigValidate_dupModule(t *testing.T) { 120 c := testConfig(t, "validate-dup-module") 121 if err := c.Validate(); err == nil { 122 t.Fatal("should not be valid") 123 } 124 } 125 126 func TestConfigValidate_dupResource(t *testing.T) { 127 c := testConfig(t, "validate-dup-resource") 128 if err := c.Validate(); err == nil { 129 t.Fatal("should not be valid") 130 } 131 } 132 133 func TestConfigValidate_moduleNameBad(t *testing.T) { 134 c := testConfig(t, "validate-module-name-bad") 135 if err := c.Validate(); err == nil { 136 t.Fatal("should not be valid") 137 } 138 } 139 140 func TestConfigValidate_moduleSourceVar(t *testing.T) { 141 c := testConfig(t, "validate-module-source-var") 142 if err := c.Validate(); err == nil { 143 t.Fatal("should not be valid") 144 } 145 } 146 147 func TestConfigValidate_moduleVarInt(t *testing.T) { 148 c := testConfig(t, "validate-module-var-int") 149 if err := c.Validate(); err != nil { 150 t.Fatalf("should be valid: %s", err) 151 } 152 } 153 154 func TestConfigValidate_moduleVarMap(t *testing.T) { 155 c := testConfig(t, "validate-module-var-map") 156 if err := c.Validate(); err == nil { 157 t.Fatal("should be invalid") 158 } 159 } 160 161 func TestConfigValidate_nil(t *testing.T) { 162 var c Config 163 if err := c.Validate(); err != nil { 164 t.Fatalf("err: %s", err) 165 } 166 } 167 168 func TestConfigValidate_outputBadField(t *testing.T) { 169 c := testConfig(t, "validate-output-bad-field") 170 if err := c.Validate(); err == nil { 171 t.Fatal("should not be valid") 172 } 173 } 174 175 func TestConfigValidate_pathVar(t *testing.T) { 176 c := testConfig(t, "validate-path-var") 177 if err := c.Validate(); err != nil { 178 t.Fatalf("err: %s", err) 179 } 180 } 181 182 func TestConfigValidate_pathVarInvalid(t *testing.T) { 183 c := testConfig(t, "validate-path-var-invalid") 184 if err := c.Validate(); err == nil { 185 t.Fatal("should not be valid") 186 } 187 } 188 189 func TestConfigValidate_provConnSplatOther(t *testing.T) { 190 c := testConfig(t, "validate-prov-conn-splat-other") 191 if err := c.Validate(); err != nil { 192 t.Fatalf("should be valid: %s", err) 193 } 194 } 195 196 func TestConfigValidate_provConnSplatSelf(t *testing.T) { 197 c := testConfig(t, "validate-prov-conn-splat-self") 198 if err := c.Validate(); err == nil { 199 t.Fatal("should not be valid") 200 } 201 } 202 203 func TestConfigValidate_provSplatOther(t *testing.T) { 204 c := testConfig(t, "validate-prov-splat-other") 205 if err := c.Validate(); err != nil { 206 t.Fatalf("should be valid: %s", err) 207 } 208 } 209 210 func TestConfigValidate_provSplatSelf(t *testing.T) { 211 c := testConfig(t, "validate-prov-splat-self") 212 if err := c.Validate(); err == nil { 213 t.Fatal("should not be valid") 214 } 215 } 216 217 func TestConfigValidate_resourceProvVarSelf(t *testing.T) { 218 c := testConfig(t, "validate-resource-prov-self") 219 if err := c.Validate(); err != nil { 220 t.Fatalf("should be valid: %s", err) 221 } 222 } 223 224 func TestConfigValidate_resourceVarSelf(t *testing.T) { 225 c := testConfig(t, "validate-resource-self") 226 if err := c.Validate(); err == nil { 227 t.Fatal("should not be valid") 228 } 229 } 230 231 func TestConfigValidate_unknownThing(t *testing.T) { 232 c := testConfig(t, "validate-unknownthing") 233 if err := c.Validate(); err == nil { 234 t.Fatal("should not be valid") 235 } 236 } 237 238 func TestConfigValidate_unknownResourceVar(t *testing.T) { 239 c := testConfig(t, "validate-unknown-resource-var") 240 if err := c.Validate(); err == nil { 241 t.Fatal("should not be valid") 242 } 243 } 244 245 func TestConfigValidate_unknownResourceVar_output(t *testing.T) { 246 c := testConfig(t, "validate-unknown-resource-var-output") 247 if err := c.Validate(); err == nil { 248 t.Fatal("should not be valid") 249 } 250 } 251 252 func TestConfigValidate_unknownVar(t *testing.T) { 253 c := testConfig(t, "validate-unknownvar") 254 if err := c.Validate(); err == nil { 255 t.Fatal("should not be valid") 256 } 257 } 258 259 func TestConfigValidate_unknownVarCount(t *testing.T) { 260 c := testConfig(t, "validate-unknownvar-count") 261 if err := c.Validate(); err == nil { 262 t.Fatal("should not be valid") 263 } 264 } 265 266 func TestConfigValidate_varDefault(t *testing.T) { 267 c := testConfig(t, "validate-var-default") 268 if err := c.Validate(); err != nil { 269 t.Fatalf("should be valid: %s", err) 270 } 271 } 272 273 func TestConfigValidate_varDefaultBadType(t *testing.T) { 274 c := testConfig(t, "validate-var-default-bad-type") 275 if err := c.Validate(); err == nil { 276 t.Fatal("should not be valid") 277 } 278 } 279 280 func TestConfigValidate_varDefaultInterpolate(t *testing.T) { 281 c := testConfig(t, "validate-var-default-interpolate") 282 if err := c.Validate(); err == nil { 283 t.Fatal("should not be valid") 284 } 285 } 286 287 func TestConfigValidate_varMultiExactNonSlice(t *testing.T) { 288 c := testConfig(t, "validate-var-multi-exact-non-slice") 289 if err := c.Validate(); err != nil { 290 t.Fatalf("should be valid: %s", err) 291 } 292 } 293 294 func TestConfigValidate_varMultiNonSlice(t *testing.T) { 295 c := testConfig(t, "validate-var-multi-non-slice") 296 if err := c.Validate(); err == nil { 297 t.Fatal("should not be valid") 298 } 299 } 300 301 func TestConfigValidate_varMultiNonSliceProvisioner(t *testing.T) { 302 c := testConfig(t, "validate-var-multi-non-slice-provisioner") 303 if err := c.Validate(); err == nil { 304 t.Fatal("should not be valid") 305 } 306 } 307 308 func TestConfigValidate_varMultiFunctionCall(t *testing.T) { 309 c := testConfig(t, "validate-var-multi-func") 310 if err := c.Validate(); err != nil { 311 t.Fatalf("should be valid: %s", err) 312 } 313 } 314 315 func TestConfigValidate_varModule(t *testing.T) { 316 c := testConfig(t, "validate-var-module") 317 if err := c.Validate(); err != nil { 318 t.Fatalf("err: %s", err) 319 } 320 } 321 322 func TestConfigValidate_varModuleInvalid(t *testing.T) { 323 c := testConfig(t, "validate-var-module-invalid") 324 if err := c.Validate(); err == nil { 325 t.Fatal("should not be valid") 326 } 327 } 328 329 func TestNameRegexp(t *testing.T) { 330 cases := []struct { 331 Input string 332 Match bool 333 }{ 334 {"hello", true}, 335 {"foo-bar", true}, 336 {"foo_bar", true}, 337 {"_hello", true}, 338 {"foo bar", false}, 339 {"foo.bar", false}, 340 } 341 342 for _, tc := range cases { 343 if NameRegexp.Match([]byte(tc.Input)) != tc.Match { 344 t.Fatalf("Input: %s\n\nExpected: %#v", tc.Input, tc.Match) 345 } 346 } 347 } 348 349 func TestProviderConfigName(t *testing.T) { 350 pcs := []*ProviderConfig{ 351 &ProviderConfig{Name: "aw"}, 352 &ProviderConfig{Name: "aws"}, 353 &ProviderConfig{Name: "a"}, 354 &ProviderConfig{Name: "gce_"}, 355 } 356 357 n := ProviderConfigName("aws_instance", pcs) 358 if n != "aws" { 359 t.Fatalf("bad: %s", n) 360 } 361 } 362 363 func TestVariableDefaultsMap(t *testing.T) { 364 cases := []struct { 365 Default interface{} 366 Output map[string]string 367 }{ 368 { 369 nil, 370 nil, 371 }, 372 373 { 374 "foo", 375 map[string]string{"var.foo": "foo"}, 376 }, 377 378 { 379 map[interface{}]interface{}{ 380 "foo": "bar", 381 "bar": "baz", 382 }, 383 map[string]string{ 384 "var.foo": "foo", 385 "var.foo.foo": "bar", 386 "var.foo.bar": "baz", 387 }, 388 }, 389 } 390 391 for i, tc := range cases { 392 v := &Variable{Name: "foo", Default: tc.Default} 393 actual := v.DefaultsMap() 394 if !reflect.DeepEqual(actual, tc.Output) { 395 t.Fatalf("%d: bad: %#v", i, actual) 396 } 397 } 398 } 399 400 func testConfig(t *testing.T, name string) *Config { 401 c, err := Load(filepath.Join(fixtureDir, name, "main.tf")) 402 if err != nil { 403 t.Fatalf("file: %s\n\nerr: %s", name, err) 404 } 405 406 return c 407 }