github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azure/resource_azure_storage_container_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccAzureStorageContainer(t *testing.T) {
    13  	name := "azure_storage_container.foo"
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAzureStorageContainerDestroyed,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAzureStorageContainerConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAzureStorageContainerExists(name),
    24  					resource.TestCheckResourceAttr(name, "name", testAccStorageContainerName),
    25  					resource.TestCheckResourceAttr(name, "storage_service_name", testAccStorageServiceName),
    26  					resource.TestCheckResourceAttr(name, "container_access_type", "blob"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  
    32  	// because containers take a while to get deleted, sleep for one minute:
    33  	time.Sleep(3 * time.Minute)
    34  }
    35  
    36  func testAccCheckAzureStorageContainerExists(name string) resource.TestCheckFunc {
    37  	return func(s *terraform.State) error {
    38  		resource, ok := s.RootModule().Resources[name]
    39  		if !ok {
    40  			return fmt.Errorf("Azure Storage Container resource not found: %s", name)
    41  		}
    42  
    43  		if resource.Primary.ID == "" {
    44  			return fmt.Errorf("Azure Storage Container ID not set: %s", name)
    45  		}
    46  
    47  		azureClient := testAccProvider.Meta().(*Client)
    48  		blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName)
    49  		if err != nil {
    50  			return err
    51  		}
    52  
    53  		container := blobClient.GetContainerReference(resource.Primary.ID)
    54  		exists, err := container.Exists()
    55  		if err != nil {
    56  			return err
    57  		}
    58  		if !exists {
    59  			return fmt.Errorf("Azure Storage Container %s doesn't exist.", name)
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  func testAccCheckAzureStorageContainerDestroyed(s *terraform.State) error {
    67  	for _, resource := range s.RootModule().Resources {
    68  		if resource.Type != "azure_storage_container" {
    69  			continue
    70  		}
    71  
    72  		azureClient := testAccProvider.Meta().(*Client)
    73  		blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName)
    74  		if err != nil {
    75  			return err
    76  		}
    77  
    78  		container := blobClient.GetContainerReference(resource.Primary.ID)
    79  		exists, err := container.Exists()
    80  		if err != nil {
    81  			return err
    82  		}
    83  		if exists {
    84  			return fmt.Errorf("Azure Storage Container still exists.")
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  var testAccAzureStorageContainerConfig = fmt.Sprintf(`
    92  resource "azure_storage_container" "foo" {
    93  	name = "%s"
    94  	container_access_type = "blob"
    95      # NOTE: A pre-existing Storage Service is used here so as to avoid
    96      # the huge wait for creation of one.
    97  	storage_service_name = "%s"
    98  }
    99  `, testAccStorageContainerName, testAccStorageServiceName)