github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/aws/structure_test.go (about) 1 package aws 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/awslabs/aws-sdk-go/aws" 8 "github.com/awslabs/aws-sdk-go/service/ec2" 9 "github.com/awslabs/aws-sdk-go/service/elb" 10 "github.com/awslabs/aws-sdk-go/service/rds" 11 "github.com/awslabs/aws-sdk-go/service/route53" 12 "github.com/hashicorp/terraform/flatmap" 13 "github.com/hashicorp/terraform/helper/schema" 14 ) 15 16 // Returns test configuration 17 func testConf() map[string]string { 18 return map[string]string{ 19 "listener.#": "1", 20 "listener.0.lb_port": "80", 21 "listener.0.lb_protocol": "http", 22 "listener.0.instance_port": "8000", 23 "listener.0.instance_protocol": "http", 24 "availability_zones.#": "2", 25 "availability_zones.0": "us-east-1a", 26 "availability_zones.1": "us-east-1b", 27 "ingress.#": "1", 28 "ingress.0.protocol": "icmp", 29 "ingress.0.from_port": "1", 30 "ingress.0.to_port": "-1", 31 "ingress.0.cidr_blocks.#": "1", 32 "ingress.0.cidr_blocks.0": "0.0.0.0/0", 33 "ingress.0.security_groups.#": "2", 34 "ingress.0.security_groups.0": "sg-11111", 35 "ingress.0.security_groups.1": "foo/sg-22222", 36 } 37 } 38 39 func TestexpandIPPerms(t *testing.T) { 40 hash := schema.HashString 41 42 expanded := []interface{}{ 43 map[string]interface{}{ 44 "protocol": "icmp", 45 "from_port": 1, 46 "to_port": -1, 47 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 48 "security_groups": schema.NewSet(hash, []interface{}{ 49 "sg-11111", 50 "foo/sg-22222", 51 }), 52 }, 53 map[string]interface{}{ 54 "protocol": "icmp", 55 "from_port": 1, 56 "to_port": -1, 57 "self": true, 58 }, 59 } 60 group := &ec2.SecurityGroup{ 61 GroupID: aws.String("foo"), 62 VPCID: aws.String("bar"), 63 } 64 perms, err := expandIPPerms(group, expanded) 65 if err != nil { 66 t.Fatalf("error expanding perms: %v", err) 67 } 68 69 expected := []ec2.IPPermission{ 70 ec2.IPPermission{ 71 IPProtocol: aws.String("icmp"), 72 FromPort: aws.Long(int64(1)), 73 ToPort: aws.Long(int64(-1)), 74 IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}}, 75 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 76 &ec2.UserIDGroupPair{ 77 UserID: aws.String("foo"), 78 GroupID: aws.String("sg-22222"), 79 }, 80 &ec2.UserIDGroupPair{ 81 GroupID: aws.String("sg-22222"), 82 }, 83 }, 84 }, 85 ec2.IPPermission{ 86 IPProtocol: aws.String("icmp"), 87 FromPort: aws.Long(int64(1)), 88 ToPort: aws.Long(int64(-1)), 89 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 90 &ec2.UserIDGroupPair{ 91 UserID: aws.String("foo"), 92 }, 93 }, 94 }, 95 } 96 97 exp := expected[0] 98 perm := perms[0] 99 100 if *exp.FromPort != *perm.FromPort { 101 t.Fatalf( 102 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 103 *perm.FromPort, 104 *exp.FromPort) 105 } 106 107 if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP { 108 t.Fatalf( 109 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 110 *perm.IPRanges[0].CIDRIP, 111 *exp.IPRanges[0].CIDRIP) 112 } 113 114 if *exp.UserIDGroupPairs[0].UserID != *perm.UserIDGroupPairs[0].UserID { 115 t.Fatalf( 116 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 117 *perm.UserIDGroupPairs[0].UserID, 118 *exp.UserIDGroupPairs[0].UserID) 119 } 120 121 } 122 123 func TestExpandIPPerms_NegOneProtocol(t *testing.T) { 124 hash := schema.HashString 125 126 expanded := []interface{}{ 127 map[string]interface{}{ 128 "protocol": "-1", 129 "from_port": 0, 130 "to_port": 0, 131 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 132 "security_groups": schema.NewSet(hash, []interface{}{ 133 "sg-11111", 134 "foo/sg-22222", 135 }), 136 }, 137 } 138 group := &ec2.SecurityGroup{ 139 GroupID: aws.String("foo"), 140 VPCID: aws.String("bar"), 141 } 142 143 perms, err := expandIPPerms(group, expanded) 144 if err != nil { 145 t.Fatalf("error expanding perms: %v", err) 146 } 147 148 expected := []ec2.IPPermission{ 149 ec2.IPPermission{ 150 IPProtocol: aws.String("-1"), 151 FromPort: aws.Long(int64(0)), 152 ToPort: aws.Long(int64(0)), 153 IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}}, 154 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 155 &ec2.UserIDGroupPair{ 156 UserID: aws.String("foo"), 157 GroupID: aws.String("sg-22222"), 158 }, 159 &ec2.UserIDGroupPair{ 160 GroupID: aws.String("sg-22222"), 161 }, 162 }, 163 }, 164 } 165 166 exp := expected[0] 167 perm := perms[0] 168 169 if *exp.FromPort != *perm.FromPort { 170 t.Fatalf( 171 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 172 *perm.FromPort, 173 *exp.FromPort) 174 } 175 176 if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP { 177 t.Fatalf( 178 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 179 *perm.IPRanges[0].CIDRIP, 180 *exp.IPRanges[0].CIDRIP) 181 } 182 183 if *exp.UserIDGroupPairs[0].UserID != *perm.UserIDGroupPairs[0].UserID { 184 t.Fatalf( 185 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 186 *perm.UserIDGroupPairs[0].UserID, 187 *exp.UserIDGroupPairs[0].UserID) 188 } 189 190 // Now test the error case. This *should* error when either from_port 191 // or to_port is not zero, but protocal is "-1". 192 errorCase := []interface{}{ 193 map[string]interface{}{ 194 "protocol": "-1", 195 "from_port": 0, 196 "to_port": 65535, 197 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 198 "security_groups": schema.NewSet(hash, []interface{}{ 199 "sg-11111", 200 "foo/sg-22222", 201 }), 202 }, 203 } 204 securityGroups := &ec2.SecurityGroup{ 205 GroupID: aws.String("foo"), 206 VPCID: aws.String("bar"), 207 } 208 209 _, expandErr := expandIPPerms(securityGroups, errorCase) 210 if expandErr == nil { 211 t.Fatal("expandIPPerms should have errored!") 212 } 213 } 214 215 func TestExpandIPPerms_nonVPC(t *testing.T) { 216 hash := schema.HashString 217 218 expanded := []interface{}{ 219 map[string]interface{}{ 220 "protocol": "icmp", 221 "from_port": 1, 222 "to_port": -1, 223 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 224 "security_groups": schema.NewSet(hash, []interface{}{ 225 "sg-11111", 226 "foo/sg-22222", 227 }), 228 }, 229 map[string]interface{}{ 230 "protocol": "icmp", 231 "from_port": 1, 232 "to_port": -1, 233 "self": true, 234 }, 235 } 236 group := &ec2.SecurityGroup{ 237 GroupName: aws.String("foo"), 238 } 239 perms, err := expandIPPerms(group, expanded) 240 if err != nil { 241 t.Fatalf("error expanding perms: %v", err) 242 } 243 244 expected := []ec2.IPPermission{ 245 ec2.IPPermission{ 246 IPProtocol: aws.String("icmp"), 247 FromPort: aws.Long(int64(1)), 248 ToPort: aws.Long(int64(-1)), 249 IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}}, 250 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 251 &ec2.UserIDGroupPair{ 252 GroupName: aws.String("sg-22222"), 253 }, 254 &ec2.UserIDGroupPair{ 255 GroupName: aws.String("sg-22222"), 256 }, 257 }, 258 }, 259 ec2.IPPermission{ 260 IPProtocol: aws.String("icmp"), 261 FromPort: aws.Long(int64(1)), 262 ToPort: aws.Long(int64(-1)), 263 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 264 &ec2.UserIDGroupPair{ 265 GroupName: aws.String("foo"), 266 }, 267 }, 268 }, 269 } 270 271 exp := expected[0] 272 perm := perms[0] 273 274 if *exp.FromPort != *perm.FromPort { 275 t.Fatalf( 276 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 277 *perm.FromPort, 278 *exp.FromPort) 279 } 280 281 if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP { 282 t.Fatalf( 283 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 284 *perm.IPRanges[0].CIDRIP, 285 *exp.IPRanges[0].CIDRIP) 286 } 287 } 288 289 func TestexpandListeners(t *testing.T) { 290 expanded := []interface{}{ 291 map[string]interface{}{ 292 "instance_port": 8000, 293 "lb_port": 80, 294 "instance_protocol": "http", 295 "lb_protocol": "http", 296 }, 297 } 298 listeners, err := expandListeners(expanded) 299 if err != nil { 300 t.Fatalf("bad: %#v", err) 301 } 302 303 expected := &elb.Listener{ 304 InstancePort: aws.Long(int64(8000)), 305 LoadBalancerPort: aws.Long(int64(80)), 306 InstanceProtocol: aws.String("http"), 307 Protocol: aws.String("http"), 308 } 309 310 if !reflect.DeepEqual(listeners[0], expected) { 311 t.Fatalf( 312 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 313 listeners[0], 314 expected) 315 } 316 317 } 318 319 func TestflattenHealthCheck(t *testing.T) { 320 cases := []struct { 321 Input *elb.HealthCheck 322 Output []map[string]interface{} 323 }{ 324 { 325 Input: &elb.HealthCheck{ 326 UnhealthyThreshold: aws.Long(int64(10)), 327 HealthyThreshold: aws.Long(int64(10)), 328 Target: aws.String("HTTP:80/"), 329 Timeout: aws.Long(int64(30)), 330 Interval: aws.Long(int64(30)), 331 }, 332 Output: []map[string]interface{}{ 333 map[string]interface{}{ 334 "unhealthy_threshold": int64(10), 335 "healthy_threshold": int64(10), 336 "target": "HTTP:80/", 337 "timeout": int64(30), 338 "interval": int64(30), 339 }, 340 }, 341 }, 342 } 343 344 for _, tc := range cases { 345 output := flattenHealthCheck(tc.Input) 346 if !reflect.DeepEqual(output, tc.Output) { 347 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 348 } 349 } 350 } 351 352 func TestExpandStringList(t *testing.T) { 353 expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{}) 354 stringList := expandStringList(expanded) 355 expected := []*string{ 356 aws.String("us-east-1a"), 357 aws.String("us-east-1b"), 358 } 359 360 if !reflect.DeepEqual(stringList, expected) { 361 t.Fatalf( 362 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 363 stringList, 364 expected) 365 } 366 367 } 368 369 func TestexpandParameters(t *testing.T) { 370 expanded := []interface{}{ 371 map[string]interface{}{ 372 "name": "character_set_client", 373 "value": "utf8", 374 "apply_method": "immediate", 375 }, 376 } 377 parameters, err := expandParameters(expanded) 378 if err != nil { 379 t.Fatalf("bad: %#v", err) 380 } 381 382 expected := &rds.Parameter{ 383 ParameterName: aws.String("character_set_client"), 384 ParameterValue: aws.String("utf8"), 385 ApplyMethod: aws.String("immediate"), 386 } 387 388 if !reflect.DeepEqual(parameters[0], expected) { 389 t.Fatalf( 390 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 391 parameters[0], 392 expected) 393 } 394 } 395 396 func TestflattenParameters(t *testing.T) { 397 cases := []struct { 398 Input []*rds.Parameter 399 Output []map[string]interface{} 400 }{ 401 { 402 Input: []*rds.Parameter{ 403 &rds.Parameter{ 404 ParameterName: aws.String("character_set_client"), 405 ParameterValue: aws.String("utf8"), 406 }, 407 }, 408 Output: []map[string]interface{}{ 409 map[string]interface{}{ 410 "name": "character_set_client", 411 "value": "utf8", 412 }, 413 }, 414 }, 415 } 416 417 for _, tc := range cases { 418 output := flattenParameters(tc.Input) 419 if !reflect.DeepEqual(output, tc.Output) { 420 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 421 } 422 } 423 } 424 425 func TestexpandInstanceString(t *testing.T) { 426 427 expected := []*elb.Instance{ 428 &elb.Instance{InstanceID: aws.String("test-one")}, 429 &elb.Instance{InstanceID: aws.String("test-two")}, 430 } 431 432 ids := []interface{}{ 433 "test-one", 434 "test-two", 435 } 436 437 expanded := expandInstanceString(ids) 438 439 if !reflect.DeepEqual(expanded, expected) { 440 t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected) 441 } 442 } 443 444 func TestflattenNetworkInterfacesPrivateIPAddesses(t *testing.T) { 445 expanded := []*ec2.NetworkInterfacePrivateIPAddress{ 446 &ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.1")}, 447 &ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.2")}, 448 } 449 450 result := flattenNetworkInterfacesPrivateIPAddesses(expanded) 451 452 if result == nil { 453 t.Fatal("result was nil") 454 } 455 456 if len(result) != 2 { 457 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 458 } 459 460 if result[0] != "192.168.0.1" { 461 t.Fatalf("expected ip to be 192.168.0.1, but was %s", result[0]) 462 } 463 464 if result[1] != "192.168.0.2" { 465 t.Fatalf("expected ip to be 192.168.0.2, but was %s", result[1]) 466 } 467 } 468 469 func TestflattenGroupIdentifiers(t *testing.T) { 470 expanded := []*ec2.GroupIdentifier{ 471 &ec2.GroupIdentifier{GroupID: aws.String("sg-001")}, 472 &ec2.GroupIdentifier{GroupID: aws.String("sg-002")}, 473 } 474 475 result := flattenGroupIdentifiers(expanded) 476 477 if len(result) != 2 { 478 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 479 } 480 481 if result[0] != "sg-001" { 482 t.Fatalf("expected id to be sg-001, but was %s", result[0]) 483 } 484 485 if result[1] != "sg-002" { 486 t.Fatalf("expected id to be sg-002, but was %s", result[1]) 487 } 488 } 489 490 func TestexpandPrivateIPAddesses(t *testing.T) { 491 492 ip1 := "192.168.0.1" 493 ip2 := "192.168.0.2" 494 flattened := []interface{}{ 495 ip1, 496 ip2, 497 } 498 499 result := expandPrivateIPAddesses(flattened) 500 501 if len(result) != 2 { 502 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 503 } 504 505 if *result[0].PrivateIPAddress != "192.168.0.1" || !*result[0].Primary { 506 t.Fatalf("expected ip to be 192.168.0.1 and Primary, but got %v, %t", *result[0].PrivateIPAddress, *result[0].Primary) 507 } 508 509 if *result[1].PrivateIPAddress != "192.168.0.2" || *result[1].Primary { 510 t.Fatalf("expected ip to be 192.168.0.2 and not Primary, but got %v, %t", *result[1].PrivateIPAddress, *result[1].Primary) 511 } 512 } 513 514 func TestflattenAttachment(t *testing.T) { 515 expanded := &ec2.NetworkInterfaceAttachment{ 516 InstanceID: aws.String("i-00001"), 517 DeviceIndex: aws.Long(int64(1)), 518 AttachmentID: aws.String("at-002"), 519 } 520 521 result := flattenAttachment(expanded) 522 523 if result == nil { 524 t.Fatal("expected result to have value, but got nil") 525 } 526 527 if result["instance"] != "i-00001" { 528 t.Fatalf("expected instance to be i-00001, but got %s", result["instance"]) 529 } 530 531 if result["device_index"] != int64(1) { 532 t.Fatalf("expected device_index to be 1, but got %d", result["device_index"]) 533 } 534 535 if result["attachment_id"] != "at-002" { 536 t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"]) 537 } 538 } 539 540 func TestFlattenResourceRecords(t *testing.T) { 541 expanded := []*route53.ResourceRecord{ 542 &route53.ResourceRecord{ 543 Value: aws.String("127.0.0.1"), 544 }, 545 &route53.ResourceRecord{ 546 Value: aws.String("127.0.0.3"), 547 }, 548 } 549 550 result := flattenResourceRecords(expanded) 551 552 if result == nil { 553 t.Fatal("expected result to have value, but got nil") 554 } 555 556 if len(result) != 2 { 557 t.Fatal("expected result to have value, but got nil") 558 } 559 }