github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_elasticache_cluster_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/service/elasticache"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSElasticacheCluster_basic(t *testing.T) {
    17  	var ec elasticache.CacheCluster
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSElasticacheClusterConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
    27  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_elasticache_cluster.bar", "cache_nodes.0.id", "0001"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccAWSElasticacheCluster_snapshotsWithUpdates(t *testing.T) {
    37  	var ec elasticache.CacheCluster
    38  
    39  	ri := genRandInt()
    40  	preConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshots, ri, ri, ri)
    41  	postConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshotsUpdated, ri, ri, ri)
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: preConfig,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
    52  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
    53  					resource.TestCheckResourceAttr(
    54  						"aws_elasticache_cluster.bar", "snapshot_window", "05:00-09:00"),
    55  					resource.TestCheckResourceAttr(
    56  						"aws_elasticache_cluster.bar", "snapshot_retention_limit", "3"),
    57  				),
    58  			},
    59  
    60  			resource.TestStep{
    61  				Config: postConfig,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
    64  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
    65  					resource.TestCheckResourceAttr(
    66  						"aws_elasticache_cluster.bar", "snapshot_window", "07:00-09:00"),
    67  					resource.TestCheckResourceAttr(
    68  						"aws_elasticache_cluster.bar", "snapshot_retention_limit", "7"),
    69  				),
    70  			},
    71  		},
    72  	})
    73  }
    74  
    75  func TestAccAWSElasticacheCluster_vpc(t *testing.T) {
    76  	var csg elasticache.CacheSubnetGroup
    77  	var ec elasticache.CacheCluster
    78  	resource.Test(t, resource.TestCase{
    79  		PreCheck:     func() { testAccPreCheck(t) },
    80  		Providers:    testAccProviders,
    81  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
    82  		Steps: []resource.TestStep{
    83  			resource.TestStep{
    84  				Config: testAccAWSElasticacheClusterInVPCConfig,
    85  				Check: resource.ComposeTestCheckFunc(
    86  					testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
    87  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
    88  					testAccCheckAWSElasticacheClusterAttributes(&ec),
    89  				),
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func testAccCheckAWSElasticacheClusterAttributes(v *elasticache.CacheCluster) resource.TestCheckFunc {
    96  	return func(s *terraform.State) error {
    97  		if v.NotificationConfiguration == nil {
    98  			return fmt.Errorf("Expected NotificationConfiguration for ElastiCache Cluster (%s)", *v.CacheClusterId)
    99  		}
   100  
   101  		if strings.ToLower(*v.NotificationConfiguration.TopicStatus) != "active" {
   102  			return fmt.Errorf("Expected NotificationConfiguration status to be 'active', got (%s)", *v.NotificationConfiguration.TopicStatus)
   103  		}
   104  
   105  		return nil
   106  	}
   107  }
   108  
   109  func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error {
   110  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
   111  
   112  	for _, rs := range s.RootModule().Resources {
   113  		if rs.Type != "aws_elasticache_cluster" {
   114  			continue
   115  		}
   116  		res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
   117  			CacheClusterId: aws.String(rs.Primary.ID),
   118  		})
   119  		if err != nil {
   120  			return err
   121  		}
   122  		if len(res.CacheClusters) > 0 {
   123  			return fmt.Errorf("still exist.")
   124  		}
   125  	}
   126  	return nil
   127  }
   128  
   129  func testAccCheckAWSElasticacheClusterExists(n string, v *elasticache.CacheCluster) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  		rs, ok := s.RootModule().Resources[n]
   132  		if !ok {
   133  			return fmt.Errorf("Not found: %s", n)
   134  		}
   135  
   136  		if rs.Primary.ID == "" {
   137  			return fmt.Errorf("No cache cluster ID is set")
   138  		}
   139  
   140  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
   141  		resp, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
   142  			CacheClusterId: aws.String(rs.Primary.ID),
   143  		})
   144  		if err != nil {
   145  			return fmt.Errorf("Elasticache error: %v", err)
   146  		}
   147  
   148  		for _, c := range resp.CacheClusters {
   149  			if *c.CacheClusterId == rs.Primary.ID {
   150  				*v = *c
   151  			}
   152  		}
   153  
   154  		return nil
   155  	}
   156  }
   157  
   158  func genRandInt() int {
   159  	return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000
   160  }
   161  
   162  var testAccAWSElasticacheClusterConfig = fmt.Sprintf(`
   163  provider "aws" {
   164  	region = "us-east-1"
   165  }
   166  resource "aws_security_group" "bar" {
   167      name = "tf-test-security-group-%03d"
   168      description = "tf-test-security-group-descr"
   169      ingress {
   170          from_port = -1
   171          to_port = -1
   172          protocol = "icmp"
   173          cidr_blocks = ["0.0.0.0/0"]
   174      }
   175  }
   176  
   177  resource "aws_elasticache_security_group" "bar" {
   178      name = "tf-test-security-group-%03d"
   179      description = "tf-test-security-group-descr"
   180      security_group_names = ["${aws_security_group.bar.name}"]
   181  }
   182  
   183  resource "aws_elasticache_cluster" "bar" {
   184      cluster_id = "tf-test-%03d"
   185      engine = "memcached"
   186      node_type = "cache.m1.small"
   187      num_cache_nodes = 1
   188      port = 11211
   189      parameter_group_name = "default.memcached1.4"
   190      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   191  }
   192  `, genRandInt(), genRandInt(), genRandInt())
   193  
   194  var testAccAWSElasticacheClusterConfig_snapshots = `
   195  provider "aws" {
   196  	region = "us-east-1"
   197  }
   198  resource "aws_security_group" "bar" {
   199      name = "tf-test-security-group-%03d"
   200      description = "tf-test-security-group-descr"
   201      ingress {
   202          from_port = -1
   203          to_port = -1
   204          protocol = "icmp"
   205          cidr_blocks = ["0.0.0.0/0"]
   206      }
   207  }
   208  
   209  resource "aws_elasticache_security_group" "bar" {
   210      name = "tf-test-security-group-%03d"
   211      description = "tf-test-security-group-descr"
   212      security_group_names = ["${aws_security_group.bar.name}"]
   213  }
   214  
   215  resource "aws_elasticache_cluster" "bar" {
   216      cluster_id = "tf-test-%03d"
   217      engine = "redis"
   218      node_type = "cache.m1.small"
   219      num_cache_nodes = 1
   220      port = 6379
   221    	parameter_group_name = "default.redis2.8"
   222      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   223      snapshot_window = "05:00-09:00"
   224      snapshot_retention_limit = 3
   225  }
   226  `
   227  
   228  var testAccAWSElasticacheClusterConfig_snapshotsUpdated = `
   229  provider "aws" {
   230  	region = "us-east-1"
   231  }
   232  resource "aws_security_group" "bar" {
   233      name = "tf-test-security-group-%03d"
   234      description = "tf-test-security-group-descr"
   235      ingress {
   236          from_port = -1
   237          to_port = -1
   238          protocol = "icmp"
   239          cidr_blocks = ["0.0.0.0/0"]
   240      }
   241  }
   242  
   243  resource "aws_elasticache_security_group" "bar" {
   244      name = "tf-test-security-group-%03d"
   245      description = "tf-test-security-group-descr"
   246      security_group_names = ["${aws_security_group.bar.name}"]
   247  }
   248  
   249  resource "aws_elasticache_cluster" "bar" {
   250      cluster_id = "tf-test-%03d"
   251      engine = "redis"
   252      node_type = "cache.m1.small"
   253      num_cache_nodes = 1
   254      port = 6379
   255    	parameter_group_name = "default.redis2.8"
   256      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   257      snapshot_window = "07:00-09:00"
   258      snapshot_retention_limit = 7
   259      apply_immediately = true
   260  }
   261  `
   262  
   263  var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(`
   264  resource "aws_vpc" "foo" {
   265      cidr_block = "192.168.0.0/16"
   266      tags {
   267              Name = "tf-test"
   268      }
   269  }
   270  
   271  resource "aws_subnet" "foo" {
   272      vpc_id = "${aws_vpc.foo.id}"
   273      cidr_block = "192.168.0.0/20"
   274      availability_zone = "us-west-2a"
   275      tags {
   276              Name = "tf-test"
   277      }
   278  }
   279  
   280  resource "aws_elasticache_subnet_group" "bar" {
   281      name = "tf-test-cache-subnet-%03d"
   282      description = "tf-test-cache-subnet-group-descr"
   283      subnet_ids = ["${aws_subnet.foo.id}"]
   284  }
   285  
   286  resource "aws_security_group" "bar" {
   287      name = "tf-test-security-group-%03d"
   288      description = "tf-test-security-group-descr"
   289      vpc_id = "${aws_vpc.foo.id}"
   290      ingress {
   291          from_port = -1
   292          to_port = -1
   293          protocol = "icmp"
   294          cidr_blocks = ["0.0.0.0/0"]
   295      }
   296  }
   297  
   298  resource "aws_elasticache_cluster" "bar" {
   299      // Including uppercase letters in this name to ensure
   300      // that we correctly handle the fact that the API
   301      // normalizes names to lowercase.
   302      cluster_id = "tf-TEST-%03d"
   303      node_type = "cache.m1.small"
   304      num_cache_nodes = 1
   305      engine = "redis"
   306      engine_version = "2.8.19"
   307      port = 6379
   308      subnet_group_name = "${aws_elasticache_subnet_group.bar.name}"
   309      security_group_ids = ["${aws_security_group.bar.id}"]
   310      parameter_group_name = "default.redis2.8"
   311      notification_topic_arn      = "${aws_sns_topic.topic_example.arn}"
   312  }
   313  
   314  resource "aws_sns_topic" "topic_example" {
   315    name = "tf-ecache-cluster-test"
   316  }
   317  `, genRandInt(), genRandInt(), genRandInt())