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