github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  			{
    93  				Config: config,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
    96  				),
    97  			},
    98  		},
    99  	})
   100  }
   101  
   102  func TestAccAzureRMEventHubNamespace_standard(t *testing.T) {
   103  
   104  	ri := acctest.RandInt()
   105  	config := fmt.Sprintf(testAccAzureRMEventHubNamespace_standard, ri, ri)
   106  
   107  	resource.Test(t, resource.TestCase{
   108  		PreCheck:     func() { testAccPreCheck(t) },
   109  		Providers:    testAccProviders,
   110  		CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
   111  		Steps: []resource.TestStep{
   112  			{
   113  				Config: config,
   114  				Check: resource.ComposeTestCheckFunc(
   115  					testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
   116  				),
   117  			},
   118  		},
   119  	})
   120  }
   121  
   122  func TestAccAzureRMEventHubNamespace_readDefaultKeys(t *testing.T) {
   123  	ri := acctest.RandInt()
   124  	config := fmt.Sprintf(testAccAzureRMEventHubNamespace_basic, ri, ri)
   125  
   126  	resource.Test(t, resource.TestCase{
   127  		PreCheck:     func() { testAccPreCheck(t) },
   128  		Providers:    testAccProviders,
   129  		CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
   130  		Steps: []resource.TestStep{
   131  			{
   132  				Config: config,
   133  				Check: resource.ComposeTestCheckFunc(
   134  					testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
   135  					resource.TestMatchResourceAttr(
   136  						"azurerm_eventhub_namespace.test", "default_primary_connection_string", regexp.MustCompile("Endpoint=.+")),
   137  					resource.TestMatchResourceAttr(
   138  						"azurerm_eventhub_namespace.test", "default_secondary_connection_string", regexp.MustCompile("Endpoint=.+")),
   139  					resource.TestMatchResourceAttr(
   140  						"azurerm_eventhub_namespace.test", "default_primary_key", regexp.MustCompile(".+")),
   141  					resource.TestMatchResourceAttr(
   142  						"azurerm_eventhub_namespace.test", "default_secondary_key", regexp.MustCompile(".+")),
   143  				),
   144  			},
   145  		},
   146  	})
   147  }
   148  
   149  func TestAccAzureRMEventHubNamespace_NonStandardCasing(t *testing.T) {
   150  
   151  	ri := acctest.RandInt()
   152  	config := testAccAzureRMEventHubNamespaceNonStandardCasing(ri)
   153  
   154  	resource.Test(t, resource.TestCase{
   155  		PreCheck:     func() { testAccPreCheck(t) },
   156  		Providers:    testAccProviders,
   157  		CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
   158  		Steps: []resource.TestStep{
   159  			resource.TestStep{
   160  				Config: config,
   161  				Check: resource.ComposeTestCheckFunc(
   162  					testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
   163  				),
   164  			},
   165  			resource.TestStep{
   166  				Config:             config,
   167  				PlanOnly:           true,
   168  				ExpectNonEmptyPlan: false,
   169  			},
   170  		},
   171  	})
   172  }
   173  
   174  func testCheckAzureRMEventHubNamespaceDestroy(s *terraform.State) error {
   175  	conn := testAccProvider.Meta().(*ArmClient).eventHubNamespacesClient
   176  
   177  	for _, rs := range s.RootModule().Resources {
   178  		if rs.Type != "azurerm_eventhub_namespace" {
   179  			continue
   180  		}
   181  
   182  		name := rs.Primary.Attributes["name"]
   183  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   184  
   185  		resp, err := conn.Get(resourceGroup, name)
   186  
   187  		if err != nil {
   188  			return nil
   189  		}
   190  
   191  		if resp.StatusCode != http.StatusNotFound {
   192  			return fmt.Errorf("EventHub Namespace still exists:\n%#v", resp.NamespaceProperties)
   193  		}
   194  	}
   195  
   196  	return nil
   197  }
   198  
   199  func testCheckAzureRMEventHubNamespaceExists(name string) resource.TestCheckFunc {
   200  	return func(s *terraform.State) error {
   201  		// Ensure we have enough information in state to look up in API
   202  		rs, ok := s.RootModule().Resources[name]
   203  		if !ok {
   204  			return fmt.Errorf("Not found: %s", name)
   205  		}
   206  
   207  		namespaceName := rs.Primary.Attributes["name"]
   208  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   209  		if !hasResourceGroup {
   210  			return fmt.Errorf("Bad: no resource group found in state for Event Hub Namespace: %s", namespaceName)
   211  		}
   212  
   213  		conn := testAccProvider.Meta().(*ArmClient).eventHubNamespacesClient
   214  
   215  		resp, err := conn.Get(resourceGroup, namespaceName)
   216  		if err != nil {
   217  			return fmt.Errorf("Bad: Get on eventHubNamespacesClient: %s", err)
   218  		}
   219  
   220  		if resp.StatusCode == http.StatusNotFound {
   221  			return fmt.Errorf("Bad: Event Hub Namespace %q (resource group: %q) does not exist", namespaceName, resourceGroup)
   222  		}
   223  
   224  		return nil
   225  	}
   226  }
   227  
   228  var testAccAzureRMEventHubNamespace_basic = `
   229  resource "azurerm_resource_group" "test" {
   230      name = "acctestRG-%d"
   231      location = "West US"
   232  }
   233  resource "azurerm_eventhub_namespace" "test" {
   234      name = "acctesteventhubnamespace-%d"
   235      location = "${azurerm_resource_group.test.location}"
   236      resource_group_name = "${azurerm_resource_group.test.name}"
   237      sku = "Basic"
   238  }
   239  `
   240  
   241  var testAccAzureRMEventHubNamespace_standard = `
   242  resource "azurerm_resource_group" "test" {
   243      name = "acctestRG-%d"
   244      location = "West US"
   245  }
   246  resource "azurerm_eventhub_namespace" "test" {
   247      name = "acctesteventhubnamespace-%d"
   248      location = "${azurerm_resource_group.test.location}"
   249      resource_group_name = "${azurerm_resource_group.test.name}"
   250      sku = "Standard"
   251      capacity = "2"
   252  }
   253  `
   254  
   255  func testAccAzureRMEventHubNamespaceNonStandardCasing(ri int) string {
   256  	return fmt.Sprintf(`
   257  resource "azurerm_resource_group" "test" {
   258      name = "acctestRG-%d"
   259      location = "West US"
   260  }
   261  resource "azurerm_eventhub_namespace" "test" {
   262      name = "acctesteventhubnamespace-%d"
   263      location = "${azurerm_resource_group.test.location}"
   264      resource_group_name = "${azurerm_resource_group.test.name}"
   265      sku = "basic"
   266  }
   267  `, ri, ri)
   268  }