github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 TestAccAzureRMStorageAccount_NonStandardCasing(t *testing.T) { 174 ri := acctest.RandInt() 175 rs := acctest.RandString(4) 176 preConfig := testAccAzureRMStorageAccountNonStandardCasing(ri, rs) 177 178 resource.Test(t, resource.TestCase{ 179 PreCheck: func() { testAccPreCheck(t) }, 180 Providers: testAccProviders, 181 CheckDestroy: testCheckAzureRMStorageAccountDestroy, 182 Steps: []resource.TestStep{ 183 resource.TestStep{ 184 Config: preConfig, 185 Check: resource.ComposeTestCheckFunc( 186 testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"), 187 ), 188 }, 189 190 resource.TestStep{ 191 Config: preConfig, 192 PlanOnly: true, 193 ExpectNonEmptyPlan: false, 194 }, 195 }, 196 }) 197 } 198 199 func testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc { 200 return func(s *terraform.State) error { 201 // Ensure we have enough information in state to look up in API 202 rs, ok := s.RootModule().Resources[name] 203 if !ok { 204 return fmt.Errorf("Not found: %s", name) 205 } 206 207 storageAccount := rs.Primary.Attributes["name"] 208 resourceGroup := rs.Primary.Attributes["resource_group_name"] 209 210 // Ensure resource group exists in API 211 conn := testAccProvider.Meta().(*ArmClient).storageServiceClient 212 213 resp, err := conn.GetProperties(resourceGroup, storageAccount) 214 if err != nil { 215 return fmt.Errorf("Bad: Get on storageServiceClient: %s", err) 216 } 217 218 if resp.StatusCode == http.StatusNotFound { 219 return fmt.Errorf("Bad: StorageAccount %q (resource group: %q) does not exist", name, resourceGroup) 220 } 221 222 return nil 223 } 224 } 225 226 func testCheckAzureRMStorageAccountDisappears(name string) resource.TestCheckFunc { 227 return func(s *terraform.State) error { 228 // Ensure we have enough information in state to look up in API 229 rs, ok := s.RootModule().Resources[name] 230 if !ok { 231 return fmt.Errorf("Not found: %s", name) 232 } 233 234 storageAccount := rs.Primary.Attributes["name"] 235 resourceGroup := rs.Primary.Attributes["resource_group_name"] 236 237 // Ensure resource group exists in API 238 conn := testAccProvider.Meta().(*ArmClient).storageServiceClient 239 240 _, err := conn.Delete(resourceGroup, storageAccount) 241 if err != nil { 242 return fmt.Errorf("Bad: Delete on storageServiceClient: %s", err) 243 } 244 245 return nil 246 } 247 } 248 249 func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error { 250 conn := testAccProvider.Meta().(*ArmClient).storageServiceClient 251 252 for _, rs := range s.RootModule().Resources { 253 if rs.Type != "azurerm_storage_account" { 254 continue 255 } 256 257 name := rs.Primary.Attributes["name"] 258 resourceGroup := rs.Primary.Attributes["resource_group_name"] 259 260 resp, err := conn.GetProperties(resourceGroup, name) 261 if err != nil { 262 return nil 263 } 264 265 if resp.StatusCode != http.StatusNotFound { 266 return fmt.Errorf("Storage Account still exists:\n%#v", resp.AccountProperties) 267 } 268 } 269 270 return nil 271 } 272 273 var testAccAzureRMStorageAccount_basic = ` 274 resource "azurerm_resource_group" "testrg" { 275 name = "testAccAzureRMSA-%d" 276 location = "westus" 277 } 278 279 resource "azurerm_storage_account" "testsa" { 280 name = "unlikely23exst2acct%s" 281 resource_group_name = "${azurerm_resource_group.testrg.name}" 282 283 location = "westus" 284 account_type = "Standard_LRS" 285 286 tags { 287 environment = "production" 288 } 289 }` 290 291 var testAccAzureRMStorageAccount_update = ` 292 resource "azurerm_resource_group" "testrg" { 293 name = "testAccAzureRMSA-%d" 294 location = "westus" 295 } 296 297 resource "azurerm_storage_account" "testsa" { 298 name = "unlikely23exst2acct%s" 299 resource_group_name = "${azurerm_resource_group.testrg.name}" 300 301 location = "westus" 302 account_type = "Standard_GRS" 303 304 tags { 305 environment = "staging" 306 } 307 }` 308 309 var testAccAzureRMStorageAccount_blobEncryption = ` 310 resource "azurerm_resource_group" "testrg" { 311 name = "testAccAzureRMSA-%d" 312 location = "westus" 313 } 314 315 resource "azurerm_storage_account" "testsa" { 316 name = "unlikely23exst2acct%s" 317 resource_group_name = "${azurerm_resource_group.testrg.name}" 318 319 location = "westus" 320 account_type = "Standard_LRS" 321 enable_blob_encryption = true 322 323 tags { 324 environment = "production" 325 } 326 }` 327 328 var testAccAzureRMStorageAccount_blobEncryptionDisabled = ` 329 resource "azurerm_resource_group" "testrg" { 330 name = "testAccAzureRMSA-%d" 331 location = "westus" 332 } 333 334 resource "azurerm_storage_account" "testsa" { 335 name = "unlikely23exst2acct%s" 336 resource_group_name = "${azurerm_resource_group.testrg.name}" 337 338 location = "westus" 339 account_type = "Standard_LRS" 340 enable_blob_encryption = false 341 342 tags { 343 environment = "production" 344 } 345 }` 346 347 // BlobStorage accounts are not available in WestUS 348 var testAccAzureRMStorageAccount_blobStorage = ` 349 resource "azurerm_resource_group" "testrg" { 350 name = "testAccAzureRMSA-%d" 351 location = "northeurope" 352 } 353 354 resource "azurerm_storage_account" "testsa" { 355 name = "unlikely23exst2acct%s" 356 resource_group_name = "${azurerm_resource_group.testrg.name}" 357 358 location = "northeurope" 359 account_kind = "BlobStorage" 360 account_type = "Standard_LRS" 361 362 tags { 363 environment = "production" 364 } 365 }` 366 367 var testAccAzureRMStorageAccount_blobStorageUpdate = ` 368 resource "azurerm_resource_group" "testrg" { 369 name = "testAccAzureRMSA-%d" 370 location = "northeurope" 371 } 372 373 resource "azurerm_storage_account" "testsa" { 374 name = "unlikely23exst2acct%s" 375 resource_group_name = "${azurerm_resource_group.testrg.name}" 376 377 location = "northeurope" 378 account_kind = "BlobStorage" 379 account_type = "Standard_LRS" 380 access_tier = "Cool" 381 382 tags { 383 environment = "production" 384 } 385 }` 386 387 func testAccAzureRMStorageAccountNonStandardCasing(ri int, rs string) string { 388 return fmt.Sprintf(` 389 resource "azurerm_resource_group" "testrg" { 390 name = "testAccAzureRMSA-%d" 391 location = "westus" 392 } 393 resource "azurerm_storage_account" "testsa" { 394 name = "unlikely23exst2acct%s" 395 resource_group_name = "${azurerm_resource_group.testrg.name}" 396 location = "westus" 397 account_type = "standard_LRS" 398 tags { 399 environment = "production" 400 } 401 }`, ri, rs) 402 }