github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_elasticache_cluster_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/elasticache"
    11  	"github.com/hashicorp/terraform/helper/acctest"
    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 := acctest.RandInt()
    40  	preConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshots, ri, ri, acctest.RandString(10))
    41  	postConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshotsUpdated, ri, ri, acctest.RandString(10))
    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_decreasingCacheNodes(t *testing.T) {
    76  	var ec elasticache.CacheCluster
    77  
    78  	ri := acctest.RandInt()
    79  	preConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfigDecreasingNodes, ri, ri, acctest.RandString(10))
    80  	postConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfigDecreasingNodes_update, ri, ri, acctest.RandString(10))
    81  
    82  	resource.Test(t, resource.TestCase{
    83  		PreCheck:     func() { testAccPreCheck(t) },
    84  		Providers:    testAccProviders,
    85  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
    86  		Steps: []resource.TestStep{
    87  			resource.TestStep{
    88  				Config: preConfig,
    89  				Check: resource.ComposeTestCheckFunc(
    90  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
    91  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
    92  					resource.TestCheckResourceAttr(
    93  						"aws_elasticache_cluster.bar", "num_cache_nodes", "3"),
    94  				),
    95  			},
    96  
    97  			resource.TestStep{
    98  				Config: postConfig,
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
   101  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
   102  					resource.TestCheckResourceAttr(
   103  						"aws_elasticache_cluster.bar", "num_cache_nodes", "1"),
   104  				),
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func TestAccAWSElasticacheCluster_vpc(t *testing.T) {
   111  	var csg elasticache.CacheSubnetGroup
   112  	var ec elasticache.CacheCluster
   113  	resource.Test(t, resource.TestCase{
   114  		PreCheck:     func() { testAccPreCheck(t) },
   115  		Providers:    testAccProviders,
   116  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
   117  		Steps: []resource.TestStep{
   118  			resource.TestStep{
   119  				Config: testAccAWSElasticacheClusterInVPCConfig,
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
   122  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
   123  					testAccCheckAWSElasticacheClusterAttributes(&ec),
   124  					resource.TestCheckResourceAttr(
   125  						"aws_elasticache_cluster.bar", "availability_zone", "us-west-2a"),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccAWSElasticacheCluster_multiAZInVpc(t *testing.T) {
   133  	var csg elasticache.CacheSubnetGroup
   134  	var ec elasticache.CacheCluster
   135  	resource.Test(t, resource.TestCase{
   136  		PreCheck:     func() { testAccPreCheck(t) },
   137  		Providers:    testAccProviders,
   138  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
   139  		Steps: []resource.TestStep{
   140  			resource.TestStep{
   141  				Config: testAccAWSElasticacheClusterMultiAZInVPCConfig,
   142  				Check: resource.ComposeTestCheckFunc(
   143  					testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
   144  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec),
   145  					resource.TestCheckResourceAttr(
   146  						"aws_elasticache_cluster.bar", "availability_zone", "Multiple"),
   147  				),
   148  			},
   149  		},
   150  	})
   151  }
   152  
   153  func testAccCheckAWSElasticacheClusterAttributes(v *elasticache.CacheCluster) resource.TestCheckFunc {
   154  	return func(s *terraform.State) error {
   155  		if v.NotificationConfiguration == nil {
   156  			return fmt.Errorf("Expected NotificationConfiguration for ElastiCache Cluster (%s)", *v.CacheClusterId)
   157  		}
   158  
   159  		if strings.ToLower(*v.NotificationConfiguration.TopicStatus) != "active" {
   160  			return fmt.Errorf("Expected NotificationConfiguration status to be 'active', got (%s)", *v.NotificationConfiguration.TopicStatus)
   161  		}
   162  
   163  		return nil
   164  	}
   165  }
   166  
   167  func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error {
   168  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
   169  
   170  	for _, rs := range s.RootModule().Resources {
   171  		if rs.Type != "aws_elasticache_cluster" {
   172  			continue
   173  		}
   174  		res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
   175  			CacheClusterId: aws.String(rs.Primary.ID),
   176  		})
   177  		if err != nil {
   178  			// Verify the error is what we want
   179  			if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "CacheClusterNotFound" {
   180  				continue
   181  			}
   182  			return err
   183  		}
   184  		if len(res.CacheClusters) > 0 {
   185  			return fmt.Errorf("still exist.")
   186  		}
   187  	}
   188  	return nil
   189  }
   190  
   191  func testAccCheckAWSElasticacheClusterExists(n string, v *elasticache.CacheCluster) resource.TestCheckFunc {
   192  	return func(s *terraform.State) error {
   193  		rs, ok := s.RootModule().Resources[n]
   194  		if !ok {
   195  			return fmt.Errorf("Not found: %s", n)
   196  		}
   197  
   198  		if rs.Primary.ID == "" {
   199  			return fmt.Errorf("No cache cluster ID is set")
   200  		}
   201  
   202  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
   203  		resp, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
   204  			CacheClusterId: aws.String(rs.Primary.ID),
   205  		})
   206  		if err != nil {
   207  			return fmt.Errorf("Elasticache error: %v", err)
   208  		}
   209  
   210  		for _, c := range resp.CacheClusters {
   211  			if *c.CacheClusterId == rs.Primary.ID {
   212  				*v = *c
   213  			}
   214  		}
   215  
   216  		return nil
   217  	}
   218  }
   219  
   220  var testAccAWSElasticacheClusterConfig = fmt.Sprintf(`
   221  provider "aws" {
   222  	region = "us-east-1"
   223  }
   224  resource "aws_security_group" "bar" {
   225      name = "tf-test-security-group-%03d"
   226      description = "tf-test-security-group-descr"
   227      ingress {
   228          from_port = -1
   229          to_port = -1
   230          protocol = "icmp"
   231          cidr_blocks = ["0.0.0.0/0"]
   232      }
   233  }
   234  
   235  resource "aws_elasticache_security_group" "bar" {
   236      name = "tf-test-security-group-%03d"
   237      description = "tf-test-security-group-descr"
   238      security_group_names = ["${aws_security_group.bar.name}"]
   239  }
   240  
   241  resource "aws_elasticache_cluster" "bar" {
   242      cluster_id = "tf-%s"
   243      engine = "memcached"
   244      node_type = "cache.m1.small"
   245      num_cache_nodes = 1
   246      port = 11211
   247      parameter_group_name = "default.memcached1.4"
   248      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   249  }
   250  `, acctest.RandInt(), acctest.RandInt(), acctest.RandString(10))
   251  
   252  var testAccAWSElasticacheClusterConfig_snapshots = `
   253  provider "aws" {
   254  	region = "us-east-1"
   255  }
   256  resource "aws_security_group" "bar" {
   257      name = "tf-test-security-group-%03d"
   258      description = "tf-test-security-group-descr"
   259      ingress {
   260          from_port = -1
   261          to_port = -1
   262          protocol = "icmp"
   263          cidr_blocks = ["0.0.0.0/0"]
   264      }
   265  }
   266  
   267  resource "aws_elasticache_security_group" "bar" {
   268      name = "tf-test-security-group-%03d"
   269      description = "tf-test-security-group-descr"
   270      security_group_names = ["${aws_security_group.bar.name}"]
   271  }
   272  
   273  resource "aws_elasticache_cluster" "bar" {
   274      cluster_id = "tf-%s"
   275      engine = "redis"
   276      node_type = "cache.m1.small"
   277      num_cache_nodes = 1
   278      port = 6379
   279    	parameter_group_name = "default.redis2.8"
   280      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   281      snapshot_window = "05:00-09:00"
   282      snapshot_retention_limit = 3
   283  }
   284  `
   285  
   286  var testAccAWSElasticacheClusterConfig_snapshotsUpdated = `
   287  provider "aws" {
   288  	region = "us-east-1"
   289  }
   290  resource "aws_security_group" "bar" {
   291      name = "tf-test-security-group-%03d"
   292      description = "tf-test-security-group-descr"
   293      ingress {
   294          from_port = -1
   295          to_port = -1
   296          protocol = "icmp"
   297          cidr_blocks = ["0.0.0.0/0"]
   298      }
   299  }
   300  
   301  resource "aws_elasticache_security_group" "bar" {
   302      name = "tf-test-security-group-%03d"
   303      description = "tf-test-security-group-descr"
   304      security_group_names = ["${aws_security_group.bar.name}"]
   305  }
   306  
   307  resource "aws_elasticache_cluster" "bar" {
   308      cluster_id = "tf-%s"
   309      engine = "redis"
   310      node_type = "cache.m1.small"
   311      num_cache_nodes = 1
   312      port = 6379
   313    	parameter_group_name = "default.redis2.8"
   314      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   315      snapshot_window = "07:00-09:00"
   316      snapshot_retention_limit = 7
   317      apply_immediately = true
   318  }
   319  `
   320  
   321  var testAccAWSElasticacheClusterConfigDecreasingNodes = `
   322  provider "aws" {
   323  	region = "us-east-1"
   324  }
   325  resource "aws_security_group" "bar" {
   326      name = "tf-test-security-group-%03d"
   327      description = "tf-test-security-group-descr"
   328      ingress {
   329          from_port = -1
   330          to_port = -1
   331          protocol = "icmp"
   332          cidr_blocks = ["0.0.0.0/0"]
   333      }
   334  }
   335  
   336  resource "aws_elasticache_security_group" "bar" {
   337      name = "tf-test-security-group-%03d"
   338      description = "tf-test-security-group-descr"
   339      security_group_names = ["${aws_security_group.bar.name}"]
   340  }
   341  
   342  resource "aws_elasticache_cluster" "bar" {
   343      cluster_id = "tf-%s"
   344      engine = "memcached"
   345      node_type = "cache.m1.small"
   346      num_cache_nodes = 3
   347      port = 11211
   348      parameter_group_name = "default.memcached1.4"
   349      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   350  }
   351  `
   352  
   353  var testAccAWSElasticacheClusterConfigDecreasingNodes_update = `
   354  provider "aws" {
   355  	region = "us-east-1"
   356  }
   357  resource "aws_security_group" "bar" {
   358      name = "tf-test-security-group-%03d"
   359      description = "tf-test-security-group-descr"
   360      ingress {
   361          from_port = -1
   362          to_port = -1
   363          protocol = "icmp"
   364          cidr_blocks = ["0.0.0.0/0"]
   365      }
   366  }
   367  
   368  resource "aws_elasticache_security_group" "bar" {
   369      name = "tf-test-security-group-%03d"
   370      description = "tf-test-security-group-descr"
   371      security_group_names = ["${aws_security_group.bar.name}"]
   372  }
   373  
   374  resource "aws_elasticache_cluster" "bar" {
   375      cluster_id = "tf-%s"
   376      engine = "memcached"
   377      node_type = "cache.m1.small"
   378      num_cache_nodes = 1
   379      port = 11211
   380      parameter_group_name = "default.memcached1.4"
   381      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   382      apply_immediately = true
   383  }
   384  `
   385  
   386  var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(`
   387  resource "aws_vpc" "foo" {
   388      cidr_block = "192.168.0.0/16"
   389      tags {
   390              Name = "tf-test"
   391      }
   392  }
   393  
   394  resource "aws_subnet" "foo" {
   395      vpc_id = "${aws_vpc.foo.id}"
   396      cidr_block = "192.168.0.0/20"
   397      availability_zone = "us-west-2a"
   398      tags {
   399              Name = "tf-test"
   400      }
   401  }
   402  
   403  resource "aws_elasticache_subnet_group" "bar" {
   404      name = "tf-test-cache-subnet-%03d"
   405      description = "tf-test-cache-subnet-group-descr"
   406      subnet_ids = ["${aws_subnet.foo.id}"]
   407  }
   408  
   409  resource "aws_security_group" "bar" {
   410      name = "tf-test-security-group-%03d"
   411      description = "tf-test-security-group-descr"
   412      vpc_id = "${aws_vpc.foo.id}"
   413      ingress {
   414          from_port = -1
   415          to_port = -1
   416          protocol = "icmp"
   417          cidr_blocks = ["0.0.0.0/0"]
   418      }
   419  }
   420  
   421  resource "aws_elasticache_cluster" "bar" {
   422      // Including uppercase letters in this name to ensure
   423      // that we correctly handle the fact that the API
   424      // normalizes names to lowercase.
   425      cluster_id = "tf-%s"
   426      node_type = "cache.m1.small"
   427      num_cache_nodes = 1
   428      engine = "redis"
   429      engine_version = "2.8.19"
   430      port = 6379
   431      subnet_group_name = "${aws_elasticache_subnet_group.bar.name}"
   432      security_group_ids = ["${aws_security_group.bar.id}"]
   433      parameter_group_name = "default.redis2.8"
   434      notification_topic_arn      = "${aws_sns_topic.topic_example.arn}"
   435      availability_zone = "us-west-2a"
   436  }
   437  
   438  resource "aws_sns_topic" "topic_example" {
   439    name = "tf-ecache-cluster-test"
   440  }
   441  `, acctest.RandInt(), acctest.RandInt(), acctest.RandString(10))
   442  
   443  var testAccAWSElasticacheClusterMultiAZInVPCConfig = fmt.Sprintf(`
   444  resource "aws_vpc" "foo" {
   445      cidr_block = "192.168.0.0/16"
   446      tags {
   447              Name = "tf-test"
   448      }
   449  }
   450  
   451  resource "aws_subnet" "foo" {
   452      vpc_id = "${aws_vpc.foo.id}"
   453      cidr_block = "192.168.0.0/20"
   454      availability_zone = "us-west-2a"
   455      tags {
   456              Name = "tf-test-%03d"
   457      }
   458  }
   459  
   460  resource "aws_subnet" "bar" {
   461      vpc_id = "${aws_vpc.foo.id}"
   462      cidr_block = "192.168.16.0/20"
   463      availability_zone = "us-west-2b"
   464      tags {
   465              Name = "tf-test-%03d"
   466      }
   467  }
   468  
   469  resource "aws_elasticache_subnet_group" "bar" {
   470      name = "tf-test-cache-subnet-%03d"
   471      description = "tf-test-cache-subnet-group-descr"
   472      subnet_ids = [
   473          "${aws_subnet.foo.id}",
   474          "${aws_subnet.bar.id}"
   475      ]
   476  }
   477  
   478  resource "aws_security_group" "bar" {
   479      name = "tf-test-security-group-%03d"
   480      description = "tf-test-security-group-descr"
   481      vpc_id = "${aws_vpc.foo.id}"
   482      ingress {
   483          from_port = -1
   484          to_port = -1
   485          protocol = "icmp"
   486          cidr_blocks = ["0.0.0.0/0"]
   487      }
   488  }
   489  
   490  resource "aws_elasticache_cluster" "bar" {
   491      cluster_id = "tf-%s"
   492      engine = "memcached"
   493      node_type = "cache.m1.small"
   494      num_cache_nodes = 2
   495      port = 11211
   496      subnet_group_name = "${aws_elasticache_subnet_group.bar.name}"
   497      security_group_ids = ["${aws_security_group.bar.id}"]
   498      parameter_group_name = "default.memcached1.4"
   499      az_mode = "cross-az"
   500      availability_zones = [
   501          "us-west-2a",
   502          "us-west-2b"
   503      ]
   504  }
   505  `, acctest.RandInt(), acctest.RandInt(), acctest.RandInt(), acctest.RandInt(), acctest.RandString(10))