github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   124  		if err != nil {
   125  			return err
   126  		}
   127  
   128  		exists, err := blobClient.BlobExists(storageContainerName, name)
   129  		if err != nil {
   130  			return err
   131  		}
   132  
   133  		if !exists {
   134  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName)
   135  		}
   136  
   137  		return nil
   138  	}
   139  }
   140  
   141  func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error {
   142  	for _, rs := range s.RootModule().Resources {
   143  		if rs.Type != "azurerm_storage_blob" {
   144  			continue
   145  		}
   146  
   147  		name := rs.Primary.Attributes["name"]
   148  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   149  		storageContainerName := rs.Primary.Attributes["storage_container_name"]
   150  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   151  		if !hasResourceGroup {
   152  			return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
   153  		}
   154  
   155  		armClient := testAccProvider.Meta().(*ArmClient)
   156  		blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
   157  		if err != nil {
   158  			return nil
   159  		}
   160  
   161  		exists, err := blobClient.BlobExists(storageContainerName, name)
   162  		if err != nil {
   163  			return nil
   164  		}
   165  
   166  		if exists {
   167  			return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName)
   168  		}
   169  	}
   170  
   171  	return nil
   172  }
   173  
   174  var testAccAzureRMStorageBlob_basic = `
   175  resource "azurerm_resource_group" "test" {
   176      name = "acctestrg-%d"
   177      location = "westus"
   178  }
   179  
   180  resource "azurerm_storage_account" "test" {
   181      name = "acctestacc%s"
   182      resource_group_name = "${azurerm_resource_group.test.name}"
   183      location = "westus"
   184      account_type = "Standard_LRS"
   185  
   186      tags {
   187          environment = "staging"
   188      }
   189  }
   190  
   191  resource "azurerm_storage_container" "test" {
   192      name = "vhds"
   193      resource_group_name = "${azurerm_resource_group.test.name}"
   194      storage_account_name = "${azurerm_storage_account.test.name}"
   195      container_access_type = "private"
   196  }
   197  
   198  resource "azurerm_storage_blob" "test" {
   199      name = "herpderp1.vhd"
   200  
   201      resource_group_name = "${azurerm_resource_group.test.name}"
   202      storage_account_name = "${azurerm_storage_account.test.name}"
   203      storage_container_name = "${azurerm_storage_container.test.name}"
   204  
   205      type = "page"
   206      size = 5120
   207  }
   208  `