github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_servicebus_topic_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 TestAccAzureRMServiceBusTopic_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMServiceBusTopic_update(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
    35  	postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_update, ri, ri, ri)
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: preConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
    46  				),
    47  			},
    48  			resource.TestStep{
    49  				Config: postConfig,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					resource.TestCheckResourceAttr(
    52  						"azurerm_servicebus_topic.test", "enable_batched_operations", "true"),
    53  					resource.TestCheckResourceAttr(
    54  						"azurerm_servicebus_topic.test", "enable_express", "true"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccAzureRMServiceBusTopic_enablePartitioning(t *testing.T) {
    62  	ri := acctest.RandInt()
    63  	preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
    64  	postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_enablePartitioning, ri, ri, ri)
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:     func() { testAccPreCheck(t) },
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: preConfig,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
    75  				),
    76  			},
    77  			resource.TestStep{
    78  				Config: postConfig,
    79  				Check: resource.ComposeTestCheckFunc(
    80  					resource.TestCheckResourceAttr(
    81  						"azurerm_servicebus_topic.test", "enable_partitioning", "true"),
    82  					// Ensure size is read back in it's original value and not the x16 value returned by Azure
    83  					resource.TestCheckResourceAttr(
    84  						"azurerm_servicebus_topic.test", "max_size_in_megabytes", "5120"),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func TestAccAzureRMServiceBusTopic_enableDuplicateDetection(t *testing.T) {
    92  	ri := acctest.RandInt()
    93  	preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
    94  	postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_enableDuplicateDetection, ri, ri, ri)
    95  
    96  	resource.Test(t, resource.TestCase{
    97  		PreCheck:     func() { testAccPreCheck(t) },
    98  		Providers:    testAccProviders,
    99  		CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
   100  		Steps: []resource.TestStep{
   101  			resource.TestStep{
   102  				Config: preConfig,
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
   105  				),
   106  			},
   107  			resource.TestStep{
   108  				Config: postConfig,
   109  				Check: resource.ComposeTestCheckFunc(
   110  					resource.TestCheckResourceAttr(
   111  						"azurerm_servicebus_topic.test", "requires_duplicate_detection", "true"),
   112  				),
   113  			},
   114  		},
   115  	})
   116  }
   117  
   118  func testCheckAzureRMServiceBusTopicDestroy(s *terraform.State) error {
   119  	client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient
   120  
   121  	for _, rs := range s.RootModule().Resources {
   122  		if rs.Type != "azurerm_servicebus_topic" {
   123  			continue
   124  		}
   125  
   126  		name := rs.Primary.Attributes["name"]
   127  		namespaceName := rs.Primary.Attributes["namespace_name"]
   128  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   129  
   130  		resp, err := client.Get(resourceGroup, namespaceName, name)
   131  		if err != nil {
   132  			if resp.StatusCode == http.StatusNotFound {
   133  				return nil
   134  			}
   135  			return err
   136  		}
   137  
   138  		if resp.StatusCode != http.StatusNotFound {
   139  			return fmt.Errorf("ServiceBus Topic still exists:\n%#v", resp.Properties)
   140  		}
   141  	}
   142  
   143  	return nil
   144  }
   145  
   146  func testCheckAzureRMServiceBusTopicExists(name string) resource.TestCheckFunc {
   147  	return func(s *terraform.State) error {
   148  		// Ensure we have enough information in state to look up in API
   149  		rs, ok := s.RootModule().Resources[name]
   150  		if !ok {
   151  			return fmt.Errorf("Not found: %s", name)
   152  		}
   153  
   154  		topicName := rs.Primary.Attributes["name"]
   155  		namespaceName := rs.Primary.Attributes["namespace_name"]
   156  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   157  		if !hasResourceGroup {
   158  			return fmt.Errorf("Bad: no resource group found in state for topic: %s", topicName)
   159  		}
   160  
   161  		client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient
   162  
   163  		resp, err := client.Get(resourceGroup, namespaceName, topicName)
   164  		if err != nil {
   165  			return fmt.Errorf("Bad: Get on serviceBusTopicsClient: %s", err)
   166  		}
   167  
   168  		if resp.StatusCode == http.StatusNotFound {
   169  			return fmt.Errorf("Bad: Topic %q (resource group: %q) does not exist", namespaceName, resourceGroup)
   170  		}
   171  
   172  		return nil
   173  	}
   174  }
   175  
   176  var testAccAzureRMServiceBusTopic_basic = `
   177  resource "azurerm_resource_group" "test" {
   178      name = "acctestRG-%d"
   179      location = "West US"
   180  }
   181  
   182  resource "azurerm_servicebus_namespace" "test" {
   183      name = "acctestservicebusnamespace-%d"
   184      location = "West US"
   185      resource_group_name = "${azurerm_resource_group.test.name}"
   186      sku = "standard"
   187  }
   188  
   189  resource "azurerm_servicebus_topic" "test" {
   190      name = "acctestservicebustopic-%d"
   191      location = "West US"
   192      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   193      resource_group_name = "${azurerm_resource_group.test.name}"
   194  }
   195  `
   196  
   197  var testAccAzureRMServiceBusTopic_update = `
   198  resource "azurerm_resource_group" "test" {
   199      name = "acctestRG-%d"
   200      location = "West US"
   201  }
   202  
   203  resource "azurerm_servicebus_namespace" "test" {
   204      name = "acctestservicebusnamespace-%d"
   205      location = "West US"
   206      resource_group_name = "${azurerm_resource_group.test.name}"
   207      sku = "standard"
   208  }
   209  
   210  resource "azurerm_servicebus_topic" "test" {
   211      name = "acctestservicebustopic-%d"
   212      location = "West US"
   213      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   214      resource_group_name = "${azurerm_resource_group.test.name}"
   215      enable_batched_operations = true
   216      enable_express = true
   217  }
   218  `
   219  
   220  var testAccAzureRMServiceBusTopic_enablePartitioning = `
   221  resource "azurerm_resource_group" "test" {
   222      name = "acctestRG-%d"
   223      location = "West US"
   224  }
   225  
   226  resource "azurerm_servicebus_namespace" "test" {
   227      name = "acctestservicebusnamespace-%d"
   228      location = "West US"
   229      resource_group_name = "${azurerm_resource_group.test.name}"
   230      sku = "standard"
   231  }
   232  
   233  resource "azurerm_servicebus_topic" "test" {
   234      name = "acctestservicebustopic-%d"
   235      location = "West US"
   236      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   237      resource_group_name = "${azurerm_resource_group.test.name}"
   238      enable_partitioning = true
   239  	max_size_in_megabytes = 5120
   240  }
   241  `
   242  
   243  var testAccAzureRMServiceBusTopic_enableDuplicateDetection = `
   244  resource "azurerm_resource_group" "test" {
   245      name = "acctestRG-%d"
   246      location = "West US"
   247  }
   248  
   249  resource "azurerm_servicebus_namespace" "test" {
   250      name = "acctestservicebusnamespace-%d"
   251      location = "West US"
   252      resource_group_name = "${azurerm_resource_group.test.name}"
   253      sku = "standard"
   254  }
   255  
   256  resource "azurerm_servicebus_topic" "test" {
   257      name = "acctestservicebustopic-%d"
   258      location = "West US"
   259      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   260      resource_group_name = "${azurerm_resource_group.test.name}"
   261      requires_duplicate_detection = true
   262  }
   263  `