github.com/anfernee/terraform@v0.6.16-0.20160430000239-06e5085a92f2/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 resource.TestCheckResourceAttr( 32 "aws_redshift_cluster.default", "cluster_type", "single-node"), 33 resource.TestCheckResourceAttr( 34 "aws_redshift_cluster.default", "publicly_accessible", "true"), 35 ), 36 }, 37 }, 38 }) 39 } 40 41 func TestAccAWSRedshiftCluster_publiclyAccessible(t *testing.T) { 42 var v redshift.Cluster 43 44 ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() 45 preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_notPubliclyAccessible, ri) 46 postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updatePubliclyAccessible, ri) 47 48 resource.Test(t, resource.TestCase{ 49 PreCheck: func() { testAccPreCheck(t) }, 50 Providers: testAccProviders, 51 CheckDestroy: testAccCheckAWSRedshiftClusterDestroy, 52 Steps: []resource.TestStep{ 53 resource.TestStep{ 54 Config: preConfig, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v), 57 resource.TestCheckResourceAttr( 58 "aws_redshift_cluster.default", "publicly_accessible", "false"), 59 ), 60 }, 61 62 resource.TestStep{ 63 Config: postConfig, 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v), 66 resource.TestCheckResourceAttr( 67 "aws_redshift_cluster.default", "publicly_accessible", "true"), 68 ), 69 }, 70 }, 71 }) 72 } 73 74 func TestAccAWSRedshiftCluster_updateNodeCount(t *testing.T) { 75 var v redshift.Cluster 76 77 ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() 78 preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri) 79 postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateNodeCount, ri) 80 81 resource.Test(t, resource.TestCase{ 82 PreCheck: func() { testAccPreCheck(t) }, 83 Providers: testAccProviders, 84 CheckDestroy: testAccCheckAWSRedshiftClusterDestroy, 85 Steps: []resource.TestStep{ 86 resource.TestStep{ 87 Config: preConfig, 88 Check: resource.ComposeTestCheckFunc( 89 testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v), 90 resource.TestCheckResourceAttr( 91 "aws_redshift_cluster.default", "number_of_nodes", "1"), 92 ), 93 }, 94 95 resource.TestStep{ 96 Config: postConfig, 97 Check: resource.ComposeTestCheckFunc( 98 testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v), 99 resource.TestCheckResourceAttr( 100 "aws_redshift_cluster.default", "number_of_nodes", "2"), 101 ), 102 }, 103 }, 104 }) 105 } 106 107 func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error { 108 for _, rs := range s.RootModule().Resources { 109 if rs.Type != "aws_redshift_cluster" { 110 continue 111 } 112 113 // Try to find the Group 114 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 115 var err error 116 resp, err := conn.DescribeClusters( 117 &redshift.DescribeClustersInput{ 118 ClusterIdentifier: aws.String(rs.Primary.ID), 119 }) 120 121 if err == nil { 122 if len(resp.Clusters) != 0 && 123 *resp.Clusters[0].ClusterIdentifier == rs.Primary.ID { 124 return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID) 125 } 126 } 127 128 // Return nil if the cluster is already destroyed 129 if awsErr, ok := err.(awserr.Error); ok { 130 if awsErr.Code() == "ClusterNotFound" { 131 return nil 132 } 133 } 134 135 return err 136 } 137 138 return nil 139 } 140 141 func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resource.TestCheckFunc { 142 return func(s *terraform.State) error { 143 rs, ok := s.RootModule().Resources[n] 144 if !ok { 145 return fmt.Errorf("Not found: %s", n) 146 } 147 148 if rs.Primary.ID == "" { 149 return fmt.Errorf("No Redshift Cluster Instance ID is set") 150 } 151 152 conn := testAccProvider.Meta().(*AWSClient).redshiftconn 153 resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{ 154 ClusterIdentifier: aws.String(rs.Primary.ID), 155 }) 156 157 if err != nil { 158 return err 159 } 160 161 for _, c := range resp.Clusters { 162 if *c.ClusterIdentifier == rs.Primary.ID { 163 *v = *c 164 return nil 165 } 166 } 167 168 return fmt.Errorf("Redshift Cluster (%s) not found", rs.Primary.ID) 169 } 170 } 171 172 func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) { 173 cases := []struct { 174 Value string 175 ErrCount int 176 }{ 177 { 178 Value: "tEsting", 179 ErrCount: 1, 180 }, 181 { 182 Value: "1testing", 183 ErrCount: 1, 184 }, 185 { 186 Value: "testing--123", 187 ErrCount: 1, 188 }, 189 { 190 Value: "testing!", 191 ErrCount: 1, 192 }, 193 { 194 Value: "testing-", 195 ErrCount: 1, 196 }, 197 } 198 199 for _, tc := range cases { 200 _, errors := validateRedshiftClusterIdentifier(tc.Value, "aws_redshift_cluster_identifier") 201 202 if len(errors) != tc.ErrCount { 203 t.Fatalf("Expected the Redshift Cluster cluster_identifier to trigger a validation error") 204 } 205 } 206 } 207 208 func TestResourceAWSRedshiftClusterDbNameValidation(t *testing.T) { 209 cases := []struct { 210 Value string 211 ErrCount int 212 }{ 213 { 214 Value: "tEsting", 215 ErrCount: 1, 216 }, 217 { 218 Value: "testing1", 219 ErrCount: 1, 220 }, 221 { 222 Value: "testing-", 223 ErrCount: 1, 224 }, 225 { 226 Value: "", 227 ErrCount: 2, 228 }, 229 { 230 Value: randomString(65), 231 ErrCount: 1, 232 }, 233 } 234 235 for _, tc := range cases { 236 _, errors := validateRedshiftClusterDbName(tc.Value, "aws_redshift_cluster_database_name") 237 238 if len(errors) != tc.ErrCount { 239 t.Fatalf("Expected the Redshift Cluster database_name to trigger a validation error") 240 } 241 } 242 } 243 244 func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) { 245 cases := []struct { 246 Value string 247 ErrCount int 248 }{ 249 { 250 Value: "testing--123", 251 ErrCount: 1, 252 }, 253 { 254 Value: "testing-", 255 ErrCount: 1, 256 }, 257 { 258 Value: "Testingq123!", 259 ErrCount: 1, 260 }, 261 { 262 Value: randomString(256), 263 ErrCount: 1, 264 }, 265 } 266 267 for _, tc := range cases { 268 _, errors := validateRedshiftClusterFinalSnapshotIdentifier(tc.Value, "aws_redshift_cluster_final_snapshot_identifier") 269 270 if len(errors) != tc.ErrCount { 271 t.Fatalf("Expected the Redshift Cluster final_snapshot_identifier to trigger a validation error") 272 } 273 } 274 } 275 276 func TestResourceAWSRedshiftClusterMasterUsernameValidation(t *testing.T) { 277 cases := []struct { 278 Value string 279 ErrCount int 280 }{ 281 { 282 Value: "1Testing", 283 ErrCount: 1, 284 }, 285 { 286 Value: "Testing!!", 287 ErrCount: 1, 288 }, 289 { 290 Value: randomString(129), 291 ErrCount: 1, 292 }, 293 { 294 Value: "testing_testing123", 295 ErrCount: 0, 296 }, 297 } 298 299 for _, tc := range cases { 300 _, errors := validateRedshiftClusterMasterUsername(tc.Value, "aws_redshift_cluster_master_username") 301 302 if len(errors) != tc.ErrCount { 303 t.Fatalf("Expected the Redshift Cluster master_username to trigger a validation error") 304 } 305 } 306 } 307 308 var testAccAWSRedshiftClusterConfig_updateNodeCount = ` 309 provider "aws" { 310 region = "us-west-2" 311 } 312 313 resource "aws_redshift_cluster" "default" { 314 cluster_identifier = "tf-redshift-cluster-%d" 315 availability_zone = "us-west-2a" 316 database_name = "mydb" 317 master_username = "foo_test" 318 master_password = "Mustbe8characters" 319 node_type = "dc1.large" 320 automated_snapshot_retention_period = 0 321 allow_version_upgrade = false 322 number_of_nodes = 2 323 } 324 ` 325 326 var testAccAWSRedshiftClusterConfig_basic = ` 327 provider "aws" { 328 region = "us-west-2" 329 } 330 331 resource "aws_redshift_cluster" "default" { 332 cluster_identifier = "tf-redshift-cluster-%d" 333 availability_zone = "us-west-2a" 334 database_name = "mydb" 335 master_username = "foo_test" 336 master_password = "Mustbe8characters" 337 node_type = "dc1.large" 338 automated_snapshot_retention_period = 0 339 allow_version_upgrade = false 340 }` 341 342 var testAccAWSRedshiftClusterConfig_notPubliclyAccessible = ` 343 provider "aws" { 344 region = "us-west-2" 345 } 346 347 resource "aws_vpc" "foo" { 348 cidr_block = "10.1.0.0/16" 349 } 350 351 resource "aws_internet_gateway" "foo" { 352 vpc_id = "${aws_vpc.foo.id}" 353 tags { 354 foo = "bar" 355 } 356 } 357 358 resource "aws_subnet" "foo" { 359 cidr_block = "10.1.1.0/24" 360 availability_zone = "us-west-2a" 361 vpc_id = "${aws_vpc.foo.id}" 362 tags { 363 Name = "tf-dbsubnet-test-1" 364 } 365 } 366 367 resource "aws_subnet" "bar" { 368 cidr_block = "10.1.2.0/24" 369 availability_zone = "us-west-2b" 370 vpc_id = "${aws_vpc.foo.id}" 371 tags { 372 Name = "tf-dbsubnet-test-2" 373 } 374 } 375 376 resource "aws_subnet" "foobar" { 377 cidr_block = "10.1.3.0/24" 378 availability_zone = "us-west-2c" 379 vpc_id = "${aws_vpc.foo.id}" 380 tags { 381 Name = "tf-dbsubnet-test-3" 382 } 383 } 384 385 resource "aws_redshift_subnet_group" "foo" { 386 name = "foo" 387 description = "foo description" 388 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"] 389 } 390 391 resource "aws_redshift_cluster" "default" { 392 cluster_identifier = "tf-redshift-cluster-%d" 393 availability_zone = "us-west-2a" 394 database_name = "mydb" 395 master_username = "foo" 396 master_password = "Mustbe8characters" 397 node_type = "dc1.large" 398 automated_snapshot_retention_period = 0 399 allow_version_upgrade = false 400 cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}" 401 publicly_accessible = false 402 }` 403 404 var testAccAWSRedshiftClusterConfig_updatePubliclyAccessible = ` 405 provider "aws" { 406 region = "us-west-2" 407 } 408 409 resource "aws_vpc" "foo" { 410 cidr_block = "10.1.0.0/16" 411 } 412 413 resource "aws_internet_gateway" "foo" { 414 vpc_id = "${aws_vpc.foo.id}" 415 tags { 416 foo = "bar" 417 } 418 } 419 420 resource "aws_subnet" "foo" { 421 cidr_block = "10.1.1.0/24" 422 availability_zone = "us-west-2a" 423 vpc_id = "${aws_vpc.foo.id}" 424 tags { 425 Name = "tf-dbsubnet-test-1" 426 } 427 } 428 429 resource "aws_subnet" "bar" { 430 cidr_block = "10.1.2.0/24" 431 availability_zone = "us-west-2b" 432 vpc_id = "${aws_vpc.foo.id}" 433 tags { 434 Name = "tf-dbsubnet-test-2" 435 } 436 } 437 438 resource "aws_subnet" "foobar" { 439 cidr_block = "10.1.3.0/24" 440 availability_zone = "us-west-2c" 441 vpc_id = "${aws_vpc.foo.id}" 442 tags { 443 Name = "tf-dbsubnet-test-3" 444 } 445 } 446 447 resource "aws_redshift_subnet_group" "foo" { 448 name = "foo" 449 description = "foo description" 450 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"] 451 } 452 453 resource "aws_redshift_cluster" "default" { 454 cluster_identifier = "tf-redshift-cluster-%d" 455 availability_zone = "us-west-2a" 456 database_name = "mydb" 457 master_username = "foo" 458 master_password = "Mustbe8characters" 459 node_type = "dc1.large" 460 automated_snapshot_retention_period = 0 461 allow_version_upgrade = false 462 cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}" 463 publicly_accessible = true 464 }`