github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_storage_share_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/Azure/azure-sdk-for-go/storage"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAzureRMStorageShare_basic(t *testing.T) {
    16  	var sS storage.Share
    17  
    18  	ri := acctest.RandInt()
    19  	rs := strings.ToLower(acctest.RandString(11))
    20  	config := fmt.Sprintf(testAccAzureRMStorageShare_basic, ri, rs)
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testCheckAzureRMStorageShareDestroy,
    26  		Steps: []resource.TestStep{
    27  			{
    28  				Config: config,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testCheckAzureRMStorageShareExists("azurerm_storage_share.test", &sS),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAzureRMStorageShare_disappears(t *testing.T) {
    38  	var sS storage.Share
    39  
    40  	ri := acctest.RandInt()
    41  	rs := strings.ToLower(acctest.RandString(11))
    42  	config := fmt.Sprintf(testAccAzureRMStorageShare_basic, ri, rs)
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testCheckAzureRMStorageShareDestroy,
    48  		Steps: []resource.TestStep{
    49  			{
    50  				Config: config,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testCheckAzureRMStorageShareExists("azurerm_storage_share.test", &sS),
    53  					testAccARMStorageShareDisappears("azurerm_storage_share.test", &sS),
    54  				),
    55  				ExpectNonEmptyPlan: true,
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testCheckAzureRMStorageShareExists(name string, sS *storage.Share) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  
    64  		rs, ok := s.RootModule().Resources[name]
    65  		if !ok {
    66  			return fmt.Errorf("Not found: %s", name)
    67  		}
    68  
    69  		name := rs.Primary.Attributes["name"]
    70  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
    71  		resourceGroupName, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    72  		if !hasResourceGroup {
    73  			return fmt.Errorf("Bad: no resource group found in state for share: %s", name)
    74  		}
    75  
    76  		armClient := testAccProvider.Meta().(*ArmClient)
    77  		fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		if !accountExists {
    82  			return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
    83  		}
    84  
    85  		shares, err := fileClient.ListShares(storage.ListSharesParameters{
    86  			Prefix:  name,
    87  			Timeout: 90,
    88  		})
    89  
    90  		if len(shares.Shares) == 0 {
    91  			return fmt.Errorf("Bad: Share %q (storage account: %q) does not exist", name, storageAccountName)
    92  		}
    93  
    94  		var found bool
    95  		for _, share := range shares.Shares {
    96  			if share.Name == name {
    97  				found = true
    98  				*sS = share
    99  			}
   100  		}
   101  
   102  		if !found {
   103  			return fmt.Errorf("Bad: Share %q (storage account: %q) does not exist", name, storageAccountName)
   104  		}
   105  
   106  		return nil
   107  	}
   108  }
   109  
   110  func testAccARMStorageShareDisappears(name string, sS *storage.Share) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		rs, ok := s.RootModule().Resources[name]
   113  		if !ok {
   114  			return fmt.Errorf("Not found: %s", name)
   115  		}
   116  
   117  		armClient := testAccProvider.Meta().(*ArmClient)
   118  
   119  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   120  		resourceGroupName, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   121  		if !hasResourceGroup {
   122  			return fmt.Errorf("Bad: no resource group found in state for storage share: %s", sS.Name)
   123  		}
   124  
   125  		fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   126  		if err != nil {
   127  			return err
   128  		}
   129  		if !accountExists {
   130  			log.Printf("[INFO]Storage Account %q doesn't exist so the share won't exist", storageAccountName)
   131  			return nil
   132  		}
   133  
   134  		reference := fileClient.GetShareReference(sS.Name)
   135  		err = reference.Create()
   136  
   137  		if _, err = reference.DeleteIfExists(); err != nil {
   138  			return fmt.Errorf("Error deleting storage file %q: %s", name, err)
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  func testCheckAzureRMStorageShareDestroy(s *terraform.State) error {
   146  	for _, rs := range s.RootModule().Resources {
   147  		if rs.Type != "azurerm_storage_share" {
   148  			continue
   149  		}
   150  
   151  		name := rs.Primary.Attributes["name"]
   152  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   153  		resourceGroupName, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   154  		if !hasResourceGroup {
   155  			return fmt.Errorf("Bad: no resource group found in state for share: %s", name)
   156  		}
   157  
   158  		armClient := testAccProvider.Meta().(*ArmClient)
   159  		fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   160  		if err != nil {
   161  			//If we can't get keys then the blob can't exist
   162  			return nil
   163  		}
   164  		if !accountExists {
   165  			return nil
   166  		}
   167  
   168  		shares, err := fileClient.ListShares(storage.ListSharesParameters{
   169  			Prefix:  name,
   170  			Timeout: 90,
   171  		})
   172  
   173  		if err != nil {
   174  			return nil
   175  		}
   176  
   177  		var found bool
   178  		for _, share := range shares.Shares {
   179  			if share.Name == name {
   180  				found = true
   181  			}
   182  		}
   183  
   184  		if found {
   185  			return fmt.Errorf("Bad: Share %q (storage account: %q) still exists", name, storageAccountName)
   186  		}
   187  	}
   188  
   189  	return nil
   190  }
   191  
   192  func TestValidateArmStorageShareName(t *testing.T) {
   193  	validNames := []string{
   194  		"valid-name",
   195  		"valid02-name",
   196  	}
   197  	for _, v := range validNames {
   198  		_, errors := validateArmStorageShareName(v, "name")
   199  		if len(errors) != 0 {
   200  			t.Fatalf("%q should be a valid Share Name: %q", v, errors)
   201  		}
   202  	}
   203  
   204  	invalidNames := []string{
   205  		"InvalidName1",
   206  		"-invalidname1",
   207  		"invalid_name",
   208  		"invalid!",
   209  		"double-hyphen--invalid",
   210  		"ww",
   211  		strings.Repeat("w", 65),
   212  	}
   213  	for _, v := range invalidNames {
   214  		_, errors := validateArmStorageShareName(v, "name")
   215  		if len(errors) == 0 {
   216  			t.Fatalf("%q should be an invalid Share Name", v)
   217  		}
   218  	}
   219  }
   220  
   221  var testAccAzureRMStorageShare_basic = `
   222  resource "azurerm_resource_group" "test" {
   223      name = "acctestrg-%d"
   224      location = "westus"
   225  }
   226  
   227  resource "azurerm_storage_account" "test" {
   228      name = "acctestacc%s"
   229      resource_group_name = "${azurerm_resource_group.test.name}"
   230      location = "westus"
   231      account_type = "Standard_LRS"
   232  
   233      tags {
   234          environment = "staging"
   235      }
   236  }
   237  
   238  resource "azurerm_storage_share" "test" {
   239      name = "testshare"
   240      resource_group_name = "${azurerm_resource_group.test.name}"
   241      storage_account_name = "${azurerm_storage_account.test.name}"
   242  }
   243  `