github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 TestAccAzureRMStorageBlob_disappears(t *testing.T) { 167 ri := acctest.RandInt() 168 rs := strings.ToLower(acctest.RandString(11)) 169 config := fmt.Sprintf(testAccAzureRMStorageBlob_basic, ri, rs) 170 171 resource.Test(t, resource.TestCase{ 172 PreCheck: func() { testAccPreCheck(t) }, 173 Providers: testAccProviders, 174 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 175 Steps: []resource.TestStep{ 176 resource.TestStep{ 177 Config: config, 178 Check: resource.ComposeTestCheckFunc( 179 testCheckAzureRMStorageBlobExists("azurerm_storage_blob.test"), 180 testCheckAzureRMStorageBlobDisappears("azurerm_storage_blob.test"), 181 ), 182 ExpectNonEmptyPlan: true, 183 }, 184 }, 185 }) 186 } 187 188 func TestAccAzureRMStorageBlobBlock_source(t *testing.T) { 189 ri := acctest.RandInt() 190 rs1 := strings.ToLower(acctest.RandString(11)) 191 sourceBlob, err := ioutil.TempFile("", "") 192 if err != nil { 193 t.Fatalf("Failed to create local source blob file") 194 } 195 196 _, err = io.CopyN(sourceBlob, rand.Reader, 25*1024*1024) 197 if err != nil { 198 t.Fatalf("Failed to write random test to source blob") 199 } 200 201 err = sourceBlob.Close() 202 if err != nil { 203 t.Fatalf("Failed to close source blob") 204 } 205 206 config := fmt.Sprintf(testAccAzureRMStorageBlobBlock_source, ri, rs1, sourceBlob.Name()) 207 208 resource.Test(t, resource.TestCase{ 209 PreCheck: func() { testAccPreCheck(t) }, 210 Providers: testAccProviders, 211 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 212 Steps: []resource.TestStep{ 213 resource.TestStep{ 214 Config: config, 215 Check: resource.ComposeTestCheckFunc( 216 testCheckAzureRMStorageBlobMatchesFile("azurerm_storage_blob.source", storage.BlobTypeBlock, sourceBlob.Name()), 217 ), 218 }, 219 }, 220 }) 221 } 222 223 func TestAccAzureRMStorageBlobPage_source(t *testing.T) { 224 ri := acctest.RandInt() 225 rs1 := strings.ToLower(acctest.RandString(11)) 226 sourceBlob, err := ioutil.TempFile("", "") 227 if err != nil { 228 t.Fatalf("Failed to create local source blob file") 229 } 230 231 err = sourceBlob.Truncate(25*1024*1024 + 512) 232 if err != nil { 233 t.Fatalf("Failed to truncate file to 25M") 234 } 235 236 for i := int64(0); i < 20; i = i + 2 { 237 randomBytes := make([]byte, 1*1024*1024) 238 _, err = rand.Read(randomBytes) 239 if err != nil { 240 t.Fatalf("Failed to read random bytes") 241 } 242 243 _, err = sourceBlob.WriteAt(randomBytes, i*1024*1024) 244 if err != nil { 245 t.Fatalf("Failed to write random bytes to file") 246 } 247 } 248 249 randomBytes := make([]byte, 5*1024*1024) 250 _, err = rand.Read(randomBytes) 251 if err != nil { 252 t.Fatalf("Failed to read random bytes") 253 } 254 255 _, err = sourceBlob.WriteAt(randomBytes, 20*1024*1024) 256 if err != nil { 257 t.Fatalf("Failed to write random bytes to file") 258 } 259 260 err = sourceBlob.Close() 261 if err != nil { 262 t.Fatalf("Failed to close source blob") 263 } 264 265 config := fmt.Sprintf(testAccAzureRMStorageBlobPage_source, ri, rs1, sourceBlob.Name()) 266 267 resource.Test(t, resource.TestCase{ 268 PreCheck: func() { testAccPreCheck(t) }, 269 Providers: testAccProviders, 270 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 271 Steps: []resource.TestStep{ 272 resource.TestStep{ 273 Config: config, 274 Check: resource.ComposeTestCheckFunc( 275 testCheckAzureRMStorageBlobMatchesFile("azurerm_storage_blob.source", storage.BlobTypePage, sourceBlob.Name()), 276 ), 277 }, 278 }, 279 }) 280 } 281 282 func TestAccAzureRMStorageBlob_source_uri(t *testing.T) { 283 ri := acctest.RandInt() 284 rs1 := strings.ToLower(acctest.RandString(11)) 285 sourceBlob, err := ioutil.TempFile("", "") 286 if err != nil { 287 t.Fatalf("Failed to create local source blob file") 288 } 289 290 _, err = io.CopyN(sourceBlob, rand.Reader, 25*1024*1024) 291 if err != nil { 292 t.Fatalf("Failed to write random test to source blob") 293 } 294 295 err = sourceBlob.Close() 296 if err != nil { 297 t.Fatalf("Failed to close source blob") 298 } 299 300 config := fmt.Sprintf(testAccAzureRMStorageBlob_source_uri, ri, rs1, sourceBlob.Name()) 301 302 resource.Test(t, resource.TestCase{ 303 PreCheck: func() { testAccPreCheck(t) }, 304 Providers: testAccProviders, 305 CheckDestroy: testCheckAzureRMStorageBlobDestroy, 306 Steps: []resource.TestStep{ 307 resource.TestStep{ 308 Config: config, 309 Check: resource.ComposeTestCheckFunc( 310 testCheckAzureRMStorageBlobMatchesFile("azurerm_storage_blob.destination", storage.BlobTypeBlock, sourceBlob.Name()), 311 ), 312 }, 313 }, 314 }) 315 } 316 317 func testCheckAzureRMStorageBlobExists(name string) resource.TestCheckFunc { 318 return func(s *terraform.State) error { 319 320 rs, ok := s.RootModule().Resources[name] 321 if !ok { 322 return fmt.Errorf("Not found: %s", name) 323 } 324 325 name := rs.Primary.Attributes["name"] 326 storageAccountName := rs.Primary.Attributes["storage_account_name"] 327 storageContainerName := rs.Primary.Attributes["storage_container_name"] 328 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 329 if !hasResourceGroup { 330 return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name) 331 } 332 333 armClient := testAccProvider.Meta().(*ArmClient) 334 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 335 if err != nil { 336 return err 337 } 338 if !accountExists { 339 return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName) 340 } 341 342 container := blobClient.GetContainerReference(storageContainerName) 343 blob := container.GetBlobReference(name) 344 exists, err := blob.Exists() 345 if err != nil { 346 return err 347 } 348 349 if !exists { 350 return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName) 351 } 352 353 return nil 354 } 355 } 356 357 func testCheckAzureRMStorageBlobDisappears(name string) resource.TestCheckFunc { 358 return func(s *terraform.State) error { 359 360 rs, ok := s.RootModule().Resources[name] 361 if !ok { 362 return fmt.Errorf("Not found: %s", name) 363 } 364 365 name := rs.Primary.Attributes["name"] 366 storageAccountName := rs.Primary.Attributes["storage_account_name"] 367 storageContainerName := rs.Primary.Attributes["storage_container_name"] 368 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 369 if !hasResourceGroup { 370 return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name) 371 } 372 373 armClient := testAccProvider.Meta().(*ArmClient) 374 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 375 if err != nil { 376 return err 377 } 378 if !accountExists { 379 return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName) 380 } 381 382 container := blobClient.GetContainerReference(storageContainerName) 383 blob := container.GetBlobReference(name) 384 options := &storage.DeleteBlobOptions{} 385 _, err = blob.DeleteIfExists(options) 386 if err != nil { 387 return err 388 } 389 390 return nil 391 } 392 } 393 394 func testCheckAzureRMStorageBlobMatchesFile(name string, kind storage.BlobType, filePath string) resource.TestCheckFunc { 395 return func(s *terraform.State) error { 396 397 rs, ok := s.RootModule().Resources[name] 398 if !ok { 399 return fmt.Errorf("Not found: %s", name) 400 } 401 402 name := rs.Primary.Attributes["name"] 403 storageAccountName := rs.Primary.Attributes["storage_account_name"] 404 storageContainerName := rs.Primary.Attributes["storage_container_name"] 405 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 406 if !hasResourceGroup { 407 return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name) 408 } 409 410 armClient := testAccProvider.Meta().(*ArmClient) 411 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 412 if err != nil { 413 return err 414 } 415 if !accountExists { 416 return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName) 417 } 418 419 containerReference := blobClient.GetContainerReference(storageContainerName) 420 blobReference := containerReference.GetBlobReference(name) 421 propertyOptions := &storage.GetBlobPropertiesOptions{} 422 err = blobReference.GetProperties(propertyOptions) 423 if err != nil { 424 return err 425 } 426 427 properties := blobReference.Properties 428 429 if properties.BlobType != kind { 430 return fmt.Errorf("Bad: blob type %q does not match expected type %q", properties.BlobType, kind) 431 } 432 433 getOptions := &storage.GetBlobOptions{} 434 blob, err := blobReference.Get(getOptions) 435 if err != nil { 436 return err 437 } 438 439 contents, err := ioutil.ReadAll(blob) 440 if err != nil { 441 return err 442 } 443 defer blob.Close() 444 445 expectedContents, err := ioutil.ReadFile(filePath) 446 if err != nil { 447 return err 448 } 449 450 if string(contents) != string(expectedContents) { 451 return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not match contents", name, storageContainerName) 452 } 453 454 return nil 455 } 456 } 457 458 func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error { 459 for _, rs := range s.RootModule().Resources { 460 if rs.Type != "azurerm_storage_blob" { 461 continue 462 } 463 464 name := rs.Primary.Attributes["name"] 465 storageAccountName := rs.Primary.Attributes["storage_account_name"] 466 storageContainerName := rs.Primary.Attributes["storage_container_name"] 467 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 468 if !hasResourceGroup { 469 return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name) 470 } 471 472 armClient := testAccProvider.Meta().(*ArmClient) 473 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 474 if err != nil { 475 return nil 476 } 477 if !accountExists { 478 return nil 479 } 480 481 container := blobClient.GetContainerReference(storageContainerName) 482 blob := container.GetBlobReference(name) 483 exists, err := blob.Exists() 484 if err != nil { 485 return nil 486 } 487 488 if exists { 489 return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName) 490 } 491 } 492 493 return nil 494 } 495 496 var testAccAzureRMStorageBlob_basic = ` 497 resource "azurerm_resource_group" "test" { 498 name = "acctestRG-%d" 499 location = "westus" 500 } 501 502 resource "azurerm_storage_account" "test" { 503 name = "acctestacc%s" 504 resource_group_name = "${azurerm_resource_group.test.name}" 505 location = "westus" 506 account_type = "Standard_LRS" 507 508 tags { 509 environment = "staging" 510 } 511 } 512 513 resource "azurerm_storage_container" "test" { 514 name = "vhds" 515 resource_group_name = "${azurerm_resource_group.test.name}" 516 storage_account_name = "${azurerm_storage_account.test.name}" 517 container_access_type = "private" 518 } 519 520 resource "azurerm_storage_blob" "test" { 521 name = "herpderp1.vhd" 522 523 resource_group_name = "${azurerm_resource_group.test.name}" 524 storage_account_name = "${azurerm_storage_account.test.name}" 525 storage_container_name = "${azurerm_storage_container.test.name}" 526 527 type = "page" 528 size = 5120 529 } 530 ` 531 532 var testAccAzureRMStorageBlobBlock_source = ` 533 resource "azurerm_resource_group" "test" { 534 name = "acctestRG-%d" 535 location = "westus" 536 } 537 538 resource "azurerm_storage_account" "source" { 539 name = "acctestacc%s" 540 resource_group_name = "${azurerm_resource_group.test.name}" 541 location = "westus" 542 account_type = "Standard_LRS" 543 544 tags { 545 environment = "staging" 546 } 547 } 548 549 resource "azurerm_storage_container" "source" { 550 name = "source" 551 resource_group_name = "${azurerm_resource_group.test.name}" 552 storage_account_name = "${azurerm_storage_account.source.name}" 553 container_access_type = "blob" 554 } 555 556 resource "azurerm_storage_blob" "source" { 557 name = "source.vhd" 558 559 resource_group_name = "${azurerm_resource_group.test.name}" 560 storage_account_name = "${azurerm_storage_account.source.name}" 561 storage_container_name = "${azurerm_storage_container.source.name}" 562 563 type = "block" 564 source = "%s" 565 parallelism = 4 566 attempts = 2 567 } 568 ` 569 570 var testAccAzureRMStorageBlobPage_source = ` 571 resource "azurerm_resource_group" "test" { 572 name = "acctestRG-%d" 573 location = "westus" 574 } 575 576 resource "azurerm_storage_account" "source" { 577 name = "acctestacc%s" 578 resource_group_name = "${azurerm_resource_group.test.name}" 579 location = "westus" 580 account_type = "Standard_LRS" 581 582 tags { 583 environment = "staging" 584 } 585 } 586 587 resource "azurerm_storage_container" "source" { 588 name = "source" 589 resource_group_name = "${azurerm_resource_group.test.name}" 590 storage_account_name = "${azurerm_storage_account.source.name}" 591 container_access_type = "blob" 592 } 593 594 resource "azurerm_storage_blob" "source" { 595 name = "source.vhd" 596 597 resource_group_name = "${azurerm_resource_group.test.name}" 598 storage_account_name = "${azurerm_storage_account.source.name}" 599 storage_container_name = "${azurerm_storage_container.source.name}" 600 601 type = "page" 602 source = "%s" 603 parallelism = 3 604 attempts = 3 605 } 606 ` 607 608 var testAccAzureRMStorageBlob_source_uri = ` 609 resource "azurerm_resource_group" "test" { 610 name = "acctestRG-%d" 611 location = "westus" 612 } 613 614 resource "azurerm_storage_account" "source" { 615 name = "acctestacc%s" 616 resource_group_name = "${azurerm_resource_group.test.name}" 617 location = "westus" 618 account_type = "Standard_LRS" 619 620 tags { 621 environment = "staging" 622 } 623 } 624 625 resource "azurerm_storage_container" "source" { 626 name = "source" 627 resource_group_name = "${azurerm_resource_group.test.name}" 628 storage_account_name = "${azurerm_storage_account.source.name}" 629 container_access_type = "blob" 630 } 631 632 resource "azurerm_storage_blob" "source" { 633 name = "source.vhd" 634 635 resource_group_name = "${azurerm_resource_group.test.name}" 636 storage_account_name = "${azurerm_storage_account.source.name}" 637 storage_container_name = "${azurerm_storage_container.source.name}" 638 639 type = "block" 640 source = "%s" 641 parallelism = 4 642 attempts = 2 643 } 644 645 resource "azurerm_storage_blob" "destination" { 646 name = "destination.vhd" 647 648 resource_group_name = "${azurerm_resource_group.test.name}" 649 storage_account_name = "${azurerm_storage_account.source.name}" 650 storage_container_name = "${azurerm_storage_container.source.name}" 651 652 source_uri = "${azurerm_storage_blob.source.url}" 653 } 654 `