github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_security_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "reflect" 7 "testing" 8 9 "github.com/hashicorp/aws-sdk-go/aws" 10 "github.com/hashicorp/aws-sdk-go/gen/ec2" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSSecurityGroup_normal(t *testing.T) { 16 var group ec2.SecurityGroup 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSSecurityGroupDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAWSSecurityGroupConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), 27 testAccCheckAWSSecurityGroupAttributes(&group), 28 resource.TestCheckResourceAttr( 29 "aws_security_group.web", "name", "terraform_acceptance_test_example"), 30 resource.TestCheckResourceAttr( 31 "aws_security_group.web", "description", "Used in the terraform acceptance tests"), 32 resource.TestCheckResourceAttr( 33 "aws_security_group.web", "ingress.332851786.protocol", "tcp"), 34 resource.TestCheckResourceAttr( 35 "aws_security_group.web", "ingress.332851786.from_port", "80"), 36 resource.TestCheckResourceAttr( 37 "aws_security_group.web", "ingress.332851786.to_port", "8000"), 38 resource.TestCheckResourceAttr( 39 "aws_security_group.web", "ingress.332851786.cidr_blocks.#", "1"), 40 resource.TestCheckResourceAttr( 41 "aws_security_group.web", "ingress.332851786.cidr_blocks.0", "10.0.0.0/8"), 42 ), 43 }, 44 }, 45 }) 46 } 47 48 func TestAccAWSSecurityGroup_self(t *testing.T) { 49 var group ec2.SecurityGroup 50 51 checkSelf := func(s *terraform.State) (err error) { 52 defer func() { 53 if e := recover(); e != nil { 54 err = fmt.Errorf("bad: %#v", group) 55 } 56 }() 57 58 if *group.IPPermissions[0].UserIDGroupPairs[0].GroupID != *group.GroupID { 59 return fmt.Errorf("bad: %#v", group) 60 } 61 62 return nil 63 } 64 65 resource.Test(t, resource.TestCase{ 66 PreCheck: func() { testAccPreCheck(t) }, 67 Providers: testAccProviders, 68 CheckDestroy: testAccCheckAWSSecurityGroupDestroy, 69 Steps: []resource.TestStep{ 70 resource.TestStep{ 71 Config: testAccAWSSecurityGroupConfigSelf, 72 Check: resource.ComposeTestCheckFunc( 73 testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), 74 resource.TestCheckResourceAttr( 75 "aws_security_group.web", "name", "terraform_acceptance_test_example"), 76 resource.TestCheckResourceAttr( 77 "aws_security_group.web", "description", "Used in the terraform acceptance tests"), 78 resource.TestCheckResourceAttr( 79 "aws_security_group.web", "ingress.3128515109.protocol", "tcp"), 80 resource.TestCheckResourceAttr( 81 "aws_security_group.web", "ingress.3128515109.from_port", "80"), 82 resource.TestCheckResourceAttr( 83 "aws_security_group.web", "ingress.3128515109.to_port", "8000"), 84 resource.TestCheckResourceAttr( 85 "aws_security_group.web", "ingress.3128515109.self", "true"), 86 checkSelf, 87 ), 88 }, 89 }, 90 }) 91 } 92 93 func TestAccAWSSecurityGroup_vpc(t *testing.T) { 94 var group ec2.SecurityGroup 95 96 testCheck := func(*terraform.State) error { 97 if *group.VPCID == "" { 98 return fmt.Errorf("should have vpc ID") 99 } 100 101 return nil 102 } 103 104 resource.Test(t, resource.TestCase{ 105 PreCheck: func() { testAccPreCheck(t) }, 106 Providers: testAccProviders, 107 CheckDestroy: testAccCheckAWSSecurityGroupDestroy, 108 Steps: []resource.TestStep{ 109 resource.TestStep{ 110 Config: testAccAWSSecurityGroupConfigVpc, 111 Check: resource.ComposeTestCheckFunc( 112 testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), 113 testAccCheckAWSSecurityGroupAttributes(&group), 114 resource.TestCheckResourceAttr( 115 "aws_security_group.web", "name", "terraform_acceptance_test_example"), 116 resource.TestCheckResourceAttr( 117 "aws_security_group.web", "description", "Used in the terraform acceptance tests"), 118 resource.TestCheckResourceAttr( 119 "aws_security_group.web", "ingress.332851786.protocol", "tcp"), 120 resource.TestCheckResourceAttr( 121 "aws_security_group.web", "ingress.332851786.from_port", "80"), 122 resource.TestCheckResourceAttr( 123 "aws_security_group.web", "ingress.332851786.to_port", "8000"), 124 resource.TestCheckResourceAttr( 125 "aws_security_group.web", "ingress.332851786.cidr_blocks.#", "1"), 126 resource.TestCheckResourceAttr( 127 "aws_security_group.web", "ingress.332851786.cidr_blocks.0", "10.0.0.0/8"), 128 resource.TestCheckResourceAttr( 129 "aws_security_group.web", "egress.332851786.protocol", "tcp"), 130 resource.TestCheckResourceAttr( 131 "aws_security_group.web", "egress.332851786.from_port", "80"), 132 resource.TestCheckResourceAttr( 133 "aws_security_group.web", "egress.332851786.to_port", "8000"), 134 resource.TestCheckResourceAttr( 135 "aws_security_group.web", "egress.332851786.cidr_blocks.#", "1"), 136 resource.TestCheckResourceAttr( 137 "aws_security_group.web", "egress.332851786.cidr_blocks.0", "10.0.0.0/8"), 138 testCheck, 139 ), 140 }, 141 }, 142 }) 143 } 144 145 func TestAccAWSSecurityGroup_MultiIngress(t *testing.T) { 146 var group ec2.SecurityGroup 147 148 resource.Test(t, resource.TestCase{ 149 PreCheck: func() { testAccPreCheck(t) }, 150 Providers: testAccProviders, 151 CheckDestroy: testAccCheckAWSSecurityGroupDestroy, 152 Steps: []resource.TestStep{ 153 resource.TestStep{ 154 Config: testAccAWSSecurityGroupConfigMultiIngress, 155 Check: resource.ComposeTestCheckFunc( 156 testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), 157 ), 158 }, 159 }, 160 }) 161 } 162 163 func TestAccAWSSecurityGroup_Change(t *testing.T) { 164 var group ec2.SecurityGroup 165 166 resource.Test(t, resource.TestCase{ 167 PreCheck: func() { testAccPreCheck(t) }, 168 Providers: testAccProviders, 169 CheckDestroy: testAccCheckAWSSecurityGroupDestroy, 170 Steps: []resource.TestStep{ 171 resource.TestStep{ 172 Config: testAccAWSSecurityGroupConfig, 173 Check: resource.ComposeTestCheckFunc( 174 testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), 175 ), 176 }, 177 resource.TestStep{ 178 Config: testAccAWSSecurityGroupConfigChange, 179 Check: resource.ComposeTestCheckFunc( 180 testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), 181 testAccCheckAWSSecurityGroupAttributesChanged(&group), 182 ), 183 }, 184 }, 185 }) 186 } 187 188 func testAccCheckAWSSecurityGroupDestroy(s *terraform.State) error { 189 conn := testAccProvider.Meta().(*AWSClient).ec2conn 190 191 for _, rs := range s.RootModule().Resources { 192 if rs.Type != "aws_security_group" { 193 continue 194 } 195 196 // Retrieve our group 197 req := &ec2.DescribeSecurityGroupsRequest{ 198 GroupIDs: []string{rs.Primary.ID}, 199 } 200 resp, err := conn.DescribeSecurityGroups(req) 201 if err == nil { 202 if len(resp.SecurityGroups) > 0 && *resp.SecurityGroups[0].GroupID == rs.Primary.ID { 203 return fmt.Errorf("Security Group (%s) still exists.", rs.Primary.ID) 204 } 205 206 return nil 207 } 208 209 ec2err, ok := err.(aws.APIError) 210 if !ok { 211 return err 212 } 213 // Confirm error code is what we want 214 if ec2err.Code != "InvalidGroup.NotFound" { 215 return err 216 } 217 } 218 219 return nil 220 } 221 222 func testAccCheckAWSSecurityGroupExists(n string, group *ec2.SecurityGroup) resource.TestCheckFunc { 223 return func(s *terraform.State) error { 224 rs, ok := s.RootModule().Resources[n] 225 if !ok { 226 return fmt.Errorf("Not found: %s", n) 227 } 228 229 if rs.Primary.ID == "" { 230 return fmt.Errorf("No Security Group is set") 231 } 232 233 conn := testAccProvider.Meta().(*AWSClient).ec2conn 234 req := &ec2.DescribeSecurityGroupsRequest{ 235 GroupIDs: []string{rs.Primary.ID}, 236 } 237 resp, err := conn.DescribeSecurityGroups(req) 238 if err != nil { 239 return err 240 } 241 242 if len(resp.SecurityGroups) > 0 && *resp.SecurityGroups[0].GroupID == rs.Primary.ID { 243 244 log.Printf("\n==\n===\nfound group\n===\n==\n") 245 *group = resp.SecurityGroups[0] 246 247 return nil 248 } 249 250 return fmt.Errorf("Security Group not found") 251 } 252 } 253 254 func testAccCheckAWSSecurityGroupAttributes(group *ec2.SecurityGroup) resource.TestCheckFunc { 255 return func(s *terraform.State) error { 256 p := ec2.IPPermission{ 257 FromPort: aws.Integer(80), 258 ToPort: aws.Integer(8000), 259 IPProtocol: aws.String("tcp"), 260 IPRanges: []ec2.IPRange{ec2.IPRange{aws.String("10.0.0.0/8")}}, 261 } 262 263 if *group.GroupName != "terraform_acceptance_test_example" { 264 return fmt.Errorf("Bad name: %s", *group.GroupName) 265 } 266 267 if *group.Description != "Used in the terraform acceptance tests" { 268 return fmt.Errorf("Bad description: %s", *group.Description) 269 } 270 271 if len(group.IPPermissions) == 0 { 272 return fmt.Errorf("No IPPerms") 273 } 274 275 // Compare our ingress 276 if !reflect.DeepEqual(group.IPPermissions[0], p) { 277 return fmt.Errorf( 278 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 279 group.IPPermissions[0], 280 p) 281 } 282 283 return nil 284 } 285 } 286 287 func TestAccAWSSecurityGroup_tags(t *testing.T) { 288 var group ec2.SecurityGroup 289 290 resource.Test(t, resource.TestCase{ 291 PreCheck: func() { testAccPreCheck(t) }, 292 Providers: testAccProviders, 293 CheckDestroy: testAccCheckAWSSecurityGroupDestroy, 294 Steps: []resource.TestStep{ 295 resource.TestStep{ 296 Config: testAccAWSSecurityGroupConfigTags, 297 Check: resource.ComposeTestCheckFunc( 298 testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group), 299 testAccCheckTags(&group.Tags, "foo", "bar"), 300 ), 301 }, 302 303 resource.TestStep{ 304 Config: testAccAWSSecurityGroupConfigTagsUpdate, 305 Check: resource.ComposeTestCheckFunc( 306 testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group), 307 testAccCheckTags(&group.Tags, "foo", ""), 308 testAccCheckTags(&group.Tags, "bar", "baz"), 309 ), 310 }, 311 }, 312 }) 313 } 314 315 func testAccCheckAWSSecurityGroupAttributesChanged(group *ec2.SecurityGroup) resource.TestCheckFunc { 316 return func(s *terraform.State) error { 317 p := []ec2.IPPermission{ 318 ec2.IPPermission{ 319 FromPort: aws.Integer(80), 320 ToPort: aws.Integer(9000), 321 IPProtocol: aws.String("tcp"), 322 IPRanges: []ec2.IPRange{ec2.IPRange{aws.String("10.0.0.0/8")}}, 323 }, 324 ec2.IPPermission{ 325 FromPort: aws.Integer(80), 326 ToPort: aws.Integer(8000), 327 IPProtocol: aws.String("tcp"), 328 IPRanges: []ec2.IPRange{ec2.IPRange{aws.String("0.0.0.0/0")}, ec2.IPRange{aws.String("10.0.0.0/8")}}, 329 }, 330 } 331 332 if *group.GroupName != "terraform_acceptance_test_example" { 333 return fmt.Errorf("Bad name: %s", *group.GroupName) 334 } 335 336 if *group.Description != "Used in the terraform acceptance tests" { 337 return fmt.Errorf("Bad description: %s", *group.Description) 338 } 339 340 // Compare our ingress 341 if len(group.IPPermissions) != 2 { 342 return fmt.Errorf( 343 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 344 group.IPPermissions, 345 p) 346 } 347 348 if *group.IPPermissions[0].ToPort == 8000 { 349 group.IPPermissions[1], group.IPPermissions[0] = 350 group.IPPermissions[0], group.IPPermissions[1] 351 } 352 353 if !reflect.DeepEqual(group.IPPermissions, p) { 354 return fmt.Errorf( 355 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 356 group.IPPermissions, 357 p) 358 } 359 360 return nil 361 } 362 } 363 364 const testAccAWSSecurityGroupConfig = ` 365 resource "aws_security_group" "web" { 366 name = "terraform_acceptance_test_example" 367 description = "Used in the terraform acceptance tests" 368 369 ingress { 370 protocol = "tcp" 371 from_port = 80 372 to_port = 8000 373 cidr_blocks = ["10.0.0.0/8"] 374 } 375 } 376 ` 377 378 const testAccAWSSecurityGroupConfigChange = ` 379 resource "aws_security_group" "web" { 380 name = "terraform_acceptance_test_example" 381 description = "Used in the terraform acceptance tests" 382 383 ingress { 384 protocol = "tcp" 385 from_port = 80 386 to_port = 9000 387 cidr_blocks = ["10.0.0.0/8"] 388 } 389 390 ingress { 391 protocol = "tcp" 392 from_port = 80 393 to_port = 8000 394 cidr_blocks = ["0.0.0.0/0", "10.0.0.0/8"] 395 } 396 } 397 ` 398 399 const testAccAWSSecurityGroupConfigSelf = ` 400 resource "aws_security_group" "web" { 401 name = "terraform_acceptance_test_example" 402 description = "Used in the terraform acceptance tests" 403 404 ingress { 405 protocol = "tcp" 406 from_port = 80 407 to_port = 8000 408 self = true 409 } 410 } 411 ` 412 413 const testAccAWSSecurityGroupConfigVpc = ` 414 resource "aws_vpc" "foo" { 415 cidr_block = "10.1.0.0/16" 416 } 417 418 resource "aws_security_group" "web" { 419 name = "terraform_acceptance_test_example" 420 description = "Used in the terraform acceptance tests" 421 vpc_id = "${aws_vpc.foo.id}" 422 423 ingress { 424 protocol = "tcp" 425 from_port = 80 426 to_port = 8000 427 cidr_blocks = ["10.0.0.0/8"] 428 } 429 430 egress { 431 protocol = "tcp" 432 from_port = 80 433 to_port = 8000 434 cidr_blocks = ["10.0.0.0/8"] 435 } 436 } 437 ` 438 439 const testAccAWSSecurityGroupConfigMultiIngress = ` 440 resource "aws_security_group" "worker" { 441 name = "terraform_acceptance_test_example_1" 442 description = "Used in the terraform acceptance tests" 443 444 ingress { 445 protocol = "tcp" 446 from_port = 80 447 to_port = 8000 448 cidr_blocks = ["10.0.0.0/8"] 449 } 450 } 451 452 resource "aws_security_group" "web" { 453 name = "terraform_acceptance_test_example_2" 454 description = "Used in the terraform acceptance tests" 455 456 ingress { 457 protocol = "tcp" 458 from_port = 22 459 to_port = 22 460 cidr_blocks = ["10.0.0.0/8"] 461 } 462 463 ingress { 464 protocol = "tcp" 465 from_port = 800 466 to_port = 800 467 cidr_blocks = ["10.0.0.0/8"] 468 } 469 470 ingress { 471 protocol = "tcp" 472 from_port = 80 473 to_port = 8000 474 security_groups = ["${aws_security_group.worker.id}"] 475 } 476 } 477 ` 478 479 const testAccAWSSecurityGroupConfigTags = ` 480 resource "aws_security_group" "foo" { 481 name = "terraform_acceptance_test_example" 482 description = "Used in the terraform acceptance tests" 483 484 ingress { 485 protocol = "tcp" 486 from_port = 80 487 to_port = 8000 488 cidr_blocks = ["10.0.0.0/8"] 489 } 490 491 tags { 492 foo = "bar" 493 } 494 } 495 ` 496 497 const testAccAWSSecurityGroupConfigTagsUpdate = ` 498 resource "aws_security_group" "foo" { 499 name = "terraform_acceptance_test_example" 500 description = "Used in the terraform acceptance tests" 501 502 ingress { 503 protocol = "tcp" 504 from_port = 80 505 to_port = 8000 506 cidr_blocks = ["10.0.0.0/8"] 507 } 508 509 tags { 510 bar = "baz" 511 } 512 } 513 `