github.com/memsql/terraform@v0.7.0-rc2.0.20160706152241-21e2173e0a32/builtin/providers/azurerm/resource_arm_storage_blob_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"strings"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestResourceAzureRMStorageBlobType_validation(t *testing.T) {
    15  	cases := []struct {
    16  		Value    string
    17  		ErrCount int
    18  	}{
    19  		{
    20  			Value:    "unknown",
    21  			ErrCount: 1,
    22  		},
    23  		{
    24  			Value:    "page",
    25  			ErrCount: 0,
    26  		},
    27  		{
    28  			Value:    "blob",
    29  			ErrCount: 0,
    30  		},
    31  		{
    32  			Value:    "BLOB",
    33  			ErrCount: 0,
    34  		},
    35  		{
    36  			Value:    "Blob",
    37  			ErrCount: 0,
    38  		},
    39  	}
    40  
    41  	for _, tc := range cases {
    42  		_, errors := validateArmStorageBlobType(tc.Value, "azurerm_storage_blob")
    43  
    44  		if len(errors) != tc.ErrCount {
    45  			t.Fatalf("Expected the Azure RM Storage Blob type to trigger a validation error")
    46  		}
    47  	}
    48  }
    49  
    50  func TestResourceAzureRMStorageBlobSize_validation(t *testing.T) {
    51  	cases := []struct {
    52  		Value    int
    53  		ErrCount int
    54  	}{
    55  		{
    56  			Value:    511,
    57  			ErrCount: 1,
    58  		},
    59  		{
    60  			Value:    512,
    61  			ErrCount: 0,
    62  		},
    63  		{
    64  			Value:    1024,
    65  			ErrCount: 0,
    66  		},
    67  		{
    68  			Value:    2048,
    69  			ErrCount: 0,
    70  		},
    71  		{
    72  			Value:    5120,
    73  			ErrCount: 0,
    74  		},
    75  	}
    76  
    77  	for _, tc := range cases {
    78  		_, errors := validateArmStorageBlobSize(tc.Value, "azurerm_storage_blob")
    79  
    80  		if len(errors) != tc.ErrCount {
    81  			t.Fatalf("Expected the Azure RM Storage Blob size to trigger a validation error")
    82  		}
    83  	}
    84  }
    85  
    86  func TestAccAzureRMStorageBlob_basic(t *testing.T) {
    87  	ri := acctest.RandInt()
    88  	rs := strings.ToLower(acctest.RandString(11))
    89  	config := fmt.Sprintf(testAccAzureRMStorageBlob_basic, ri, rs)
    90  
    91  	resource.Test(t, resource.TestCase{
    92  		PreCheck:     func() { testAccPreCheck(t) },
    93  		Providers:    testAccProviders,
    94  		CheckDestroy: testCheckAzureRMStorageBlobDestroy,
    95  		Steps: []resource.TestStep{
    96  			resource.TestStep{
    97  				Config: config,
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testCheckAzureRMStorageBlobExists("azurerm_storage_blob.test"),
   100  				),
   101  			},
   102  		},
   103  	})
   104  }
   105  
   106  func testCheckAzureRMStorageBlobExists(name string) resource.TestCheckFunc {
   107  	return func(s *terraform.State) error {
   108  
   109  		rs, ok := s.RootModule().Resources[name]
   110  		if !ok {
   111  			return fmt.Errorf("Not found: %s", name)
   112  		}
   113  
   114  		name := rs.Primary.Attributes["name"]
   115  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   116  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   117  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   118  		if !hasResourceGroup {
   119  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   120  		}
   121  
   122  		armClient := testAccProvider.Meta().(*ArmClient)
   123  		blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   124  		if err != nil {
   125  			return err
   126  		}
   127  		if !accountExists {
   128  			return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
   129  		}
   130  
   131  		exists, err := blobClient.BlobExists(storageContainerName, name)
   132  		if err != nil {
   133  			return err
   134  		}
   135  
   136  		if !exists {
   137  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName)
   138  		}
   139  
   140  		return nil
   141  	}
   142  }
   143  
   144  func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error {
   145  	for _, rs := range s.RootModule().Resources {
   146  		if rs.Type != "azurerm_storage_blob" {
   147  			continue
   148  		}
   149  
   150  		name := rs.Primary.Attributes["name"]
   151  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   152  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   153  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   154  		if !hasResourceGroup {
   155  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   156  		}
   157  
   158  		armClient := testAccProvider.Meta().(*ArmClient)
   159  		blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   160  		if err != nil {
   161  			return nil
   162  		}
   163  		if !accountExists {
   164  			return nil
   165  		}
   166  
   167  		exists, err := blobClient.BlobExists(storageContainerName, name)
   168  		if err != nil {
   169  			return nil
   170  		}
   171  
   172  		if exists {
   173  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName)
   174  		}
   175  	}
   176  
   177  	return nil
   178  }
   179  
   180  var testAccAzureRMStorageBlob_basic = `
   181  resource "azurerm_resource_group" "test" {
   182      name = "acctestrg-%d"
   183      location = "westus"
   184  }
   185  
   186  resource "azurerm_storage_account" "test" {
   187      name = "acctestacc%s"
   188      resource_group_name = "${azurerm_resource_group.test.name}"
   189      location = "westus"
   190      account_type = "Standard_LRS"
   191  
   192      tags {
   193          environment = "staging"
   194      }
   195  }
   196  
   197  resource "azurerm_storage_container" "test" {
   198      name = "vhds"
   199      resource_group_name = "${azurerm_resource_group.test.name}"
   200      storage_account_name = "${azurerm_storage_account.test.name}"
   201      container_access_type = "private"
   202  }
   203  
   204  resource "azurerm_storage_blob" "test" {
   205      name = "herpderp1.vhd"
   206  
   207      resource_group_name = "${azurerm_resource_group.test.name}"
   208      storage_account_name = "${azurerm_storage_account.test.name}"
   209      storage_container_name = "${azurerm_storage_container.test.name}"
   210  
   211      type = "page"
   212      size = 5120
   213  }
   214  `