github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_storage_account_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 TestValidateArmStorageAccountType(t *testing.T) {
    14  	testCases := []struct {
    15  		input       string
    16  		shouldError bool
    17  	}{
    18  		{"standard_lrs", false},
    19  		{"invalid", true},
    20  	}
    21  
    22  	for _, test := range testCases {
    23  		_, es := validateArmStorageAccountType(test.input, "account_type")
    24  
    25  		if test.shouldError && len(es) == 0 {
    26  			t.Fatalf("Expected validating account_type %q to fail", test.input)
    27  		}
    28  	}
    29  }
    30  
    31  func TestValidateArmStorageAccountName(t *testing.T) {
    32  	testCases := []struct {
    33  		input       string
    34  		shouldError bool
    35  	}{
    36  		{"ab", true},
    37  		{"ABC", true},
    38  		{"abc", false},
    39  		{"123456789012345678901234", false},
    40  		{"1234567890123456789012345", true},
    41  		{"abc12345", false},
    42  	}
    43  
    44  	for _, test := range testCases {
    45  		_, es := validateArmStorageAccountName(test.input, "name")
    46  
    47  		if test.shouldError && len(es) == 0 {
    48  			t.Fatalf("Expected validating name %q to fail", test.input)
    49  		}
    50  	}
    51  }
    52  
    53  func TestAccAzureRMStorageAccount_basic(t *testing.T) {
    54  	ri := acctest.RandInt()
    55  	rs := acctest.RandString(4)
    56  	preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_basic, ri, rs)
    57  	postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_update, ri, rs)
    58  
    59  	resource.Test(t, resource.TestCase{
    60  		PreCheck:     func() { testAccPreCheck(t) },
    61  		Providers:    testAccProviders,
    62  		CheckDestroy: testCheckAzureRMStorageAccountDestroy,
    63  		Steps: []resource.TestStep{
    64  			resource.TestStep{
    65  				Config: preConfig,
    66  				Check: resource.ComposeTestCheckFunc(
    67  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
    68  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_LRS"),
    69  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.%", "1"),
    70  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "production"),
    71  				),
    72  			},
    73  
    74  			resource.TestStep{
    75  				Config: postConfig,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
    78  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_GRS"),
    79  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.%", "1"),
    80  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "staging"),
    81  				),
    82  			},
    83  		},
    84  	})
    85  }
    86  
    87  func TestAccAzureRMStorageAccount_disappears(t *testing.T) {
    88  	ri := acctest.RandInt()
    89  	rs := acctest.RandString(4)
    90  	preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_basic, ri, rs)
    91  
    92  	resource.Test(t, resource.TestCase{
    93  		PreCheck:     func() { testAccPreCheck(t) },
    94  		Providers:    testAccProviders,
    95  		CheckDestroy: testCheckAzureRMStorageAccountDestroy,
    96  		Steps: []resource.TestStep{
    97  			resource.TestStep{
    98  				Config: preConfig,
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
   101  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_LRS"),
   102  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.%", "1"),
   103  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "production"),
   104  					testCheckAzureRMStorageAccountDisappears("azurerm_storage_account.testsa"),
   105  				),
   106  				ExpectNonEmptyPlan: true,
   107  			},
   108  		},
   109  	})
   110  }
   111  
   112  func TestAccAzureRMStorageAccount_blobEncryption(t *testing.T) {
   113  	ri := acctest.RandInt()
   114  	rs := acctest.RandString(4)
   115  	preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobEncryption, ri, rs)
   116  	postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobEncryptionDisabled, ri, rs)
   117  
   118  	resource.Test(t, resource.TestCase{
   119  		PreCheck:     func() { testAccPreCheck(t) },
   120  		Providers:    testAccProviders,
   121  		CheckDestroy: testCheckAzureRMStorageAccountDestroy,
   122  		Steps: []resource.TestStep{
   123  			resource.TestStep{
   124  				Config: preConfig,
   125  				Check: resource.ComposeTestCheckFunc(
   126  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
   127  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "enable_blob_encryption", "true"),
   128  				),
   129  			},
   130  
   131  			resource.TestStep{
   132  				Config: postConfig,
   133  				Check: resource.ComposeTestCheckFunc(
   134  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
   135  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "enable_blob_encryption", "false"),
   136  				),
   137  			},
   138  		},
   139  	})
   140  }
   141  
   142  func TestAccAzureRMStorageAccount_blobStorageWithUpdate(t *testing.T) {
   143  	ri := acctest.RandInt()
   144  	rs := acctest.RandString(4)
   145  	preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobStorage, ri, rs)
   146  	postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobStorageUpdate, ri, rs)
   147  
   148  	resource.Test(t, resource.TestCase{
   149  		PreCheck:     func() { testAccPreCheck(t) },
   150  		Providers:    testAccProviders,
   151  		CheckDestroy: testCheckAzureRMStorageAccountDestroy,
   152  		Steps: []resource.TestStep{
   153  			resource.TestStep{
   154  				Config: preConfig,
   155  				Check: resource.ComposeTestCheckFunc(
   156  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
   157  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_kind", "BlobStorage"),
   158  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "access_tier", "Hot"),
   159  				),
   160  			},
   161  
   162  			resource.TestStep{
   163  				Config: postConfig,
   164  				Check: resource.ComposeTestCheckFunc(
   165  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
   166  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "access_tier", "Cool"),
   167  				),
   168  			},
   169  		},
   170  	})
   171  }
   172  
   173  func testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc {
   174  	return func(s *terraform.State) error {
   175  		// Ensure we have enough information in state to look up in API
   176  		rs, ok := s.RootModule().Resources[name]
   177  		if !ok {
   178  			return fmt.Errorf("Not found: %s", name)
   179  		}
   180  
   181  		storageAccount := rs.Primary.Attributes["name"]
   182  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   183  
   184  		// Ensure resource group exists in API
   185  		conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   186  
   187  		resp, err := conn.GetProperties(resourceGroup, storageAccount)
   188  		if err != nil {
   189  			return fmt.Errorf("Bad: Get on storageServiceClient: %s", err)
   190  		}
   191  
   192  		if resp.StatusCode == http.StatusNotFound {
   193  			return fmt.Errorf("Bad: StorageAccount %q (resource group: %q) does not exist", name, resourceGroup)
   194  		}
   195  
   196  		return nil
   197  	}
   198  }
   199  
   200  func testCheckAzureRMStorageAccountDisappears(name string) resource.TestCheckFunc {
   201  	return func(s *terraform.State) error {
   202  		// Ensure we have enough information in state to look up in API
   203  		rs, ok := s.RootModule().Resources[name]
   204  		if !ok {
   205  			return fmt.Errorf("Not found: %s", name)
   206  		}
   207  
   208  		storageAccount := rs.Primary.Attributes["name"]
   209  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   210  
   211  		// Ensure resource group exists in API
   212  		conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   213  
   214  		_, err := conn.Delete(resourceGroup, storageAccount)
   215  		if err != nil {
   216  			return fmt.Errorf("Bad: Delete on storageServiceClient: %s", err)
   217  		}
   218  
   219  		return nil
   220  	}
   221  }
   222  
   223  func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error {
   224  	conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   225  
   226  	for _, rs := range s.RootModule().Resources {
   227  		if rs.Type != "azurerm_storage_account" {
   228  			continue
   229  		}
   230  
   231  		name := rs.Primary.Attributes["name"]
   232  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   233  
   234  		resp, err := conn.GetProperties(resourceGroup, name)
   235  		if err != nil {
   236  			return nil
   237  		}
   238  
   239  		if resp.StatusCode != http.StatusNotFound {
   240  			return fmt.Errorf("Storage Account still exists:\n%#v", resp.Properties)
   241  		}
   242  	}
   243  
   244  	return nil
   245  }
   246  
   247  var testAccAzureRMStorageAccount_basic = `
   248  resource "azurerm_resource_group" "testrg" {
   249      name = "testAccAzureRMSA-%d"
   250      location = "westus"
   251  }
   252  
   253  resource "azurerm_storage_account" "testsa" {
   254      name = "unlikely23exst2acct%s"
   255      resource_group_name = "${azurerm_resource_group.testrg.name}"
   256  
   257      location = "westus"
   258      account_type = "Standard_LRS"
   259  
   260      tags {
   261          environment = "production"
   262      }
   263  }`
   264  
   265  var testAccAzureRMStorageAccount_update = `
   266  resource "azurerm_resource_group" "testrg" {
   267      name = "testAccAzureRMSA-%d"
   268      location = "westus"
   269  }
   270  
   271  resource "azurerm_storage_account" "testsa" {
   272      name = "unlikely23exst2acct%s"
   273      resource_group_name = "${azurerm_resource_group.testrg.name}"
   274  
   275      location = "westus"
   276      account_type = "Standard_GRS"
   277  
   278      tags {
   279          environment = "staging"
   280      }
   281  }`
   282  
   283  var testAccAzureRMStorageAccount_blobEncryption = `
   284  resource "azurerm_resource_group" "testrg" {
   285      name = "testAccAzureRMSA-%d"
   286      location = "westus"
   287  }
   288  
   289  resource "azurerm_storage_account" "testsa" {
   290      name = "unlikely23exst2acct%s"
   291      resource_group_name = "${azurerm_resource_group.testrg.name}"
   292  
   293      location = "westus"
   294      account_type = "Standard_LRS"
   295      enable_blob_encryption = true
   296  
   297      tags {
   298          environment = "production"
   299      }
   300  }`
   301  
   302  var testAccAzureRMStorageAccount_blobEncryptionDisabled = `
   303  resource "azurerm_resource_group" "testrg" {
   304      name = "testAccAzureRMSA-%d"
   305      location = "westus"
   306  }
   307  
   308  resource "azurerm_storage_account" "testsa" {
   309      name = "unlikely23exst2acct%s"
   310      resource_group_name = "${azurerm_resource_group.testrg.name}"
   311  
   312      location = "westus"
   313      account_type = "Standard_LRS"
   314      enable_blob_encryption = false
   315  
   316      tags {
   317          environment = "production"
   318      }
   319  }`
   320  
   321  // BlobStorage accounts are not available in WestUS
   322  var testAccAzureRMStorageAccount_blobStorage = `
   323  resource "azurerm_resource_group" "testrg" {
   324      name = "testAccAzureRMSA-%d"
   325      location = "northeurope"
   326  }
   327  
   328  resource "azurerm_storage_account" "testsa" {
   329      name = "unlikely23exst2acct%s"
   330      resource_group_name = "${azurerm_resource_group.testrg.name}"
   331  
   332      location = "northeurope"
   333  	account_kind = "BlobStorage"
   334      account_type = "Standard_LRS"
   335  
   336      tags {
   337          environment = "production"
   338      }
   339  }`
   340  
   341  var testAccAzureRMStorageAccount_blobStorageUpdate = `
   342  resource "azurerm_resource_group" "testrg" {
   343      name = "testAccAzureRMSA-%d"
   344      location = "northeurope"
   345  }
   346  
   347  resource "azurerm_storage_account" "testsa" {
   348      name = "unlikely23exst2acct%s"
   349      resource_group_name = "${azurerm_resource_group.testrg.name}"
   350  
   351      location = "northeurope"
   352  	account_kind = "BlobStorage"
   353      account_type = "Standard_LRS"
   354  	access_tier = "Cool"
   355  
   356      tags {
   357          environment = "production"
   358      }
   359  }`