github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  		containerName := fmt.Sprintf("%s-%s", testAccStorageContainerName, typ)
    76  		container := blobClient.GetContainerReference(containerName)
    77  		blob := container.GetBlobReference(resource.Primary.ID)
    78  		exists, err := blob.Exists()
    79  		if err != nil {
    80  			return err
    81  		}
    82  		if !exists {
    83  			return fmt.Errorf("Azure Storage Blob %s doesn't exist.", name)
    84  		}
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  func testAccCheckAzureStorageBlobDeleted(typ string) resource.TestCheckFunc {
    91  	return func(s *terraform.State) error {
    92  		for _, resource := range s.RootModule().Resources {
    93  			if resource.Type != "azure_storage_blob" {
    94  				continue
    95  			}
    96  
    97  			azureClient := testAccProvider.Meta().(*Client)
    98  			blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName)
    99  			if err != nil {
   100  				return err
   101  			}
   102  
   103  			containerName := fmt.Sprintf("%s-%s", testAccStorageContainerName, typ)
   104  			container := blobClient.GetContainerReference(containerName)
   105  			blob := container.GetBlobReference(resource.Primary.ID)
   106  			exists, err := blob.Exists()
   107  			if err != nil {
   108  				return err
   109  			}
   110  			if exists {
   111  				return fmt.Errorf("Azure Storage Blob still exists.")
   112  			}
   113  		}
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  var testAccAzureStorageBlockBlobConfig = fmt.Sprintf(`
   120  resource "azure_storage_container" "foo" {
   121  	name = "%s-block"
   122  	container_access_type = "blob"
   123      # NOTE: A pre-existing Storage Service is used here so as to avoid
   124      # the huge wait for creation of one.
   125  	storage_service_name = "%s"
   126  }
   127  
   128  resource "azure_storage_blob" "foo" {
   129  	name = "tftesting-blob"
   130  	type = "BlockBlob"
   131      # NOTE: A pre-existing Storage Service is used here so as to avoid
   132      # the huge wait for creation of one.
   133  	storage_service_name = "${azure_storage_container.foo.storage_service_name}"
   134  	storage_container_name = "${azure_storage_container.foo.name}"
   135  }
   136  `, testAccStorageContainerName, testAccStorageServiceName)
   137  
   138  var testAccAzureStoragePageBlobConfig = fmt.Sprintf(`
   139  resource "azure_storage_container" "foo" {
   140  	name = "%s-page"
   141  	container_access_type = "blob"
   142      # NOTE: A pre-existing Storage Service is used here so as to avoid
   143      # the huge wait for creation of one.
   144  	storage_service_name = "%s"
   145  }
   146  
   147  resource "azure_storage_blob" "foo" {
   148  	name = "tftesting-blob"
   149  	type = "PageBlob"
   150      # NOTE: A pre-existing Storage Service is used here so as to avoid
   151      # the huge wait for creation of one.
   152  	storage_service_name = "${azure_storage_container.foo.storage_service_name}"
   153  	storage_container_name = "${azure_storage_container.foo.name}"
   154      # NOTE: must be a multiple of 512:
   155      size = 512
   156  }
   157  `, testAccStorageContainerName, testAccStorageServiceName)