github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/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 := expandIPPerms(group, expanded) 65 66 expected := []ec2.IPPermission{ 67 ec2.IPPermission{ 68 IPProtocol: aws.String("icmp"), 69 FromPort: aws.Long(int64(1)), 70 ToPort: aws.Long(int64(-1)), 71 IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}}, 72 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 73 &ec2.UserIDGroupPair{ 74 UserID: aws.String("foo"), 75 GroupID: aws.String("sg-22222"), 76 }, 77 &ec2.UserIDGroupPair{ 78 GroupID: aws.String("sg-22222"), 79 }, 80 }, 81 }, 82 ec2.IPPermission{ 83 IPProtocol: aws.String("icmp"), 84 FromPort: aws.Long(int64(1)), 85 ToPort: aws.Long(int64(-1)), 86 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 87 &ec2.UserIDGroupPair{ 88 UserID: aws.String("foo"), 89 }, 90 }, 91 }, 92 } 93 94 exp := expected[0] 95 perm := perms[0] 96 97 if *exp.FromPort != *perm.FromPort { 98 t.Fatalf( 99 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 100 *perm.FromPort, 101 *exp.FromPort) 102 } 103 104 if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP { 105 t.Fatalf( 106 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 107 *perm.IPRanges[0].CIDRIP, 108 *exp.IPRanges[0].CIDRIP) 109 } 110 111 if *exp.UserIDGroupPairs[0].UserID != *perm.UserIDGroupPairs[0].UserID { 112 t.Fatalf( 113 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 114 *perm.UserIDGroupPairs[0].UserID, 115 *exp.UserIDGroupPairs[0].UserID) 116 } 117 118 } 119 120 func TestExpandIPPerms_nonVPC(t *testing.T) { 121 hash := schema.HashString 122 123 expanded := []interface{}{ 124 map[string]interface{}{ 125 "protocol": "icmp", 126 "from_port": 1, 127 "to_port": -1, 128 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 129 "security_groups": schema.NewSet(hash, []interface{}{ 130 "sg-11111", 131 "foo/sg-22222", 132 }), 133 }, 134 map[string]interface{}{ 135 "protocol": "icmp", 136 "from_port": 1, 137 "to_port": -1, 138 "self": true, 139 }, 140 } 141 group := &ec2.SecurityGroup{ 142 GroupName: aws.String("foo"), 143 } 144 perms := expandIPPerms(group, expanded) 145 146 expected := []ec2.IPPermission{ 147 ec2.IPPermission{ 148 IPProtocol: aws.String("icmp"), 149 FromPort: aws.Long(int64(1)), 150 ToPort: aws.Long(int64(-1)), 151 IPRanges: []*ec2.IPRange{&ec2.IPRange{CIDRIP: aws.String("0.0.0.0/0")}}, 152 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 153 &ec2.UserIDGroupPair{ 154 GroupName: aws.String("sg-22222"), 155 }, 156 &ec2.UserIDGroupPair{ 157 GroupName: aws.String("sg-22222"), 158 }, 159 }, 160 }, 161 ec2.IPPermission{ 162 IPProtocol: aws.String("icmp"), 163 FromPort: aws.Long(int64(1)), 164 ToPort: aws.Long(int64(-1)), 165 UserIDGroupPairs: []*ec2.UserIDGroupPair{ 166 &ec2.UserIDGroupPair{ 167 GroupName: aws.String("foo"), 168 }, 169 }, 170 }, 171 } 172 173 exp := expected[0] 174 perm := perms[0] 175 176 if *exp.FromPort != *perm.FromPort { 177 t.Fatalf( 178 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 179 *perm.FromPort, 180 *exp.FromPort) 181 } 182 183 if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP { 184 t.Fatalf( 185 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 186 *perm.IPRanges[0].CIDRIP, 187 *exp.IPRanges[0].CIDRIP) 188 } 189 } 190 191 func TestexpandListeners(t *testing.T) { 192 expanded := []interface{}{ 193 map[string]interface{}{ 194 "instance_port": 8000, 195 "lb_port": 80, 196 "instance_protocol": "http", 197 "lb_protocol": "http", 198 }, 199 } 200 listeners, err := expandListeners(expanded) 201 if err != nil { 202 t.Fatalf("bad: %#v", err) 203 } 204 205 expected := &elb.Listener{ 206 InstancePort: aws.Long(int64(8000)), 207 LoadBalancerPort: aws.Long(int64(80)), 208 InstanceProtocol: aws.String("http"), 209 Protocol: aws.String("http"), 210 } 211 212 if !reflect.DeepEqual(listeners[0], expected) { 213 t.Fatalf( 214 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 215 listeners[0], 216 expected) 217 } 218 219 } 220 221 func TestflattenHealthCheck(t *testing.T) { 222 cases := []struct { 223 Input *elb.HealthCheck 224 Output []map[string]interface{} 225 }{ 226 { 227 Input: &elb.HealthCheck{ 228 UnhealthyThreshold: aws.Long(int64(10)), 229 HealthyThreshold: aws.Long(int64(10)), 230 Target: aws.String("HTTP:80/"), 231 Timeout: aws.Long(int64(30)), 232 Interval: aws.Long(int64(30)), 233 }, 234 Output: []map[string]interface{}{ 235 map[string]interface{}{ 236 "unhealthy_threshold": int64(10), 237 "healthy_threshold": int64(10), 238 "target": "HTTP:80/", 239 "timeout": int64(30), 240 "interval": int64(30), 241 }, 242 }, 243 }, 244 } 245 246 for _, tc := range cases { 247 output := flattenHealthCheck(tc.Input) 248 if !reflect.DeepEqual(output, tc.Output) { 249 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 250 } 251 } 252 } 253 254 func TestExpandStringList(t *testing.T) { 255 expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{}) 256 stringList := expandStringList(expanded) 257 expected := []*string{ 258 aws.String("us-east-1a"), 259 aws.String("us-east-1b"), 260 } 261 262 if !reflect.DeepEqual(stringList, expected) { 263 t.Fatalf( 264 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 265 stringList, 266 expected) 267 } 268 269 } 270 271 func TestexpandParameters(t *testing.T) { 272 expanded := []interface{}{ 273 map[string]interface{}{ 274 "name": "character_set_client", 275 "value": "utf8", 276 "apply_method": "immediate", 277 }, 278 } 279 parameters, err := expandParameters(expanded) 280 if err != nil { 281 t.Fatalf("bad: %#v", err) 282 } 283 284 expected := &rds.Parameter{ 285 ParameterName: aws.String("character_set_client"), 286 ParameterValue: aws.String("utf8"), 287 ApplyMethod: aws.String("immediate"), 288 } 289 290 if !reflect.DeepEqual(parameters[0], expected) { 291 t.Fatalf( 292 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 293 parameters[0], 294 expected) 295 } 296 } 297 298 func TestflattenParameters(t *testing.T) { 299 cases := []struct { 300 Input []*rds.Parameter 301 Output []map[string]interface{} 302 }{ 303 { 304 Input: []*rds.Parameter{ 305 &rds.Parameter{ 306 ParameterName: aws.String("character_set_client"), 307 ParameterValue: aws.String("utf8"), 308 }, 309 }, 310 Output: []map[string]interface{}{ 311 map[string]interface{}{ 312 "name": "character_set_client", 313 "value": "utf8", 314 }, 315 }, 316 }, 317 } 318 319 for _, tc := range cases { 320 output := flattenParameters(tc.Input) 321 if !reflect.DeepEqual(output, tc.Output) { 322 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 323 } 324 } 325 } 326 327 func TestexpandInstanceString(t *testing.T) { 328 329 expected := []*elb.Instance{ 330 &elb.Instance{InstanceID: aws.String("test-one")}, 331 &elb.Instance{InstanceID: aws.String("test-two")}, 332 } 333 334 ids := []interface{}{ 335 "test-one", 336 "test-two", 337 } 338 339 expanded := expandInstanceString(ids) 340 341 if !reflect.DeepEqual(expanded, expected) { 342 t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected) 343 } 344 } 345 346 func TestflattenNetworkInterfacesPrivateIPAddesses(t *testing.T) { 347 expanded := []*ec2.NetworkInterfacePrivateIPAddress{ 348 &ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.1")}, 349 &ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.2")}, 350 } 351 352 result := flattenNetworkInterfacesPrivateIPAddesses(expanded) 353 354 if result == nil { 355 t.Fatal("result was nil") 356 } 357 358 if len(result) != 2 { 359 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 360 } 361 362 if result[0] != "192.168.0.1" { 363 t.Fatalf("expected ip to be 192.168.0.1, but was %s", result[0]) 364 } 365 366 if result[1] != "192.168.0.2" { 367 t.Fatalf("expected ip to be 192.168.0.2, but was %s", result[1]) 368 } 369 } 370 371 func TestflattenGroupIdentifiers(t *testing.T) { 372 expanded := []*ec2.GroupIdentifier{ 373 &ec2.GroupIdentifier{GroupID: aws.String("sg-001")}, 374 &ec2.GroupIdentifier{GroupID: aws.String("sg-002")}, 375 } 376 377 result := flattenGroupIdentifiers(expanded) 378 379 if len(result) != 2 { 380 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 381 } 382 383 if result[0] != "sg-001" { 384 t.Fatalf("expected id to be sg-001, but was %s", result[0]) 385 } 386 387 if result[1] != "sg-002" { 388 t.Fatalf("expected id to be sg-002, but was %s", result[1]) 389 } 390 } 391 392 func TestexpandPrivateIPAddesses(t *testing.T) { 393 394 ip1 := "192.168.0.1" 395 ip2 := "192.168.0.2" 396 flattened := []interface{}{ 397 ip1, 398 ip2, 399 } 400 401 result := expandPrivateIPAddesses(flattened) 402 403 if len(result) != 2 { 404 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 405 } 406 407 if *result[0].PrivateIPAddress != "192.168.0.1" || !*result[0].Primary { 408 t.Fatalf("expected ip to be 192.168.0.1 and Primary, but got %v, %t", *result[0].PrivateIPAddress, *result[0].Primary) 409 } 410 411 if *result[1].PrivateIPAddress != "192.168.0.2" || *result[1].Primary { 412 t.Fatalf("expected ip to be 192.168.0.2 and not Primary, but got %v, %t", *result[1].PrivateIPAddress, *result[1].Primary) 413 } 414 } 415 416 func TestflattenAttachment(t *testing.T) { 417 expanded := &ec2.NetworkInterfaceAttachment{ 418 InstanceID: aws.String("i-00001"), 419 DeviceIndex: aws.Long(int64(1)), 420 AttachmentID: aws.String("at-002"), 421 } 422 423 result := flattenAttachment(expanded) 424 425 if result == nil { 426 t.Fatal("expected result to have value, but got nil") 427 } 428 429 if result["instance"] != "i-00001" { 430 t.Fatalf("expected instance to be i-00001, but got %s", result["instance"]) 431 } 432 433 if result["device_index"] != int64(1) { 434 t.Fatalf("expected device_index to be 1, but got %d", result["device_index"]) 435 } 436 437 if result["attachment_id"] != "at-002" { 438 t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"]) 439 } 440 } 441 442 func TestFlattenResourceRecords(t *testing.T) { 443 expanded := []*route53.ResourceRecord{ 444 &route53.ResourceRecord{ 445 Value: aws.String("127.0.0.1"), 446 }, 447 &route53.ResourceRecord{ 448 Value: aws.String("127.0.0.3"), 449 }, 450 } 451 452 result := flattenResourceRecords(expanded) 453 454 if result == nil { 455 t.Fatal("expected result to have value, but got nil") 456 } 457 458 if len(result) != 2 { 459 t.Fatal("expected result to have value, but got nil") 460 } 461 }