github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/builtin/providers/azurerm/resource_arm_loadbalancer_probe_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/network"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAzureRMLoadBalancerProbe_basic(t *testing.T) {
    15  	var lb network.LoadBalancer
    16  	ri := acctest.RandInt()
    17  	probeName := fmt.Sprintf("probe-%d", ri)
    18  
    19  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    20  	probe_id := fmt.Sprintf(
    21  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/probes/%s",
    22  		subscriptionID, ri, ri, probeName)
    23  
    24  	resource.Test(t, resource.TestCase{
    25  		PreCheck:     func() { testAccPreCheck(t) },
    26  		Providers:    testAccProviders,
    27  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    28  		Steps: []resource.TestStep{
    29  			{
    30  				Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    33  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
    34  					resource.TestCheckResourceAttr(
    35  						"azurerm_lb_probe.test", "id", probe_id),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAzureRMLoadBalancerProbe_removal(t *testing.T) {
    43  	var lb network.LoadBalancer
    44  	ri := acctest.RandInt()
    45  	probeName := fmt.Sprintf("probe-%d", ri)
    46  
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    51  		Steps: []resource.TestStep{
    52  			{
    53  				Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    56  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
    57  				),
    58  			},
    59  			{
    60  				Config: testAccAzureRMLoadBalancerProbe_removal(ri),
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    63  					testCheckAzureRMLoadBalancerProbeNotExists(probeName, &lb),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAzureRMLoadBalancerProbe_update(t *testing.T) {
    71  	var lb network.LoadBalancer
    72  	ri := acctest.RandInt()
    73  	probeName := fmt.Sprintf("probe-%d", ri)
    74  	probe2Name := fmt.Sprintf("probe-%d", acctest.RandInt())
    75  
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:     func() { testAccPreCheck(t) },
    78  		Providers:    testAccProviders,
    79  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    80  		Steps: []resource.TestStep{
    81  			{
    82  				Config: testAccAzureRMLoadBalancerProbe_multipleProbes(ri, probeName, probe2Name),
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    85  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
    86  					testCheckAzureRMLoadBalancerProbeExists(probe2Name, &lb),
    87  					resource.TestCheckResourceAttr("azurerm_lb_probe.test2", "port", "80"),
    88  				),
    89  			},
    90  			{
    91  				Config: testAccAzureRMLoadBalancerProbe_multipleProbesUpdate(ri, probeName, probe2Name),
    92  				Check: resource.ComposeTestCheckFunc(
    93  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    94  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
    95  					testCheckAzureRMLoadBalancerProbeExists(probe2Name, &lb),
    96  					resource.TestCheckResourceAttr("azurerm_lb_probe.test2", "port", "8080"),
    97  				),
    98  			},
    99  		},
   100  	})
   101  }
   102  
   103  func TestAccAzureRMLoadBalancerProbe_updateProtocol(t *testing.T) {
   104  	var lb network.LoadBalancer
   105  	ri := acctest.RandInt()
   106  	probeName := fmt.Sprintf("probe-%d", ri)
   107  
   108  	resource.Test(t, resource.TestCase{
   109  		PreCheck:     func() { testAccPreCheck(t) },
   110  		Providers:    testAccProviders,
   111  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   112  		Steps: []resource.TestStep{
   113  			{
   114  				Config: testAccAzureRMLoadBalancerProbe_updateProtocolBefore(ri, probeName),
   115  				Check: resource.ComposeTestCheckFunc(
   116  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   117  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
   118  					resource.TestCheckResourceAttr("azurerm_lb_probe.test", "protocol", "Http"),
   119  				),
   120  			},
   121  			{
   122  				Config: testAccAzureRMLoadBalancerProbe_updateProtocolAfter(ri, probeName),
   123  				Check: resource.ComposeTestCheckFunc(
   124  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   125  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
   126  					resource.TestCheckResourceAttr("azurerm_lb_probe.test", "protocol", "Tcp"),
   127  				),
   128  			},
   129  		},
   130  	})
   131  }
   132  
   133  func TestAccAzureRMLoadBalancerProbe_reapply(t *testing.T) {
   134  	var lb network.LoadBalancer
   135  	ri := acctest.RandInt()
   136  	probeName := fmt.Sprintf("probe-%d", ri)
   137  
   138  	deleteProbeState := func(s *terraform.State) error {
   139  		return s.Remove("azurerm_lb_probe.test")
   140  	}
   141  
   142  	resource.Test(t, resource.TestCase{
   143  		PreCheck:     func() { testAccPreCheck(t) },
   144  		Providers:    testAccProviders,
   145  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   146  		Steps: []resource.TestStep{
   147  			{
   148  				Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
   149  				Check: resource.ComposeTestCheckFunc(
   150  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   151  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
   152  					deleteProbeState,
   153  				),
   154  				ExpectNonEmptyPlan: true,
   155  			},
   156  			{
   157  				Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
   158  				Check: resource.ComposeTestCheckFunc(
   159  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   160  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
   161  				),
   162  			},
   163  		},
   164  	})
   165  }
   166  
   167  func testCheckAzureRMLoadBalancerProbeExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   168  	return func(s *terraform.State) error {
   169  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
   170  		if !exists {
   171  			return fmt.Errorf("A Probe with name %q cannot be found.", natRuleName)
   172  		}
   173  
   174  		return nil
   175  	}
   176  }
   177  
   178  func testCheckAzureRMLoadBalancerProbeNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   179  	return func(s *terraform.State) error {
   180  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
   181  		if exists {
   182  			return fmt.Errorf("A Probe with name %q has been found.", natRuleName)
   183  		}
   184  
   185  		return nil
   186  	}
   187  }
   188  
   189  func testAccAzureRMLoadBalancerProbe_basic(rInt int, probeName string) string {
   190  	return fmt.Sprintf(`
   191  resource "azurerm_resource_group" "test" {
   192      name = "acctestrg-%d"
   193      location = "West US"
   194  }
   195  
   196  resource "azurerm_public_ip" "test" {
   197      name = "test-ip-%d"
   198      location = "West US"
   199      resource_group_name = "${azurerm_resource_group.test.name}"
   200      public_ip_address_allocation = "static"
   201  }
   202  
   203  resource "azurerm_lb" "test" {
   204      name = "arm-test-loadbalancer-%d"
   205      location = "West US"
   206      resource_group_name = "${azurerm_resource_group.test.name}"
   207  
   208      frontend_ip_configuration {
   209        name = "one-%d"
   210        public_ip_address_id = "${azurerm_public_ip.test.id}"
   211      }
   212  }
   213  
   214  resource "azurerm_lb_probe" "test" {
   215    location = "West US"
   216    resource_group_name = "${azurerm_resource_group.test.name}"
   217    loadbalancer_id = "${azurerm_lb.test.id}"
   218    name = "%s"
   219    port = 22
   220  }
   221  `, rInt, rInt, rInt, rInt, probeName)
   222  }
   223  
   224  func testAccAzureRMLoadBalancerProbe_removal(rInt int) string {
   225  	return fmt.Sprintf(`
   226  resource "azurerm_resource_group" "test" {
   227      name = "acctestrg-%d"
   228      location = "West US"
   229  }
   230  
   231  resource "azurerm_public_ip" "test" {
   232      name = "test-ip-%d"
   233      location = "West US"
   234      resource_group_name = "${azurerm_resource_group.test.name}"
   235      public_ip_address_allocation = "static"
   236  }
   237  
   238  resource "azurerm_lb" "test" {
   239      name = "arm-test-loadbalancer-%d"
   240      location = "West US"
   241      resource_group_name = "${azurerm_resource_group.test.name}"
   242  
   243      frontend_ip_configuration {
   244        name = "one-%d"
   245        public_ip_address_id = "${azurerm_public_ip.test.id}"
   246      }
   247  }
   248  `, rInt, rInt, rInt, rInt)
   249  }
   250  
   251  func testAccAzureRMLoadBalancerProbe_multipleProbes(rInt int, probeName, probe2Name string) string {
   252  	return fmt.Sprintf(`
   253  resource "azurerm_resource_group" "test" {
   254      name = "acctestrg-%d"
   255      location = "West US"
   256  }
   257  
   258  resource "azurerm_public_ip" "test" {
   259      name = "test-ip-%d"
   260      location = "West US"
   261      resource_group_name = "${azurerm_resource_group.test.name}"
   262      public_ip_address_allocation = "static"
   263  }
   264  
   265  resource "azurerm_lb" "test" {
   266      name = "arm-test-loadbalancer-%d"
   267      location = "West US"
   268      resource_group_name = "${azurerm_resource_group.test.name}"
   269  
   270      frontend_ip_configuration {
   271        name = "one-%d"
   272        public_ip_address_id = "${azurerm_public_ip.test.id}"
   273      }
   274  }
   275  
   276  resource "azurerm_lb_probe" "test" {
   277    location = "West US"
   278    resource_group_name = "${azurerm_resource_group.test.name}"
   279    loadbalancer_id = "${azurerm_lb.test.id}"
   280    name = "%s"
   281    port = 22
   282  }
   283  
   284  resource "azurerm_lb_probe" "test2" {
   285    location = "West US"
   286    resource_group_name = "${azurerm_resource_group.test.name}"
   287    loadbalancer_id = "${azurerm_lb.test.id}"
   288    name = "%s"
   289    port = 80
   290  }
   291  `, rInt, rInt, rInt, rInt, probeName, probe2Name)
   292  }
   293  
   294  func testAccAzureRMLoadBalancerProbe_multipleProbesUpdate(rInt int, probeName, probe2Name string) string {
   295  	return fmt.Sprintf(`
   296  resource "azurerm_resource_group" "test" {
   297      name = "acctestrg-%d"
   298      location = "West US"
   299  }
   300  
   301  resource "azurerm_public_ip" "test" {
   302      name = "test-ip-%d"
   303      location = "West US"
   304      resource_group_name = "${azurerm_resource_group.test.name}"
   305      public_ip_address_allocation = "static"
   306  }
   307  
   308  resource "azurerm_lb" "test" {
   309      name = "arm-test-loadbalancer-%d"
   310      location = "West US"
   311      resource_group_name = "${azurerm_resource_group.test.name}"
   312  
   313      frontend_ip_configuration {
   314        name = "one-%d"
   315        public_ip_address_id = "${azurerm_public_ip.test.id}"
   316      }
   317  }
   318  
   319  resource "azurerm_lb_probe" "test" {
   320    location = "West US"
   321    resource_group_name = "${azurerm_resource_group.test.name}"
   322    loadbalancer_id = "${azurerm_lb.test.id}"
   323    name = "%s"
   324    port = 22
   325  }
   326  
   327  resource "azurerm_lb_probe" "test2" {
   328    location = "West US"
   329    resource_group_name = "${azurerm_resource_group.test.name}"
   330    loadbalancer_id = "${azurerm_lb.test.id}"
   331    name = "%s"
   332    port = 8080
   333  }
   334  `, rInt, rInt, rInt, rInt, probeName, probe2Name)
   335  }
   336  
   337  func testAccAzureRMLoadBalancerProbe_updateProtocolBefore(rInt int, probeName string) string {
   338  	return fmt.Sprintf(`
   339  resource "azurerm_resource_group" "test" {
   340      name = "acctestrg-%d"
   341      location = "West US"
   342  }
   343  
   344  resource "azurerm_public_ip" "test" {
   345      name = "test-ip-%d"
   346      location = "West US"
   347      resource_group_name = "${azurerm_resource_group.test.name}"
   348      public_ip_address_allocation = "static"
   349  }
   350  
   351  resource "azurerm_lb" "test" {
   352      name = "arm-test-loadbalancer-%d"
   353      location = "West US"
   354      resource_group_name = "${azurerm_resource_group.test.name}"
   355  
   356      frontend_ip_configuration {
   357        name = "one-%d"
   358        public_ip_address_id = "${azurerm_public_ip.test.id}"
   359      }
   360  }
   361  
   362  resource "azurerm_lb_probe" "test" {
   363    location = "West US"
   364    resource_group_name = "${azurerm_resource_group.test.name}"
   365    loadbalancer_id = "${azurerm_lb.test.id}"
   366    name = "%s"
   367    protocol = "Http"
   368    request_path = "/"
   369    port = 80
   370  }
   371  `, rInt, rInt, rInt, rInt, probeName)
   372  }
   373  
   374  func testAccAzureRMLoadBalancerProbe_updateProtocolAfter(rInt int, probeName string) string {
   375  	return fmt.Sprintf(`
   376  resource "azurerm_resource_group" "test" {
   377      name = "acctestrg-%d"
   378      location = "West US"
   379  }
   380  
   381  resource "azurerm_public_ip" "test" {
   382      name = "test-ip-%d"
   383      location = "West US"
   384      resource_group_name = "${azurerm_resource_group.test.name}"
   385      public_ip_address_allocation = "static"
   386  }
   387  
   388  resource "azurerm_lb" "test" {
   389      name = "arm-test-loadbalancer-%d"
   390      location = "West US"
   391      resource_group_name = "${azurerm_resource_group.test.name}"
   392  
   393      frontend_ip_configuration {
   394        name = "one-%d"
   395        public_ip_address_id = "${azurerm_public_ip.test.id}"
   396      }
   397  }
   398  
   399  resource "azurerm_lb_probe" "test" {
   400    location = "West US"
   401    resource_group_name = "${azurerm_resource_group.test.name}"
   402    loadbalancer_id = "${azurerm_lb.test.id}"
   403    name = "%s"
   404    protocol = "Tcp"
   405    port = 80
   406  }
   407  `, rInt, rInt, rInt, rInt, probeName)
   408  }