github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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_iamRoles(t *testing.T) {
    42  	var v redshift.Cluster
    43  
    44  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    45  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_iamRoles, ri, ri, ri)
    46  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateIamRoles, ri, ri, 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", "iam_roles.#", "2"),
    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", "iam_roles.#", "1"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func TestAccAWSRedshiftCluster_publiclyAccessible(t *testing.T) {
    75  	var v redshift.Cluster
    76  
    77  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    78  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_notPubliclyAccessible, ri)
    79  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updatePubliclyAccessible, 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", "publicly_accessible", "false"),
    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", "publicly_accessible", "true"),
   101  				),
   102  			},
   103  		},
   104  	})
   105  }
   106  
   107  func TestAccAWSRedshiftCluster_updateNodeCount(t *testing.T) {
   108  	var v redshift.Cluster
   109  
   110  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
   111  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri)
   112  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateNodeCount, ri)
   113  
   114  	resource.Test(t, resource.TestCase{
   115  		PreCheck:     func() { testAccPreCheck(t) },
   116  		Providers:    testAccProviders,
   117  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   118  		Steps: []resource.TestStep{
   119  			resource.TestStep{
   120  				Config: preConfig,
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   123  					resource.TestCheckResourceAttr(
   124  						"aws_redshift_cluster.default", "number_of_nodes", "1"),
   125  				),
   126  			},
   127  
   128  			resource.TestStep{
   129  				Config: postConfig,
   130  				Check: resource.ComposeTestCheckFunc(
   131  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   132  					resource.TestCheckResourceAttr(
   133  						"aws_redshift_cluster.default", "number_of_nodes", "2"),
   134  				),
   135  			},
   136  		},
   137  	})
   138  }
   139  
   140  func TestAccAWSRedshiftCluster_tags(t *testing.T) {
   141  	var v redshift.Cluster
   142  
   143  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
   144  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_tags, ri)
   145  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updatedTags, ri)
   146  
   147  	resource.Test(t, resource.TestCase{
   148  		PreCheck:     func() { testAccPreCheck(t) },
   149  		Providers:    testAccProviders,
   150  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   151  		Steps: []resource.TestStep{
   152  			resource.TestStep{
   153  				Config: preConfig,
   154  				Check: resource.ComposeTestCheckFunc(
   155  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   156  					resource.TestCheckResourceAttr(
   157  						"aws_redshift_cluster.default", "tags.%", "3"),
   158  					resource.TestCheckResourceAttr("aws_redshift_cluster.default", "tags.environment", "Production"),
   159  				),
   160  			},
   161  
   162  			resource.TestStep{
   163  				Config: postConfig,
   164  				Check: resource.ComposeTestCheckFunc(
   165  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   166  					resource.TestCheckResourceAttr(
   167  						"aws_redshift_cluster.default", "tags.%", "1"),
   168  					resource.TestCheckResourceAttr("aws_redshift_cluster.default", "tags.environment", "Production"),
   169  				),
   170  			},
   171  		},
   172  	})
   173  }
   174  
   175  func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error {
   176  	for _, rs := range s.RootModule().Resources {
   177  		if rs.Type != "aws_redshift_cluster" {
   178  			continue
   179  		}
   180  
   181  		// Try to find the Group
   182  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   183  		var err error
   184  		resp, err := conn.DescribeClusters(
   185  			&redshift.DescribeClustersInput{
   186  				ClusterIdentifier: aws.String(rs.Primary.ID),
   187  			})
   188  
   189  		if err == nil {
   190  			if len(resp.Clusters) != 0 &&
   191  				*resp.Clusters[0].ClusterIdentifier == rs.Primary.ID {
   192  				return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID)
   193  			}
   194  		}
   195  
   196  		// Return nil if the cluster is already destroyed
   197  		if awsErr, ok := err.(awserr.Error); ok {
   198  			if awsErr.Code() == "ClusterNotFound" {
   199  				return nil
   200  			}
   201  		}
   202  
   203  		return err
   204  	}
   205  
   206  	return nil
   207  }
   208  
   209  func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resource.TestCheckFunc {
   210  	return func(s *terraform.State) error {
   211  		rs, ok := s.RootModule().Resources[n]
   212  		if !ok {
   213  			return fmt.Errorf("Not found: %s", n)
   214  		}
   215  
   216  		if rs.Primary.ID == "" {
   217  			return fmt.Errorf("No Redshift Cluster Instance ID is set")
   218  		}
   219  
   220  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   221  		resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{
   222  			ClusterIdentifier: aws.String(rs.Primary.ID),
   223  		})
   224  
   225  		if err != nil {
   226  			return err
   227  		}
   228  
   229  		for _, c := range resp.Clusters {
   230  			if *c.ClusterIdentifier == rs.Primary.ID {
   231  				*v = *c
   232  				return nil
   233  			}
   234  		}
   235  
   236  		return fmt.Errorf("Redshift Cluster (%s) not found", rs.Primary.ID)
   237  	}
   238  }
   239  
   240  func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) {
   241  	cases := []struct {
   242  		Value    string
   243  		ErrCount int
   244  	}{
   245  		{
   246  			Value:    "tEsting",
   247  			ErrCount: 1,
   248  		},
   249  		{
   250  			Value:    "1testing",
   251  			ErrCount: 1,
   252  		},
   253  		{
   254  			Value:    "testing--123",
   255  			ErrCount: 1,
   256  		},
   257  		{
   258  			Value:    "testing!",
   259  			ErrCount: 1,
   260  		},
   261  		{
   262  			Value:    "testing-",
   263  			ErrCount: 1,
   264  		},
   265  	}
   266  
   267  	for _, tc := range cases {
   268  		_, errors := validateRedshiftClusterIdentifier(tc.Value, "aws_redshift_cluster_identifier")
   269  
   270  		if len(errors) != tc.ErrCount {
   271  			t.Fatalf("Expected the Redshift Cluster cluster_identifier to trigger a validation error")
   272  		}
   273  	}
   274  }
   275  
   276  func TestResourceAWSRedshiftClusterDbNameValidation(t *testing.T) {
   277  	cases := []struct {
   278  		Value    string
   279  		ErrCount int
   280  	}{
   281  		{
   282  			Value:    "tEsting",
   283  			ErrCount: 1,
   284  		},
   285  		{
   286  			Value:    "testing1",
   287  			ErrCount: 1,
   288  		},
   289  		{
   290  			Value:    "testing-",
   291  			ErrCount: 1,
   292  		},
   293  		{
   294  			Value:    "",
   295  			ErrCount: 2,
   296  		},
   297  		{
   298  			Value:    randomString(65),
   299  			ErrCount: 1,
   300  		},
   301  	}
   302  
   303  	for _, tc := range cases {
   304  		_, errors := validateRedshiftClusterDbName(tc.Value, "aws_redshift_cluster_database_name")
   305  
   306  		if len(errors) != tc.ErrCount {
   307  			t.Fatalf("Expected the Redshift Cluster database_name to trigger a validation error")
   308  		}
   309  	}
   310  }
   311  
   312  func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) {
   313  	cases := []struct {
   314  		Value    string
   315  		ErrCount int
   316  	}{
   317  		{
   318  			Value:    "testing--123",
   319  			ErrCount: 1,
   320  		},
   321  		{
   322  			Value:    "testing-",
   323  			ErrCount: 1,
   324  		},
   325  		{
   326  			Value:    "Testingq123!",
   327  			ErrCount: 1,
   328  		},
   329  		{
   330  			Value:    randomString(256),
   331  			ErrCount: 1,
   332  		},
   333  	}
   334  
   335  	for _, tc := range cases {
   336  		_, errors := validateRedshiftClusterFinalSnapshotIdentifier(tc.Value, "aws_redshift_cluster_final_snapshot_identifier")
   337  
   338  		if len(errors) != tc.ErrCount {
   339  			t.Fatalf("Expected the Redshift Cluster final_snapshot_identifier to trigger a validation error")
   340  		}
   341  	}
   342  }
   343  
   344  func TestResourceAWSRedshiftClusterMasterUsernameValidation(t *testing.T) {
   345  	cases := []struct {
   346  		Value    string
   347  		ErrCount int
   348  	}{
   349  		{
   350  			Value:    "1Testing",
   351  			ErrCount: 1,
   352  		},
   353  		{
   354  			Value:    "Testing!!",
   355  			ErrCount: 1,
   356  		},
   357  		{
   358  			Value:    randomString(129),
   359  			ErrCount: 1,
   360  		},
   361  		{
   362  			Value:    "testing_testing123",
   363  			ErrCount: 0,
   364  		},
   365  	}
   366  
   367  	for _, tc := range cases {
   368  		_, errors := validateRedshiftClusterMasterUsername(tc.Value, "aws_redshift_cluster_master_username")
   369  
   370  		if len(errors) != tc.ErrCount {
   371  			t.Fatalf("Expected the Redshift Cluster master_username to trigger a validation error")
   372  		}
   373  	}
   374  }
   375  
   376  var testAccAWSRedshiftClusterConfig_updateNodeCount = `
   377  resource "aws_redshift_cluster" "default" {
   378    cluster_identifier = "tf-redshift-cluster-%d"
   379    availability_zone = "us-west-2a"
   380    database_name = "mydb"
   381    master_username = "foo_test"
   382    master_password = "Mustbe8characters"
   383    node_type = "dc1.large"
   384    automated_snapshot_retention_period = 0
   385    allow_version_upgrade = false
   386    number_of_nodes = 2
   387  }
   388  `
   389  
   390  var testAccAWSRedshiftClusterConfig_basic = `
   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_test"
   396    master_password = "Mustbe8characters"
   397    node_type = "dc1.large"
   398    automated_snapshot_retention_period = 0
   399    allow_version_upgrade = false
   400  }`
   401  
   402  var testAccAWSRedshiftClusterConfig_tags = `
   403  resource "aws_redshift_cluster" "default" {
   404    cluster_identifier = "tf-redshift-cluster-%d"
   405    availability_zone = "us-west-2a"
   406    database_name = "mydb"
   407    master_username = "foo"
   408    master_password = "Mustbe8characters"
   409    node_type = "dc1.large"
   410    automated_snapshot_retention_period = 7
   411    allow_version_upgrade = false
   412    tags {
   413      environment = "Production"
   414      cluster = "reader"
   415      Type = "master"
   416    }
   417  }`
   418  
   419  var testAccAWSRedshiftClusterConfig_updatedTags = `
   420  resource "aws_redshift_cluster" "default" {
   421    cluster_identifier = "tf-redshift-cluster-%d"
   422    availability_zone = "us-west-2a"
   423    database_name = "mydb"
   424    master_username = "foo"
   425    master_password = "Mustbe8characters"
   426    node_type = "dc1.large"
   427    automated_snapshot_retention_period = 7
   428    allow_version_upgrade = false
   429    tags {
   430      environment = "Production"
   431    }
   432  }`
   433  
   434  var testAccAWSRedshiftClusterConfig_notPubliclyAccessible = `
   435  resource "aws_vpc" "foo" {
   436  	cidr_block = "10.1.0.0/16"
   437  }
   438  resource "aws_internet_gateway" "foo" {
   439  	vpc_id = "${aws_vpc.foo.id}"
   440  	tags {
   441  		foo = "bar"
   442  	}
   443  }
   444  resource "aws_subnet" "foo" {
   445  	cidr_block = "10.1.1.0/24"
   446  	availability_zone = "us-west-2a"
   447  	vpc_id = "${aws_vpc.foo.id}"
   448  	tags {
   449  		Name = "tf-dbsubnet-test-1"
   450  	}
   451  }
   452  resource "aws_subnet" "bar" {
   453  	cidr_block = "10.1.2.0/24"
   454  	availability_zone = "us-west-2b"
   455  	vpc_id = "${aws_vpc.foo.id}"
   456  	tags {
   457  		Name = "tf-dbsubnet-test-2"
   458  	}
   459  }
   460  resource "aws_subnet" "foobar" {
   461  	cidr_block = "10.1.3.0/24"
   462  	availability_zone = "us-west-2c"
   463  	vpc_id = "${aws_vpc.foo.id}"
   464  	tags {
   465  		Name = "tf-dbsubnet-test-3"
   466  	}
   467  }
   468  resource "aws_redshift_subnet_group" "foo" {
   469  	name = "foo"
   470  	description = "foo description"
   471  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   472  }
   473  resource "aws_redshift_cluster" "default" {
   474    cluster_identifier = "tf-redshift-cluster-%d"
   475    availability_zone = "us-west-2a"
   476    database_name = "mydb"
   477    master_username = "foo"
   478    master_password = "Mustbe8characters"
   479    node_type = "dc1.large"
   480    automated_snapshot_retention_period = 0
   481    allow_version_upgrade = false
   482    cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   483    publicly_accessible = false
   484  }`
   485  
   486  var testAccAWSRedshiftClusterConfig_updatePubliclyAccessible = `
   487  resource "aws_vpc" "foo" {
   488  	cidr_block = "10.1.0.0/16"
   489  }
   490  resource "aws_internet_gateway" "foo" {
   491  	vpc_id = "${aws_vpc.foo.id}"
   492  	tags {
   493  		foo = "bar"
   494  	}
   495  }
   496  resource "aws_subnet" "foo" {
   497  	cidr_block = "10.1.1.0/24"
   498  	availability_zone = "us-west-2a"
   499  	vpc_id = "${aws_vpc.foo.id}"
   500  	tags {
   501  		Name = "tf-dbsubnet-test-1"
   502  	}
   503  }
   504  resource "aws_subnet" "bar" {
   505  	cidr_block = "10.1.2.0/24"
   506  	availability_zone = "us-west-2b"
   507  	vpc_id = "${aws_vpc.foo.id}"
   508  	tags {
   509  		Name = "tf-dbsubnet-test-2"
   510  	}
   511  }
   512  resource "aws_subnet" "foobar" {
   513  	cidr_block = "10.1.3.0/24"
   514  	availability_zone = "us-west-2c"
   515  	vpc_id = "${aws_vpc.foo.id}"
   516  	tags {
   517  		Name = "tf-dbsubnet-test-3"
   518  	}
   519  }
   520  resource "aws_redshift_subnet_group" "foo" {
   521  	name = "foo"
   522  	description = "foo description"
   523  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   524  }
   525  resource "aws_redshift_cluster" "default" {
   526    cluster_identifier = "tf-redshift-cluster-%d"
   527    availability_zone = "us-west-2a"
   528    database_name = "mydb"
   529    master_username = "foo"
   530    master_password = "Mustbe8characters"
   531    node_type = "dc1.large"
   532    automated_snapshot_retention_period = 0
   533    allow_version_upgrade = false
   534    cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   535    publicly_accessible = true
   536  }`
   537  
   538  var testAccAWSRedshiftClusterConfig_iamRoles = `
   539  resource "aws_iam_role" "ec2-role" {
   540  	name   = "test-role-ec2-%d"
   541  	path = "/"
   542   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   543  }
   544  
   545  resource "aws_iam_role" "lambda-role" {
   546   	name   = "test-role-lambda-%d"
   547   	path = "/"
   548   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"lambda.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   549  }
   550  
   551  resource "aws_redshift_cluster" "default" {
   552     cluster_identifier = "tf-redshift-cluster-%d"
   553     availability_zone = "us-west-2a"
   554     database_name = "mydb"
   555     master_username = "foo_test"
   556     master_password = "Mustbe8characters"
   557     node_type = "dc1.large"
   558     automated_snapshot_retention_period = 0
   559     allow_version_upgrade = false
   560     iam_roles = ["${aws_iam_role.ec2-role.arn}", "${aws_iam_role.lambda-role.arn}"]
   561  }`
   562  
   563  var testAccAWSRedshiftClusterConfig_updateIamRoles = `
   564  resource "aws_iam_role" "ec2-role" {
   565   	name   = "test-role-ec2-%d"
   566   	path = "/"
   567   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   568   }
   569  
   570   resource "aws_iam_role" "lambda-role" {
   571   	name   = "test-role-lambda-%d"
   572   	path = "/"
   573   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"lambda.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   574   }
   575  
   576   resource "aws_redshift_cluster" "default" {
   577     cluster_identifier = "tf-redshift-cluster-%d"
   578     availability_zone = "us-west-2a"
   579     database_name = "mydb"
   580     master_username = "foo_test"
   581     master_password = "Mustbe8characters"
   582     node_type = "dc1.large"
   583     automated_snapshot_retention_period = 0
   584     allow_version_upgrade = false
   585     iam_roles = ["${aws_iam_role.ec2-role.arn}"]
   586   }`