github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/terraform/resource_address_test.go (about) 1 package terraform 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/hashicorp/terraform/config" 8 ) 9 10 func TestParseResourceAddress(t *testing.T) { 11 cases := map[string]struct { 12 Input string 13 Expected *ResourceAddress 14 Output string 15 }{ 16 "implicit primary managed instance, no specific index": { 17 "aws_instance.foo", 18 &ResourceAddress{ 19 Mode: config.ManagedResourceMode, 20 Type: "aws_instance", 21 Name: "foo", 22 InstanceType: TypePrimary, 23 Index: -1, 24 }, 25 "", 26 }, 27 "implicit primary data instance, no specific index": { 28 "data.aws_instance.foo", 29 &ResourceAddress{ 30 Mode: config.DataResourceMode, 31 Type: "aws_instance", 32 Name: "foo", 33 InstanceType: TypePrimary, 34 Index: -1, 35 }, 36 "", 37 }, 38 "implicit primary, explicit index": { 39 "aws_instance.foo[2]", 40 &ResourceAddress{ 41 Mode: config.ManagedResourceMode, 42 Type: "aws_instance", 43 Name: "foo", 44 InstanceType: TypePrimary, 45 Index: 2, 46 }, 47 "", 48 }, 49 "implicit primary, explicit index over ten": { 50 "aws_instance.foo[12]", 51 &ResourceAddress{ 52 Mode: config.ManagedResourceMode, 53 Type: "aws_instance", 54 Name: "foo", 55 InstanceType: TypePrimary, 56 Index: 12, 57 }, 58 "", 59 }, 60 "explicit primary, explicit index": { 61 "aws_instance.foo.primary[2]", 62 &ResourceAddress{ 63 Mode: config.ManagedResourceMode, 64 Type: "aws_instance", 65 Name: "foo", 66 InstanceType: TypePrimary, 67 InstanceTypeSet: true, 68 Index: 2, 69 }, 70 "", 71 }, 72 "tainted": { 73 "aws_instance.foo.tainted", 74 &ResourceAddress{ 75 Mode: config.ManagedResourceMode, 76 Type: "aws_instance", 77 Name: "foo", 78 InstanceType: TypeTainted, 79 InstanceTypeSet: true, 80 Index: -1, 81 }, 82 "", 83 }, 84 "deposed": { 85 "aws_instance.foo.deposed", 86 &ResourceAddress{ 87 Mode: config.ManagedResourceMode, 88 Type: "aws_instance", 89 Name: "foo", 90 InstanceType: TypeDeposed, 91 InstanceTypeSet: true, 92 Index: -1, 93 }, 94 "", 95 }, 96 "with a hyphen": { 97 "aws_instance.foo-bar", 98 &ResourceAddress{ 99 Mode: config.ManagedResourceMode, 100 Type: "aws_instance", 101 Name: "foo-bar", 102 InstanceType: TypePrimary, 103 Index: -1, 104 }, 105 "", 106 }, 107 "managed in a module": { 108 "module.child.aws_instance.foo", 109 &ResourceAddress{ 110 Path: []string{"child"}, 111 Mode: config.ManagedResourceMode, 112 Type: "aws_instance", 113 Name: "foo", 114 InstanceType: TypePrimary, 115 Index: -1, 116 }, 117 "", 118 }, 119 "data in a module": { 120 "module.child.data.aws_instance.foo", 121 &ResourceAddress{ 122 Path: []string{"child"}, 123 Mode: config.DataResourceMode, 124 Type: "aws_instance", 125 Name: "foo", 126 InstanceType: TypePrimary, 127 Index: -1, 128 }, 129 "", 130 }, 131 "nested modules": { 132 "module.a.module.b.module.forever.aws_instance.foo", 133 &ResourceAddress{ 134 Path: []string{"a", "b", "forever"}, 135 Mode: config.ManagedResourceMode, 136 Type: "aws_instance", 137 Name: "foo", 138 InstanceType: TypePrimary, 139 Index: -1, 140 }, 141 "", 142 }, 143 "just a module": { 144 "module.a", 145 &ResourceAddress{ 146 Path: []string{"a"}, 147 Type: "", 148 Name: "", 149 InstanceType: TypePrimary, 150 Index: -1, 151 }, 152 "", 153 }, 154 "just a nested module": { 155 "module.a.module.b", 156 &ResourceAddress{ 157 Path: []string{"a", "b"}, 158 Type: "", 159 Name: "", 160 InstanceType: TypePrimary, 161 Index: -1, 162 }, 163 "", 164 }, 165 } 166 167 for tn, tc := range cases { 168 out, err := ParseResourceAddress(tc.Input) 169 if err != nil { 170 t.Fatalf("%s: unexpected err: %#v", tn, err) 171 } 172 173 if !reflect.DeepEqual(out, tc.Expected) { 174 t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out) 175 } 176 177 expected := tc.Input 178 if tc.Output != "" { 179 expected = tc.Output 180 } 181 if out.String() != expected { 182 t.Fatalf("bad: %q\n\nexpected: %s\n\ngot: %s", tn, expected, out) 183 } 184 } 185 } 186 187 func TestResourceAddressEquals(t *testing.T) { 188 cases := map[string]struct { 189 Address *ResourceAddress 190 Other interface{} 191 Expect bool 192 }{ 193 "basic match": { 194 Address: &ResourceAddress{ 195 Mode: config.ManagedResourceMode, 196 Type: "aws_instance", 197 Name: "foo", 198 InstanceType: TypePrimary, 199 Index: 0, 200 }, 201 Other: &ResourceAddress{ 202 Mode: config.ManagedResourceMode, 203 Type: "aws_instance", 204 Name: "foo", 205 InstanceType: TypePrimary, 206 Index: 0, 207 }, 208 Expect: true, 209 }, 210 "address does not set index": { 211 Address: &ResourceAddress{ 212 Mode: config.ManagedResourceMode, 213 Type: "aws_instance", 214 Name: "foo", 215 InstanceType: TypePrimary, 216 Index: -1, 217 }, 218 Other: &ResourceAddress{ 219 Mode: config.ManagedResourceMode, 220 Type: "aws_instance", 221 Name: "foo", 222 InstanceType: TypePrimary, 223 Index: 3, 224 }, 225 Expect: true, 226 }, 227 "other does not set index": { 228 Address: &ResourceAddress{ 229 Mode: config.ManagedResourceMode, 230 Type: "aws_instance", 231 Name: "foo", 232 InstanceType: TypePrimary, 233 Index: 3, 234 }, 235 Other: &ResourceAddress{ 236 Mode: config.ManagedResourceMode, 237 Type: "aws_instance", 238 Name: "foo", 239 InstanceType: TypePrimary, 240 Index: -1, 241 }, 242 Expect: true, 243 }, 244 "neither sets index": { 245 Address: &ResourceAddress{ 246 Mode: config.ManagedResourceMode, 247 Type: "aws_instance", 248 Name: "foo", 249 InstanceType: TypePrimary, 250 Index: -1, 251 }, 252 Other: &ResourceAddress{ 253 Mode: config.ManagedResourceMode, 254 Type: "aws_instance", 255 Name: "foo", 256 InstanceType: TypePrimary, 257 Index: -1, 258 }, 259 Expect: true, 260 }, 261 "index over ten": { 262 Address: &ResourceAddress{ 263 Mode: config.ManagedResourceMode, 264 Type: "aws_instance", 265 Name: "foo", 266 InstanceType: TypePrimary, 267 Index: 1, 268 }, 269 Other: &ResourceAddress{ 270 Mode: config.ManagedResourceMode, 271 Type: "aws_instance", 272 Name: "foo", 273 InstanceType: TypePrimary, 274 Index: 13, 275 }, 276 Expect: false, 277 }, 278 "different type": { 279 Address: &ResourceAddress{ 280 Mode: config.ManagedResourceMode, 281 Type: "aws_instance", 282 Name: "foo", 283 InstanceType: TypePrimary, 284 Index: 0, 285 }, 286 Other: &ResourceAddress{ 287 Mode: config.ManagedResourceMode, 288 Type: "aws_vpc", 289 Name: "foo", 290 InstanceType: TypePrimary, 291 Index: 0, 292 }, 293 Expect: false, 294 }, 295 "different mode": { 296 Address: &ResourceAddress{ 297 Mode: config.ManagedResourceMode, 298 Type: "aws_instance", 299 Name: "foo", 300 InstanceType: TypePrimary, 301 Index: 0, 302 }, 303 Other: &ResourceAddress{ 304 Mode: config.DataResourceMode, 305 Type: "aws_instance", 306 Name: "foo", 307 InstanceType: TypePrimary, 308 Index: 0, 309 }, 310 Expect: false, 311 }, 312 "different name": { 313 Address: &ResourceAddress{ 314 Mode: config.ManagedResourceMode, 315 Type: "aws_instance", 316 Name: "foo", 317 InstanceType: TypePrimary, 318 Index: 0, 319 }, 320 Other: &ResourceAddress{ 321 Mode: config.ManagedResourceMode, 322 Type: "aws_instance", 323 Name: "bar", 324 InstanceType: TypePrimary, 325 Index: 0, 326 }, 327 Expect: false, 328 }, 329 "different instance type": { 330 Address: &ResourceAddress{ 331 Mode: config.ManagedResourceMode, 332 Type: "aws_instance", 333 Name: "foo", 334 InstanceType: TypePrimary, 335 Index: 0, 336 }, 337 Other: &ResourceAddress{ 338 Mode: config.ManagedResourceMode, 339 Type: "aws_instance", 340 Name: "foo", 341 InstanceType: TypeTainted, 342 Index: 0, 343 }, 344 Expect: false, 345 }, 346 "different index": { 347 Address: &ResourceAddress{ 348 Mode: config.ManagedResourceMode, 349 Type: "aws_instance", 350 Name: "foo", 351 InstanceType: TypePrimary, 352 Index: 0, 353 }, 354 Other: &ResourceAddress{ 355 Mode: config.ManagedResourceMode, 356 Type: "aws_instance", 357 Name: "foo", 358 InstanceType: TypePrimary, 359 Index: 1, 360 }, 361 Expect: false, 362 }, 363 "module address matches address of managed resource inside module": { 364 Address: &ResourceAddress{ 365 Path: []string{"a", "b"}, 366 Type: "", 367 Name: "", 368 InstanceType: TypePrimary, 369 Index: -1, 370 }, 371 Other: &ResourceAddress{ 372 Path: []string{"a", "b"}, 373 Mode: config.ManagedResourceMode, 374 Type: "aws_instance", 375 Name: "foo", 376 InstanceType: TypePrimary, 377 Index: 0, 378 }, 379 Expect: true, 380 }, 381 "module address matches address of data resource inside module": { 382 Address: &ResourceAddress{ 383 Path: []string{"a", "b"}, 384 Type: "", 385 Name: "", 386 InstanceType: TypePrimary, 387 Index: -1, 388 }, 389 Other: &ResourceAddress{ 390 Path: []string{"a", "b"}, 391 Mode: config.DataResourceMode, 392 Type: "aws_instance", 393 Name: "foo", 394 InstanceType: TypePrimary, 395 Index: 0, 396 }, 397 Expect: true, 398 }, 399 "module address doesn't match managed resource outside module": { 400 Address: &ResourceAddress{ 401 Path: []string{"a", "b"}, 402 Type: "", 403 Name: "", 404 InstanceType: TypePrimary, 405 Index: -1, 406 }, 407 Other: &ResourceAddress{ 408 Path: []string{"a"}, 409 Mode: config.ManagedResourceMode, 410 Type: "aws_instance", 411 Name: "foo", 412 InstanceType: TypePrimary, 413 Index: 0, 414 }, 415 Expect: false, 416 }, 417 "module address doesn't match data resource outside module": { 418 Address: &ResourceAddress{ 419 Path: []string{"a", "b"}, 420 Type: "", 421 Name: "", 422 InstanceType: TypePrimary, 423 Index: -1, 424 }, 425 Other: &ResourceAddress{ 426 Path: []string{"a"}, 427 Mode: config.DataResourceMode, 428 Type: "aws_instance", 429 Name: "foo", 430 InstanceType: TypePrimary, 431 Index: 0, 432 }, 433 Expect: false, 434 }, 435 "nil path vs empty path should match": { 436 Address: &ResourceAddress{ 437 Path: []string{}, 438 Mode: config.ManagedResourceMode, 439 Type: "aws_instance", 440 Name: "foo", 441 InstanceType: TypePrimary, 442 Index: -1, 443 }, 444 Other: &ResourceAddress{ 445 Path: nil, 446 Mode: config.ManagedResourceMode, 447 Type: "aws_instance", 448 Name: "foo", 449 InstanceType: TypePrimary, 450 Index: 0, 451 }, 452 Expect: true, 453 }, 454 } 455 456 for tn, tc := range cases { 457 actual := tc.Address.Equals(tc.Other) 458 if actual != tc.Expect { 459 t.Fatalf("%q: expected equals: %t, got %t for:\n%#v\n%#v", 460 tn, tc.Expect, actual, tc.Address, tc.Other) 461 } 462 } 463 }