github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_eventhub_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 TestAccAzureRMEventHubPartitionCount_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    int
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    1,
    20  			ErrCount: 1,
    21  		},
    22  		{
    23  			Value:    2,
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    3,
    28  			ErrCount: 0,
    29  		},
    30  		{
    31  			Value:    21,
    32  			ErrCount: 0,
    33  		},
    34  		{
    35  			Value:    32,
    36  			ErrCount: 0,
    37  		},
    38  		{
    39  			Value:    33,
    40  			ErrCount: 1,
    41  		},
    42  	}
    43  
    44  	for _, tc := range cases {
    45  		_, errors := validateEventHubPartitionCount(tc.Value, "azurerm_eventhub")
    46  
    47  		if len(errors) != tc.ErrCount {
    48  			t.Fatalf("Expected the Azure RM EventHub Partition Count to trigger a validation error")
    49  		}
    50  	}
    51  }
    52  
    53  func TestAccAzureRMEventHubMessageRetentionCount_validation(t *testing.T) {
    54  	cases := []struct {
    55  		Value    int
    56  		ErrCount int
    57  	}{
    58  		{
    59  			Value:    0,
    60  			ErrCount: 1,
    61  		}, {
    62  			Value:    1,
    63  			ErrCount: 0,
    64  		}, {
    65  			Value:    2,
    66  			ErrCount: 0,
    67  		}, {
    68  			Value:    3,
    69  			ErrCount: 0,
    70  		}, {
    71  			Value:    4,
    72  			ErrCount: 0,
    73  		}, {
    74  			Value:    5,
    75  			ErrCount: 0,
    76  		}, {
    77  			Value:    6,
    78  			ErrCount: 0,
    79  		}, {
    80  			Value:    7,
    81  			ErrCount: 0,
    82  		}, {
    83  			Value:    8,
    84  			ErrCount: 1,
    85  		},
    86  	}
    87  
    88  	for _, tc := range cases {
    89  		_, errors := validateEventHubMessageRetentionCount(tc.Value, "azurerm_eventhub")
    90  
    91  		if len(errors) != tc.ErrCount {
    92  			t.Fatalf("Expected the Azure RM EventHub Message Retention Count to trigger a validation error")
    93  		}
    94  	}
    95  }
    96  
    97  func TestAccAzureRMEventHub_basic(t *testing.T) {
    98  
    99  	ri := acctest.RandInt()
   100  	config := fmt.Sprintf(testAccAzureRMEventHub_basic, ri, ri, ri)
   101  
   102  	resource.Test(t, resource.TestCase{
   103  		PreCheck:     func() { testAccPreCheck(t) },
   104  		Providers:    testAccProviders,
   105  		CheckDestroy: testCheckAzureRMEventHubDestroy,
   106  		Steps: []resource.TestStep{
   107  			{
   108  				Config: config,
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testCheckAzureRMEventHubExists("azurerm_eventhub.test"),
   111  				),
   112  			},
   113  		},
   114  	})
   115  }
   116  
   117  func TestAccAzureRMEventHub_standard(t *testing.T) {
   118  
   119  	ri := acctest.RandInt()
   120  	config := fmt.Sprintf(testAccAzureRMEventHub_standard, ri, ri, ri)
   121  
   122  	resource.Test(t, resource.TestCase{
   123  		PreCheck:     func() { testAccPreCheck(t) },
   124  		Providers:    testAccProviders,
   125  		CheckDestroy: testCheckAzureRMEventHubDestroy,
   126  		Steps: []resource.TestStep{
   127  			{
   128  				Config: config,
   129  				Check: resource.ComposeTestCheckFunc(
   130  					testCheckAzureRMEventHubExists("azurerm_eventhub.test"),
   131  				),
   132  			},
   133  		},
   134  	})
   135  }
   136  
   137  func testCheckAzureRMEventHubDestroy(s *terraform.State) error {
   138  	conn := testAccProvider.Meta().(*ArmClient).eventHubClient
   139  
   140  	for _, rs := range s.RootModule().Resources {
   141  		if rs.Type != "azurerm_eventhub" {
   142  			continue
   143  		}
   144  
   145  		name := rs.Primary.Attributes["name"]
   146  		namespaceName := rs.Primary.Attributes["namespace_name"]
   147  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   148  
   149  		resp, err := conn.Get(resourceGroup, namespaceName, name)
   150  
   151  		if err != nil {
   152  			return nil
   153  		}
   154  
   155  		if resp.StatusCode != http.StatusNotFound {
   156  			return fmt.Errorf("EventHub still exists:\n%#v", resp.Properties)
   157  		}
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  func testCheckAzureRMEventHubExists(name string) resource.TestCheckFunc {
   164  	return func(s *terraform.State) error {
   165  		// Ensure we have enough information in state to look up in API
   166  		rs, ok := s.RootModule().Resources[name]
   167  		if !ok {
   168  			return fmt.Errorf("Not found: %s", name)
   169  		}
   170  
   171  		name := rs.Primary.Attributes["name"]
   172  		namespaceName := rs.Primary.Attributes["namespace_name"]
   173  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   174  		if !hasResourceGroup {
   175  			return fmt.Errorf("Bad: no resource group found in state for Event Hub: %s", name)
   176  		}
   177  
   178  		conn := testAccProvider.Meta().(*ArmClient).eventHubClient
   179  
   180  		resp, err := conn.Get(resourceGroup, namespaceName, name)
   181  		if err != nil {
   182  			return fmt.Errorf("Bad: Get on eventHubClient: %s", err)
   183  		}
   184  
   185  		if resp.StatusCode == http.StatusNotFound {
   186  			return fmt.Errorf("Bad: Event Hub %q (namespace %q / resource group: %q) does not exist", name, namespaceName, resourceGroup)
   187  		}
   188  
   189  		return nil
   190  	}
   191  }
   192  
   193  var testAccAzureRMEventHub_basic = `
   194  resource "azurerm_resource_group" "test" {
   195      name = "acctestRG-%d"
   196      location = "West US"
   197  }
   198  resource "azurerm_eventhub_namespace" "test" {
   199      name = "acctesteventhubnamespace-%d"
   200      location = "${azurerm_resource_group.test.location}"
   201      resource_group_name = "${azurerm_resource_group.test.name}"
   202      sku = "Basic"
   203  }
   204  
   205  resource "azurerm_eventhub" "test" {
   206    name                = "acctesteventhub-%d"
   207    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   208    location            = "${azurerm_resource_group.test.location}"
   209    resource_group_name = "${azurerm_resource_group.test.name}"
   210    partition_count     = 2
   211    message_retention   = 1
   212  }
   213  `
   214  
   215  var testAccAzureRMEventHub_standard = `
   216  resource "azurerm_resource_group" "test" {
   217      name = "acctestRG-%d"
   218      location = "West US"
   219  }
   220  resource "azurerm_eventhub_namespace" "test" {
   221      name = "acctesteventhubnamespace-%d"
   222      location = "${azurerm_resource_group.test.location}"
   223      resource_group_name = "${azurerm_resource_group.test.name}"
   224      sku = "Standard"
   225  }
   226  
   227  resource "azurerm_eventhub" "test" {
   228    name                = "acctesteventhub-%d"
   229    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   230    location            = "${azurerm_resource_group.test.location}"
   231    resource_group_name = "${azurerm_resource_group.test.name}"
   232    partition_count     = 2
   233    message_retention   = 7
   234  }
   235  `