github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/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 testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc {
   143  	return func(s *terraform.State) error {
   144  		// Ensure we have enough information in state to look up in API
   145  		rs, ok := s.RootModule().Resources[name]
   146  		if !ok {
   147  			return fmt.Errorf("Not found: %s", name)
   148  		}
   149  
   150  		storageAccount := rs.Primary.Attributes["name"]
   151  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   152  
   153  		// Ensure resource group exists in API
   154  		conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   155  
   156  		resp, err := conn.GetProperties(resourceGroup, storageAccount)
   157  		if err != nil {
   158  			return fmt.Errorf("Bad: Get on storageServiceClient: %s", err)
   159  		}
   160  
   161  		if resp.StatusCode == http.StatusNotFound {
   162  			return fmt.Errorf("Bad: StorageAccount %q (resource group: %q) does not exist", name, resourceGroup)
   163  		}
   164  
   165  		return nil
   166  	}
   167  }
   168  
   169  func testCheckAzureRMStorageAccountDisappears(name string) resource.TestCheckFunc {
   170  	return func(s *terraform.State) error {
   171  		// Ensure we have enough information in state to look up in API
   172  		rs, ok := s.RootModule().Resources[name]
   173  		if !ok {
   174  			return fmt.Errorf("Not found: %s", name)
   175  		}
   176  
   177  		storageAccount := rs.Primary.Attributes["name"]
   178  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   179  
   180  		// Ensure resource group exists in API
   181  		conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   182  
   183  		_, err := conn.Delete(resourceGroup, storageAccount)
   184  		if err != nil {
   185  			return fmt.Errorf("Bad: Delete on storageServiceClient: %s", err)
   186  		}
   187  
   188  		return nil
   189  	}
   190  }
   191  
   192  func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error {
   193  	conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   194  
   195  	for _, rs := range s.RootModule().Resources {
   196  		if rs.Type != "azurerm_storage_account" {
   197  			continue
   198  		}
   199  
   200  		name := rs.Primary.Attributes["name"]
   201  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   202  
   203  		resp, err := conn.GetProperties(resourceGroup, name)
   204  		if err != nil {
   205  			return nil
   206  		}
   207  
   208  		if resp.StatusCode != http.StatusNotFound {
   209  			return fmt.Errorf("Storage Account still exists:\n%#v", resp.Properties)
   210  		}
   211  	}
   212  
   213  	return nil
   214  }
   215  
   216  var testAccAzureRMStorageAccount_basic = `
   217  resource "azurerm_resource_group" "testrg" {
   218      name = "testAccAzureRMSA-%d"
   219      location = "westus"
   220  }
   221  
   222  resource "azurerm_storage_account" "testsa" {
   223      name = "unlikely23exst2acct%s"
   224      resource_group_name = "${azurerm_resource_group.testrg.name}"
   225  
   226      location = "westus"
   227      account_type = "Standard_LRS"
   228  
   229      tags {
   230          environment = "production"
   231      }
   232  }`
   233  
   234  var testAccAzureRMStorageAccount_update = `
   235  resource "azurerm_resource_group" "testrg" {
   236      name = "testAccAzureRMSA-%d"
   237      location = "westus"
   238  }
   239  
   240  resource "azurerm_storage_account" "testsa" {
   241      name = "unlikely23exst2acct%s"
   242      resource_group_name = "${azurerm_resource_group.testrg.name}"
   243  
   244      location = "westus"
   245      account_type = "Standard_GRS"
   246  
   247      tags {
   248          environment = "staging"
   249      }
   250  }`
   251  
   252  var testAccAzureRMStorageAccount_blobEncryption = `
   253  resource "azurerm_resource_group" "testrg" {
   254      name = "testAccAzureRMSA-%d"
   255      location = "westus"
   256  }
   257  
   258  resource "azurerm_storage_account" "testsa" {
   259      name = "unlikely23exst2acct%s"
   260      resource_group_name = "${azurerm_resource_group.testrg.name}"
   261  
   262      location = "westus"
   263      account_type = "Standard_LRS"
   264      enable_blob_encryption = true
   265  
   266      tags {
   267          environment = "production"
   268      }
   269  }`
   270  
   271  var testAccAzureRMStorageAccount_blobEncryptionDisabled = `
   272  resource "azurerm_resource_group" "testrg" {
   273      name = "testAccAzureRMSA-%d"
   274      location = "westus"
   275  }
   276  
   277  resource "azurerm_storage_account" "testsa" {
   278      name = "unlikely23exst2acct%s"
   279      resource_group_name = "${azurerm_resource_group.testrg.name}"
   280  
   281      location = "westus"
   282      account_type = "Standard_LRS"
   283      enable_blob_encryption = false
   284  
   285      tags {
   286          environment = "production"
   287      }
   288  }`