github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 TestAccAzureRMRedisCache_NonStandardCasing(t *testing.T) {
   189  	ri := acctest.RandInt()
   190  	config := testAccAzureRMRedisCacheNonStandardCasing(ri)
   191  
   192  	resource.Test(t, resource.TestCase{
   193  		PreCheck:     func() { testAccPreCheck(t) },
   194  		Providers:    testAccProviders,
   195  		CheckDestroy: testCheckAzureRMRedisCacheDestroy,
   196  		Steps: []resource.TestStep{
   197  			resource.TestStep{
   198  				Config: config,
   199  				Check: resource.ComposeTestCheckFunc(
   200  					testCheckAzureRMRedisCacheExists("azurerm_redis_cache.test"),
   201  				),
   202  			},
   203  
   204  			resource.TestStep{
   205  				Config:             config,
   206  				PlanOnly:           true,
   207  				ExpectNonEmptyPlan: false,
   208  			},
   209  		},
   210  	})
   211  }
   212  
   213  func testCheckAzureRMRedisCacheExists(name string) resource.TestCheckFunc {
   214  	return func(s *terraform.State) error {
   215  		// Ensure we have enough information in state to look up in API
   216  		rs, ok := s.RootModule().Resources[name]
   217  		if !ok {
   218  			return fmt.Errorf("Not found: %s", name)
   219  		}
   220  
   221  		redisName := rs.Primary.Attributes["name"]
   222  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   223  		if !hasResourceGroup {
   224  			return fmt.Errorf("Bad: no resource group found in state for Redis Instance: %s", redisName)
   225  		}
   226  
   227  		conn := testAccProvider.Meta().(*ArmClient).redisClient
   228  
   229  		resp, err := conn.Get(resourceGroup, redisName)
   230  		if err != nil {
   231  			return fmt.Errorf("Bad: Get on redisClient: %s", err)
   232  		}
   233  
   234  		if resp.StatusCode == http.StatusNotFound {
   235  			return fmt.Errorf("Bad: Redis Instance %q (resource group: %q) does not exist", redisName, resourceGroup)
   236  		}
   237  
   238  		return nil
   239  	}
   240  }
   241  
   242  func testCheckAzureRMRedisCacheDestroy(s *terraform.State) error {
   243  	conn := testAccProvider.Meta().(*ArmClient).redisClient
   244  
   245  	for _, rs := range s.RootModule().Resources {
   246  		if rs.Type != "azurerm_redis_cache" {
   247  			continue
   248  		}
   249  
   250  		name := rs.Primary.Attributes["name"]
   251  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   252  
   253  		resp, err := conn.Get(resourceGroup, name)
   254  
   255  		if err != nil {
   256  			return nil
   257  		}
   258  
   259  		if resp.StatusCode != http.StatusNotFound {
   260  			return fmt.Errorf("Redis Instance still exists:\n%#v", resp)
   261  		}
   262  	}
   263  
   264  	return nil
   265  }
   266  
   267  var testAccAzureRMRedisCache_basic = `
   268  resource "azurerm_resource_group" "test" {
   269      name = "acctestRG-%d"
   270      location = "West US"
   271  }
   272  
   273  resource "azurerm_redis_cache" "test" {
   274      name                = "acctestRedis-%d"
   275      location            = "${azurerm_resource_group.test.location}"
   276      resource_group_name = "${azurerm_resource_group.test.name}"
   277      capacity            = 1
   278      family              = "C"
   279      sku_name            = "Basic"
   280      enable_non_ssl_port = false
   281  
   282      redis_configuration {
   283        maxclients = "256"
   284      }
   285  }
   286  `
   287  
   288  var testAccAzureRMRedisCache_standard = `
   289  resource "azurerm_resource_group" "test" {
   290      name = "acctestRG-%d"
   291      location = "West US"
   292  }
   293  
   294  resource "azurerm_redis_cache" "test" {
   295      name                = "acctestRedis-%d"
   296      location            = "${azurerm_resource_group.test.location}"
   297      resource_group_name = "${azurerm_resource_group.test.name}"
   298      capacity            = 1
   299      family              = "C"
   300      sku_name            = "Standard"
   301      enable_non_ssl_port = false
   302      redis_configuration {
   303        maxclients = "256"
   304      }
   305  
   306      tags {
   307      	environment = "production"
   308      }
   309  }
   310  `
   311  
   312  var testAccAzureRMRedisCache_premium = `
   313  resource "azurerm_resource_group" "test" {
   314      name = "acctestRG-%d"
   315      location = "West US"
   316  }
   317  
   318  resource "azurerm_redis_cache" "test" {
   319      name                = "acctestRedis-%d"
   320      location            = "${azurerm_resource_group.test.location}"
   321      resource_group_name = "${azurerm_resource_group.test.name}"
   322      capacity            = 1
   323      family              = "P"
   324      sku_name            = "Premium"
   325      enable_non_ssl_port = false
   326      redis_configuration {
   327        maxclients         = "256",
   328        maxmemory_reserved = "2",
   329        maxmemory_delta    = "2"
   330        maxmemory_policy   = "allkeys-lru"
   331      }
   332  }
   333  `
   334  
   335  var testAccAzureRMRedisCache_premiumSharded = `
   336  resource "azurerm_resource_group" "test" {
   337      name = "acctestRG-%d"
   338      location = "West US"
   339  }
   340  
   341  resource "azurerm_redis_cache" "test" {
   342      name                = "acctestRedis-%d"
   343      location            = "${azurerm_resource_group.test.location}"
   344      resource_group_name = "${azurerm_resource_group.test.name}"
   345      capacity            = 1
   346      family              = "P"
   347      sku_name            = "Premium"
   348      enable_non_ssl_port = true
   349      shard_count         = 3
   350      redis_configuration {
   351        maxclients         = "256",
   352        maxmemory_reserved = "2",
   353        maxmemory_delta    = "2"
   354        maxmemory_policy   = "allkeys-lru"
   355      }
   356  }
   357  `
   358  
   359  func testAccAzureRMRedisCacheNonStandardCasing(ri int) string {
   360  	return fmt.Sprintf(`
   361  resource "azurerm_resource_group" "test" {
   362      name = "acctestRG-%d"
   363      location = "West US"
   364  }
   365  resource "azurerm_redis_cache" "test" {
   366      name                = "acctestRedis-%d"
   367      location            = "${azurerm_resource_group.test.location}"
   368      resource_group_name = "${azurerm_resource_group.test.name}"
   369      capacity            = 1
   370      family              = "c"
   371      sku_name            = "basic"
   372      enable_non_ssl_port = false
   373      redis_configuration {
   374        maxclients = "256"
   375      }
   376  }
   377  `, ri, ri)
   378  }