github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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_enablePartitioningStandard(t *testing.T) { 62 ri := acctest.RandInt() 63 preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri) 64 postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_enablePartitioningStandard, 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_enablePartitioningPremium(t *testing.T) { 92 ri := acctest.RandInt() 93 preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri) 94 postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_enablePartitioningPremium, 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", "enable_partitioning", "true"), 112 resource.TestCheckResourceAttr( 113 "azurerm_servicebus_topic.test", "max_size_in_megabytes", "81920"), 114 ), 115 }, 116 }, 117 }) 118 } 119 120 func TestAccAzureRMServiceBusTopic_enableDuplicateDetection(t *testing.T) { 121 ri := acctest.RandInt() 122 preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri) 123 postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_enableDuplicateDetection, ri, ri, ri) 124 125 resource.Test(t, resource.TestCase{ 126 PreCheck: func() { testAccPreCheck(t) }, 127 Providers: testAccProviders, 128 CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, 129 Steps: []resource.TestStep{ 130 resource.TestStep{ 131 Config: preConfig, 132 Check: resource.ComposeTestCheckFunc( 133 testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"), 134 ), 135 }, 136 resource.TestStep{ 137 Config: postConfig, 138 Check: resource.ComposeTestCheckFunc( 139 resource.TestCheckResourceAttr( 140 "azurerm_servicebus_topic.test", "requires_duplicate_detection", "true"), 141 ), 142 }, 143 }, 144 }) 145 } 146 147 func testCheckAzureRMServiceBusTopicDestroy(s *terraform.State) error { 148 client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient 149 150 for _, rs := range s.RootModule().Resources { 151 if rs.Type != "azurerm_servicebus_topic" { 152 continue 153 } 154 155 name := rs.Primary.Attributes["name"] 156 namespaceName := rs.Primary.Attributes["namespace_name"] 157 resourceGroup := rs.Primary.Attributes["resource_group_name"] 158 159 resp, err := client.Get(resourceGroup, namespaceName, name) 160 if err != nil { 161 if resp.StatusCode == http.StatusNotFound { 162 return nil 163 } 164 return err 165 } 166 167 if resp.StatusCode != http.StatusNotFound { 168 return fmt.Errorf("ServiceBus Topic still exists:\n%#v", resp.TopicProperties) 169 } 170 } 171 172 return nil 173 } 174 175 func testCheckAzureRMServiceBusTopicExists(name string) resource.TestCheckFunc { 176 return func(s *terraform.State) error { 177 // Ensure we have enough information in state to look up in API 178 rs, ok := s.RootModule().Resources[name] 179 if !ok { 180 return fmt.Errorf("Not found: %s", name) 181 } 182 183 topicName := rs.Primary.Attributes["name"] 184 namespaceName := rs.Primary.Attributes["namespace_name"] 185 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 186 if !hasResourceGroup { 187 return fmt.Errorf("Bad: no resource group found in state for topic: %s", topicName) 188 } 189 190 client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient 191 192 resp, err := client.Get(resourceGroup, namespaceName, topicName) 193 if err != nil { 194 return fmt.Errorf("Bad: Get on serviceBusTopicsClient: %s", err) 195 } 196 197 if resp.StatusCode == http.StatusNotFound { 198 return fmt.Errorf("Bad: Topic %q (resource group: %q) does not exist", namespaceName, resourceGroup) 199 } 200 201 return nil 202 } 203 } 204 205 var testAccAzureRMServiceBusTopic_basic = ` 206 resource "azurerm_resource_group" "test" { 207 name = "acctestRG-%d" 208 location = "West US" 209 } 210 211 resource "azurerm_servicebus_namespace" "test" { 212 name = "acctestservicebusnamespace-%d" 213 location = "West US" 214 resource_group_name = "${azurerm_resource_group.test.name}" 215 sku = "standard" 216 } 217 218 resource "azurerm_servicebus_topic" "test" { 219 name = "acctestservicebustopic-%d" 220 location = "West US" 221 namespace_name = "${azurerm_servicebus_namespace.test.name}" 222 resource_group_name = "${azurerm_resource_group.test.name}" 223 } 224 ` 225 226 var testAccAzureRMServiceBusTopic_basicPremium = ` 227 resource "azurerm_resource_group" "test" { 228 name = "acctestRG-%d" 229 location = "West US" 230 } 231 232 resource "azurerm_servicebus_namespace" "test" { 233 name = "acctestservicebusnamespace-%d" 234 location = "West US" 235 resource_group_name = "${azurerm_resource_group.test.name}" 236 sku = "premium" 237 } 238 239 resource "azurerm_servicebus_topic" "test" { 240 name = "acctestservicebustopic-%d" 241 location = "West US" 242 namespace_name = "${azurerm_servicebus_namespace.test.name}" 243 resource_group_name = "${azurerm_resource_group.test.name}" 244 } 245 ` 246 247 var testAccAzureRMServiceBusTopic_update = ` 248 resource "azurerm_resource_group" "test" { 249 name = "acctestRG-%d" 250 location = "West US" 251 } 252 253 resource "azurerm_servicebus_namespace" "test" { 254 name = "acctestservicebusnamespace-%d" 255 location = "West US" 256 resource_group_name = "${azurerm_resource_group.test.name}" 257 sku = "standard" 258 } 259 260 resource "azurerm_servicebus_topic" "test" { 261 name = "acctestservicebustopic-%d" 262 location = "West US" 263 namespace_name = "${azurerm_servicebus_namespace.test.name}" 264 resource_group_name = "${azurerm_resource_group.test.name}" 265 enable_batched_operations = true 266 enable_express = true 267 } 268 ` 269 270 var testAccAzureRMServiceBusTopic_enablePartitioningStandard = ` 271 resource "azurerm_resource_group" "test" { 272 name = "acctestRG-%d" 273 location = "West US" 274 } 275 276 resource "azurerm_servicebus_namespace" "test" { 277 name = "acctestservicebusnamespace-%d" 278 location = "West US" 279 resource_group_name = "${azurerm_resource_group.test.name}" 280 sku = "standard" 281 } 282 283 resource "azurerm_servicebus_topic" "test" { 284 name = "acctestservicebustopic-%d" 285 location = "West US" 286 namespace_name = "${azurerm_servicebus_namespace.test.name}" 287 resource_group_name = "${azurerm_resource_group.test.name}" 288 enable_partitioning = true 289 max_size_in_megabytes = 5120 290 } 291 ` 292 293 var testAccAzureRMServiceBusTopic_enablePartitioningPremium = ` 294 resource "azurerm_resource_group" "test" { 295 name = "acctestRG-%d" 296 location = "West US" 297 } 298 299 resource "azurerm_servicebus_namespace" "test" { 300 name = "acctestservicebusnamespace-%d" 301 location = "West US" 302 resource_group_name = "${azurerm_resource_group.test.name}" 303 sku = "premium" 304 } 305 306 resource "azurerm_servicebus_topic" "test" { 307 name = "acctestservicebustopic-%d" 308 location = "West US" 309 namespace_name = "${azurerm_servicebus_namespace.test.name}" 310 resource_group_name = "${azurerm_resource_group.test.name}" 311 enable_partitioning = true 312 max_size_in_megabytes = 81920 313 } 314 ` 315 316 var testAccAzureRMServiceBusTopic_enableDuplicateDetection = ` 317 resource "azurerm_resource_group" "test" { 318 name = "acctestRG-%d" 319 location = "West US" 320 } 321 322 resource "azurerm_servicebus_namespace" "test" { 323 name = "acctestservicebusnamespace-%d" 324 location = "West US" 325 resource_group_name = "${azurerm_resource_group.test.name}" 326 sku = "standard" 327 } 328 329 resource "azurerm_servicebus_topic" "test" { 330 name = "acctestservicebustopic-%d" 331 location = "West US" 332 namespace_name = "${azurerm_servicebus_namespace.test.name}" 333 resource_group_name = "${azurerm_resource_group.test.name}" 334 requires_duplicate_detection = true 335 } 336 `