github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_storage_blob_test.go (about) 1 package azurerm 2 3 import ( 4 "crypto/rand" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "testing" 9 10 "strings" 11 12 "github.com/Azure/azure-sdk-for-go/storage" 13 "github.com/hashicorp/terraform/helper/acctest" 14 "github.com/hashicorp/terraform/helper/resource" 15 "github.com/hashicorp/terraform/terraform" 16 ) 17 18 func TestResourceAzureRMStorageBlobType_validation(t *testing.T) { 19 cases := []struct { 20 Value string 21 ErrCount int 22 }{ 23 { 24 Value: "unknown", 25 ErrCount: 1, 26 }, 27 { 28 Value: "page", 29 ErrCount: 0, 30 }, 31 { 32 Value: "block", 33 ErrCount: 0, 34 }, 35 { 36 Value: "BLOCK", 37 ErrCount: 0, 38 }, 39 { 40 Value: "Block", 41 ErrCount: 0, 42 }, 43 } 44 45 for _, tc := range cases { 46 _, errors := validateArmStorageBlobType(tc.Value, "azurerm_storage_blob") 47 48 if len(errors) != tc.ErrCount { 49 t.Fatalf("Expected the Azure RM Storage Blob type to trigger a validation error") 50 } 51 } 52 } 53 54 func TestResourceAzureRMStorageBlobSize_validation(t *testing.T) { 55 cases := []struct { 56 Value int 57 ErrCount int 58 }{ 59 { 60 Value: 511, 61 ErrCount: 1, 62 }, 63 { 64 Value: 512, 65 ErrCount: 0, 66 }, 67 { 68 Value: 1024, 69 ErrCount: 0, 70 }, 71 { 72 Value: 2048, 73 ErrCount: 0, 74 }, 75 { 76 Value: 5120, 77 ErrCount: 0, 78 }, 79 } 80 81 for _, tc := range cases { 82 _, errors := validateArmStorageBlobSize(tc.Value, "azurerm_storage_blob") 83 84 if len(errors) != tc.ErrCount { 85 t.Fatalf("Expected the Azure RM Storage Blob size to trigger a validation error") 86 } 87 } 88 } 89 90 func TestResourceAzureRMStorageBlobParallelism_validation(t *testing.T) { 91 cases := []struct { 92 Value int 93 ErrCount int 94 }{ 95 { 96 Value: 1, 97 ErrCount: 0, 98 }, 99 { 100 Value: 0, 101 ErrCount: 1, 102 }, 103 { 104 Value: -1, 105 ErrCount: 1, 106 }, 107 } 108 109 for _, tc := range cases { 110 _, errors := validateArmStorageBlobParallelism(tc.Value, "azurerm_storage_blob") 111 112 if len(errors) != tc.ErrCount { 113 t.Fatalf("Expected the Azure RM Storage Blob parallelism to trigger a validation error") 114 } 115 } 116 } 117 118 func TestResourceAzureRMStorageBlobAttempts_validation(t *testing.T) { 119 cases := []struct { 120 Value int 121 ErrCount int 122 }{ 123 { 124 Value: 1, 125 ErrCount: 0, 126 }, 127 { 128 Value: 0, 129 ErrCount: 1, 130 }, 131 { 132 Value: -1, 133 ErrCount: 1, 134 }, 135 } 136 137 for _, tc := range cases { 138 _, errors := validateArmStorageBlobAttempts(tc.Value, "azurerm_storage_blob") 139 140 if len(errors) != tc.ErrCount { 141 t.Fatalf("Expected the Azure RM Storage Blob attempts to trigger a validation error") 142 } 143 } 144 } 145 146 func TestAccAzureRMStorageBlob_basic(t *testing.T) { 147 ri := acctest.RandInt() 148 rs := strings.ToLower(acctest.RandString(11)) 149 config := fmt.Sprintf(testAccAzureRMStorageBlob_basic, ri, rs) 150 151 resource.Test(t, resource.TestCase{ 152 PreCheck: func() { testAccPreCheck(t) }, 153 Providers: testAccProviders, 154 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 155 Steps: []resource.TestStep{ 156 resource.TestStep{ 157 Config: config, 158 Check: resource.ComposeTestCheckFunc( 159 testCheckAzureRMStorageBlobExists("azurerm_storage_blob.test"), 160 ), 161 }, 162 }, 163 }) 164 } 165 166 func TestAccAzureRMStorageBlobBlock_source(t *testing.T) { 167 ri := acctest.RandInt() 168 rs1 := strings.ToLower(acctest.RandString(11)) 169 sourceBlob, err := ioutil.TempFile("", "") 170 if err != nil { 171 t.Fatalf("Failed to create local source blob file") 172 } 173 174 _, err = io.CopyN(sourceBlob, rand.Reader, 25*1024*1024) 175 if err != nil { 176 t.Fatalf("Failed to write random test to source blob") 177 } 178 179 err = sourceBlob.Close() 180 if err != nil { 181 t.Fatalf("Failed to close source blob") 182 } 183 184 config := fmt.Sprintf(testAccAzureRMStorageBlobBlock_source, ri, rs1, sourceBlob.Name()) 185 186 resource.Test(t, resource.TestCase{ 187 PreCheck: func() { testAccPreCheck(t) }, 188 Providers: testAccProviders, 189 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 190 Steps: []resource.TestStep{ 191 resource.TestStep{ 192 Config: config, 193 Check: resource.ComposeTestCheckFunc( 194 testCheckAzureRMStorageBlobMatchesFile("azurerm_storage_blob.source", storage.BlobTypeBlock, sourceBlob.Name()), 195 ), 196 }, 197 }, 198 }) 199 } 200 201 func TestAccAzureRMStorageBlobPage_source(t *testing.T) { 202 ri := acctest.RandInt() 203 rs1 := strings.ToLower(acctest.RandString(11)) 204 sourceBlob, err := ioutil.TempFile("", "") 205 if err != nil { 206 t.Fatalf("Failed to create local source blob file") 207 } 208 209 err = sourceBlob.Truncate(25*1024*1024 + 512) 210 if err != nil { 211 t.Fatalf("Failed to truncate file to 25M") 212 } 213 214 for i := int64(0); i < 20; i = i + 2 { 215 randomBytes := make([]byte, 1*1024*1024) 216 _, err = rand.Read(randomBytes) 217 if err != nil { 218 t.Fatalf("Failed to read random bytes") 219 } 220 221 _, err = sourceBlob.WriteAt(randomBytes, i*1024*1024) 222 if err != nil { 223 t.Fatalf("Failed to write random bytes to file") 224 } 225 } 226 227 randomBytes := make([]byte, 5*1024*1024) 228 _, err = rand.Read(randomBytes) 229 if err != nil { 230 t.Fatalf("Failed to read random bytes") 231 } 232 233 _, err = sourceBlob.WriteAt(randomBytes, 20*1024*1024) 234 if err != nil { 235 t.Fatalf("Failed to write random bytes to file") 236 } 237 238 err = sourceBlob.Close() 239 if err != nil { 240 t.Fatalf("Failed to close source blob") 241 } 242 243 config := fmt.Sprintf(testAccAzureRMStorageBlobPage_source, ri, rs1, sourceBlob.Name()) 244 245 resource.Test(t, resource.TestCase{ 246 PreCheck: func() { testAccPreCheck(t) }, 247 Providers: testAccProviders, 248 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 249 Steps: []resource.TestStep{ 250 resource.TestStep{ 251 Config: config, 252 Check: resource.ComposeTestCheckFunc( 253 testCheckAzureRMStorageBlobMatchesFile("azurerm_storage_blob.source", storage.BlobTypePage, sourceBlob.Name()), 254 ), 255 }, 256 }, 257 }) 258 } 259 260 func TestAccAzureRMStorageBlob_source_uri(t *testing.T) { 261 ri := acctest.RandInt() 262 rs1 := strings.ToLower(acctest.RandString(11)) 263 sourceBlob, err := ioutil.TempFile("", "") 264 if err != nil { 265 t.Fatalf("Failed to create local source blob file") 266 } 267 268 _, err = io.CopyN(sourceBlob, rand.Reader, 25*1024*1024) 269 if err != nil { 270 t.Fatalf("Failed to write random test to source blob") 271 } 272 273 err = sourceBlob.Close() 274 if err != nil { 275 t.Fatalf("Failed to close source blob") 276 } 277 278 config := fmt.Sprintf(testAccAzureRMStorageBlob_source_uri, ri, rs1, sourceBlob.Name()) 279 280 resource.Test(t, resource.TestCase{ 281 PreCheck: func() { testAccPreCheck(t) }, 282 Providers: testAccProviders, 283 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 284 Steps: []resource.TestStep{ 285 resource.TestStep{ 286 Config: config, 287 Check: resource.ComposeTestCheckFunc( 288 testCheckAzureRMStorageBlobMatchesFile("azurerm_storage_blob.destination", storage.BlobTypeBlock, sourceBlob.Name()), 289 ), 290 }, 291 }, 292 }) 293 } 294 295 func testCheckAzureRMStorageBlobExists(name string) resource.TestCheckFunc { 296 return func(s *terraform.State) error { 297 298 rs, ok := s.RootModule().Resources[name] 299 if !ok { 300 return fmt.Errorf("Not found: %s", name) 301 } 302 303 name := rs.Primary.Attributes["name"] 304 storageAccountName := rs.Primary.Attributes["storage_account_name"] 305 storageContainerName := rs.Primary.Attributes["storage_container_name"] 306 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 307 if !hasResourceGroup { 308 return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name) 309 } 310 311 armClient := testAccProvider.Meta().(*ArmClient) 312 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 313 if err != nil { 314 return err 315 } 316 if !accountExists { 317 return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName) 318 } 319 320 exists, err := blobClient.BlobExists(storageContainerName, name) 321 if err != nil { 322 return err 323 } 324 325 if !exists { 326 return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName) 327 } 328 329 return nil 330 } 331 } 332 333 func testCheckAzureRMStorageBlobMatchesFile(name string, kind storage.BlobType, filePath string) resource.TestCheckFunc { 334 return func(s *terraform.State) error { 335 336 rs, ok := s.RootModule().Resources[name] 337 if !ok { 338 return fmt.Errorf("Not found: %s", name) 339 } 340 341 name := rs.Primary.Attributes["name"] 342 storageAccountName := rs.Primary.Attributes["storage_account_name"] 343 storageContainerName := rs.Primary.Attributes["storage_container_name"] 344 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 345 if !hasResourceGroup { 346 return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name) 347 } 348 349 armClient := testAccProvider.Meta().(*ArmClient) 350 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 351 if err != nil { 352 return err 353 } 354 if !accountExists { 355 return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName) 356 } 357 358 properties, err := blobClient.GetBlobProperties(storageContainerName, name) 359 if err != nil { 360 return err 361 } 362 363 if properties.BlobType != kind { 364 return fmt.Errorf("Bad: blob type %q does not match expected type %q", properties.BlobType, kind) 365 } 366 367 blob, err := blobClient.GetBlob(storageContainerName, name) 368 if err != nil { 369 return err 370 } 371 372 contents, err := ioutil.ReadAll(blob) 373 if err != nil { 374 return err 375 } 376 defer blob.Close() 377 378 expectedContents, err := ioutil.ReadFile(filePath) 379 if err != nil { 380 return err 381 } 382 383 if string(contents) != string(expectedContents) { 384 return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not match contents", name, storageContainerName) 385 } 386 387 return nil 388 } 389 } 390 391 func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error { 392 for _, rs := range s.RootModule().Resources { 393 if rs.Type != "azurerm_storage_blob" { 394 continue 395 } 396 397 name := rs.Primary.Attributes["name"] 398 storageAccountName := rs.Primary.Attributes["storage_account_name"] 399 storageContainerName := rs.Primary.Attributes["storage_container_name"] 400 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 401 if !hasResourceGroup { 402 return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name) 403 } 404 405 armClient := testAccProvider.Meta().(*ArmClient) 406 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 407 if err != nil { 408 return nil 409 } 410 if !accountExists { 411 return nil 412 } 413 414 exists, err := blobClient.BlobExists(storageContainerName, name) 415 if err != nil { 416 return nil 417 } 418 419 if exists { 420 return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName) 421 } 422 } 423 424 return nil 425 } 426 427 var testAccAzureRMStorageBlob_basic = ` 428 resource "azurerm_resource_group" "test" { 429 name = "acctestrg-%d" 430 location = "westus" 431 } 432 433 resource "azurerm_storage_account" "test" { 434 name = "acctestacc%s" 435 resource_group_name = "${azurerm_resource_group.test.name}" 436 location = "westus" 437 account_type = "Standard_LRS" 438 439 tags { 440 environment = "staging" 441 } 442 } 443 444 resource "azurerm_storage_container" "test" { 445 name = "vhds" 446 resource_group_name = "${azurerm_resource_group.test.name}" 447 storage_account_name = "${azurerm_storage_account.test.name}" 448 container_access_type = "private" 449 } 450 451 resource "azurerm_storage_blob" "test" { 452 name = "herpderp1.vhd" 453 454 resource_group_name = "${azurerm_resource_group.test.name}" 455 storage_account_name = "${azurerm_storage_account.test.name}" 456 storage_container_name = "${azurerm_storage_container.test.name}" 457 458 type = "page" 459 size = 5120 460 } 461 ` 462 463 var testAccAzureRMStorageBlobBlock_source = ` 464 resource "azurerm_resource_group" "test" { 465 name = "acctestrg-%d" 466 location = "westus" 467 } 468 469 resource "azurerm_storage_account" "source" { 470 name = "acctestacc%s" 471 resource_group_name = "${azurerm_resource_group.test.name}" 472 location = "westus" 473 account_type = "Standard_LRS" 474 475 tags { 476 environment = "staging" 477 } 478 } 479 480 resource "azurerm_storage_container" "source" { 481 name = "source" 482 resource_group_name = "${azurerm_resource_group.test.name}" 483 storage_account_name = "${azurerm_storage_account.source.name}" 484 container_access_type = "blob" 485 } 486 487 resource "azurerm_storage_blob" "source" { 488 name = "source.vhd" 489 490 resource_group_name = "${azurerm_resource_group.test.name}" 491 storage_account_name = "${azurerm_storage_account.source.name}" 492 storage_container_name = "${azurerm_storage_container.source.name}" 493 494 type = "block" 495 source = "%s" 496 parallelism = 4 497 attempts = 2 498 } 499 ` 500 501 var testAccAzureRMStorageBlobPage_source = ` 502 resource "azurerm_resource_group" "test" { 503 name = "acctestrg-%d" 504 location = "westus" 505 } 506 507 resource "azurerm_storage_account" "source" { 508 name = "acctestacc%s" 509 resource_group_name = "${azurerm_resource_group.test.name}" 510 location = "westus" 511 account_type = "Standard_LRS" 512 513 tags { 514 environment = "staging" 515 } 516 } 517 518 resource "azurerm_storage_container" "source" { 519 name = "source" 520 resource_group_name = "${azurerm_resource_group.test.name}" 521 storage_account_name = "${azurerm_storage_account.source.name}" 522 container_access_type = "blob" 523 } 524 525 resource "azurerm_storage_blob" "source" { 526 name = "source.vhd" 527 528 resource_group_name = "${azurerm_resource_group.test.name}" 529 storage_account_name = "${azurerm_storage_account.source.name}" 530 storage_container_name = "${azurerm_storage_container.source.name}" 531 532 type = "page" 533 source = "%s" 534 parallelism = 3 535 attempts = 3 536 } 537 ` 538 539 var testAccAzureRMStorageBlob_source_uri = ` 540 resource "azurerm_resource_group" "test" { 541 name = "acctestrg-%d" 542 location = "westus" 543 } 544 545 resource "azurerm_storage_account" "source" { 546 name = "acctestacc%s" 547 resource_group_name = "${azurerm_resource_group.test.name}" 548 location = "westus" 549 account_type = "Standard_LRS" 550 551 tags { 552 environment = "staging" 553 } 554 } 555 556 resource "azurerm_storage_container" "source" { 557 name = "source" 558 resource_group_name = "${azurerm_resource_group.test.name}" 559 storage_account_name = "${azurerm_storage_account.source.name}" 560 container_access_type = "blob" 561 } 562 563 resource "azurerm_storage_blob" "source" { 564 name = "source.vhd" 565 566 resource_group_name = "${azurerm_resource_group.test.name}" 567 storage_account_name = "${azurerm_storage_account.source.name}" 568 storage_container_name = "${azurerm_storage_container.source.name}" 569 570 type = "block" 571 source = "%s" 572 parallelism = 4 573 attempts = 2 574 } 575 576 resource "azurerm_storage_blob" "destination" { 577 name = "destination.vhd" 578 579 resource_group_name = "${azurerm_resource_group.test.name}" 580 storage_account_name = "${azurerm_storage_account.source.name}" 581 storage_container_name = "${azurerm_storage_container.source.name}" 582 583 source_uri = "${azurerm_storage_blob.source.url}" 584 } 585 `