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