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

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