github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/builtin/providers/aws/structure_test.go (about) 1 package aws 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/hashicorp/aws-sdk-go/aws" 8 ec2 "github.com/hashicorp/aws-sdk-go/gen/ec2" 9 "github.com/hashicorp/aws-sdk-go/gen/elb" 10 "github.com/hashicorp/aws-sdk-go/gen/rds" 11 "github.com/hashicorp/aws-sdk-go/gen/route53" 12 "github.com/hashicorp/terraform/flatmap" 13 "github.com/hashicorp/terraform/helper/hashcode" 14 "github.com/hashicorp/terraform/helper/schema" 15 ) 16 17 // Returns test configuration 18 func testConf() map[string]string { 19 return map[string]string{ 20 "listener.#": "1", 21 "listener.0.lb_port": "80", 22 "listener.0.lb_protocol": "http", 23 "listener.0.instance_port": "8000", 24 "listener.0.instance_protocol": "http", 25 "availability_zones.#": "2", 26 "availability_zones.0": "us-east-1a", 27 "availability_zones.1": "us-east-1b", 28 "ingress.#": "1", 29 "ingress.0.protocol": "icmp", 30 "ingress.0.from_port": "1", 31 "ingress.0.to_port": "-1", 32 "ingress.0.cidr_blocks.#": "1", 33 "ingress.0.cidr_blocks.0": "0.0.0.0/0", 34 "ingress.0.security_groups.#": "2", 35 "ingress.0.security_groups.0": "sg-11111", 36 "ingress.0.security_groups.1": "foo/sg-22222", 37 } 38 } 39 40 func TestExpandIPPerms(t *testing.T) { 41 hash := func(v interface{}) int { 42 return hashcode.String(v.(string)) 43 } 44 45 expanded := []interface{}{ 46 map[string]interface{}{ 47 "protocol": "icmp", 48 "from_port": 1, 49 "to_port": -1, 50 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 51 "security_groups": schema.NewSet(hash, []interface{}{ 52 "sg-11111", 53 "foo/sg-22222", 54 }), 55 }, 56 map[string]interface{}{ 57 "protocol": "icmp", 58 "from_port": 1, 59 "to_port": -1, 60 "self": true, 61 }, 62 } 63 group := ec2.SecurityGroup{ 64 GroupID: aws.String("foo"), 65 VPCID: aws.String("bar"), 66 } 67 perms := expandIPPerms(group, expanded) 68 69 expected := []ec2.IPPermission{ 70 ec2.IPPermission{ 71 IPProtocol: aws.String("icmp"), 72 FromPort: aws.Integer(1), 73 ToPort: aws.Integer(-1), 74 IPRanges: []ec2.IPRange{ec2.IPRange{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.Integer(1), 88 ToPort: aws.Integer(-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_nonVPC(t *testing.T) { 124 hash := func(v interface{}) int { 125 return hashcode.String(v.(string)) 126 } 127 128 expanded := []interface{}{ 129 map[string]interface{}{ 130 "protocol": "icmp", 131 "from_port": 1, 132 "to_port": -1, 133 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 134 "security_groups": schema.NewSet(hash, []interface{}{ 135 "sg-11111", 136 "foo/sg-22222", 137 }), 138 }, 139 map[string]interface{}{ 140 "protocol": "icmp", 141 "from_port": 1, 142 "to_port": -1, 143 "self": true, 144 }, 145 } 146 group := ec2.SecurityGroup{ 147 GroupName: aws.String("foo"), 148 } 149 perms := expandIPPerms(group, expanded) 150 151 expected := []ec2.IPPermission{ 152 ec2.IPPermission{ 153 IPProtocol: aws.String("icmp"), 154 FromPort: aws.Integer(1), 155 ToPort: aws.Integer(-1), 156 IPRanges: []ec2.IPRange{ec2.IPRange{aws.String("0.0.0.0/0")}}, 157 UserIDGroupPairs: []ec2.UserIDGroupPair{ 158 ec2.UserIDGroupPair{ 159 GroupName: aws.String("sg-22222"), 160 }, 161 ec2.UserIDGroupPair{ 162 GroupName: aws.String("sg-22222"), 163 }, 164 }, 165 }, 166 ec2.IPPermission{ 167 IPProtocol: aws.String("icmp"), 168 FromPort: aws.Integer(1), 169 ToPort: aws.Integer(-1), 170 UserIDGroupPairs: []ec2.UserIDGroupPair{ 171 ec2.UserIDGroupPair{ 172 GroupName: aws.String("foo"), 173 }, 174 }, 175 }, 176 } 177 178 exp := expected[0] 179 perm := perms[0] 180 181 if *exp.FromPort != *perm.FromPort { 182 t.Fatalf( 183 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 184 *perm.FromPort, 185 *exp.FromPort) 186 } 187 188 if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP { 189 t.Fatalf( 190 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 191 *perm.IPRanges[0].CIDRIP, 192 *exp.IPRanges[0].CIDRIP) 193 } 194 } 195 196 func TestExpandListeners(t *testing.T) { 197 expanded := []interface{}{ 198 map[string]interface{}{ 199 "instance_port": 8000, 200 "lb_port": 80, 201 "instance_protocol": "http", 202 "lb_protocol": "http", 203 }, 204 } 205 listeners, err := expandListeners(expanded) 206 if err != nil { 207 t.Fatalf("bad: %#v", err) 208 } 209 210 expected := elb.Listener{ 211 InstancePort: aws.Integer(8000), 212 LoadBalancerPort: aws.Integer(80), 213 InstanceProtocol: aws.String("http"), 214 Protocol: aws.String("http"), 215 } 216 217 if !reflect.DeepEqual(listeners[0], expected) { 218 t.Fatalf( 219 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 220 listeners[0], 221 expected) 222 } 223 224 } 225 226 func TestFlattenHealthCheck(t *testing.T) { 227 cases := []struct { 228 Input elb.HealthCheck 229 Output []map[string]interface{} 230 }{ 231 { 232 Input: elb.HealthCheck{ 233 UnhealthyThreshold: aws.Integer(10), 234 HealthyThreshold: aws.Integer(10), 235 Target: aws.String("HTTP:80/"), 236 Timeout: aws.Integer(30), 237 Interval: aws.Integer(30), 238 }, 239 Output: []map[string]interface{}{ 240 map[string]interface{}{ 241 "unhealthy_threshold": 10, 242 "healthy_threshold": 10, 243 "target": "HTTP:80/", 244 "timeout": 30, 245 "interval": 30, 246 }, 247 }, 248 }, 249 } 250 251 for _, tc := range cases { 252 output := flattenHealthCheck(&tc.Input) 253 if !reflect.DeepEqual(output, tc.Output) { 254 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 255 } 256 } 257 } 258 259 func TestExpandStringList(t *testing.T) { 260 expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{}) 261 stringList := expandStringList(expanded) 262 expected := []string{ 263 "us-east-1a", 264 "us-east-1b", 265 } 266 267 if !reflect.DeepEqual(stringList, expected) { 268 t.Fatalf( 269 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 270 stringList, 271 expected) 272 } 273 274 } 275 276 func TestExpandParameters(t *testing.T) { 277 expanded := []interface{}{ 278 map[string]interface{}{ 279 "name": "character_set_client", 280 "value": "utf8", 281 "apply_method": "immediate", 282 }, 283 } 284 parameters, err := expandParameters(expanded) 285 if err != nil { 286 t.Fatalf("bad: %#v", err) 287 } 288 289 expected := rds.Parameter{ 290 ParameterName: aws.String("character_set_client"), 291 ParameterValue: aws.String("utf8"), 292 ApplyMethod: aws.String("immediate"), 293 } 294 295 if !reflect.DeepEqual(parameters[0], expected) { 296 t.Fatalf( 297 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 298 parameters[0], 299 expected) 300 } 301 } 302 303 func TestFlattenParameters(t *testing.T) { 304 cases := []struct { 305 Input []rds.Parameter 306 Output []map[string]interface{} 307 }{ 308 { 309 Input: []rds.Parameter{ 310 rds.Parameter{ 311 ParameterName: aws.String("character_set_client"), 312 ParameterValue: aws.String("utf8"), 313 }, 314 }, 315 Output: []map[string]interface{}{ 316 map[string]interface{}{ 317 "name": "character_set_client", 318 "value": "utf8", 319 }, 320 }, 321 }, 322 } 323 324 for _, tc := range cases { 325 output := flattenParameters(tc.Input) 326 if !reflect.DeepEqual(output, tc.Output) { 327 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 328 } 329 } 330 } 331 332 func TestExpandInstanceString(t *testing.T) { 333 334 expected := []elb.Instance{ 335 elb.Instance{aws.String("test-one")}, 336 elb.Instance{aws.String("test-two")}, 337 } 338 339 ids := []interface{}{ 340 "test-one", 341 "test-two", 342 } 343 344 expanded := expandInstanceString(ids) 345 346 if !reflect.DeepEqual(expanded, expected) { 347 t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected) 348 } 349 } 350 351 func TestFlattenNetworkInterfacesPrivateIPAddesses(t *testing.T) { 352 expanded := []ec2.NetworkInterfacePrivateIPAddress{ 353 ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.1")}, 354 ec2.NetworkInterfacePrivateIPAddress{PrivateIPAddress: aws.String("192.168.0.2")}, 355 } 356 357 result := flattenNetworkInterfacesPrivateIPAddesses(expanded) 358 359 if result == nil { 360 t.Fatal("result was nil") 361 } 362 363 if len(result) != 2 { 364 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 365 } 366 367 if result[0] != "192.168.0.1" { 368 t.Fatalf("expected ip to be 192.168.0.1, but was %s", result[0]) 369 } 370 371 if result[1] != "192.168.0.2" { 372 t.Fatalf("expected ip to be 192.168.0.2, but was %s", result[1]) 373 } 374 } 375 376 func TestFlattenGroupIdentifiers(t *testing.T) { 377 expanded := []ec2.GroupIdentifier{ 378 ec2.GroupIdentifier{GroupID: aws.String("sg-001")}, 379 ec2.GroupIdentifier{GroupID: aws.String("sg-002")}, 380 } 381 382 result := flattenGroupIdentifiers(expanded) 383 384 if len(result) != 2 { 385 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 386 } 387 388 if result[0] != "sg-001" { 389 t.Fatalf("expected id to be sg-001, but was %s", result[0]) 390 } 391 392 if result[1] != "sg-002" { 393 t.Fatalf("expected id to be sg-002, but was %s", result[1]) 394 } 395 } 396 397 func TestExpandPrivateIPAddesses(t *testing.T) { 398 399 ip1 := "192.168.0.1" 400 ip2 := "192.168.0.2" 401 flattened := []interface{}{ 402 ip1, 403 ip2, 404 } 405 406 result := expandPrivateIPAddesses(flattened) 407 408 if len(result) != 2 { 409 t.Fatalf("expected result had %d elements, but got %d", 2, len(result)) 410 } 411 412 if *result[0].PrivateIPAddress != "192.168.0.1" || !*result[0].Primary { 413 t.Fatalf("expected ip to be 192.168.0.1 and Primary, but got %v, %t", *result[0].PrivateIPAddress, *result[0].Primary) 414 } 415 416 if *result[1].PrivateIPAddress != "192.168.0.2" || *result[1].Primary { 417 t.Fatalf("expected ip to be 192.168.0.2 and not Primary, but got %v, %t", *result[1].PrivateIPAddress, *result[1].Primary) 418 } 419 } 420 421 func TestFlattenAttachment(t *testing.T) { 422 expanded := &ec2.NetworkInterfaceAttachment{ 423 InstanceID: aws.String("i-00001"), 424 DeviceIndex: aws.Integer(1), 425 AttachmentID: aws.String("at-002"), 426 } 427 428 result := flattenAttachment(expanded) 429 430 if result == nil { 431 t.Fatal("expected result to have value, but got nil") 432 } 433 434 if result["instance"] != "i-00001" { 435 t.Fatalf("expected instance to be i-00001, but got %s", result["instance"]) 436 } 437 438 if result["device_index"] != 1 { 439 t.Fatalf("expected device_index to be 1, but got %d", result["device_index"]) 440 } 441 442 if result["attachment_id"] != "at-002" { 443 t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"]) 444 } 445 } 446 447 func TestFlattenResourceRecords(t *testing.T) { 448 expanded := []route53.ResourceRecord{ 449 route53.ResourceRecord{ 450 Value: aws.String("127.0.0.1"), 451 }, 452 route53.ResourceRecord{ 453 Value: aws.String("127.0.0.3"), 454 }, 455 } 456 457 result := flattenResourceRecords(expanded) 458 459 if result == nil { 460 t.Fatal("expected result to have value, but got nil") 461 } 462 463 if len(result) != 2 { 464 t.Fatal("expected result to have value, but got nil") 465 } 466 }