github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azure/resource_azure_storage_blob_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccAzureStorageBlockBlob(t *testing.T) {
    12  	name := "azure_storage_blob.foo"
    13  
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckAzureStorageBlobDeleted("block"),
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccAzureStorageBlockBlobConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckAzureStorageBlobExists(name, "block"),
    23  					resource.TestCheckResourceAttr(name, "name", "tftesting-blob"),
    24  					resource.TestCheckResourceAttr(name, "type", "BlockBlob"),
    25  					resource.TestCheckResourceAttr(name, "storage_container_name",
    26  						fmt.Sprintf("%s-block", testAccStorageContainerName)),
    27  					resource.TestCheckResourceAttr(name, "storage_service_name", testAccStorageServiceName),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureStoragePageBlob(t *testing.T) {
    35  	name := "azure_storage_blob.foo"
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckAzureStorageBlobDeleted("page"),
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccAzureStoragePageBlobConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckAzureStorageBlobExists(name, "page"),
    46  					resource.TestCheckResourceAttr(name, "name", "tftesting-blob"),
    47  					resource.TestCheckResourceAttr(name, "type", "PageBlob"),
    48  					resource.TestCheckResourceAttr(name, "size", "512"),
    49  					resource.TestCheckResourceAttr(name, "storage_container_name",
    50  						fmt.Sprintf("%s-page", testAccStorageContainerName)),
    51  					resource.TestCheckResourceAttr(name, "storage_service_name", testAccStorageServiceName),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func testAccCheckAzureStorageBlobExists(name, typ string) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		resource, ok := s.RootModule().Resources[name]
    61  		if !ok {
    62  			return fmt.Errorf("Azure Storage Container resource not found: %s", name)
    63  		}
    64  
    65  		if resource.Primary.ID == "" {
    66  			return fmt.Errorf("Azure Storage Container ID not set: %s", name)
    67  		}
    68  
    69  		azureClient := testAccProvider.Meta().(*Client)
    70  		blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName)
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		exists, err := blobClient.BlobExists(fmt.Sprintf("%s-%s", testAccStorageContainerName, typ),
    76  			resource.Primary.ID)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		if !exists {
    81  			return fmt.Errorf("Azure Storage Blob %s doesn't exist.", name)
    82  		}
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  func testAccCheckAzureStorageBlobDeleted(typ string) resource.TestCheckFunc {
    89  	return func(s *terraform.State) error {
    90  		for _, resource := range s.RootModule().Resources {
    91  			if resource.Type != "azure_storage_blob" {
    92  				continue
    93  			}
    94  
    95  			azureClient := testAccProvider.Meta().(*Client)
    96  			blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName)
    97  			if err != nil {
    98  				return err
    99  			}
   100  
   101  			exists, err := blobClient.BlobExists(fmt.Sprintf("%s-%s", testAccStorageContainerName,
   102  				typ), resource.Primary.ID)
   103  			if err != nil {
   104  				return err
   105  			}
   106  			if exists {
   107  				return fmt.Errorf("Azure Storage Blob still exists.")
   108  			}
   109  		}
   110  
   111  		return nil
   112  	}
   113  }
   114  
   115  var testAccAzureStorageBlockBlobConfig = fmt.Sprintf(`
   116  resource "azure_storage_container" "foo" {
   117  	name = "%s-block"
   118  	container_access_type = "blob"
   119      # NOTE: A pre-existing Storage Service is used here so as to avoid
   120      # the huge wait for creation of one.
   121  	storage_service_name = "%s"
   122  }
   123  
   124  resource "azure_storage_blob" "foo" {
   125  	name = "tftesting-blob"
   126  	type = "BlockBlob"
   127      # NOTE: A pre-existing Storage Service is used here so as to avoid
   128      # the huge wait for creation of one.
   129  	storage_service_name = "${azure_storage_container.foo.storage_service_name}"
   130  	storage_container_name = "${azure_storage_container.foo.name}"
   131  }
   132  `, testAccStorageContainerName, testAccStorageServiceName)
   133  
   134  var testAccAzureStoragePageBlobConfig = fmt.Sprintf(`
   135  resource "azure_storage_container" "foo" {
   136  	name = "%s-page"
   137  	container_access_type = "blob"
   138      # NOTE: A pre-existing Storage Service is used here so as to avoid
   139      # the huge wait for creation of one.
   140  	storage_service_name = "%s"
   141  }
   142  
   143  resource "azure_storage_blob" "foo" {
   144  	name = "tftesting-blob"
   145  	type = "PageBlob"
   146      # NOTE: A pre-existing Storage Service is used here so as to avoid
   147      # the huge wait for creation of one.
   148  	storage_service_name = "${azure_storage_container.foo.storage_service_name}"
   149  	storage_container_name = "${azure_storage_container.foo.name}"
   150      # NOTE: must be a multiple of 512:
   151      size = 512
   152  }
   153  `, testAccStorageContainerName, testAccStorageServiceName)