github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/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 testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error {
    75  	for _, rs := range s.RootModule().Resources {
    76  		if rs.Type != "aws_redshift_cluster" {
    77  			continue
    78  		}
    79  
    80  		// Try to find the Group
    81  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
    82  		var err error
    83  		resp, err := conn.DescribeClusters(
    84  			&redshift.DescribeClustersInput{
    85  				ClusterIdentifier: aws.String(rs.Primary.ID),
    86  			})
    87  
    88  		if err == nil {
    89  			if len(resp.Clusters) != 0 &&
    90  				*resp.Clusters[0].ClusterIdentifier == rs.Primary.ID {
    91  				return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID)
    92  			}
    93  		}
    94  
    95  		// Return nil if the cluster is already destroyed
    96  		if awsErr, ok := err.(awserr.Error); ok {
    97  			if awsErr.Code() == "ClusterNotFound" {
    98  				return nil
    99  			}
   100  		}
   101  
   102  		return err
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resource.TestCheckFunc {
   109  	return func(s *terraform.State) error {
   110  		rs, ok := s.RootModule().Resources[n]
   111  		if !ok {
   112  			return fmt.Errorf("Not found: %s", n)
   113  		}
   114  
   115  		if rs.Primary.ID == "" {
   116  			return fmt.Errorf("No Redshift Cluster Instance ID is set")
   117  		}
   118  
   119  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   120  		resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{
   121  			ClusterIdentifier: aws.String(rs.Primary.ID),
   122  		})
   123  
   124  		if err != nil {
   125  			return err
   126  		}
   127  
   128  		for _, c := range resp.Clusters {
   129  			if *c.ClusterIdentifier == rs.Primary.ID {
   130  				*v = *c
   131  				return nil
   132  			}
   133  		}
   134  
   135  		return fmt.Errorf("Redshift Cluster (%s) not found", rs.Primary.ID)
   136  	}
   137  }
   138  
   139  func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) {
   140  	cases := []struct {
   141  		Value    string
   142  		ErrCount int
   143  	}{
   144  		{
   145  			Value:    "tEsting",
   146  			ErrCount: 1,
   147  		},
   148  		{
   149  			Value:    "1testing",
   150  			ErrCount: 1,
   151  		},
   152  		{
   153  			Value:    "testing--123",
   154  			ErrCount: 1,
   155  		},
   156  		{
   157  			Value:    "testing!",
   158  			ErrCount: 1,
   159  		},
   160  		{
   161  			Value:    "testing-",
   162  			ErrCount: 1,
   163  		},
   164  	}
   165  
   166  	for _, tc := range cases {
   167  		_, errors := validateRedshiftClusterIdentifier(tc.Value, "aws_redshift_cluster_identifier")
   168  
   169  		if len(errors) != tc.ErrCount {
   170  			t.Fatalf("Expected the Redshift Cluster cluster_identifier to trigger a validation error")
   171  		}
   172  	}
   173  }
   174  
   175  func TestResourceAWSRedshiftClusterDbNameValidation(t *testing.T) {
   176  	cases := []struct {
   177  		Value    string
   178  		ErrCount int
   179  	}{
   180  		{
   181  			Value:    "tEsting",
   182  			ErrCount: 1,
   183  		},
   184  		{
   185  			Value:    "testing1",
   186  			ErrCount: 1,
   187  		},
   188  		{
   189  			Value:    "testing-",
   190  			ErrCount: 1,
   191  		},
   192  		{
   193  			Value:    "",
   194  			ErrCount: 2,
   195  		},
   196  		{
   197  			Value:    randomString(65),
   198  			ErrCount: 1,
   199  		},
   200  	}
   201  
   202  	for _, tc := range cases {
   203  		_, errors := validateRedshiftClusterDbName(tc.Value, "aws_redshift_cluster_database_name")
   204  
   205  		if len(errors) != tc.ErrCount {
   206  			t.Fatalf("Expected the Redshift Cluster database_name to trigger a validation error")
   207  		}
   208  	}
   209  }
   210  
   211  func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) {
   212  	cases := []struct {
   213  		Value    string
   214  		ErrCount int
   215  	}{
   216  		{
   217  			Value:    "testing--123",
   218  			ErrCount: 1,
   219  		},
   220  		{
   221  			Value:    "testing-",
   222  			ErrCount: 1,
   223  		},
   224  		{
   225  			Value:    "Testingq123!",
   226  			ErrCount: 1,
   227  		},
   228  		{
   229  			Value:    randomString(256),
   230  			ErrCount: 1,
   231  		},
   232  	}
   233  
   234  	for _, tc := range cases {
   235  		_, errors := validateRedshiftClusterFinalSnapshotIdentifier(tc.Value, "aws_redshift_cluster_final_snapshot_identifier")
   236  
   237  		if len(errors) != tc.ErrCount {
   238  			t.Fatalf("Expected the Redshift Cluster final_snapshot_identifier to trigger a validation error")
   239  		}
   240  	}
   241  }
   242  
   243  func TestResourceAWSRedshiftClusterMasterUsernameValidation(t *testing.T) {
   244  	cases := []struct {
   245  		Value    string
   246  		ErrCount int
   247  	}{
   248  		{
   249  			Value:    "1Testing",
   250  			ErrCount: 1,
   251  		},
   252  		{
   253  			Value:    "Testing!!",
   254  			ErrCount: 1,
   255  		},
   256  		{
   257  			Value:    randomString(129),
   258  			ErrCount: 1,
   259  		},
   260  	}
   261  
   262  	for _, tc := range cases {
   263  		_, errors := validateRedshiftClusterMasterUsername(tc.Value, "aws_redshift_cluster_master_username")
   264  
   265  		if len(errors) != tc.ErrCount {
   266  			t.Fatalf("Expected the Redshift Cluster master_username to trigger a validation error")
   267  		}
   268  	}
   269  }
   270  
   271  var testAccAWSRedshiftClusterConfig_basic = `
   272  provider "aws" {
   273  	region = "us-west-2"
   274  }
   275  
   276  resource "aws_redshift_cluster" "default" {
   277    cluster_identifier = "tf-redshift-cluster-%d"
   278    availability_zone = "us-west-2a"
   279    database_name = "mydb"
   280    master_username = "foo"
   281    master_password = "Mustbe8characters"
   282    node_type = "dc1.large"
   283    automated_snapshot_retention_period = 7
   284    allow_version_upgrade = false
   285  }`
   286  
   287  var testAccAWSRedshiftClusterConfig_notPubliclyAccessible = `
   288  provider "aws" {
   289  	region = "us-west-2"
   290  }
   291  
   292  resource "aws_vpc" "foo" {
   293  	cidr_block = "10.1.0.0/16"
   294  }
   295  
   296  resource "aws_internet_gateway" "foo" {
   297  	vpc_id = "${aws_vpc.foo.id}"
   298  	tags {
   299  		foo = "bar"
   300  	}
   301  }
   302  
   303  resource "aws_subnet" "foo" {
   304  	cidr_block = "10.1.1.0/24"
   305  	availability_zone = "us-west-2a"
   306  	vpc_id = "${aws_vpc.foo.id}"
   307  	tags {
   308  		Name = "tf-dbsubnet-test-1"
   309  	}
   310  }
   311  
   312  resource "aws_subnet" "bar" {
   313  	cidr_block = "10.1.2.0/24"
   314  	availability_zone = "us-west-2b"
   315  	vpc_id = "${aws_vpc.foo.id}"
   316  	tags {
   317  		Name = "tf-dbsubnet-test-2"
   318  	}
   319  }
   320  
   321  resource "aws_subnet" "foobar" {
   322  	cidr_block = "10.1.3.0/24"
   323  	availability_zone = "us-west-2c"
   324  	vpc_id = "${aws_vpc.foo.id}"
   325  	tags {
   326  		Name = "tf-dbsubnet-test-3"
   327  	}
   328  }
   329  
   330  resource "aws_redshift_subnet_group" "foo" {
   331  	name = "foo"
   332  	description = "foo description"
   333  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   334  }
   335  
   336  resource "aws_redshift_cluster" "default" {
   337    cluster_identifier = "tf-redshift-cluster-%d"
   338    availability_zone = "us-west-2a"
   339    database_name = "mydb"
   340    master_username = "foo"
   341    master_password = "Mustbe8characters"
   342    node_type = "dc1.large"
   343    automated_snapshot_retention_period = 7
   344    allow_version_upgrade = false
   345    cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   346    publicly_accessible = false
   347  }`
   348  
   349  var testAccAWSRedshiftClusterConfig_updatePubliclyAccessible = `
   350  provider "aws" {
   351  	region = "us-west-2"
   352  }
   353  
   354  resource "aws_vpc" "foo" {
   355  	cidr_block = "10.1.0.0/16"
   356  }
   357  
   358  resource "aws_internet_gateway" "foo" {
   359  	vpc_id = "${aws_vpc.foo.id}"
   360  	tags {
   361  		foo = "bar"
   362  	}
   363  }
   364  
   365  resource "aws_subnet" "foo" {
   366  	cidr_block = "10.1.1.0/24"
   367  	availability_zone = "us-west-2a"
   368  	vpc_id = "${aws_vpc.foo.id}"
   369  	tags {
   370  		Name = "tf-dbsubnet-test-1"
   371  	}
   372  }
   373  
   374  resource "aws_subnet" "bar" {
   375  	cidr_block = "10.1.2.0/24"
   376  	availability_zone = "us-west-2b"
   377  	vpc_id = "${aws_vpc.foo.id}"
   378  	tags {
   379  		Name = "tf-dbsubnet-test-2"
   380  	}
   381  }
   382  
   383  resource "aws_subnet" "foobar" {
   384  	cidr_block = "10.1.3.0/24"
   385  	availability_zone = "us-west-2c"
   386  	vpc_id = "${aws_vpc.foo.id}"
   387  	tags {
   388  		Name = "tf-dbsubnet-test-3"
   389  	}
   390  }
   391  
   392  resource "aws_redshift_subnet_group" "foo" {
   393  	name = "foo"
   394  	description = "foo description"
   395  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   396  }
   397  
   398  resource "aws_redshift_cluster" "default" {
   399    cluster_identifier = "tf-redshift-cluster-%d"
   400    availability_zone = "us-west-2a"
   401    database_name = "mydb"
   402    master_username = "foo"
   403    master_password = "Mustbe8characters"
   404    node_type = "dc1.large"
   405    automated_snapshot_retention_period = 7
   406    allow_version_upgrade = false
   407    cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   408    publicly_accessible = true
   409  }`