github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestValidateArmStorageAccountType(t *testing.T) {
    13  	testCases := []struct {
    14  		input       string
    15  		shouldError bool
    16  	}{
    17  		{"standard_lrs", false},
    18  		{"invalid", true},
    19  	}
    20  
    21  	for _, test := range testCases {
    22  		_, es := validateArmStorageAccountType(test.input, "account_type")
    23  
    24  		if test.shouldError && len(es) == 0 {
    25  			t.Fatalf("Expected validating account_type %q to fail", test.input)
    26  		}
    27  	}
    28  }
    29  
    30  func TestValidateArmStorageAccountName(t *testing.T) {
    31  	testCases := []struct {
    32  		input       string
    33  		shouldError bool
    34  	}{
    35  		{"ab", true},
    36  		{"ABC", true},
    37  		{"abc", false},
    38  		{"123456789012345678901234", false},
    39  		{"1234567890123456789012345", true},
    40  		{"abc12345", false},
    41  	}
    42  
    43  	for _, test := range testCases {
    44  		_, es := validateArmStorageAccountName(test.input, "name")
    45  
    46  		if test.shouldError && len(es) == 0 {
    47  			t.Fatalf("Expected validating name %q to fail", test.input)
    48  		}
    49  	}
    50  }
    51  
    52  func TestAccAzureRMStorageAccount_basic(t *testing.T) {
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck:     func() { testAccPreCheck(t) },
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testCheckAzureRMStorageAccountDestroy,
    57  		Steps: []resource.TestStep{
    58  			resource.TestStep{
    59  				Config: testAccAzureRMStorageAccount_basic,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
    62  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_LRS"),
    63  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.#", "1"),
    64  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "production"),
    65  				),
    66  			},
    67  
    68  			resource.TestStep{
    69  				Config: testAccAzureRMStorageAccount_update,
    70  				Check: resource.ComposeTestCheckFunc(
    71  					testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
    72  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_GRS"),
    73  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.#", "1"),
    74  					resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "staging"),
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  func testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  		// Ensure we have enough information in state to look up in API
    84  		rs, ok := s.RootModule().Resources[name]
    85  		if !ok {
    86  			return fmt.Errorf("Not found: %s", name)
    87  		}
    88  
    89  		storageAccount := rs.Primary.Attributes["name"]
    90  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    91  
    92  		// Ensure resource group exists in API
    93  		conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
    94  
    95  		resp, err := conn.GetProperties(resourceGroup, storageAccount)
    96  		if err != nil {
    97  			return fmt.Errorf("Bad: Get on storageServiceClient: %s", err)
    98  		}
    99  
   100  		if resp.StatusCode == http.StatusNotFound {
   101  			return fmt.Errorf("Bad: StorageAccount %q (resource group: %q) does not exist", name, resourceGroup)
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error {
   109  	conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   110  
   111  	for _, rs := range s.RootModule().Resources {
   112  		if rs.Type != "azurerm_storage_account" {
   113  			continue
   114  		}
   115  
   116  		name := rs.Primary.Attributes["name"]
   117  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   118  
   119  		resp, err := conn.GetProperties(resourceGroup, name)
   120  		if err != nil {
   121  			return nil
   122  		}
   123  
   124  		if resp.StatusCode != http.StatusNotFound {
   125  			return fmt.Errorf("Storage Account still exists:\n%#v", resp.Properties)
   126  		}
   127  	}
   128  
   129  	return nil
   130  }
   131  
   132  var testAccAzureRMStorageAccount_basic = `
   133  resource "azurerm_resource_group" "testrg" {
   134      name = "testAccAzureRMStorageAccountBasic"
   135      location = "westus"
   136  }
   137  
   138  resource "azurerm_storage_account" "testsa" {
   139      name = "unlikely23exst2acct1435"
   140      resource_group_name = "${azurerm_resource_group.testrg.name}"
   141  
   142      location = "westus"
   143      account_type = "Standard_LRS"
   144  
   145      tags {
   146          environment = "production"
   147      }
   148  }`
   149  
   150  var testAccAzureRMStorageAccount_update = `
   151  resource "azurerm_resource_group" "testrg" {
   152      name = "testAccAzureRMStorageAccountBasic"
   153      location = "westus"
   154  }
   155  
   156  resource "azurerm_storage_account" "testsa" {
   157      name = "unlikely23exst2acct1435"
   158      resource_group_name = "${azurerm_resource_group.testrg.name}"
   159  
   160      location = "westus"
   161      account_type = "Standard_GRS"
   162  
   163      tags {
   164          environment = "staging"
   165      }
   166  }`