Skip to content

Commit

Permalink
r/redshift_cluster: Add test + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 7, 2017
1 parent 3780cc6 commit 6c6ca26
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 30 deletions.
3 changes: 1 addition & 2 deletions aws/import_aws_redshift_cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand All @@ -10,7 +9,7 @@ import (

func TestAccAWSRedshiftCluster_importBasic(t *testing.T) {
resourceName := "aws_redshift_cluster.default"
config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, acctest.RandInt())
config := testAccAWSRedshiftClusterConfig_basic(acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
120 changes: 92 additions & 28 deletions aws/resource_aws_redshift_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestAccAWSRedshiftCluster_basic(t *testing.T) {
var v redshift.Cluster

ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri)
config := testAccAWSRedshiftClusterConfig_basic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestAccAWSRedshiftCluster_kmsKey(t *testing.T) {
var v redshift.Cluster

ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_kmsKey, ri, ri)
config := testAccAWSRedshiftClusterConfig_kmsKey(ri)
keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$")

resource.Test(t, resource.TestCase{
Expand All @@ -129,8 +129,8 @@ func TestAccAWSRedshiftCluster_enhancedVpcRoutingEnabled(t *testing.T) {
var v redshift.Cluster

ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled, ri)
postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled, ri)
preConfig := testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled(ri)
postConfig := testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -193,8 +193,8 @@ func TestAccAWSRedshiftCluster_iamRoles(t *testing.T) {
var v redshift.Cluster

ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_iamRoles, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateIamRoles, ri, ri, ri)
preConfig := testAccAWSRedshiftClusterConfig_iamRoles(ri)
postConfig := testAccAWSRedshiftClusterConfig_updateIamRoles(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -256,8 +256,8 @@ func TestAccAWSRedshiftCluster_updateNodeCount(t *testing.T) {
var v redshift.Cluster

ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri)
postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateNodeCount, ri)
preConfig := testAccAWSRedshiftClusterConfig_basic(ri)
postConfig := testAccAWSRedshiftClusterConfig_updateNodeCount(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -289,8 +289,8 @@ func TestAccAWSRedshiftCluster_tags(t *testing.T) {
var v redshift.Cluster

ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_tags, ri)
postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updatedTags, ri)
preConfig := testAccAWSRedshiftClusterConfig_tags(ri)
postConfig := testAccAWSRedshiftClusterConfig_updatedTags(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -320,6 +320,37 @@ func TestAccAWSRedshiftCluster_tags(t *testing.T) {
})
}

func TestAccAWSRedshiftCluster_forceNewUsername(t *testing.T) {
var v redshift.Cluster

ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
preConfig := testAccAWSRedshiftClusterConfig_basic(ri)
postConfig := testAccAWSRedshiftClusterConfig_updatedUsername(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
resource.TestCheckResourceAttr("aws_redshift_cluster.default", "master_username", "foo_test"),
),
},

{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
resource.TestCheckResourceAttr("aws_redshift_cluster.default", "master_username", "new_username"),
),
},
},
})
}

func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_redshift_cluster" {
Expand Down Expand Up @@ -581,7 +612,8 @@ func TestResourceAWSRedshiftClusterMasterPasswordValidation(t *testing.T) {
}
}

var testAccAWSRedshiftClusterConfig_updateNodeCount = `
func testAccAWSRedshiftClusterConfig_updateNodeCount(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -594,9 +626,11 @@ resource "aws_redshift_cluster" "default" {
number_of_nodes = 2
skip_final_snapshot = true
}
`
`, rInt)
}

var testAccAWSRedshiftClusterConfig_basic = `
func testAccAWSRedshiftClusterConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -607,7 +641,8 @@ resource "aws_redshift_cluster" "default" {
automated_snapshot_retention_period = 0
allow_version_upgrade = false
skip_final_snapshot = true
}`
}`, rInt)
}

func testAccAWSRedshiftClusterConfigWithFinalSnapshot(rInt int) string {
return fmt.Sprintf(`
Expand All @@ -625,7 +660,8 @@ resource "aws_redshift_cluster" "default" {
}`, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_kmsKey = `
func testAccAWSRedshiftClusterConfig_kmsKey(rInt int) string {
return fmt.Sprintf(`
resource "aws_kms_key" "foo" {
description = "Terraform acc test %d"
policy = <<POLICY
Expand Down Expand Up @@ -659,9 +695,11 @@ resource "aws_redshift_cluster" "default" {
kms_key_id = "${aws_kms_key.foo.arn}"
encrypted = true
skip_final_snapshot = true
}`
}`, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled = `
func testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -674,9 +712,11 @@ resource "aws_redshift_cluster" "default" {
enhanced_vpc_routing = true
skip_final_snapshot = true
}
`
`, rInt)
}

var testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled = `
func testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -689,7 +729,8 @@ resource "aws_redshift_cluster" "default" {
enhanced_vpc_routing = false
skip_final_snapshot = true
}
`
`, rInt)
}

func testAccAWSRedshiftClusterConfig_loggingDisabled(rInt int) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -755,7 +796,8 @@ EOF
}`, rInt, rInt, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_tags = `
func testAccAWSRedshiftClusterConfig_tags(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -771,9 +813,11 @@ resource "aws_redshift_cluster" "default" {
cluster = "reader"
Type = "master"
}
}`
}`, rInt)
}

var testAccAWSRedshiftClusterConfig_updatedTags = `
func testAccAWSRedshiftClusterConfig_updatedTags(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -787,7 +831,8 @@ resource "aws_redshift_cluster" "default" {
tags {
environment = "Production"
}
}`
}`, rInt)
}

func testAccAWSRedshiftClusterConfig_notPubliclyAccessible(rInt int) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -909,7 +954,8 @@ func testAccAWSRedshiftClusterConfig_updatePubliclyAccessible(rInt int) string {
}`, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_iamRoles = `
func testAccAWSRedshiftClusterConfig_iamRoles(rInt int) string {
return fmt.Sprintf(`
resource "aws_iam_role" "ec2-role" {
name = "test-role-ec2-%d"
path = "/"
Expand All @@ -933,9 +979,11 @@ resource "aws_redshift_cluster" "default" {
allow_version_upgrade = false
iam_roles = ["${aws_iam_role.ec2-role.arn}", "${aws_iam_role.lambda-role.arn}"]
skip_final_snapshot = true
}`
}`, rInt, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_updateIamRoles = `
func testAccAWSRedshiftClusterConfig_updateIamRoles(rInt int) string {
return fmt.Sprintf(`
resource "aws_iam_role" "ec2-role" {
name = "test-role-ec2-%d"
path = "/"
Expand All @@ -959,4 +1007,20 @@ resource "aws_iam_role" "ec2-role" {
allow_version_upgrade = false
iam_roles = ["${aws_iam_role.ec2-role.arn}"]
skip_final_snapshot = true
}`
}`, rInt, rInt, rInt)
}

func testAccAWSRedshiftClusterConfig_updatedUsername(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
database_name = "mydb"
master_username = "new_username"
master_password = "Mustbe8characters"
node_type = "dc1.large"
automated_snapshot_retention_period = 0
allow_version_upgrade = false
skip_final_snapshot = true
}`, rInt)
}

0 comments on commit 6c6ca26

Please sign in to comment.