github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  		exists, err := blobClient.ContainerExists(resource.Primary.ID)
    54  		if err != nil {
    55  			return err
    56  		}
    57  		if !exists {
    58  			return fmt.Errorf("Azure Storage Container %s doesn't exist.", name)
    59  		}
    60  
    61  		return nil
    62  	}
    63  }
    64  
    65  func testAccCheckAzureStorageContainerDestroyed(s *terraform.State) error {
    66  	for _, resource := range s.RootModule().Resources {
    67  		if resource.Type != "azure_storage_container" {
    68  			continue
    69  		}
    70  
    71  		azureClient := testAccProvider.Meta().(*Client)
    72  		blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName)
    73  		if err != nil {
    74  			return err
    75  		}
    76  
    77  		exists, err := blobClient.ContainerExists(resource.Primary.ID)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		if exists {
    82  			return fmt.Errorf("Azure Storage Container still exists.")
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  var testAccAzureStorageContainerConfig = fmt.Sprintf(`
    90  resource "azure_storage_container" "foo" {
    91  	name = "%s"
    92  	container_access_type = "blob"
    93      # NOTE: A pre-existing Storage Service is used here so as to avoid
    94      # the huge wait for creation of one.
    95  	storage_service_name = "%s"
    96  }
    97  `, testAccStorageContainerName, testAccStorageServiceName)