github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/aws/structure_test.go (about) 1 package aws 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/apigateway" 10 "github.com/aws/aws-sdk-go/service/autoscaling" 11 "github.com/aws/aws-sdk-go/service/ec2" 12 "github.com/aws/aws-sdk-go/service/elasticache" 13 "github.com/aws/aws-sdk-go/service/elb" 14 "github.com/aws/aws-sdk-go/service/rds" 15 "github.com/aws/aws-sdk-go/service/redshift" 16 "github.com/aws/aws-sdk-go/service/route53" 17 "github.com/hashicorp/terraform/flatmap" 18 "github.com/hashicorp/terraform/helper/schema" 19 ) 20 21 // Returns test configuration 22 func testConf() map[string]string { 23 return map[string]string{ 24 "listener.#": "1", 25 "listener.0.lb_port": "80", 26 "listener.0.lb_protocol": "http", 27 "listener.0.instance_port": "8000", 28 "listener.0.instance_protocol": "http", 29 "availability_zones.#": "2", 30 "availability_zones.0": "us-east-1a", 31 "availability_zones.1": "us-east-1b", 32 "ingress.#": "1", 33 "ingress.0.protocol": "icmp", 34 "ingress.0.from_port": "1", 35 "ingress.0.to_port": "-1", 36 "ingress.0.cidr_blocks.#": "1", 37 "ingress.0.cidr_blocks.0": "0.0.0.0/0", 38 "ingress.0.security_groups.#": "2", 39 "ingress.0.security_groups.0": "sg-11111", 40 "ingress.0.security_groups.1": "foo/sg-22222", 41 } 42 } 43 44 func TestExpandIPPerms(t *testing.T) { 45 hash := schema.HashString 46 47 expanded := []interface{}{ 48 map[string]interface{}{ 49 "protocol": "icmp", 50 "from_port": 1, 51 "to_port": -1, 52 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 53 "security_groups": schema.NewSet(hash, []interface{}{ 54 "sg-11111", 55 "foo/sg-22222", 56 }), 57 }, 58 map[string]interface{}{ 59 "protocol": "icmp", 60 "from_port": 1, 61 "to_port": -1, 62 "self": true, 63 }, 64 } 65 group := &ec2.SecurityGroup{ 66 GroupId: aws.String("foo"), 67 VpcId: aws.String("bar"), 68 } 69 perms, err := expandIPPerms(group, expanded) 70 if err != nil { 71 t.Fatalf("error expanding perms: %v", err) 72 } 73 74 expected := []ec2.IpPermission{ 75 ec2.IpPermission{ 76 IpProtocol: aws.String("icmp"), 77 FromPort: aws.Int64(int64(1)), 78 ToPort: aws.Int64(int64(-1)), 79 IpRanges: []*ec2.IpRange{&ec2.IpRange{CidrIp: aws.String("0.0.0.0/0")}}, 80 UserIdGroupPairs: []*ec2.UserIdGroupPair{ 81 &ec2.UserIdGroupPair{ 82 UserId: aws.String("foo"), 83 GroupId: aws.String("sg-22222"), 84 }, 85 &ec2.UserIdGroupPair{ 86 GroupId: aws.String("sg-11111"), 87 }, 88 }, 89 }, 90 ec2.IpPermission{ 91 IpProtocol: aws.String("icmp"), 92 FromPort: aws.Int64(int64(1)), 93 ToPort: aws.Int64(int64(-1)), 94 UserIdGroupPairs: []*ec2.UserIdGroupPair{ 95 &ec2.UserIdGroupPair{ 96 GroupId: aws.String("foo"), 97 }, 98 }, 99 }, 100 } 101 102 exp := expected[0] 103 perm := perms[0] 104 105 if *exp.FromPort != *perm.FromPort { 106 t.Fatalf( 107 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 108 *perm.FromPort, 109 *exp.FromPort) 110 } 111 112 if *exp.IpRanges[0].CidrIp != *perm.IpRanges[0].CidrIp { 113 t.Fatalf( 114 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 115 *perm.IpRanges[0].CidrIp, 116 *exp.IpRanges[0].CidrIp) 117 } 118 119 if *exp.UserIdGroupPairs[0].UserId != *perm.UserIdGroupPairs[0].UserId { 120 t.Fatalf( 121 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 122 *perm.UserIdGroupPairs[0].UserId, 123 *exp.UserIdGroupPairs[0].UserId) 124 } 125 126 if *exp.UserIdGroupPairs[0].GroupId != *perm.UserIdGroupPairs[0].GroupId { 127 t.Fatalf( 128 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 129 *perm.UserIdGroupPairs[0].GroupId, 130 *exp.UserIdGroupPairs[0].GroupId) 131 } 132 133 if *exp.UserIdGroupPairs[1].GroupId != *perm.UserIdGroupPairs[1].GroupId { 134 t.Fatalf( 135 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 136 *perm.UserIdGroupPairs[1].GroupId, 137 *exp.UserIdGroupPairs[1].GroupId) 138 } 139 140 exp = expected[1] 141 perm = perms[1] 142 143 if *exp.UserIdGroupPairs[0].GroupId != *perm.UserIdGroupPairs[0].GroupId { 144 t.Fatalf( 145 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 146 *perm.UserIdGroupPairs[0].GroupId, 147 *exp.UserIdGroupPairs[0].GroupId) 148 } 149 } 150 151 func TestExpandIPPerms_NegOneProtocol(t *testing.T) { 152 hash := schema.HashString 153 154 expanded := []interface{}{ 155 map[string]interface{}{ 156 "protocol": "-1", 157 "from_port": 0, 158 "to_port": 0, 159 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 160 "security_groups": schema.NewSet(hash, []interface{}{ 161 "sg-11111", 162 "foo/sg-22222", 163 }), 164 }, 165 } 166 group := &ec2.SecurityGroup{ 167 GroupId: aws.String("foo"), 168 VpcId: aws.String("bar"), 169 } 170 171 perms, err := expandIPPerms(group, expanded) 172 if err != nil { 173 t.Fatalf("error expanding perms: %v", err) 174 } 175 176 expected := []ec2.IpPermission{ 177 ec2.IpPermission{ 178 IpProtocol: aws.String("-1"), 179 FromPort: aws.Int64(int64(0)), 180 ToPort: aws.Int64(int64(0)), 181 IpRanges: []*ec2.IpRange{&ec2.IpRange{CidrIp: aws.String("0.0.0.0/0")}}, 182 UserIdGroupPairs: []*ec2.UserIdGroupPair{ 183 &ec2.UserIdGroupPair{ 184 UserId: aws.String("foo"), 185 GroupId: aws.String("sg-22222"), 186 }, 187 &ec2.UserIdGroupPair{ 188 GroupId: aws.String("sg-11111"), 189 }, 190 }, 191 }, 192 } 193 194 exp := expected[0] 195 perm := perms[0] 196 197 if *exp.FromPort != *perm.FromPort { 198 t.Fatalf( 199 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 200 *perm.FromPort, 201 *exp.FromPort) 202 } 203 204 if *exp.IpRanges[0].CidrIp != *perm.IpRanges[0].CidrIp { 205 t.Fatalf( 206 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 207 *perm.IpRanges[0].CidrIp, 208 *exp.IpRanges[0].CidrIp) 209 } 210 211 if *exp.UserIdGroupPairs[0].UserId != *perm.UserIdGroupPairs[0].UserId { 212 t.Fatalf( 213 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 214 *perm.UserIdGroupPairs[0].UserId, 215 *exp.UserIdGroupPairs[0].UserId) 216 } 217 218 // Now test the error case. This *should* error when either from_port 219 // or to_port is not zero, but protocol is "-1". 220 errorCase := []interface{}{ 221 map[string]interface{}{ 222 "protocol": "-1", 223 "from_port": 0, 224 "to_port": 65535, 225 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 226 "security_groups": schema.NewSet(hash, []interface{}{ 227 "sg-11111", 228 "foo/sg-22222", 229 }), 230 }, 231 } 232 securityGroups := &ec2.SecurityGroup{ 233 GroupId: aws.String("foo"), 234 VpcId: aws.String("bar"), 235 } 236 237 _, expandErr := expandIPPerms(securityGroups, errorCase) 238 if expandErr == nil { 239 t.Fatal("expandIPPerms should have errored!") 240 } 241 } 242 243 func TestExpandIPPerms_nonVPC(t *testing.T) { 244 hash := schema.HashString 245 246 expanded := []interface{}{ 247 map[string]interface{}{ 248 "protocol": "icmp", 249 "from_port": 1, 250 "to_port": -1, 251 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 252 "security_groups": schema.NewSet(hash, []interface{}{ 253 "sg-11111", 254 "foo/sg-22222", 255 }), 256 }, 257 map[string]interface{}{ 258 "protocol": "icmp", 259 "from_port": 1, 260 "to_port": -1, 261 "self": true, 262 }, 263 } 264 group := &ec2.SecurityGroup{ 265 GroupName: aws.String("foo"), 266 } 267 perms, err := expandIPPerms(group, expanded) 268 if err != nil { 269 t.Fatalf("error expanding perms: %v", err) 270 } 271 272 expected := []ec2.IpPermission{ 273 ec2.IpPermission{ 274 IpProtocol: aws.String("icmp"), 275 FromPort: aws.Int64(int64(1)), 276 ToPort: aws.Int64(int64(-1)), 277 IpRanges: []*ec2.IpRange{&ec2.IpRange{CidrIp: aws.String("0.0.0.0/0")}}, 278 UserIdGroupPairs: []*ec2.UserIdGroupPair{ 279 &ec2.UserIdGroupPair{ 280 GroupName: aws.String("sg-22222"), 281 }, 282 &ec2.UserIdGroupPair{ 283 GroupName: aws.String("sg-11111"), 284 }, 285 }, 286 }, 287 ec2.IpPermission{ 288 IpProtocol: aws.String("icmp"), 289 FromPort: aws.Int64(int64(1)), 290 ToPort: aws.Int64(int64(-1)), 291 UserIdGroupPairs: []*ec2.UserIdGroupPair{ 292 &ec2.UserIdGroupPair{ 293 GroupName: aws.String("foo"), 294 }, 295 }, 296 }, 297 } 298 299 exp := expected[0] 300 perm := perms[0] 301 302 if *exp.FromPort != *perm.FromPort { 303 t.Fatalf( 304 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 305 *perm.FromPort, 306 *exp.FromPort) 307 } 308 309 if *exp.IpRanges[0].CidrIp != *perm.IpRanges[0].CidrIp { 310 t.Fatalf( 311 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 312 *perm.IpRanges[0].CidrIp, 313 *exp.IpRanges[0].CidrIp) 314 } 315 316 if *exp.UserIdGroupPairs[0].GroupName != *perm.UserIdGroupPairs[0].GroupName { 317 t.Fatalf( 318 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 319 *perm.UserIdGroupPairs[0].GroupName, 320 *exp.UserIdGroupPairs[0].GroupName) 321 } 322 323 if *exp.UserIdGroupPairs[1].GroupName != *perm.UserIdGroupPairs[1].GroupName { 324 t.Fatalf( 325 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 326 *perm.UserIdGroupPairs[1].GroupName, 327 *exp.UserIdGroupPairs[1].GroupName) 328 } 329 330 exp = expected[1] 331 perm = perms[1] 332 333 if *exp.UserIdGroupPairs[0].GroupName != *perm.UserIdGroupPairs[0].GroupName { 334 t.Fatalf( 335 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 336 *perm.UserIdGroupPairs[0].GroupName, 337 *exp.UserIdGroupPairs[0].GroupName) 338 } 339 } 340 341 func TestExpandListeners(t *testing.T) { 342 expanded := []interface{}{ 343 map[string]interface{}{ 344 "instance_port": 8000, 345 "lb_port": 80, 346 "instance_protocol": "http", 347 "lb_protocol": "http", 348 }, 349 map[string]interface{}{ 350 "instance_port": 8000, 351 "lb_port": 80, 352 "instance_protocol": "https", 353 "lb_protocol": "https", 354 "ssl_certificate_id": "something", 355 }, 356 } 357 listeners, err := expandListeners(expanded) 358 if err != nil { 359 t.Fatalf("bad: %#v", err) 360 } 361 362 expected := &elb.Listener{ 363 InstancePort: aws.Int64(int64(8000)), 364 LoadBalancerPort: aws.Int64(int64(80)), 365 InstanceProtocol: aws.String("http"), 366 Protocol: aws.String("http"), 367 } 368 369 if !reflect.DeepEqual(listeners[0], expected) { 370 t.Fatalf( 371 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 372 listeners[0], 373 expected) 374 } 375 } 376 377 // this test should produce an error from expandlisteners on an invalid 378 // combination 379 func TestExpandListeners_invalid(t *testing.T) { 380 expanded := []interface{}{ 381 map[string]interface{}{ 382 "instance_port": 8000, 383 "lb_port": 80, 384 "instance_protocol": "http", 385 "lb_protocol": "http", 386 "ssl_certificate_id": "something", 387 }, 388 } 389 _, err := expandListeners(expanded) 390 if err != nil { 391 // Check the error we got 392 if !strings.Contains(err.Error(), "ssl_certificate_id may be set only when protocol") { 393 t.Fatalf("Got error in TestExpandListeners_invalid, but not what we expected: %s", err) 394 } 395 } 396 397 if err == nil { 398 t.Fatalf("Expected TestExpandListeners_invalid to fail, but passed") 399 } 400 } 401 402 func TestFlattenHealthCheck(t *testing.T) { 403 cases := []struct { 404 Input *elb.HealthCheck 405 Output []map[string]interface{} 406 }{ 407 { 408 Input: &elb.HealthCheck{ 409 UnhealthyThreshold: aws.Int64(int64(10)), 410 HealthyThreshold: aws.Int64(int64(10)), 411 Target: aws.String("HTTP:80/"), 412 Timeout: aws.Int64(int64(30)), 413 Interval: aws.Int64(int64(30)), 414 }, 415 Output: []map[string]interface{}{ 416 map[string]interface{}{ 417 "unhealthy_threshold": int64(10), 418 "healthy_threshold": int64(10), 419 "target": "HTTP:80/", 420 "timeout": int64(30), 421 "interval": int64(30), 422 }, 423 }, 424 }, 425 } 426 427 for _, tc := range cases { 428 output := flattenHealthCheck(tc.Input) 429 if !reflect.DeepEqual(output, tc.Output) { 430 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 431 } 432 } 433 } 434 435 func TestExpandStringList(t *testing.T) { 436 expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{}) 437 stringList := expandStringList(expanded) 438 expected := []*string{ 439 aws.String("us-east-1a"), 440 aws.String("us-east-1b"), 441 } 442 443 if !reflect.DeepEqual(stringList, expected) { 444 t.Fatalf( 445 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 446 stringList, 447 expected) 448 } 449 450 } 451 452 func TestExpandParameters(t *testing.T) { 453 expanded := []interface{}{ 454 map[string]interface{}{ 455 "name": "character_set_client", 456 "value": "utf8", 457 "apply_method": "immediate", 458 }, 459 } 460 parameters, err := expandParameters(expanded) 461 if err != nil { 462 t.Fatalf("bad: %#v", err) 463 } 464 465 expected := &rds.Parameter{ 466 ParameterName: aws.String("character_set_client"), 467 ParameterValue: aws.String("utf8"), 468 ApplyMethod: aws.String("immediate"), 469 } 470 471 if !reflect.DeepEqual(parameters[0], expected) { 472 t.Fatalf( 473 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 474 parameters[0], 475 expected) 476 } 477 } 478 479 func TestexpandRedshiftParameters(t *testing.T) { 480 expanded := []interface{}{ 481 map[string]interface{}{ 482 "name": "character_set_client", 483 "value": "utf8", 484 }, 485 } 486 parameters, err := expandRedshiftParameters(expanded) 487 if err != nil { 488 t.Fatalf("bad: %#v", err) 489 } 490 491 expected := &redshift.Parameter{ 492 ParameterName: aws.String("character_set_client"), 493 ParameterValue: aws.String("utf8"), 494 } 495 496 if !reflect.DeepEqual(parameters[0], expected) { 497 t.Fatalf( 498 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 499 parameters[0], 500 expected) 501 } 502 } 503 504 func TestexpandElasticacheParameters(t *testing.T) { 505 expanded := []interface{}{ 506 map[string]interface{}{ 507 "name": "activerehashing", 508 "value": "yes", 509 "apply_method": "immediate", 510 }, 511 } 512 parameters, err := expandElastiCacheParameters(expanded) 513 if err != nil { 514 t.Fatalf("bad: %#v", err) 515 } 516 517 expected := &elasticache.ParameterNameValue{ 518 ParameterName: aws.String("activerehashing"), 519 ParameterValue: aws.String("yes"), 520 } 521 522 if !reflect.DeepEqual(parameters[0], expected) { 523 t.Fatalf( 524 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 525 parameters[0], 526 expected) 527 } 528 } 529 530 func TestExpandStepAdjustments(t *testing.T) { 531 expanded := []interface{}{ 532 map[string]interface{}{ 533 "metric_interval_lower_bound": "1.0", 534 "metric_interval_upper_bound": "2.0", 535 "scaling_adjustment": 1, 536 }, 537 } 538 parameters, err := expandStepAdjustments(expanded) 539 if err != nil { 540 t.Fatalf("bad: %#v", err) 541 } 542 543 expected := &autoscaling.StepAdjustment{ 544 MetricIntervalLowerBound: aws.Float64(1.0), 545 MetricIntervalUpperBound: aws.Float64(2.0), 546 ScalingAdjustment: aws.Int64(int64(1)), 547 } 548 549 if !reflect.DeepEqual(parameters[0], expected) { 550 t.Fatalf( 551 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 552 parameters[0], 553 expected) 554 } 555 } 556 557 func TestFlattenParameters(t *testing.T) { 558 cases := []struct { 559 Input []*rds.Parameter 560 Output []map[string]interface{} 561 }{ 562 { 563 Input: []*rds.Parameter{ 564 &rds.Parameter{ 565 ParameterName: aws.String("character_set_client"), 566 ParameterValue: aws.String("utf8"), 567 }, 568 }, 569 Output: []map[string]interface{}{ 570 map[string]interface{}{ 571 "name": "character_set_client", 572 "value": "utf8", 573 }, 574 }, 575 }, 576 } 577 578 for _, tc := range cases { 579 output := flattenParameters(tc.Input) 580 if !reflect.DeepEqual(output, tc.Output) { 581 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 582 } 583 } 584 } 585 586 func TestflattenRedshiftParameters(t *testing.T) { 587 cases := []struct { 588 Input []*redshift.Parameter 589 Output []map[string]interface{} 590 }{ 591 { 592 Input: []*redshift.Parameter{ 593 &redshift.Parameter{ 594 ParameterName: aws.String("character_set_client"), 595 ParameterValue: aws.String("utf8"), 596 }, 597 }, 598 Output: []map[string]interface{}{ 599 map[string]interface{}{ 600 "name": "character_set_client", 601 "value": "utf8", 602 }, 603 }, 604 }, 605 } 606 607 for _, tc := range cases { 608 output := flattenRedshiftParameters(tc.Input) 609 if !reflect.DeepEqual(output, tc.Output) { 610 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 611 } 612 } 613 } 614 615 func TestflattenElasticacheParameters(t *testing.T) { 616 cases := []struct { 617 Input []*elasticache.Parameter 618 Output []map[string]interface{} 619 }{ 620 { 621 Input: []*elasticache.Parameter{ 622 &elasticache.Parameter{ 623 ParameterName: aws.String("activerehashing"), 624 ParameterValue: aws.String("yes"), 625 }, 626 }, 627 Output: []map[string]interface{}{ 628 map[string]interface{}{ 629 "name": "activerehashing", 630 "value": "yes", 631 }, 632 }, 633 }, 634 } 635 636 for _, tc := range cases { 637 output := flattenElastiCacheParameters(tc.Input) 638 if !reflect.DeepEqual(output, tc.Output) { 639 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 640 } 641 } 642 } 643 644 func TestExpandInstanceString(t *testing.T) { 645 646 expected := []*elb.Instance{ 647 &elb.Instance{InstanceId: aws.String("test-one")}, 648 &elb.Instance{InstanceId: aws.String("test-two")}, 649 } 650 651 ids := []interface{}{ 652 "test-one", 653 "test-two", 654 } 655 656 expanded := expandInstanceString(ids) 657 658 if !reflect.DeepEqual(expanded, expected) { 659 t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected) 660 } 661 } 662 663 func TestFlattenNetworkInterfacesPrivateIPAddresses(t *testing.T) { 664 expanded := []*ec2.NetworkInterfacePrivateIpAddress{ 665 &ec2.NetworkInterfacePrivateIpAddress{PrivateIpAddress: aws.String("192.168.0.1")}, 666 &ec2.NetworkInterfacePrivateIpAddress{PrivateIpAddress: aws.String("192.168.0.2")}, 667 } 668 669 result := flattenNetworkInterfacesPrivateIPAddresses(expanded) 670 671 if result == nil { 672 t.Fatal("result was nil") 673 } 674 675 if len(result) != 2 { 676 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 677 } 678 679 if result[0] != "192.168.0.1" { 680 t.Fatalf("expected ip to be 192.168.0.1, but was %s", result[0]) 681 } 682 683 if result[1] != "192.168.0.2" { 684 t.Fatalf("expected ip to be 192.168.0.2, but was %s", result[1]) 685 } 686 } 687 688 func TestFlattenGroupIdentifiers(t *testing.T) { 689 expanded := []*ec2.GroupIdentifier{ 690 &ec2.GroupIdentifier{GroupId: aws.String("sg-001")}, 691 &ec2.GroupIdentifier{GroupId: aws.String("sg-002")}, 692 } 693 694 result := flattenGroupIdentifiers(expanded) 695 696 if len(result) != 2 { 697 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 698 } 699 700 if result[0] != "sg-001" { 701 t.Fatalf("expected id to be sg-001, but was %s", result[0]) 702 } 703 704 if result[1] != "sg-002" { 705 t.Fatalf("expected id to be sg-002, but was %s", result[1]) 706 } 707 } 708 709 func TestExpandPrivateIPAddresses(t *testing.T) { 710 711 ip1 := "192.168.0.1" 712 ip2 := "192.168.0.2" 713 flattened := []interface{}{ 714 ip1, 715 ip2, 716 } 717 718 result := expandPrivateIPAddresses(flattened) 719 720 if len(result) != 2 { 721 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 722 } 723 724 if *result[0].PrivateIpAddress != "192.168.0.1" || !*result[0].Primary { 725 t.Fatalf("expected ip to be 192.168.0.1 and Primary, but got %v, %t", *result[0].PrivateIpAddress, *result[0].Primary) 726 } 727 728 if *result[1].PrivateIpAddress != "192.168.0.2" || *result[1].Primary { 729 t.Fatalf("expected ip to be 192.168.0.2 and not Primary, but got %v, %t", *result[1].PrivateIpAddress, *result[1].Primary) 730 } 731 } 732 733 func TestFlattenAttachment(t *testing.T) { 734 expanded := &ec2.NetworkInterfaceAttachment{ 735 InstanceId: aws.String("i-00001"), 736 DeviceIndex: aws.Int64(int64(1)), 737 AttachmentId: aws.String("at-002"), 738 } 739 740 result := flattenAttachment(expanded) 741 742 if result == nil { 743 t.Fatal("expected result to have value, but got nil") 744 } 745 746 if result["instance"] != "i-00001" { 747 t.Fatalf("expected instance to be i-00001, but got %s", result["instance"]) 748 } 749 750 if result["device_index"] != int64(1) { 751 t.Fatalf("expected device_index to be 1, but got %d", result["device_index"]) 752 } 753 754 if result["attachment_id"] != "at-002" { 755 t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"]) 756 } 757 } 758 759 func TestflattenStepAdjustments(t *testing.T) { 760 expanded := []*autoscaling.StepAdjustment{ 761 &autoscaling.StepAdjustment{ 762 MetricIntervalLowerBound: aws.Float64(1.0), 763 MetricIntervalUpperBound: aws.Float64(2.0), 764 ScalingAdjustment: aws.Int64(int64(1)), 765 }, 766 } 767 768 result := flattenStepAdjustments(expanded)[0] 769 if result == nil { 770 t.Fatal("expected result to have value, but got nil") 771 } 772 if result["metric_interval_lower_bound"] != float64(1.0) { 773 t.Fatalf("expected metric_interval_lower_bound to be 1.0, but got %d", result["metric_interval_lower_bound"]) 774 } 775 if result["metric_interval_upper_bound"] != float64(2.0) { 776 t.Fatalf("expected metric_interval_upper_bound to be 1.0, but got %d", result["metric_interval_upper_bound"]) 777 } 778 if result["scaling_adjustment"] != int64(1) { 779 t.Fatalf("expected scaling_adjustment to be 1, but got %d", result["scaling_adjustment"]) 780 } 781 } 782 783 func TestFlattenResourceRecords(t *testing.T) { 784 expanded := []*route53.ResourceRecord{ 785 &route53.ResourceRecord{ 786 Value: aws.String("127.0.0.1"), 787 }, 788 &route53.ResourceRecord{ 789 Value: aws.String("127.0.0.3"), 790 }, 791 } 792 793 result := flattenResourceRecords(expanded) 794 795 if result == nil { 796 t.Fatal("expected result to have value, but got nil") 797 } 798 799 if len(result) != 2 { 800 t.Fatal("expected result to have value, but got nil") 801 } 802 } 803 804 func TestFlattenAsgEnabledMetrics(t *testing.T) { 805 expanded := []*autoscaling.EnabledMetric{ 806 &autoscaling.EnabledMetric{Granularity: aws.String("1Minute"), Metric: aws.String("GroupTotalInstances")}, 807 &autoscaling.EnabledMetric{Granularity: aws.String("1Minute"), Metric: aws.String("GroupMaxSize")}, 808 } 809 810 result := flattenAsgEnabledMetrics(expanded) 811 812 if len(result) != 2 { 813 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 814 } 815 816 if result[0] != "GroupTotalInstances" { 817 t.Fatalf("expected id to be GroupTotalInstances, but was %s", result[0]) 818 } 819 820 if result[1] != "GroupMaxSize" { 821 t.Fatalf("expected id to be GroupMaxSize, but was %s", result[1]) 822 } 823 } 824 825 func TestFlattenSecurityGroups(t *testing.T) { 826 cases := []struct { 827 ownerId *string 828 pairs []*ec2.UserIdGroupPair 829 expected []*ec2.GroupIdentifier 830 }{ 831 // simple, no user id included (we ignore it mostly) 832 { 833 ownerId: aws.String("user1234"), 834 pairs: []*ec2.UserIdGroupPair{ 835 &ec2.UserIdGroupPair{ 836 GroupId: aws.String("sg-12345"), 837 }, 838 }, 839 expected: []*ec2.GroupIdentifier{ 840 &ec2.GroupIdentifier{ 841 GroupId: aws.String("sg-12345"), 842 }, 843 }, 844 }, 845 // include the owner id, but keep it consitent with the same account. Tests 846 // EC2 classic situation 847 { 848 ownerId: aws.String("user1234"), 849 pairs: []*ec2.UserIdGroupPair{ 850 &ec2.UserIdGroupPair{ 851 GroupId: aws.String("sg-12345"), 852 UserId: aws.String("user1234"), 853 }, 854 }, 855 expected: []*ec2.GroupIdentifier{ 856 &ec2.GroupIdentifier{ 857 GroupId: aws.String("sg-12345"), 858 }, 859 }, 860 }, 861 862 // include the owner id, but from a different account. This is reflects 863 // EC2 Classic when refering to groups by name 864 { 865 ownerId: aws.String("user1234"), 866 pairs: []*ec2.UserIdGroupPair{ 867 &ec2.UserIdGroupPair{ 868 GroupId: aws.String("sg-12345"), 869 GroupName: aws.String("somegroup"), // GroupName is only included in Classic 870 UserId: aws.String("user4321"), 871 }, 872 }, 873 expected: []*ec2.GroupIdentifier{ 874 &ec2.GroupIdentifier{ 875 GroupId: aws.String("sg-12345"), 876 GroupName: aws.String("user4321/somegroup"), 877 }, 878 }, 879 }, 880 881 // include the owner id, but from a different account. This reflects in 882 // EC2 VPC when refering to groups by id 883 { 884 ownerId: aws.String("user1234"), 885 pairs: []*ec2.UserIdGroupPair{ 886 &ec2.UserIdGroupPair{ 887 GroupId: aws.String("sg-12345"), 888 UserId: aws.String("user4321"), 889 }, 890 }, 891 expected: []*ec2.GroupIdentifier{ 892 &ec2.GroupIdentifier{ 893 GroupId: aws.String("user4321/sg-12345"), 894 }, 895 }, 896 }, 897 } 898 899 for _, c := range cases { 900 out := flattenSecurityGroups(c.pairs, c.ownerId) 901 if !reflect.DeepEqual(out, c.expected) { 902 t.Fatalf("Error matching output and expected: %#v vs %#v", out, c.expected) 903 } 904 } 905 } 906 907 func TestFlattenApiGatewayThrottleSettings(t *testing.T) { 908 expectedBurstLimit := int64(140) 909 expectedRateLimit := 120.0 910 911 ts := &apigateway.ThrottleSettings{ 912 BurstLimit: aws.Int64(expectedBurstLimit), 913 RateLimit: aws.Float64(expectedRateLimit), 914 } 915 result := flattenApiGatewayThrottleSettings(ts) 916 917 if len(result) != 1 { 918 t.Fatalf("Expected map to have exactly 1 element, got %d", len(result)) 919 } 920 921 burstLimit, ok := result[0]["burst_limit"] 922 if !ok { 923 t.Fatal("Expected 'burst_limit' key in the map") 924 } 925 burstLimitInt, ok := burstLimit.(int64) 926 if !ok { 927 t.Fatal("Expected 'burst_limit' to be int") 928 } 929 if burstLimitInt != expectedBurstLimit { 930 t.Fatalf("Expected 'burst_limit' to equal %d, got %d", expectedBurstLimit, burstLimitInt) 931 } 932 933 rateLimit, ok := result[0]["rate_limit"] 934 if !ok { 935 t.Fatal("Expected 'rate_limit' key in the map") 936 } 937 rateLimitFloat, ok := rateLimit.(float64) 938 if !ok { 939 t.Fatal("Expected 'rate_limit' to be float64") 940 } 941 if rateLimitFloat != expectedRateLimit { 942 t.Fatalf("Expected 'rate_limit' to equal %f, got %f", expectedRateLimit, rateLimitFloat) 943 } 944 }