github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_storage_queue_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"strings"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestResourceAzureRMStorageQueueName_Validation(t *testing.T) {
    15  	cases := []struct {
    16  		Value    string
    17  		ErrCount int
    18  	}{
    19  		{
    20  			Value:    "testing_123",
    21  			ErrCount: 1,
    22  		},
    23  		{
    24  			Value:    "testing123-",
    25  			ErrCount: 1,
    26  		},
    27  		{
    28  			Value:    "-testing123",
    29  			ErrCount: 1,
    30  		},
    31  		{
    32  			Value:    "TestingSG",
    33  			ErrCount: 1,
    34  		},
    35  		{
    36  			Value:    acctest.RandString(256),
    37  			ErrCount: 1,
    38  		},
    39  		{
    40  			Value:    acctest.RandString(1),
    41  			ErrCount: 1,
    42  		},
    43  	}
    44  
    45  	for _, tc := range cases {
    46  		_, errors := validateArmStorageQueueName(tc.Value, "azurerm_storage_queue")
    47  
    48  		if len(errors) != tc.ErrCount {
    49  			t.Fatalf("Expected the ARM Storage Queue Name to trigger a validation error")
    50  		}
    51  	}
    52  }
    53  
    54  func TestAccAzureRMStorageQueue_basic(t *testing.T) {
    55  	ri := acctest.RandInt()
    56  	rs := strings.ToLower(acctest.RandString(11))
    57  	config := fmt.Sprintf(testAccAzureRMStorageQueue_basic, ri, rs, ri)
    58  
    59  	resource.Test(t, resource.TestCase{
    60  		PreCheck:     func() { testAccPreCheck(t) },
    61  		Providers:    testAccProviders,
    62  		CheckDestroy: testCheckAzureRMStorageQueueDestroy,
    63  		Steps: []resource.TestStep{
    64  			resource.TestStep{
    65  				Config: config,
    66  				Check: resource.ComposeTestCheckFunc(
    67  					testCheckAzureRMStorageQueueExists("azurerm_storage_queue.test"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func testCheckAzureRMStorageQueueExists(name string) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  
    77  		rs, ok := s.RootModule().Resources[name]
    78  		if !ok {
    79  			return fmt.Errorf("Not found: %s", name)
    80  		}
    81  
    82  		name := rs.Primary.Attributes["name"]
    83  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
    84  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    85  		if !hasResourceGroup {
    86  			return fmt.Errorf("Bad: no resource group found in state for storage queue: %s", name)
    87  		}
    88  
    89  		armClient := testAccProvider.Meta().(*ArmClient)
    90  		queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroup, storageAccountName)
    91  		if err != nil {
    92  			return err
    93  		}
    94  		if !accountExists {
    95  			return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
    96  		}
    97  
    98  		exists, err := queueClient.QueueExists(name)
    99  		if err != nil {
   100  			return err
   101  		}
   102  
   103  		if !exists {
   104  			return fmt.Errorf("Bad: Storage Queue %q (storage account: %q) does not exist", name, storageAccountName)
   105  		}
   106  
   107  		return nil
   108  	}
   109  }
   110  
   111  func testCheckAzureRMStorageQueueDestroy(s *terraform.State) error {
   112  	for _, rs := range s.RootModule().Resources {
   113  		if rs.Type != "azurerm_storage_queue" {
   114  			continue
   115  		}
   116  
   117  		name := rs.Primary.Attributes["name"]
   118  		storageAccountName := rs.Primary.Attributes["storage_account_name"]
   119  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   120  		if !hasResourceGroup {
   121  			return fmt.Errorf("Bad: no resource group found in state for storage queue: %s", name)
   122  		}
   123  
   124  		armClient := testAccProvider.Meta().(*ArmClient)
   125  		queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroup, storageAccountName)
   126  		if err != nil {
   127  			return nil
   128  		}
   129  		if !accountExists {
   130  			return nil
   131  		}
   132  
   133  		exists, err := queueClient.QueueExists(name)
   134  		if err != nil {
   135  			return nil
   136  		}
   137  
   138  		if exists {
   139  			return fmt.Errorf("Bad: Storage Queue %q (storage account: %q) still exists", name, storageAccountName)
   140  		}
   141  	}
   142  
   143  	return nil
   144  }
   145  
   146  var testAccAzureRMStorageQueue_basic = `
   147  resource "azurerm_resource_group" "test" {
   148      name = "acctestrg-%d"
   149      location = "westus"
   150  }
   151  
   152  resource "azurerm_storage_account" "test" {
   153      name = "acctestacc%s"
   154      resource_group_name = "${azurerm_resource_group.test.name}"
   155      location = "westus"
   156      account_type = "Standard_LRS"
   157  
   158      tags {
   159          environment = "staging"
   160      }
   161  }
   162  
   163  resource "azurerm_storage_queue" "test" {
   164      name = "mysamplequeue-%d"
   165      resource_group_name = "${azurerm_resource_group.test.name}"
   166      storage_account_name = "${azurerm_storage_account.test.name}"
   167  }
   168  `