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