From eafbc8d8c607713101444d2cbdfb3a7ee1c2b9d9 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Wed, 16 Jul 2014 11:14:23 -0400 Subject: [PATCH] providers/aws: improve security group acc tests --- builtin/providers/aws/resource_aws_elb.go | 2 +- .../aws/resource_aws_security_group_test.go | 59 ++++++++++++++++++- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 823de083a..5fc7d9bbe 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -199,7 +199,7 @@ func resource_aws_elb_validation() *config.Validator { "listener.*.lb_protocol", }, Optional: []string{ - "instances", + "instances.*", }, } } diff --git a/builtin/providers/aws/resource_aws_security_group_test.go b/builtin/providers/aws/resource_aws_security_group_test.go index 8aac3ed09..0da802155 100644 --- a/builtin/providers/aws/resource_aws_security_group_test.go +++ b/builtin/providers/aws/resource_aws_security_group_test.go @@ -3,13 +3,16 @@ package aws import ( "fmt" "testing" + "reflect" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/goamz/ec2" ) -func TestAccAwsSecurityGroup(t *testing.T) { +func TestAccAWSSecurityGroup(t *testing.T) { + var group ec2.SecurityGroupInfo + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -17,7 +20,24 @@ func TestAccAwsSecurityGroup(t *testing.T) { Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSSecurityGroupConfig, - Check: testAccCheckAWSSecurityGroupExists("aws_security_group.web"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), + testAccCheckAWSSecurityGroupAttributes(&group), + resource.TestCheckResourceAttr( + "aws_security_group.web", "name", "terraform_acceptance_test_example"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "description", "Used in the terraform acceptance tests"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.protocol", "tcp"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.from_port", "80"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.to_port", "8000"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.cidr_blocks.#", "1"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.cidr_blocks.0", "10.0.0.0/0"), + ), }, }, }) @@ -60,7 +80,8 @@ func testAccCheckAWSSecurityGroupDestroy(s *terraform.State) error { return nil } -func testAccCheckAWSSecurityGroupExists(n string) resource.TestCheckFunc { + +func testAccCheckAWSSecurityGroupExists(n string, group *ec2.SecurityGroupInfo) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.Resources[n] if !ok { @@ -83,6 +104,9 @@ func testAccCheckAWSSecurityGroupExists(n string) resource.TestCheckFunc { } if len(resp.Groups) > 0 && resp.Groups[0].Id == rs.ID { + + *group = resp.Groups[0] + return nil } else { return fmt.Errorf("Security Group not found") @@ -92,6 +116,35 @@ func testAccCheckAWSSecurityGroupExists(n string) resource.TestCheckFunc { } } +func testAccCheckAWSSecurityGroupAttributes(group *ec2.SecurityGroupInfo) resource.TestCheckFunc { + return func(s *terraform.State) error { + p := ec2.IPPerm{ + FromPort: 80, + ToPort: 8000, + Protocol: "tcp", + SourceIPs: []string{"10.0.0.0/0"}, + } + + if group.Name != "terraform_acceptance_test_example" { + return fmt.Errorf("Bad name: %s", group.Name) + } + + if group.Description != "Used in the terraform acceptance tests" { + return fmt.Errorf("Bad description: %s", group.Description) + } + + // Compare our ingress + if !reflect.DeepEqual(group.IPPerms[0], p) { + return fmt.Errorf( + "Got:\n\n%#v\n\nExpected:\n\n%#v\n", + group.IPPerms[0], + p) + } + + return nil + } +} + const testAccAWSSecurityGroupConfig = ` resource "aws_security_group" "web" { name = "terraform_acceptance_test_example"