github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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.Table
    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.Table
    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.Table) 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  		options := &storage.QueryTablesOptions{}
    86  		tables, err := tableClient.QueryTables(storage.MinimalMetadata, options)
    87  
    88  		if len(tables.Tables) == 0 {
    89  			return fmt.Errorf("Bad: Storage Table %q (storage account: %q) does not exist", name, storageAccountName)
    90  		}
    91  
    92  		var found bool
    93  		for _, table := range tables.Tables {
    94  			if table.Name == name {
    95  				found = true
    96  				*t = table
    97  			}
    98  		}
    99  
   100  		if !found {
   101  			return fmt.Errorf("Bad: Storage Table %q (storage account: %q) does not exist", name, storageAccountName)
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  func testAccARMStorageTableDisappears(name string, t *storage.Table) resource.TestCheckFunc {
   109  	return func(s *terraform.State) error {
   110  		rs, ok := s.RootModule().Resources[name]
   111  		if !ok {
   112  			return fmt.Errorf("Not found: %s", name)
   113  		}
   114  
   115  		armClient := testAccProvider.Meta().(*ArmClient)
   116  
   117  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   118  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   119  		if !hasResourceGroup {
   120  			return fmt.Errorf("Bad: no resource group found in state for storage table: %s", t.Name)
   121  		}
   122  
   123  		tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroup, storageAccountName)
   124  		if err != nil {
   125  			return err
   126  		}
   127  		if !accountExists {
   128  			log.Printf("[INFO]Storage Account %q doesn't exist so the table won't exist", storageAccountName)
   129  			return nil
   130  		}
   131  
   132  		table := tableClient.GetTableReference(t.Name)
   133  		timeout := uint(60)
   134  		options := &storage.TableOptions{}
   135  		err = table.Delete(timeout, options)
   136  		if err != nil {
   137  			return err
   138  		}
   139  
   140  		return nil
   141  	}
   142  }
   143  
   144  func testCheckAzureRMStorageTableDestroy(s *terraform.State) error {
   145  	for _, rs := range s.RootModule().Resources {
   146  		if rs.Type != "azurerm_storage_table" {
   147  			continue
   148  		}
   149  
   150  		name := rs.Primary.Attributes["name"]
   151  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   152  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   153  		if !hasResourceGroup {
   154  			return fmt.Errorf("Bad: no resource group found in state for storage table: %s", name)
   155  		}
   156  
   157  		armClient := testAccProvider.Meta().(*ArmClient)
   158  		tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroup, storageAccountName)
   159  		if err != nil {
   160  			//If we can't get keys then the table can't exist
   161  			return nil
   162  		}
   163  		if !accountExists {
   164  			return nil
   165  		}
   166  
   167  		options := &storage.QueryTablesOptions{}
   168  		tables, err := tableClient.QueryTables(storage.NoMetadata, options)
   169  
   170  		if err != nil {
   171  			return nil
   172  		}
   173  
   174  		var found bool
   175  		for _, table := range tables.Tables {
   176  			if table.Name == name {
   177  				found = true
   178  			}
   179  		}
   180  
   181  		if found {
   182  			return fmt.Errorf("Bad: Storage Table %q (storage account: %q) still exist", name, storageAccountName)
   183  		}
   184  	}
   185  
   186  	return nil
   187  }
   188  
   189  func TestValidateArmStorageTableName(t *testing.T) {
   190  	validNames := []string{
   191  		"mytable01",
   192  		"mytable",
   193  		"myTable",
   194  		"MYTABLE",
   195  	}
   196  	for _, v := range validNames {
   197  		_, errors := validateArmStorageTableName(v, "name")
   198  		if len(errors) != 0 {
   199  			t.Fatalf("%q should be a valid Storage Table Name: %q", v, errors)
   200  		}
   201  	}
   202  
   203  	invalidNames := []string{
   204  		"table",
   205  		"-invalidname1",
   206  		"invalid_name",
   207  		"invalid!",
   208  		"ww",
   209  		strings.Repeat("w", 65),
   210  	}
   211  	for _, v := range invalidNames {
   212  		_, errors := validateArmStorageTableName(v, "name")
   213  		if len(errors) == 0 {
   214  			t.Fatalf("%q should be an invalid Storage Table Name", v)
   215  		}
   216  	}
   217  }
   218  
   219  var testAccAzureRMStorageTable_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_table" "test" {
   237      name = "tfacceptancetest%d"
   238      resource_group_name = "${azurerm_resource_group.test.name}"
   239      storage_account_name = "${azurerm_storage_account.test.name}"
   240  }
   241  `