github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  		_, err = fileClient.DeleteShareIfExists(sS.Name)
   135  		if err != nil {
   136  			return err
   137  		}
   138  
   139  		return nil
   140  	}
   141  }
   142  
   143  func testCheckAzureRMStorageShareDestroy(s *terraform.State) error {
   144  	for _, rs := range s.RootModule().Resources {
   145  		if rs.Type != "azurerm_storage_share" {
   146  			continue
   147  		}
   148  
   149  		name := rs.Primary.Attributes["name"]
   150  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   151  		resourceGroupName, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   152  		if !hasResourceGroup {
   153  			return fmt.Errorf("Bad: no resource group found in state for share: %s", name)
   154  		}
   155  
   156  		armClient := testAccProvider.Meta().(*ArmClient)
   157  		fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   158  		if err != nil {
   159  			//If we can't get keys then the blob can't exist
   160  			return nil
   161  		}
   162  		if !accountExists {
   163  			return nil
   164  		}
   165  
   166  		shares, err := fileClient.ListShares(storage.ListSharesParameters{
   167  			Prefix:  name,
   168  			Timeout: 90,
   169  		})
   170  
   171  		if err != nil {
   172  			return nil
   173  		}
   174  
   175  		var found bool
   176  		for _, share := range shares.Shares {
   177  			if share.Name == name {
   178  				found = true
   179  			}
   180  		}
   181  
   182  		if found {
   183  			return fmt.Errorf("Bad: Share %q (storage account: %q) still exists", name, storageAccountName)
   184  		}
   185  	}
   186  
   187  	return nil
   188  }
   189  
   190  func TestValidateArmStorageShareName(t *testing.T) {
   191  	validNames := []string{
   192  		"valid-name",
   193  		"valid02-name",
   194  	}
   195  	for _, v := range validNames {
   196  		_, errors := validateArmStorageShareName(v, "name")
   197  		if len(errors) != 0 {
   198  			t.Fatalf("%q should be a valid Share Name: %q", v, errors)
   199  		}
   200  	}
   201  
   202  	invalidNames := []string{
   203  		"InvalidName1",
   204  		"-invalidname1",
   205  		"invalid_name",
   206  		"invalid!",
   207  		"double-hyphen--invalid",
   208  		"ww",
   209  		strings.Repeat("w", 65),
   210  	}
   211  	for _, v := range invalidNames {
   212  		_, errors := validateArmStorageShareName(v, "name")
   213  		if len(errors) == 0 {
   214  			t.Fatalf("%q should be an invalid Share Name", v)
   215  		}
   216  	}
   217  }
   218  
   219  var testAccAzureRMStorageShare_basic = `
   220  resource "azurerm_resource_group" "test" {
   221      name = "acctestrg-%d"
   222      location = "westus"
   223  }
   224  
   225  resource "azurerm_storage_account" "test" {
   226      name = "acctestacc%s"
   227      resource_group_name = "${azurerm_resource_group.test.name}"
   228      location = "westus"
   229      account_type = "Standard_LRS"
   230  
   231      tags {
   232          environment = "staging"
   233      }
   234  }
   235  
   236  resource "azurerm_storage_share" "test" {
   237      name = "testshare"
   238      resource_group_name = "${azurerm_resource_group.test.name}"
   239      storage_account_name = "${azurerm_storage_account.test.name}"
   240  }
   241  `