github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  		exists, err := blobClient.BlobExists(storageContainerName, name)
   343  		if err != nil {
   344  			return err
   345  		}
   346  
   347  		if !exists {
   348  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName)
   349  		}
   350  
   351  		return nil
   352  	}
   353  }
   354  
   355  func testCheckAzureRMStorageBlobDisappears(name string) resource.TestCheckFunc {
   356  	return func(s *terraform.State) error {
   357  
   358  		rs, ok := s.RootModule().Resources[name]
   359  		if !ok {
   360  			return fmt.Errorf("Not found: %s", name)
   361  		}
   362  
   363  		name := rs.Primary.Attributes["name"]
   364  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   365  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   366  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   367  		if !hasResourceGroup {
   368  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   369  		}
   370  
   371  		armClient := testAccProvider.Meta().(*ArmClient)
   372  		blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   373  		if err != nil {
   374  			return err
   375  		}
   376  		if !accountExists {
   377  			return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
   378  		}
   379  
   380  		_, err = blobClient.DeleteBlobIfExists(storageContainerName, name, map[string]string{})
   381  		if err != nil {
   382  			return err
   383  		}
   384  
   385  		return nil
   386  	}
   387  }
   388  
   389  func testCheckAzureRMStorageBlobMatchesFile(name string, kind storage.BlobType, filePath string) resource.TestCheckFunc {
   390  	return func(s *terraform.State) error {
   391  
   392  		rs, ok := s.RootModule().Resources[name]
   393  		if !ok {
   394  			return fmt.Errorf("Not found: %s", name)
   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 err
   409  		}
   410  		if !accountExists {
   411  			return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
   412  		}
   413  
   414  		properties, err := blobClient.GetBlobProperties(storageContainerName, name)
   415  		if err != nil {
   416  			return err
   417  		}
   418  
   419  		if properties.BlobType != kind {
   420  			return fmt.Errorf("Bad: blob type %q does not match expected type %q", properties.BlobType, kind)
   421  		}
   422  
   423  		blob, err := blobClient.GetBlob(storageContainerName, name)
   424  		if err != nil {
   425  			return err
   426  		}
   427  
   428  		contents, err := ioutil.ReadAll(blob)
   429  		if err != nil {
   430  			return err
   431  		}
   432  		defer blob.Close()
   433  
   434  		expectedContents, err := ioutil.ReadFile(filePath)
   435  		if err != nil {
   436  			return err
   437  		}
   438  
   439  		if string(contents) != string(expectedContents) {
   440  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not match contents", name, storageContainerName)
   441  		}
   442  
   443  		return nil
   444  	}
   445  }
   446  
   447  func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error {
   448  	for _, rs := range s.RootModule().Resources {
   449  		if rs.Type != "azurerm_storage_blob" {
   450  			continue
   451  		}
   452  
   453  		name := rs.Primary.Attributes["name"]
   454  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   455  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   456  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   457  		if !hasResourceGroup {
   458  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   459  		}
   460  
   461  		armClient := testAccProvider.Meta().(*ArmClient)
   462  		blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   463  		if err != nil {
   464  			return nil
   465  		}
   466  		if !accountExists {
   467  			return nil
   468  		}
   469  
   470  		exists, err := blobClient.BlobExists(storageContainerName, name)
   471  		if err != nil {
   472  			return nil
   473  		}
   474  
   475  		if exists {
   476  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName)
   477  		}
   478  	}
   479  
   480  	return nil
   481  }
   482  
   483  var testAccAzureRMStorageBlob_basic = `
   484  resource "azurerm_resource_group" "test" {
   485      name = "acctestRG-%d"
   486      location = "westus"
   487  }
   488  
   489  resource "azurerm_storage_account" "test" {
   490      name = "acctestacc%s"
   491      resource_group_name = "${azurerm_resource_group.test.name}"
   492      location = "westus"
   493      account_type = "Standard_LRS"
   494  
   495      tags {
   496          environment = "staging"
   497      }
   498  }
   499  
   500  resource "azurerm_storage_container" "test" {
   501      name = "vhds"
   502      resource_group_name = "${azurerm_resource_group.test.name}"
   503      storage_account_name = "${azurerm_storage_account.test.name}"
   504      container_access_type = "private"
   505  }
   506  
   507  resource "azurerm_storage_blob" "test" {
   508      name = "herpderp1.vhd"
   509  
   510      resource_group_name = "${azurerm_resource_group.test.name}"
   511      storage_account_name = "${azurerm_storage_account.test.name}"
   512      storage_container_name = "${azurerm_storage_container.test.name}"
   513  
   514      type = "page"
   515      size = 5120
   516  }
   517  `
   518  
   519  var testAccAzureRMStorageBlobBlock_source = `
   520  resource "azurerm_resource_group" "test" {
   521      name = "acctestRG-%d"
   522      location = "westus"
   523  }
   524  
   525  resource "azurerm_storage_account" "source" {
   526      name = "acctestacc%s"
   527      resource_group_name = "${azurerm_resource_group.test.name}"
   528      location = "westus"
   529      account_type = "Standard_LRS"
   530  
   531      tags {
   532          environment = "staging"
   533      }
   534  }
   535  
   536  resource "azurerm_storage_container" "source" {
   537      name = "source"
   538      resource_group_name = "${azurerm_resource_group.test.name}"
   539      storage_account_name = "${azurerm_storage_account.source.name}"
   540      container_access_type = "blob"
   541  }
   542  
   543  resource "azurerm_storage_blob" "source" {
   544      name = "source.vhd"
   545  
   546      resource_group_name = "${azurerm_resource_group.test.name}"
   547      storage_account_name = "${azurerm_storage_account.source.name}"
   548      storage_container_name = "${azurerm_storage_container.source.name}"
   549  
   550      type = "block"
   551  		source = "%s"
   552  		parallelism = 4
   553  		attempts = 2
   554  }
   555  `
   556  
   557  var testAccAzureRMStorageBlobPage_source = `
   558  resource "azurerm_resource_group" "test" {
   559      name = "acctestRG-%d"
   560      location = "westus"
   561  }
   562  
   563  resource "azurerm_storage_account" "source" {
   564      name = "acctestacc%s"
   565      resource_group_name = "${azurerm_resource_group.test.name}"
   566      location = "westus"
   567      account_type = "Standard_LRS"
   568  
   569      tags {
   570          environment = "staging"
   571      }
   572  }
   573  
   574  resource "azurerm_storage_container" "source" {
   575      name = "source"
   576      resource_group_name = "${azurerm_resource_group.test.name}"
   577      storage_account_name = "${azurerm_storage_account.source.name}"
   578      container_access_type = "blob"
   579  }
   580  
   581  resource "azurerm_storage_blob" "source" {
   582      name = "source.vhd"
   583  
   584      resource_group_name = "${azurerm_resource_group.test.name}"
   585      storage_account_name = "${azurerm_storage_account.source.name}"
   586      storage_container_name = "${azurerm_storage_container.source.name}"
   587  
   588      type = "page"
   589  		source = "%s"
   590  		parallelism = 3
   591  		attempts = 3
   592  }
   593  `
   594  
   595  var testAccAzureRMStorageBlob_source_uri = `
   596  resource "azurerm_resource_group" "test" {
   597      name = "acctestRG-%d"
   598      location = "westus"
   599  }
   600  
   601  resource "azurerm_storage_account" "source" {
   602      name = "acctestacc%s"
   603      resource_group_name = "${azurerm_resource_group.test.name}"
   604      location = "westus"
   605      account_type = "Standard_LRS"
   606  
   607      tags {
   608          environment = "staging"
   609      }
   610  }
   611  
   612  resource "azurerm_storage_container" "source" {
   613      name = "source"
   614      resource_group_name = "${azurerm_resource_group.test.name}"
   615      storage_account_name = "${azurerm_storage_account.source.name}"
   616      container_access_type = "blob"
   617  }
   618  
   619  resource "azurerm_storage_blob" "source" {
   620      name = "source.vhd"
   621  
   622      resource_group_name = "${azurerm_resource_group.test.name}"
   623      storage_account_name = "${azurerm_storage_account.source.name}"
   624      storage_container_name = "${azurerm_storage_container.source.name}"
   625  
   626      type = "block"
   627  		source = "%s"
   628  		parallelism = 4
   629  		attempts = 2
   630  }
   631  
   632  resource "azurerm_storage_blob" "destination" {
   633      name = "destination.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  		source_uri = "${azurerm_storage_blob.source.url}"
   640  }
   641  `