github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		// Ensure we have enough information in state to look up in API
    90  		rs, ok := s.RootModule().Resources[name]
    91  		if !ok {
    92  			return fmt.Errorf("Not found: %s", name)
    93  		}
    94  
    95  		storageAccount := rs.Primary.Attributes["name"]
    96  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    97  
    98  		// Ensure resource group exists in API
    99  		conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   100  
   101  		resp, err := conn.GetProperties(resourceGroup, storageAccount)
   102  		if err != nil {
   103  			return fmt.Errorf("Bad: Get on storageServiceClient: %s", err)
   104  		}
   105  
   106  		if resp.StatusCode == http.StatusNotFound {
   107  			return fmt.Errorf("Bad: StorageAccount %q (resource group: %q) does not exist", name, resourceGroup)
   108  		}
   109  
   110  		return nil
   111  	}
   112  }
   113  
   114  func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error {
   115  	conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
   116  
   117  	for _, rs := range s.RootModule().Resources {
   118  		if rs.Type != "azurerm_storage_account" {
   119  			continue
   120  		}
   121  
   122  		name := rs.Primary.Attributes["name"]
   123  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   124  
   125  		resp, err := conn.GetProperties(resourceGroup, name)
   126  		if err != nil {
   127  			return nil
   128  		}
   129  
   130  		if resp.StatusCode != http.StatusNotFound {
   131  			return fmt.Errorf("Storage Account still exists:\n%#v", resp.Properties)
   132  		}
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  var testAccAzureRMStorageAccount_basic = `
   139  resource "azurerm_resource_group" "testrg" {
   140      name = "testAccAzureRMSA-%d"
   141      location = "westus"
   142  }
   143  
   144  resource "azurerm_storage_account" "testsa" {
   145      name = "unlikely23exst2acct%s"
   146      resource_group_name = "${azurerm_resource_group.testrg.name}"
   147  
   148      location = "westus"
   149      account_type = "Standard_LRS"
   150  
   151      tags {
   152          environment = "production"
   153      }
   154  }`
   155  
   156  var testAccAzureRMStorageAccount_update = `
   157  resource "azurerm_resource_group" "testrg" {
   158      name = "testAccAzureRMSA-%d"
   159      location = "westus"
   160  }
   161  
   162  resource "azurerm_storage_account" "testsa" {
   163      name = "unlikely23exst2acct%s"
   164      resource_group_name = "${azurerm_resource_group.testrg.name}"
   165  
   166      location = "westus"
   167      account_type = "Standard_GRS"
   168  
   169      tags {
   170          environment = "staging"
   171      }
   172  }`