github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_servicebus_namespace_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"regexp"
     7  	"testing"
     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 TestAccAzureRMServiceBusNamespaceCapacity_validation(t *testing.T) {
    15  	cases := []struct {
    16  		Value    int
    17  		ErrCount int
    18  	}{
    19  		{
    20  			Value:    17,
    21  			ErrCount: 1,
    22  		},
    23  		{
    24  			Value:    1,
    25  			ErrCount: 0,
    26  		},
    27  		{
    28  			Value:    2,
    29  			ErrCount: 0,
    30  		},
    31  		{
    32  			Value:    4,
    33  			ErrCount: 0,
    34  		},
    35  	}
    36  
    37  	for _, tc := range cases {
    38  		_, errors := validateServiceBusNamespaceCapacity(tc.Value, "azurerm_servicebus_namespace")
    39  
    40  		if len(errors) != tc.ErrCount {
    41  			t.Fatalf("Expected the Azure RM ServiceBus Namespace Capacity to trigger a validation error")
    42  		}
    43  	}
    44  }
    45  
    46  func TestAccAzureRMServiceBusNamespaceSku_validation(t *testing.T) {
    47  	cases := []struct {
    48  		Value    string
    49  		ErrCount int
    50  	}{
    51  		{
    52  			Value:    "Basic",
    53  			ErrCount: 0,
    54  		},
    55  		{
    56  			Value:    "Standard",
    57  			ErrCount: 0,
    58  		},
    59  		{
    60  			Value:    "Premium",
    61  			ErrCount: 0,
    62  		},
    63  		{
    64  			Value:    "Random",
    65  			ErrCount: 1,
    66  		},
    67  	}
    68  
    69  	for _, tc := range cases {
    70  		_, errors := validateServiceBusNamespaceSku(tc.Value, "azurerm_servicebus_namespace")
    71  
    72  		if len(errors) != tc.ErrCount {
    73  			t.Fatalf("Expected the Azure RM ServiceBus Namespace Sku to trigger a validation error")
    74  		}
    75  	}
    76  }
    77  
    78  func TestAccAzureRMServiceBusNamespace_basic(t *testing.T) {
    79  
    80  	ri := acctest.RandInt()
    81  	config := fmt.Sprintf(testAccAzureRMServiceBusNamespace_basic, ri, ri)
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		PreCheck:     func() { testAccPreCheck(t) },
    85  		Providers:    testAccProviders,
    86  		CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy,
    87  		Steps: []resource.TestStep{
    88  			resource.TestStep{
    89  				Config: config,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testCheckAzureRMServiceBusNamespaceExists("azurerm_servicebus_namespace.test"),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func TestAccAzureRMServiceBusNamespace_readDefaultKeys(t *testing.T) {
    99  	ri := acctest.RandInt()
   100  	config := fmt.Sprintf(testAccAzureRMServiceBusNamespace_basic, ri, ri)
   101  
   102  	resource.Test(t, resource.TestCase{
   103  		PreCheck:     func() { testAccPreCheck(t) },
   104  		Providers:    testAccProviders,
   105  		CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy,
   106  		Steps: []resource.TestStep{
   107  			resource.TestStep{
   108  				Config: config,
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testCheckAzureRMServiceBusNamespaceExists("azurerm_servicebus_namespace.test"),
   111  					resource.TestMatchResourceAttr(
   112  						"azurerm_servicebus_namespace.test", "default_primary_connection_string", regexp.MustCompile("Endpoint=.+")),
   113  					resource.TestMatchResourceAttr(
   114  						"azurerm_servicebus_namespace.test", "default_secondary_connection_string", regexp.MustCompile("Endpoint=.+")),
   115  					resource.TestMatchResourceAttr(
   116  						"azurerm_servicebus_namespace.test", "default_primary_key", regexp.MustCompile(".+")),
   117  					resource.TestMatchResourceAttr(
   118  						"azurerm_servicebus_namespace.test", "default_secondary_key", regexp.MustCompile(".+")),
   119  				),
   120  			},
   121  		},
   122  	})
   123  }
   124  
   125  func testCheckAzureRMServiceBusNamespaceDestroy(s *terraform.State) error {
   126  	conn := testAccProvider.Meta().(*ArmClient).serviceBusNamespacesClient
   127  
   128  	for _, rs := range s.RootModule().Resources {
   129  		if rs.Type != "azurerm_servicebus_namespace" {
   130  			continue
   131  		}
   132  
   133  		name := rs.Primary.Attributes["name"]
   134  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   135  
   136  		resp, err := conn.Get(resourceGroup, name)
   137  
   138  		if err != nil {
   139  			return nil
   140  		}
   141  
   142  		if resp.StatusCode != http.StatusNotFound {
   143  			return fmt.Errorf("ServiceBus Namespace still exists:\n%+v", resp)
   144  		}
   145  	}
   146  
   147  	return nil
   148  }
   149  
   150  func testCheckAzureRMServiceBusNamespaceExists(name string) resource.TestCheckFunc {
   151  	return func(s *terraform.State) error {
   152  		// Ensure we have enough information in state to look up in API
   153  		rs, ok := s.RootModule().Resources[name]
   154  		if !ok {
   155  			return fmt.Errorf("Not found: %s", name)
   156  		}
   157  
   158  		namespaceName := rs.Primary.Attributes["name"]
   159  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   160  		if !hasResourceGroup {
   161  			return fmt.Errorf("Bad: no resource group found in state for Service Bus Namespace: %s", namespaceName)
   162  		}
   163  
   164  		conn := testAccProvider.Meta().(*ArmClient).serviceBusNamespacesClient
   165  
   166  		resp, err := conn.Get(resourceGroup, namespaceName)
   167  		if err != nil {
   168  			return fmt.Errorf("Bad: Get on serviceBusNamespacesClient: %+v", err)
   169  		}
   170  
   171  		if resp.StatusCode == http.StatusNotFound {
   172  			return fmt.Errorf("Bad: Service Bus Namespace %q (resource group: %q) does not exist", namespaceName, resourceGroup)
   173  		}
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  var testAccAzureRMServiceBusNamespace_basic = `
   180  resource "azurerm_resource_group" "test" {
   181      name = "acctestRG-%d"
   182      location = "West US"
   183  }
   184  resource "azurerm_servicebus_namespace" "test" {
   185      name = "acctestservicebusnamespace-%d"
   186      location = "West US"
   187      resource_group_name = "${azurerm_resource_group.test.name}"
   188      sku = "basic"
   189  }
   190  `