github.com/subuk/terraform@v0.6.14-0.20160317140351-de1567c2e732/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_notPubliclyAccessible(t *testing.T) {
    42  	var v redshift.Cluster
    43  
    44  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    45  	config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_notPubliclyAccessible, ri)
    46  
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
    51  		Steps: []resource.TestStep{
    52  			resource.TestStep{
    53  				Config: config,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
    56  					resource.TestCheckResourceAttr(
    57  						"aws_redshift_cluster.default", "publicly_accessible", "false"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error {
    65  	for _, rs := range s.RootModule().Resources {
    66  		if rs.Type != "aws_redshift_cluster" {
    67  			continue
    68  		}
    69  
    70  		// Try to find the Group
    71  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
    72  		var err error
    73  		resp, err := conn.DescribeClusters(
    74  			&redshift.DescribeClustersInput{
    75  				ClusterIdentifier: aws.String(rs.Primary.ID),
    76  			})
    77  
    78  		if err == nil {
    79  			if len(resp.Clusters) != 0 &&
    80  				*resp.Clusters[0].ClusterIdentifier == rs.Primary.ID {
    81  				return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID)
    82  			}
    83  		}
    84  
    85  		// Return nil if the cluster is already destroyed
    86  		if awsErr, ok := err.(awserr.Error); ok {
    87  			if awsErr.Code() == "ClusterNotFound" {
    88  				return nil
    89  			}
    90  		}
    91  
    92  		return err
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resource.TestCheckFunc {
    99  	return func(s *terraform.State) error {
   100  		rs, ok := s.RootModule().Resources[n]
   101  		if !ok {
   102  			return fmt.Errorf("Not found: %s", n)
   103  		}
   104  
   105  		if rs.Primary.ID == "" {
   106  			return fmt.Errorf("No Redshift Cluster Instance ID is set")
   107  		}
   108  
   109  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   110  		resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{
   111  			ClusterIdentifier: aws.String(rs.Primary.ID),
   112  		})
   113  
   114  		if err != nil {
   115  			return err
   116  		}
   117  
   118  		for _, c := range resp.Clusters {
   119  			if *c.ClusterIdentifier == rs.Primary.ID {
   120  				*v = *c
   121  				return nil
   122  			}
   123  		}
   124  
   125  		return fmt.Errorf("Redshift Cluster (%s) not found", rs.Primary.ID)
   126  	}
   127  }
   128  
   129  func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) {
   130  	cases := []struct {
   131  		Value    string
   132  		ErrCount int
   133  	}{
   134  		{
   135  			Value:    "tEsting",
   136  			ErrCount: 1,
   137  		},
   138  		{
   139  			Value:    "1testing",
   140  			ErrCount: 1,
   141  		},
   142  		{
   143  			Value:    "testing--123",
   144  			ErrCount: 1,
   145  		},
   146  		{
   147  			Value:    "testing!",
   148  			ErrCount: 1,
   149  		},
   150  		{
   151  			Value:    "testing-",
   152  			ErrCount: 1,
   153  		},
   154  	}
   155  
   156  	for _, tc := range cases {
   157  		_, errors := validateRedshiftClusterIdentifier(tc.Value, "aws_redshift_cluster_identifier")
   158  
   159  		if len(errors) != tc.ErrCount {
   160  			t.Fatalf("Expected the Redshift Cluster cluster_identifier to trigger a validation error")
   161  		}
   162  	}
   163  }
   164  
   165  func TestResourceAWSRedshiftClusterDbNameValidation(t *testing.T) {
   166  	cases := []struct {
   167  		Value    string
   168  		ErrCount int
   169  	}{
   170  		{
   171  			Value:    "tEsting",
   172  			ErrCount: 1,
   173  		},
   174  		{
   175  			Value:    "testing1",
   176  			ErrCount: 1,
   177  		},
   178  		{
   179  			Value:    "testing-",
   180  			ErrCount: 1,
   181  		},
   182  		{
   183  			Value:    "",
   184  			ErrCount: 2,
   185  		},
   186  		{
   187  			Value:    randomString(65),
   188  			ErrCount: 1,
   189  		},
   190  	}
   191  
   192  	for _, tc := range cases {
   193  		_, errors := validateRedshiftClusterDbName(tc.Value, "aws_redshift_cluster_database_name")
   194  
   195  		if len(errors) != tc.ErrCount {
   196  			t.Fatalf("Expected the Redshift Cluster database_name to trigger a validation error")
   197  		}
   198  	}
   199  }
   200  
   201  func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) {
   202  	cases := []struct {
   203  		Value    string
   204  		ErrCount int
   205  	}{
   206  		{
   207  			Value:    "testing--123",
   208  			ErrCount: 1,
   209  		},
   210  		{
   211  			Value:    "testing-",
   212  			ErrCount: 1,
   213  		},
   214  		{
   215  			Value:    "Testingq123!",
   216  			ErrCount: 1,
   217  		},
   218  		{
   219  			Value:    randomString(256),
   220  			ErrCount: 1,
   221  		},
   222  	}
   223  
   224  	for _, tc := range cases {
   225  		_, errors := validateRedshiftClusterFinalSnapshotIdentifier(tc.Value, "aws_redshift_cluster_final_snapshot_identifier")
   226  
   227  		if len(errors) != tc.ErrCount {
   228  			t.Fatalf("Expected the Redshift Cluster final_snapshot_identifier to trigger a validation error")
   229  		}
   230  	}
   231  }
   232  
   233  func TestResourceAWSRedshiftClusterMasterUsernameValidation(t *testing.T) {
   234  	cases := []struct {
   235  		Value    string
   236  		ErrCount int
   237  	}{
   238  		{
   239  			Value:    "1Testing",
   240  			ErrCount: 1,
   241  		},
   242  		{
   243  			Value:    "Testing!!",
   244  			ErrCount: 1,
   245  		},
   246  		{
   247  			Value:    randomString(129),
   248  			ErrCount: 1,
   249  		},
   250  	}
   251  
   252  	for _, tc := range cases {
   253  		_, errors := validateRedshiftClusterMasterUsername(tc.Value, "aws_redshift_cluster_master_username")
   254  
   255  		if len(errors) != tc.ErrCount {
   256  			t.Fatalf("Expected the Redshift Cluster master_username to trigger a validation error")
   257  		}
   258  	}
   259  }
   260  
   261  var testAccAWSRedshiftClusterConfig_basic = `
   262  provider "aws" {
   263  	region = "us-west-2"
   264  }
   265  
   266  resource "aws_redshift_cluster" "default" {
   267    cluster_identifier = "tf-redshift-cluster-%d"
   268    availability_zone = "us-west-2a"
   269    database_name = "mydb"
   270    master_username = "foo"
   271    master_password = "Mustbe8characters"
   272    node_type = "dc1.large"
   273    automated_snapshot_retention_period = 7
   274    allow_version_upgrade = false
   275  }`
   276  
   277  var testAccAWSRedshiftClusterConfig_notPubliclyAccessible = `
   278  provider "aws" {
   279  	region = "us-west-2"
   280  }
   281  
   282  resource "aws_vpc" "foo" {
   283  	cidr_block = "10.1.0.0/16"
   284  }
   285  
   286  resource "aws_internet_gateway" "foo" {
   287  	vpc_id = "${aws_vpc.foo.id}"
   288  	tags {
   289  		foo = "bar"
   290  	}
   291  }
   292  
   293  resource "aws_subnet" "foo" {
   294  	cidr_block = "10.1.1.0/24"
   295  	availability_zone = "us-west-2a"
   296  	vpc_id = "${aws_vpc.foo.id}"
   297  	tags {
   298  		Name = "tf-dbsubnet-test-1"
   299  	}
   300  }
   301  
   302  resource "aws_subnet" "bar" {
   303  	cidr_block = "10.1.2.0/24"
   304  	availability_zone = "us-west-2b"
   305  	vpc_id = "${aws_vpc.foo.id}"
   306  	tags {
   307  		Name = "tf-dbsubnet-test-2"
   308  	}
   309  }
   310  
   311  resource "aws_subnet" "foobar" {
   312  	cidr_block = "10.1.3.0/24"
   313  	availability_zone = "us-west-2c"
   314  	vpc_id = "${aws_vpc.foo.id}"
   315  	tags {
   316  		Name = "tf-dbsubnet-test-3"
   317  	}
   318  }
   319  
   320  resource "aws_redshift_subnet_group" "foo" {
   321  	name = "foo"
   322  	description = "foo description"
   323  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   324  }
   325  
   326  resource "aws_redshift_cluster" "default" {
   327    cluster_identifier = "tf-redshift-cluster-%d"
   328    availability_zone = "us-west-2a"
   329    database_name = "mydb"
   330    master_username = "foo"
   331    master_password = "Mustbe8characters"
   332    node_type = "dc1.large"
   333    automated_snapshot_retention_period = 7
   334    allow_version_upgrade = false
   335    cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   336    publicly_accessible = false
   337  }`