github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/aws/resource_aws_redshift_cluster_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/redshift" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSRedshiftCluster_basic(t *testing.T) { 17 var v redshift.Cluster 18 19 ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() 20 config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri) 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAWSRedshiftClusterDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: config, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error { 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "aws_redshift_cluster" { 40 continue 41 } 42 43 // Try to find the Group 44 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 45 var err error 46 resp, err := conn.DescribeClusters( 47 &redshift.DescribeClustersInput{ 48 ClusterIdentifier: aws.String(rs.Primary.ID), 49 }) 50 51 if err == nil { 52 if len(resp.Clusters) != 0 && 53 *resp.Clusters[0].ClusterIdentifier == rs.Primary.ID { 54 return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID) 55 } 56 } 57 58 // Return nil if the cluster is already destroyed 59 if awsErr, ok := err.(awserr.Error); ok { 60 if awsErr.Code() == "ClusterNotFound" { 61 return nil 62 } 63 } 64 65 return err 66 } 67 68 return nil 69 } 70 71 func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resource.TestCheckFunc { 72 return func(s *terraform.State) error { 73 rs, ok := s.RootModule().Resources[n] 74 if !ok { 75 return fmt.Errorf("Not found: %s", n) 76 } 77 78 if rs.Primary.ID == "" { 79 return fmt.Errorf("No Redshift Cluster Instance ID is set") 80 } 81 82 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 83 resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{ 84 ClusterIdentifier: aws.String(rs.Primary.ID), 85 }) 86 87 if err != nil { 88 return err 89 } 90 91 for _, c := range resp.Clusters { 92 if *c.ClusterIdentifier == rs.Primary.ID { 93 *v = *c 94 return nil 95 } 96 } 97 98 return fmt.Errorf("Redshift Cluster (%s) not found", rs.Primary.ID) 99 } 100 } 101 102 func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) { 103 cases := []struct { 104 Value string 105 ErrCount int 106 }{ 107 { 108 Value: "tEsting", 109 ErrCount: 1, 110 }, 111 { 112 Value: "1testing", 113 ErrCount: 1, 114 }, 115 { 116 Value: "testing--123", 117 ErrCount: 1, 118 }, 119 { 120 Value: "testing!", 121 ErrCount: 1, 122 }, 123 { 124 Value: "testing-", 125 ErrCount: 1, 126 }, 127 } 128 129 for _, tc := range cases { 130 _, errors := validateRedshiftClusterIdentifier(tc.Value, "aws_redshift_cluster_identifier") 131 132 if len(errors) != tc.ErrCount { 133 t.Fatalf("Expected the Redshift Cluster cluster_identifier to trigger a validation error") 134 } 135 } 136 } 137 138 func TestResourceAWSRedshiftClusterDbNameValidation(t *testing.T) { 139 cases := []struct { 140 Value string 141 ErrCount int 142 }{ 143 { 144 Value: "tEsting", 145 ErrCount: 1, 146 }, 147 { 148 Value: "testing1", 149 ErrCount: 1, 150 }, 151 { 152 Value: "testing-", 153 ErrCount: 1, 154 }, 155 { 156 Value: "", 157 ErrCount: 2, 158 }, 159 { 160 Value: randomString(65), 161 ErrCount: 1, 162 }, 163 } 164 165 for _, tc := range cases { 166 _, errors := validateRedshiftClusterDbName(tc.Value, "aws_redshift_cluster_database_name") 167 168 if len(errors) != tc.ErrCount { 169 t.Fatalf("Expected the Redshift Cluster database_name to trigger a validation error") 170 } 171 } 172 } 173 174 func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) { 175 cases := []struct { 176 Value string 177 ErrCount int 178 }{ 179 { 180 Value: "testing--123", 181 ErrCount: 1, 182 }, 183 { 184 Value: "testing-", 185 ErrCount: 1, 186 }, 187 { 188 Value: "Testingq123!", 189 ErrCount: 1, 190 }, 191 { 192 Value: randomString(256), 193 ErrCount: 1, 194 }, 195 } 196 197 for _, tc := range cases { 198 _, errors := validateRedshiftClusterFinalSnapshotIdentifier(tc.Value, "aws_redshift_cluster_final_snapshot_identifier") 199 200 if len(errors) != tc.ErrCount { 201 t.Fatalf("Expected the Redshift Cluster final_snapshot_identifier to trigger a validation error") 202 } 203 } 204 } 205 206 func TestResourceAWSRedshiftClusterMasterUsernameValidation(t *testing.T) { 207 cases := []struct { 208 Value string 209 ErrCount int 210 }{ 211 { 212 Value: "1Testing", 213 ErrCount: 1, 214 }, 215 { 216 Value: "Testing!!", 217 ErrCount: 1, 218 }, 219 { 220 Value: randomString(129), 221 ErrCount: 1, 222 }, 223 } 224 225 for _, tc := range cases { 226 _, errors := validateRedshiftClusterMasterUsername(tc.Value, "aws_redshift_cluster_master_username") 227 228 if len(errors) != tc.ErrCount { 229 t.Fatalf("Expected the Redshift Cluster master_username to trigger a validation error") 230 } 231 } 232 } 233 234 var testAccAWSRedshiftClusterConfig_basic = ` 235 provider "aws" { 236 region = "us-west-2" 237 } 238 239 resource "aws_redshift_cluster" "default" { 240 cluster_identifier = "tf-redshift-cluster-%d" 241 availability_zone = "us-west-2a" 242 database_name = "mydb" 243 master_username = "foo" 244 master_password = "Mustbe8characters" 245 node_type = "dc1.large" 246 cluster_type = "single-node" 247 automated_snapshot_retention_period = 7 248 allow_version_upgrade = false 249 }`