opentofu/builtin/providers/azurerm/resource_arm_storage_account_test.go
James Nugent 53c23511ef provider/azurerm: Add azurerm_storage_account
This is an unusual resource (so far) in that it cannot be created in one
call, and instead must be created and the modified to set some of the
parameters.

We use the pollIndefinitelyWhileNeeded function which will continue to
poll Azure RM operation monitoring endpoints until an error is reported
or the operation meets one of the given status codes. The function was
originally part of this feature but was separated out in order to
unblock other work.

Currently there is no support for the "custom_domain" section of the
storage account API. This was originally present and was later taken out
of the scope of the storage account resource in order that the following
workflow can be used:

1. Create storage account
2. Create DNS CNAME entry once the account name is known
3. Create custom domain mapping
2016-01-20 19:47:23 -05:00

167 lines
4.5 KiB
Go

package azurerm
import (
"fmt"
"net/http"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestValidateArmStorageAccountType(t *testing.T) {
testCases := []struct {
input string
shouldError bool
}{
{"standard_lrs", false},
{"invalid", true},
}
for _, test := range testCases {
_, es := validateArmStorageAccountType(test.input, "account_type")
if test.shouldError && len(es) == 0 {
t.Fatalf("Expected validating account_type %q to fail", test.input)
}
}
}
func TestValidateArmStorageAccountName(t *testing.T) {
testCases := []struct {
input string
shouldError bool
}{
{"ab", true},
{"ABC", true},
{"abc", false},
{"123456789012345678901234", false},
{"1234567890123456789012345", true},
{"abc12345", false},
}
for _, test := range testCases {
_, es := validateArmStorageAccountName(test.input, "name")
if test.shouldError && len(es) == 0 {
t.Fatalf("Expected validating name %q to fail", test.input)
}
}
}
func TestAccAzureRMStorageAccount_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMStorageAccount_basic,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_LRS"),
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.#", "1"),
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "production"),
),
},
resource.TestStep{
Config: testAccAzureRMStorageAccount_update,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_GRS"),
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.#", "1"),
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "staging"),
),
},
},
})
}
func testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
storageAccount := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
// Ensure resource group exists in API
conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
resp, err := conn.GetProperties(resourceGroup, storageAccount)
if err != nil {
return fmt.Errorf("Bad: Get on storageServiceClient: %s", err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: StorageAccount %q (resource group: %q) does not exist", name, resourceGroup)
}
return nil
}
}
func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_storage_account" {
continue
}
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
resp, err := conn.GetProperties(resourceGroup, name)
if err != nil {
return nil
}
if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Storage Account still exists:\n%#v", resp.Properties)
}
}
return nil
}
var testAccAzureRMStorageAccount_basic = `
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMStorageAccountBasic"
location = "westus"
}
resource "azurerm_storage_account" "testsa" {
name = "unlikely23exst2acct1435"
resource_group_name = "${azurerm_resource_group.testrg.name}"
location = "westus"
account_type = "Standard_LRS"
tags {
environment = "production"
}
}`
var testAccAzureRMStorageAccount_update = `
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMStorageAccountBasic"
location = "westus"
}
resource "azurerm_storage_account" "testsa" {
name = "unlikely23exst2acct1435"
resource_group_name = "${azurerm_resource_group.testrg.name}"
location = "westus"
account_type = "Standard_GRS"
tags {
environment = "staging"
}
}`