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