package azurerm import ( "fmt" "net/http" "testing" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAzureRMEventHubPartitionCount_validation(t *testing.T) { cases := []struct { Value int ErrCount int }{ { Value: 1, ErrCount: 1, }, { Value: 2, ErrCount: 0, }, { Value: 3, ErrCount: 0, }, { Value: 21, ErrCount: 0, }, { Value: 32, ErrCount: 0, }, { Value: 33, ErrCount: 1, }, } for _, tc := range cases { _, errors := validateEventHubPartitionCount(tc.Value, "azurerm_eventhub") if len(errors) != tc.ErrCount { t.Fatalf("Expected the Azure RM EventHub Partition Count to trigger a validation error") } } } func TestAccAzureRMEventHubMessageRetentionCount_validation(t *testing.T) { cases := []struct { Value int ErrCount int }{ { Value: 0, ErrCount: 1, }, { Value: 1, ErrCount: 0, }, { Value: 2, ErrCount: 0, }, { Value: 3, ErrCount: 0, }, { Value: 4, ErrCount: 0, }, { Value: 5, ErrCount: 0, }, { Value: 6, ErrCount: 0, }, { Value: 7, ErrCount: 0, }, { Value: 8, ErrCount: 1, }, } for _, tc := range cases { _, errors := validateEventHubMessageRetentionCount(tc.Value, "azurerm_eventhub") if len(errors) != tc.ErrCount { t.Fatalf("Expected the Azure RM EventHub Message Retention Count to trigger a validation error") } } } func TestAccAzureRMEventHub_basic(t *testing.T) { ri := acctest.RandInt() config := fmt.Sprintf(testAccAzureRMEventHub_basic, ri, ri, ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMEventHubDestroy, Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( testCheckAzureRMEventHubExists("azurerm_eventhub.test"), ), }, }, }) } func TestAccAzureRMEventHub_standard(t *testing.T) { ri := acctest.RandInt() config := fmt.Sprintf(testAccAzureRMEventHub_standard, ri, ri, ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMEventHubDestroy, Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( testCheckAzureRMEventHubExists("azurerm_eventhub.test"), ), }, }, }) } func testCheckAzureRMEventHubDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*ArmClient).eventHubClient for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_eventhub" { continue } name := rs.Primary.Attributes["name"] namespaceName := rs.Primary.Attributes["namespace_name"] resourceGroup := rs.Primary.Attributes["resource_group_name"] resp, err := conn.Get(resourceGroup, namespaceName, name) if err != nil { return nil } if resp.StatusCode != http.StatusNotFound { return fmt.Errorf("EventHub still exists:\n%#v", resp.Properties) } } return nil } func testCheckAzureRMEventHubExists(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) } name := rs.Primary.Attributes["name"] namespaceName := rs.Primary.Attributes["namespace_name"] resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] if !hasResourceGroup { return fmt.Errorf("Bad: no resource group found in state for Event Hub: %s", name) } conn := testAccProvider.Meta().(*ArmClient).eventHubClient resp, err := conn.Get(resourceGroup, namespaceName, name) if err != nil { return fmt.Errorf("Bad: Get on eventHubClient: %s", err) } if resp.StatusCode == http.StatusNotFound { return fmt.Errorf("Bad: Event Hub %q (namespace %q / resource group: %q) does not exist", name, namespaceName, resourceGroup) } return nil } } var testAccAzureRMEventHub_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" } resource "azurerm_eventhub" "test" { name = "acctesteventhub-%d" namespace_name = "${azurerm_eventhub_namespace.test.name}" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" partition_count = 2 message_retention = 1 } ` var testAccAzureRMEventHub_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" } resource "azurerm_eventhub" "test" { name = "acctesteventhub-%d" namespace_name = "${azurerm_eventhub_namespace.test.name}" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" partition_count = 2 message_retention = 7 } `