github.com/kwoods/terraform@v0.6.11-0.20160809170336-13497db7138e/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 testCheckAzureRMStorageBlobExists(name string) resource.TestCheckFunc {
   261  	return func(s *terraform.State) error {
   262  
   263  		rs, ok := s.RootModule().Resources[name]
   264  		if !ok {
   265  			return fmt.Errorf("Not found: %s", name)
   266  		}
   267  
   268  		name := rs.Primary.Attributes["name"]
   269  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   270  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   271  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   272  		if !hasResourceGroup {
   273  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   274  		}
   275  
   276  		armClient := testAccProvider.Meta().(*ArmClient)
   277  		blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   278  		if err != nil {
   279  			return err
   280  		}
   281  		if !accountExists {
   282  			return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
   283  		}
   284  
   285  		exists, err := blobClient.BlobExists(storageContainerName, name)
   286  		if err != nil {
   287  			return err
   288  		}
   289  
   290  		if !exists {
   291  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName)
   292  		}
   293  
   294  		return nil
   295  	}
   296  }
   297  
   298  func testCheckAzureRMStorageBlobMatchesFile(name string, kind storage.BlobType, filePath string) resource.TestCheckFunc {
   299  	return func(s *terraform.State) error {
   300  
   301  		rs, ok := s.RootModule().Resources[name]
   302  		if !ok {
   303  			return fmt.Errorf("Not found: %s", name)
   304  		}
   305  
   306  		name := rs.Primary.Attributes["name"]
   307  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   308  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   309  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   310  		if !hasResourceGroup {
   311  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   312  		}
   313  
   314  		armClient := testAccProvider.Meta().(*ArmClient)
   315  		blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   316  		if err != nil {
   317  			return err
   318  		}
   319  		if !accountExists {
   320  			return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
   321  		}
   322  
   323  		properties, err := blobClient.GetBlobProperties(storageContainerName, name)
   324  		if err != nil {
   325  			return err
   326  		}
   327  
   328  		if properties.BlobType != kind {
   329  			return fmt.Errorf("Bad: blob type %q does not match expected type %q", properties.BlobType, kind)
   330  		}
   331  
   332  		blob, err := blobClient.GetBlob(storageContainerName, name)
   333  		if err != nil {
   334  			return err
   335  		}
   336  
   337  		contents, err := ioutil.ReadAll(blob)
   338  		if err != nil {
   339  			return err
   340  		}
   341  		defer blob.Close()
   342  
   343  		expectedContents, err := ioutil.ReadFile(filePath)
   344  		if err != nil {
   345  			return err
   346  		}
   347  
   348  		if string(contents) != string(expectedContents) {
   349  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not match contents", name, storageContainerName)
   350  		}
   351  
   352  		return nil
   353  	}
   354  }
   355  
   356  func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error {
   357  	for _, rs := range s.RootModule().Resources {
   358  		if rs.Type != "azurerm_storage_blob" {
   359  			continue
   360  		}
   361  
   362  		name := rs.Primary.Attributes["name"]
   363  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   364  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   365  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   366  		if !hasResourceGroup {
   367  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   368  		}
   369  
   370  		armClient := testAccProvider.Meta().(*ArmClient)
   371  		blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   372  		if err != nil {
   373  			return nil
   374  		}
   375  		if !accountExists {
   376  			return nil
   377  		}
   378  
   379  		exists, err := blobClient.BlobExists(storageContainerName, name)
   380  		if err != nil {
   381  			return nil
   382  		}
   383  
   384  		if exists {
   385  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName)
   386  		}
   387  	}
   388  
   389  	return nil
   390  }
   391  
   392  var testAccAzureRMStorageBlob_basic = `
   393  resource "azurerm_resource_group" "test" {
   394      name = "acctestrg-%d"
   395      location = "westus"
   396  }
   397  
   398  resource "azurerm_storage_account" "test" {
   399      name = "acctestacc%s"
   400      resource_group_name = "${azurerm_resource_group.test.name}"
   401      location = "westus"
   402      account_type = "Standard_LRS"
   403  
   404      tags {
   405          environment = "staging"
   406      }
   407  }
   408  
   409  resource "azurerm_storage_container" "test" {
   410      name = "vhds"
   411      resource_group_name = "${azurerm_resource_group.test.name}"
   412      storage_account_name = "${azurerm_storage_account.test.name}"
   413      container_access_type = "private"
   414  }
   415  
   416  resource "azurerm_storage_blob" "test" {
   417      name = "herpderp1.vhd"
   418  
   419      resource_group_name = "${azurerm_resource_group.test.name}"
   420      storage_account_name = "${azurerm_storage_account.test.name}"
   421      storage_container_name = "${azurerm_storage_container.test.name}"
   422  
   423      type = "page"
   424      size = 5120
   425  }
   426  `
   427  
   428  var testAccAzureRMStorageBlobBlock_source = `
   429  resource "azurerm_resource_group" "test" {
   430      name = "acctestrg-%d"
   431      location = "westus"
   432  }
   433  
   434  resource "azurerm_storage_account" "source" {
   435      name = "acctestacc%s"
   436      resource_group_name = "${azurerm_resource_group.test.name}"
   437      location = "westus"
   438      account_type = "Standard_LRS"
   439  
   440      tags {
   441          environment = "staging"
   442      }
   443  }
   444  
   445  resource "azurerm_storage_container" "source" {
   446      name = "source"
   447      resource_group_name = "${azurerm_resource_group.test.name}"
   448      storage_account_name = "${azurerm_storage_account.source.name}"
   449      container_access_type = "blob"
   450  }
   451  
   452  resource "azurerm_storage_blob" "source" {
   453      name = "source.vhd"
   454  
   455      resource_group_name = "${azurerm_resource_group.test.name}"
   456      storage_account_name = "${azurerm_storage_account.source.name}"
   457      storage_container_name = "${azurerm_storage_container.source.name}"
   458  
   459      type = "block"
   460  		source = "%s"
   461  		parallelism = 4
   462  		attempts = 2
   463  }
   464  `
   465  
   466  var testAccAzureRMStorageBlobPage_source = `
   467  resource "azurerm_resource_group" "test" {
   468      name = "acctestrg-%d"
   469      location = "westus"
   470  }
   471  
   472  resource "azurerm_storage_account" "source" {
   473      name = "acctestacc%s"
   474      resource_group_name = "${azurerm_resource_group.test.name}"
   475      location = "westus"
   476      account_type = "Standard_LRS"
   477  
   478      tags {
   479          environment = "staging"
   480      }
   481  }
   482  
   483  resource "azurerm_storage_container" "source" {
   484      name = "source"
   485      resource_group_name = "${azurerm_resource_group.test.name}"
   486      storage_account_name = "${azurerm_storage_account.source.name}"
   487      container_access_type = "blob"
   488  }
   489  
   490  resource "azurerm_storage_blob" "source" {
   491      name = "source.vhd"
   492  
   493      resource_group_name = "${azurerm_resource_group.test.name}"
   494      storage_account_name = "${azurerm_storage_account.source.name}"
   495      storage_container_name = "${azurerm_storage_container.source.name}"
   496  
   497      type = "page"
   498  		source = "%s"
   499  		parallelism = 3
   500  		attempts = 3
   501  }
   502  `