github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_redis_cache_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMRedisCacheFamily_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "C",
    20  			ErrCount: 0,
    21  		},
    22  		{
    23  			Value:    "P",
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    "c",
    28  			ErrCount: 0,
    29  		},
    30  		{
    31  			Value:    "p",
    32  			ErrCount: 0,
    33  		},
    34  		{
    35  			Value:    "a",
    36  			ErrCount: 1,
    37  		},
    38  		{
    39  			Value:    "b",
    40  			ErrCount: 1,
    41  		},
    42  		{
    43  			Value:    "D",
    44  			ErrCount: 1,
    45  		},
    46  	}
    47  
    48  	for _, tc := range cases {
    49  		_, errors := validateRedisFamily(tc.Value, "azurerm_redis_cache")
    50  
    51  		if len(errors) != tc.ErrCount {
    52  			t.Fatalf("Expected the Azure RM Redis Cache Family to trigger a validation error")
    53  		}
    54  	}
    55  }
    56  
    57  func TestAccAzureRMRedisCacheMaxMemoryPolicy_validation(t *testing.T) {
    58  	cases := []struct {
    59  		Value    string
    60  		ErrCount int
    61  	}{
    62  		{Value: "noeviction", ErrCount: 0},
    63  		{Value: "allkeys-lru", ErrCount: 0},
    64  		{Value: "volatile-lru", ErrCount: 0},
    65  		{Value: "allkeys-random", ErrCount: 0},
    66  		{Value: "volatile-random", ErrCount: 0},
    67  		{Value: "volatile-ttl", ErrCount: 0},
    68  		{Value: "something-else", ErrCount: 1},
    69  	}
    70  
    71  	for _, tc := range cases {
    72  		_, errors := validateRedisMaxMemoryPolicy(tc.Value, "azurerm_redis_cache")
    73  
    74  		if len(errors) != tc.ErrCount {
    75  			t.Fatalf("Expected the Azure RM Redis Cache Max Memory Policy to trigger a validation error")
    76  		}
    77  	}
    78  }
    79  
    80  func TestAccAzureRMRedisCacheSku_validation(t *testing.T) {
    81  	cases := []struct {
    82  		Value    string
    83  		ErrCount int
    84  	}{
    85  		{
    86  			Value:    "Basic",
    87  			ErrCount: 0,
    88  		},
    89  		{
    90  			Value:    "Standard",
    91  			ErrCount: 0,
    92  		},
    93  		{
    94  			Value:    "Premium",
    95  			ErrCount: 0,
    96  		},
    97  		{
    98  			Value:    "Random",
    99  			ErrCount: 1,
   100  		},
   101  	}
   102  
   103  	for _, tc := range cases {
   104  		_, errors := validateRedisSku(tc.Value, "azurerm_redis_cache")
   105  
   106  		if len(errors) != tc.ErrCount {
   107  			t.Fatalf("Expected the Azure RM Redis Cache Sku to trigger a validation error")
   108  		}
   109  	}
   110  }
   111  
   112  func TestAccAzureRMRedisCache_basic(t *testing.T) {
   113  	ri := acctest.RandInt()
   114  	config := fmt.Sprintf(testAccAzureRMRedisCache_basic, ri, ri)
   115  
   116  	resource.Test(t, resource.TestCase{
   117  		PreCheck:     func() { testAccPreCheck(t) },
   118  		Providers:    testAccProviders,
   119  		CheckDestroy: testCheckAzureRMRedisCacheDestroy,
   120  		Steps: []resource.TestStep{
   121  			{
   122  				Config: config,
   123  				Check: resource.ComposeTestCheckFunc(
   124  					testCheckAzureRMRedisCacheExists("azurerm_redis_cache.test"),
   125  				),
   126  			},
   127  		},
   128  	})
   129  }
   130  
   131  func TestAccAzureRMRedisCache_standard(t *testing.T) {
   132  	ri := acctest.RandInt()
   133  	config := fmt.Sprintf(testAccAzureRMRedisCache_standard, ri, ri)
   134  
   135  	resource.Test(t, resource.TestCase{
   136  		PreCheck:     func() { testAccPreCheck(t) },
   137  		Providers:    testAccProviders,
   138  		CheckDestroy: testCheckAzureRMRedisCacheDestroy,
   139  		Steps: []resource.TestStep{
   140  			{
   141  				Config: config,
   142  				Check: resource.ComposeTestCheckFunc(
   143  					testCheckAzureRMRedisCacheExists("azurerm_redis_cache.test"),
   144  				),
   145  			},
   146  		},
   147  	})
   148  }
   149  
   150  func TestAccAzureRMRedisCache_premium(t *testing.T) {
   151  	ri := acctest.RandInt()
   152  	config := fmt.Sprintf(testAccAzureRMRedisCache_premium, ri, ri)
   153  
   154  	resource.Test(t, resource.TestCase{
   155  		PreCheck:     func() { testAccPreCheck(t) },
   156  		Providers:    testAccProviders,
   157  		CheckDestroy: testCheckAzureRMRedisCacheDestroy,
   158  		Steps: []resource.TestStep{
   159  			{
   160  				Config: config,
   161  				Check: resource.ComposeTestCheckFunc(
   162  					testCheckAzureRMRedisCacheExists("azurerm_redis_cache.test"),
   163  				),
   164  			},
   165  		},
   166  	})
   167  }
   168  
   169  func TestAccAzureRMRedisCache_premiumSharded(t *testing.T) {
   170  	ri := acctest.RandInt()
   171  	config := fmt.Sprintf(testAccAzureRMRedisCache_premiumSharded, ri, ri)
   172  
   173  	resource.Test(t, resource.TestCase{
   174  		PreCheck:     func() { testAccPreCheck(t) },
   175  		Providers:    testAccProviders,
   176  		CheckDestroy: testCheckAzureRMRedisCacheDestroy,
   177  		Steps: []resource.TestStep{
   178  			{
   179  				Config: config,
   180  				Check: resource.ComposeTestCheckFunc(
   181  					testCheckAzureRMRedisCacheExists("azurerm_redis_cache.test"),
   182  				),
   183  			},
   184  		},
   185  	})
   186  }
   187  
   188  func testCheckAzureRMRedisCacheExists(name string) resource.TestCheckFunc {
   189  	return func(s *terraform.State) error {
   190  		// Ensure we have enough information in state to look up in API
   191  		rs, ok := s.RootModule().Resources[name]
   192  		if !ok {
   193  			return fmt.Errorf("Not found: %s", name)
   194  		}
   195  
   196  		redisName := rs.Primary.Attributes["name"]
   197  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   198  		if !hasResourceGroup {
   199  			return fmt.Errorf("Bad: no resource group found in state for Redis Instance: %s", redisName)
   200  		}
   201  
   202  		conn := testAccProvider.Meta().(*ArmClient).redisClient
   203  
   204  		resp, err := conn.Get(resourceGroup, redisName)
   205  		if err != nil {
   206  			return fmt.Errorf("Bad: Get on redisClient: %s", err)
   207  		}
   208  
   209  		if resp.StatusCode == http.StatusNotFound {
   210  			return fmt.Errorf("Bad: Redis Instance %q (resource group: %q) does not exist", redisName, resourceGroup)
   211  		}
   212  
   213  		return nil
   214  	}
   215  }
   216  
   217  func testCheckAzureRMRedisCacheDestroy(s *terraform.State) error {
   218  	conn := testAccProvider.Meta().(*ArmClient).redisClient
   219  
   220  	for _, rs := range s.RootModule().Resources {
   221  		if rs.Type != "azurerm_redis_cache" {
   222  			continue
   223  		}
   224  
   225  		name := rs.Primary.Attributes["name"]
   226  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   227  
   228  		resp, err := conn.Get(resourceGroup, name)
   229  
   230  		if err != nil {
   231  			return nil
   232  		}
   233  
   234  		if resp.StatusCode != http.StatusNotFound {
   235  			return fmt.Errorf("Redis Instance still exists:\n%#v", resp)
   236  		}
   237  	}
   238  
   239  	return nil
   240  }
   241  
   242  var testAccAzureRMRedisCache_basic = `
   243  resource "azurerm_resource_group" "test" {
   244      name = "acctestRG-%d"
   245      location = "West US"
   246  }
   247  
   248  resource "azurerm_redis_cache" "test" {
   249      name                = "acctestRedis-%d"
   250      location            = "${azurerm_resource_group.test.location}"
   251      resource_group_name = "${azurerm_resource_group.test.name}"
   252      capacity            = 1
   253      family              = "C"
   254      sku_name            = "Basic"
   255      enable_non_ssl_port = false
   256  
   257      redis_configuration {
   258        maxclients = "256"
   259      }
   260  }
   261  `
   262  
   263  var testAccAzureRMRedisCache_standard = `
   264  resource "azurerm_resource_group" "test" {
   265      name = "acctestRG-%d"
   266      location = "West US"
   267  }
   268  
   269  resource "azurerm_redis_cache" "test" {
   270      name                = "acctestRedis-%d"
   271      location            = "${azurerm_resource_group.test.location}"
   272      resource_group_name = "${azurerm_resource_group.test.name}"
   273      capacity            = 1
   274      family              = "C"
   275      sku_name            = "Standard"
   276      enable_non_ssl_port = false
   277      redis_configuration {
   278        maxclients = "256"
   279      }
   280  
   281      tags {
   282      	environment = "production"
   283      }
   284  }
   285  `
   286  
   287  var testAccAzureRMRedisCache_premium = `
   288  resource "azurerm_resource_group" "test" {
   289      name = "acctestRG-%d"
   290      location = "West US"
   291  }
   292  
   293  resource "azurerm_redis_cache" "test" {
   294      name                = "acctestRedis-%d"
   295      location            = "${azurerm_resource_group.test.location}"
   296      resource_group_name = "${azurerm_resource_group.test.name}"
   297      capacity            = 1
   298      family              = "P"
   299      sku_name            = "Premium"
   300      enable_non_ssl_port = false
   301      redis_configuration {
   302        maxclients         = "256",
   303        maxmemory_reserved = "2",
   304        maxmemory_delta    = "2"
   305        maxmemory_policy   = "allkeys-lru"
   306      }
   307  }
   308  `
   309  
   310  var testAccAzureRMRedisCache_premiumSharded = `
   311  resource "azurerm_resource_group" "test" {
   312      name = "acctestRG-%d"
   313      location = "West US"
   314  }
   315  
   316  resource "azurerm_redis_cache" "test" {
   317      name                = "acctestRedis-%d"
   318      location            = "${azurerm_resource_group.test.location}"
   319      resource_group_name = "${azurerm_resource_group.test.name}"
   320      capacity            = 1
   321      family              = "P"
   322      sku_name            = "Premium"
   323      enable_non_ssl_port = true
   324      shard_count         = 3
   325      redis_configuration {
   326        maxclients         = "256",
   327        maxmemory_reserved = "2",
   328        maxmemory_delta    = "2"
   329        maxmemory_policy   = "allkeys-lru"
   330      }
   331  }
   332  `