terraform/builtin/providers/azurerm/resource_arm_eventhub_names...

229 lines
5.8 KiB
Go

package azurerm
import (
"fmt"
"net/http"
"regexp"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAzureRMEventHubNamespaceCapacity_validation(t *testing.T) {
cases := []struct {
Value int
ErrCount int
}{
{
Value: 17,
ErrCount: 1,
},
{
Value: 1,
ErrCount: 0,
},
{
Value: 2,
ErrCount: 0,
},
{
Value: 3,
ErrCount: 1,
},
{
Value: 4,
ErrCount: 0,
},
}
for _, tc := range cases {
_, errors := validateEventHubNamespaceCapacity(tc.Value, "azurerm_eventhub_namespace")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Azure RM EventHub Namespace Capacity to trigger a validation error")
}
}
}
func TestAccAzureRMEventHubNamespaceSku_validation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "Basic",
ErrCount: 0,
},
{
Value: "Standard",
ErrCount: 0,
},
{
Value: "Premium",
ErrCount: 1,
},
{
Value: "Random",
ErrCount: 1,
},
}
for _, tc := range cases {
_, errors := validateEventHubNamespaceSku(tc.Value, "azurerm_eventhub_namespace")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Azure RM EventHub Namespace Sku to trigger a validation error")
}
}
}
func TestAccAzureRMEventHubNamespace_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubNamespace_basic, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
),
},
},
})
}
func TestAccAzureRMEventHubNamespace_standard(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubNamespace_standard, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
),
},
},
})
}
func TestAccAzureRMEventHubNamespace_readDefaultKeys(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubNamespace_basic, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
resource.TestMatchResourceAttr(
"azurerm_eventhub_namespace.test", "default_primary_connection_string", regexp.MustCompile("Endpoint=.+")),
resource.TestMatchResourceAttr(
"azurerm_eventhub_namespace.test", "default_secondary_connection_string", regexp.MustCompile("Endpoint=.+")),
resource.TestMatchResourceAttr(
"azurerm_eventhub_namespace.test", "default_primary_key", regexp.MustCompile(".+")),
resource.TestMatchResourceAttr(
"azurerm_eventhub_namespace.test", "default_secondary_key", regexp.MustCompile(".+")),
),
},
},
})
}
func testCheckAzureRMEventHubNamespaceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).eventHubNamespacesClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_eventhub_namespace" {
continue
}
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
resp, err := conn.Get(resourceGroup, name)
if err != nil {
return nil
}
if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("EventHub Namespace still exists:\n%#v", resp.Properties)
}
}
return nil
}
func testCheckAzureRMEventHubNamespaceExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
namespaceName := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for Event Hub Namespace: %s", namespaceName)
}
conn := testAccProvider.Meta().(*ArmClient).eventHubNamespacesClient
resp, err := conn.Get(resourceGroup, namespaceName)
if err != nil {
return fmt.Errorf("Bad: Get on eventHubNamespacesClient: %s", err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: Event Hub Namespace %q (resource group: %q) does not exist", namespaceName, resourceGroup)
}
return nil
}
}
var testAccAzureRMEventHubNamespace_basic = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_eventhub_namespace" "test" {
name = "acctesteventhubnamespace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Basic"
}
`
var testAccAzureRMEventHubNamespace_standard = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_eventhub_namespace" "test" {
name = "acctesteventhubnamespace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
capacity = "2"
}
`