github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_storage_table_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 TestAccAzureRMStorageTable_basic(t *testing.T) {
    16  	var table storage.AzureTable
    17  
    18  	ri := acctest.RandInt()
    19  	rs := strings.ToLower(acctest.RandString(11))
    20  	config := fmt.Sprintf(testAccAzureRMStorageTable_basic, ri, rs, ri)
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testCheckAzureRMStorageTableDestroy,
    26  		Steps: []resource.TestStep{
    27  			{
    28  				Config: config,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testCheckAzureRMStorageTableExists("azurerm_storage_table.test", &table),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAzureRMStorageTable_disappears(t *testing.T) {
    38  	var table storage.AzureTable
    39  
    40  	ri := acctest.RandInt()
    41  	rs := strings.ToLower(acctest.RandString(11))
    42  	config := fmt.Sprintf(testAccAzureRMStorageTable_basic, ri, rs, ri)
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testCheckAzureRMStorageTableDestroy,
    48  		Steps: []resource.TestStep{
    49  			{
    50  				Config: config,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testCheckAzureRMStorageTableExists("azurerm_storage_table.test", &table),
    53  					testAccARMStorageTableDisappears("azurerm_storage_table.test", &table),
    54  				),
    55  				ExpectNonEmptyPlan: true,
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testCheckAzureRMStorageTableExists(name string, t *storage.AzureTable) 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  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    72  		if !hasResourceGroup {
    73  			return fmt.Errorf("Bad: no resource group found in state for storage table: %s", name)
    74  		}
    75  
    76  		armClient := testAccProvider.Meta().(*ArmClient)
    77  		tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroup, 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  		tables, err := tableClient.QueryTables()
    86  
    87  		if len(tables) == 0 {
    88  			return fmt.Errorf("Bad: Storage Table %q (storage account: %q) does not exist", name, storageAccountName)
    89  		}
    90  
    91  		var found bool
    92  		for _, table := range tables {
    93  			if string(table) == name {
    94  				found = true
    95  				*t = table
    96  			}
    97  		}
    98  
    99  		if !found {
   100  			return fmt.Errorf("Bad: Storage Table %q (storage account: %q) does not exist", name, storageAccountName)
   101  		}
   102  
   103  		return nil
   104  	}
   105  }
   106  
   107  func testAccARMStorageTableDisappears(name string, t *storage.AzureTable) resource.TestCheckFunc {
   108  	return func(s *terraform.State) error {
   109  		rs, ok := s.RootModule().Resources[name]
   110  		if !ok {
   111  			return fmt.Errorf("Not found: %s", name)
   112  		}
   113  
   114  		armClient := testAccProvider.Meta().(*ArmClient)
   115  
   116  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   117  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   118  		if !hasResourceGroup {
   119  			return fmt.Errorf("Bad: no resource group found in state for storage table: %s", string(*t))
   120  		}
   121  
   122  		tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroup, storageAccountName)
   123  		if err != nil {
   124  			return err
   125  		}
   126  		if !accountExists {
   127  			log.Printf("[INFO]Storage Account %q doesn't exist so the table won't exist", storageAccountName)
   128  			return nil
   129  		}
   130  
   131  		table := storage.AzureTable(string(*t))
   132  		err = tableClient.DeleteTable(table)
   133  		if err != nil {
   134  			return err
   135  		}
   136  
   137  		return nil
   138  	}
   139  }
   140  
   141  func testCheckAzureRMStorageTableDestroy(s *terraform.State) error {
   142  	for _, rs := range s.RootModule().Resources {
   143  		if rs.Type != "azurerm_storage_table" {
   144  			continue
   145  		}
   146  
   147  		name := rs.Primary.Attributes["name"]
   148  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   149  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   150  		if !hasResourceGroup {
   151  			return fmt.Errorf("Bad: no resource group found in state for storage table: %s", name)
   152  		}
   153  
   154  		armClient := testAccProvider.Meta().(*ArmClient)
   155  		tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroup, storageAccountName)
   156  		if err != nil {
   157  			//If we can't get keys then the table can't exist
   158  			return nil
   159  		}
   160  		if !accountExists {
   161  			return nil
   162  		}
   163  
   164  		tables, err := tableClient.QueryTables()
   165  
   166  		if err != nil {
   167  			return nil
   168  		}
   169  
   170  		var found bool
   171  		for _, table := range tables {
   172  			if string(table) == name {
   173  				found = true
   174  			}
   175  		}
   176  
   177  		if found {
   178  			return fmt.Errorf("Bad: Storage Table %q (storage account: %q) still exist", name, storageAccountName)
   179  		}
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  func TestValidateArmStorageTableName(t *testing.T) {
   186  	validNames := []string{
   187  		"mytable01",
   188  		"mytable",
   189  		"myTable",
   190  		"MYTABLE",
   191  	}
   192  	for _, v := range validNames {
   193  		_, errors := validateArmStorageTableName(v, "name")
   194  		if len(errors) != 0 {
   195  			t.Fatalf("%q should be a valid Storage Table Name: %q", v, errors)
   196  		}
   197  	}
   198  
   199  	invalidNames := []string{
   200  		"table",
   201  		"-invalidname1",
   202  		"invalid_name",
   203  		"invalid!",
   204  		"ww",
   205  		strings.Repeat("w", 65),
   206  	}
   207  	for _, v := range invalidNames {
   208  		_, errors := validateArmStorageTableName(v, "name")
   209  		if len(errors) == 0 {
   210  			t.Fatalf("%q should be an invalid Storage Table Name", v)
   211  		}
   212  	}
   213  }
   214  
   215  var testAccAzureRMStorageTable_basic = `
   216  resource "azurerm_resource_group" "test" {
   217      name = "acctestrg-%d"
   218      location = "westus"
   219  }
   220  
   221  resource "azurerm_storage_account" "test" {
   222      name = "acctestacc%s"
   223      resource_group_name = "${azurerm_resource_group.test.name}"
   224      location = "westus"
   225      account_type = "Standard_LRS"
   226  
   227      tags {
   228          environment = "staging"
   229      }
   230  }
   231  
   232  resource "azurerm_storage_table" "test" {
   233      name = "tfacceptancetest%d"
   234      resource_group_name = "${azurerm_resource_group.test.name}"
   235      storage_account_name = "${azurerm_storage_account.test.name}"
   236  }
   237  `