github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_servicebus_subscription_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 TestAccAzureRMServiceBusSubscription_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMServiceBusSubscription_basic, ri, 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  					testCheckAzureRMServiceBusSubscriptionExists("azurerm_servicebus_subscription.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMServiceBusSubscription_update(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	preConfig := fmt.Sprintf(testAccAzureRMServiceBusSubscription_basic, ri, ri, ri, ri)
    35  	postConfig := fmt.Sprintf(testAccAzureRMServiceBusSubscription_update, ri, 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  					testCheckAzureRMServiceBusSubscriptionExists("azurerm_servicebus_subscription.test"),
    46  				),
    47  			},
    48  			resource.TestStep{
    49  				Config: postConfig,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					resource.TestCheckResourceAttr(
    52  						"azurerm_servicebus_subscription.test", "enable_batched_operations", "true"),
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func TestAccAzureRMServiceBusSubscription_updateRequiresSession(t *testing.T) {
    60  	ri := acctest.RandInt()
    61  	preConfig := fmt.Sprintf(testAccAzureRMServiceBusSubscription_basic, ri, ri, ri, ri)
    62  	postConfig := fmt.Sprintf(testAccAzureRMServiceBusSubscription_updateRequiresSession, ri, ri, ri, ri)
    63  
    64  	resource.Test(t, resource.TestCase{
    65  		PreCheck:     func() { testAccPreCheck(t) },
    66  		Providers:    testAccProviders,
    67  		CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
    68  		Steps: []resource.TestStep{
    69  			resource.TestStep{
    70  				Config: preConfig,
    71  				Check: resource.ComposeTestCheckFunc(
    72  					testCheckAzureRMServiceBusSubscriptionExists("azurerm_servicebus_subscription.test"),
    73  				),
    74  			},
    75  			resource.TestStep{
    76  				Config: postConfig,
    77  				Check: resource.ComposeTestCheckFunc(
    78  					resource.TestCheckResourceAttr(
    79  						"azurerm_servicebus_subscription.test", "requires_session", "true"),
    80  				),
    81  			},
    82  		},
    83  	})
    84  }
    85  
    86  func testCheckAzureRMServiceBusSubscriptionDestroy(s *terraform.State) error {
    87  	client := testAccProvider.Meta().(*ArmClient).serviceBusSubscriptionsClient
    88  
    89  	for _, rs := range s.RootModule().Resources {
    90  		if rs.Type != "azurerm_servicebus_subscription" {
    91  			continue
    92  		}
    93  
    94  		name := rs.Primary.Attributes["name"]
    95  		topicName := rs.Primary.Attributes["topic_name"]
    96  		namespaceName := rs.Primary.Attributes["namespace_name"]
    97  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    98  
    99  		resp, err := client.Get(resourceGroup, namespaceName, topicName, name)
   100  		if err != nil {
   101  			if resp.StatusCode == http.StatusNotFound {
   102  				return nil
   103  			}
   104  			return err
   105  		}
   106  
   107  		if resp.StatusCode != http.StatusNotFound {
   108  			return fmt.Errorf("ServiceBus Subscription still exists:\n%#v", resp.Properties)
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func testCheckAzureRMServiceBusSubscriptionExists(name string) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  		// Ensure we have enough information in state to look up in API
   118  		rs, ok := s.RootModule().Resources[name]
   119  		if !ok {
   120  			return fmt.Errorf("Not found: %s", name)
   121  		}
   122  
   123  		subscriptionName := rs.Primary.Attributes["name"]
   124  		topicName := rs.Primary.Attributes["topic_name"]
   125  		namespaceName := rs.Primary.Attributes["namespace_name"]
   126  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   127  		if !hasResourceGroup {
   128  			return fmt.Errorf("Bad: no resource group found in state for subscription: %s", topicName)
   129  		}
   130  
   131  		client := testAccProvider.Meta().(*ArmClient).serviceBusSubscriptionsClient
   132  
   133  		resp, err := client.Get(resourceGroup, namespaceName, topicName, subscriptionName)
   134  		if err != nil {
   135  			return fmt.Errorf("Bad: Get on serviceBusSubscriptionsClient: %s", err)
   136  		}
   137  
   138  		if resp.StatusCode == http.StatusNotFound {
   139  			return fmt.Errorf("Bad: Subscription %q (resource group: %q) does not exist", subscriptionName, resourceGroup)
   140  		}
   141  
   142  		return nil
   143  	}
   144  }
   145  
   146  var testAccAzureRMServiceBusSubscription_basic = `
   147  resource "azurerm_resource_group" "test" {
   148      name = "acctestRG-%d"
   149      location = "West US"
   150  }
   151  
   152  resource "azurerm_servicebus_namespace" "test" {
   153      name = "acctestservicebusnamespace-%d"
   154      location = "West US"
   155      resource_group_name = "${azurerm_resource_group.test.name}"
   156      sku = "standard"
   157  }
   158  
   159  resource "azurerm_servicebus_topic" "test" {
   160      name = "acctestservicebustopic-%d"
   161      location = "West US"
   162      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   163      resource_group_name = "${azurerm_resource_group.test.name}"
   164  }
   165  
   166  resource "azurerm_servicebus_subscription" "test" {
   167      name = "acctestservicebussubscription-%d"
   168      location = "West US"
   169      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   170      topic_name = "${azurerm_servicebus_topic.test.name}"
   171      resource_group_name = "${azurerm_resource_group.test.name}"
   172      max_delivery_count = 10
   173  }
   174  `
   175  
   176  var testAccAzureRMServiceBusSubscription_update = `
   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  resource "azurerm_servicebus_subscription" "test" {
   197      name = "acctestservicebussubscription-%d"
   198      location = "West US"
   199      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   200      topic_name = "${azurerm_servicebus_topic.test.name}"
   201      resource_group_name = "${azurerm_resource_group.test.name}"
   202      max_delivery_count = 10
   203      enable_batched_operations = true
   204  }
   205  `
   206  
   207  var testAccAzureRMServiceBusSubscription_updateRequiresSession = `
   208  resource "azurerm_resource_group" "test" {
   209      name = "acctestRG-%d"
   210      location = "West US"
   211  }
   212  
   213  resource "azurerm_servicebus_namespace" "test" {
   214      name = "acctestservicebusnamespace-%d"
   215      location = "West US"
   216      resource_group_name = "${azurerm_resource_group.test.name}"
   217      sku = "standard"
   218  }
   219  
   220  resource "azurerm_servicebus_topic" "test" {
   221      name = "acctestservicebustopic-%d"
   222      location = "West US"
   223      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   224      resource_group_name = "${azurerm_resource_group.test.name}"
   225  }
   226  
   227  resource "azurerm_servicebus_subscription" "test" {
   228      name = "acctestservicebussubscription-%d"
   229      location = "West US"
   230      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   231      topic_name = "${azurerm_servicebus_topic.test.name}"
   232      resource_group_name = "${azurerm_resource_group.test.name}"
   233      max_delivery_count = 10
   234      requires_session = true
   235  }
   236  `