github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_redshift_security_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/redshift" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSRedshiftSecurityGroup_ingressCidr(t *testing.T) { 15 var v redshift.ClusterSecurityGroup 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSRedshiftSecurityGroupDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSRedshiftSecurityGroupConfig_ingressCidr, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v), 26 resource.TestCheckResourceAttr( 27 "aws_redshift_security_group.bar", "name", "redshift-sg-terraform"), 28 resource.TestCheckResourceAttr( 29 "aws_redshift_security_group.bar", "description", "this is a description"), 30 resource.TestCheckResourceAttr( 31 "aws_redshift_security_group.bar", "ingress.2735652665.cidr", "10.0.0.1/24"), 32 resource.TestCheckResourceAttr( 33 "aws_redshift_security_group.bar", "ingress.#", "1"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func TestAccAWSRedshiftSecurityGroup_ingressSecurityGroup(t *testing.T) { 41 var v redshift.ClusterSecurityGroup 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckAWSRedshiftSecurityGroupDestroy, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: testAccAWSRedshiftSecurityGroupConfig_ingressSgId, 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckAWSRedshiftSecurityGroupExists("aws_redshift_security_group.bar", &v), 52 resource.TestCheckResourceAttr( 53 "aws_redshift_security_group.bar", "name", "redshift-sg-terraform"), 54 resource.TestCheckResourceAttr( 55 "aws_redshift_security_group.bar", "description", "this is a description"), 56 resource.TestCheckResourceAttr( 57 "aws_redshift_security_group.bar", "ingress.#", "1"), 58 resource.TestCheckResourceAttr( 59 "aws_redshift_security_group.bar", "ingress.220863.security_group_name", "terraform_redshift_acceptance_test"), 60 ), 61 }, 62 }, 63 }) 64 } 65 66 func testAccCheckAWSRedshiftSecurityGroupExists(n string, v *redshift.ClusterSecurityGroup) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 rs, ok := s.RootModule().Resources[n] 69 if !ok { 70 return fmt.Errorf("Not found: %s", n) 71 } 72 73 if rs.Primary.ID == "" { 74 return fmt.Errorf("No Redshift Security Group ID is set") 75 } 76 77 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 78 79 opts := redshift.DescribeClusterSecurityGroupsInput{ 80 ClusterSecurityGroupName: aws.String(rs.Primary.ID), 81 } 82 83 resp, err := conn.DescribeClusterSecurityGroups(&opts) 84 85 if err != nil { 86 return err 87 } 88 89 if len(resp.ClusterSecurityGroups) != 1 || 90 *resp.ClusterSecurityGroups[0].ClusterSecurityGroupName != rs.Primary.ID { 91 return fmt.Errorf("Redshift Security Group not found") 92 } 93 94 *v = *resp.ClusterSecurityGroups[0] 95 96 return nil 97 } 98 } 99 100 func testAccCheckAWSRedshiftSecurityGroupDestroy(s *terraform.State) error { 101 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 102 103 for _, rs := range s.RootModule().Resources { 104 if rs.Type != "aws_redshift_security_group" { 105 continue 106 } 107 108 // Try to find the Group 109 resp, err := conn.DescribeClusterSecurityGroups( 110 &redshift.DescribeClusterSecurityGroupsInput{ 111 ClusterSecurityGroupName: aws.String(rs.Primary.ID), 112 }) 113 114 if err == nil { 115 if len(resp.ClusterSecurityGroups) != 0 && 116 *resp.ClusterSecurityGroups[0].ClusterSecurityGroupName == rs.Primary.ID { 117 return fmt.Errorf("Redshift Security Group still exists") 118 } 119 } 120 121 // Verify the error 122 newerr, ok := err.(awserr.Error) 123 if !ok { 124 return err 125 } 126 if newerr.Code() != "ClusterSecurityGroupNotFound" { 127 return err 128 } 129 } 130 131 return nil 132 } 133 134 func TestResourceAWSRedshiftSecurityGroupNameValidation(t *testing.T) { 135 cases := []struct { 136 Value string 137 ErrCount int 138 }{ 139 { 140 Value: "default", 141 ErrCount: 1, 142 }, 143 { 144 Value: "testing123%%", 145 ErrCount: 1, 146 }, 147 { 148 Value: "TestingSG", 149 ErrCount: 1, 150 }, 151 { 152 Value: randomString(256), 153 ErrCount: 1, 154 }, 155 } 156 157 for _, tc := range cases { 158 _, errors := validateRedshiftSecurityGroupName(tc.Value, "aws_redshift_security_group_name") 159 160 if len(errors) != tc.ErrCount { 161 t.Fatalf("Expected the Redshift Security Group Name to trigger a validation error") 162 } 163 } 164 } 165 166 const testAccAWSRedshiftSecurityGroupConfig_ingressCidr = ` 167 provider "aws" { 168 region = "us-east-1" 169 } 170 171 resource "aws_redshift_security_group" "bar" { 172 name = "redshift-sg-terraform" 173 description = "this is a description" 174 175 ingress { 176 cidr = "10.0.0.1/24" 177 } 178 }` 179 180 const testAccAWSRedshiftSecurityGroupConfig_ingressSgId = ` 181 provider "aws" { 182 region = "us-east-1" 183 } 184 185 resource "aws_security_group" "redshift" { 186 name = "terraform_redshift_acceptance_test" 187 description = "Used in the redshift acceptance tests" 188 189 ingress { 190 protocol = "tcp" 191 from_port = 22 192 to_port = 22 193 cidr_blocks = ["10.0.0.0/8"] 194 } 195 } 196 197 resource "aws_redshift_security_group" "bar" { 198 name = "redshift-sg-terraform" 199 description = "this is a description" 200 201 ingress { 202 security_group_name = "${aws_security_group.redshift.name}" 203 security_group_owner_id = "${aws_security_group.redshift.owner_id}" 204 } 205 }`