github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccAzureRMLoadBalancerProbe_disappears(t *testing.T) {
   168  	var lb network.LoadBalancer
   169  	ri := acctest.RandInt()
   170  	probeName := fmt.Sprintf("probe-%d", ri)
   171  
   172  	resource.Test(t, resource.TestCase{
   173  		PreCheck:     func() { testAccPreCheck(t) },
   174  		Providers:    testAccProviders,
   175  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   176  		Steps: []resource.TestStep{
   177  			{
   178  				Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
   179  				Check: resource.ComposeTestCheckFunc(
   180  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   181  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
   182  					testCheckAzureRMLoadBalancerProbeDisappears(probeName, &lb),
   183  				),
   184  				ExpectNonEmptyPlan: true,
   185  			},
   186  		},
   187  	})
   188  }
   189  
   190  func testCheckAzureRMLoadBalancerProbeExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   191  	return func(s *terraform.State) error {
   192  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
   193  		if !exists {
   194  			return fmt.Errorf("A Probe with name %q cannot be found.", natRuleName)
   195  		}
   196  
   197  		return nil
   198  	}
   199  }
   200  
   201  func testCheckAzureRMLoadBalancerProbeNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   202  	return func(s *terraform.State) error {
   203  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
   204  		if exists {
   205  			return fmt.Errorf("A Probe with name %q has been found.", natRuleName)
   206  		}
   207  
   208  		return nil
   209  	}
   210  }
   211  
   212  func testCheckAzureRMLoadBalancerProbeDisappears(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   213  	return func(s *terraform.State) error {
   214  		conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
   215  
   216  		_, i, exists := findLoadBalancerProbeByName(lb, addressPoolName)
   217  		if !exists {
   218  			return fmt.Errorf("A Probe with name %q cannot be found.", addressPoolName)
   219  		}
   220  
   221  		currentProbes := *lb.LoadBalancerPropertiesFormat.Probes
   222  		probes := append(currentProbes[:i], currentProbes[i+1:]...)
   223  		lb.LoadBalancerPropertiesFormat.Probes = &probes
   224  
   225  		id, err := parseAzureResourceID(*lb.ID)
   226  		if err != nil {
   227  			return err
   228  		}
   229  
   230  		_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
   231  		if err != nil {
   232  			return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
   233  		}
   234  
   235  		_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
   236  		return err
   237  	}
   238  }
   239  
   240  func testAccAzureRMLoadBalancerProbe_basic(rInt int, probeName string) string {
   241  	return fmt.Sprintf(`
   242  resource "azurerm_resource_group" "test" {
   243      name = "acctestrg-%d"
   244      location = "West US"
   245  }
   246  
   247  resource "azurerm_public_ip" "test" {
   248      name = "test-ip-%d"
   249      location = "West US"
   250      resource_group_name = "${azurerm_resource_group.test.name}"
   251      public_ip_address_allocation = "static"
   252  }
   253  
   254  resource "azurerm_lb" "test" {
   255      name = "arm-test-loadbalancer-%d"
   256      location = "West US"
   257      resource_group_name = "${azurerm_resource_group.test.name}"
   258  
   259      frontend_ip_configuration {
   260        name = "one-%d"
   261        public_ip_address_id = "${azurerm_public_ip.test.id}"
   262      }
   263  }
   264  
   265  resource "azurerm_lb_probe" "test" {
   266    location = "West US"
   267    resource_group_name = "${azurerm_resource_group.test.name}"
   268    loadbalancer_id = "${azurerm_lb.test.id}"
   269    name = "%s"
   270    port = 22
   271  }
   272  `, rInt, rInt, rInt, rInt, probeName)
   273  }
   274  
   275  func testAccAzureRMLoadBalancerProbe_removal(rInt int) string {
   276  	return fmt.Sprintf(`
   277  resource "azurerm_resource_group" "test" {
   278      name = "acctestrg-%d"
   279      location = "West US"
   280  }
   281  
   282  resource "azurerm_public_ip" "test" {
   283      name = "test-ip-%d"
   284      location = "West US"
   285      resource_group_name = "${azurerm_resource_group.test.name}"
   286      public_ip_address_allocation = "static"
   287  }
   288  
   289  resource "azurerm_lb" "test" {
   290      name = "arm-test-loadbalancer-%d"
   291      location = "West US"
   292      resource_group_name = "${azurerm_resource_group.test.name}"
   293  
   294      frontend_ip_configuration {
   295        name = "one-%d"
   296        public_ip_address_id = "${azurerm_public_ip.test.id}"
   297      }
   298  }
   299  `, rInt, rInt, rInt, rInt)
   300  }
   301  
   302  func testAccAzureRMLoadBalancerProbe_multipleProbes(rInt int, probeName, probe2Name string) string {
   303  	return fmt.Sprintf(`
   304  resource "azurerm_resource_group" "test" {
   305      name = "acctestrg-%d"
   306      location = "West US"
   307  }
   308  
   309  resource "azurerm_public_ip" "test" {
   310      name = "test-ip-%d"
   311      location = "West US"
   312      resource_group_name = "${azurerm_resource_group.test.name}"
   313      public_ip_address_allocation = "static"
   314  }
   315  
   316  resource "azurerm_lb" "test" {
   317      name = "arm-test-loadbalancer-%d"
   318      location = "West US"
   319      resource_group_name = "${azurerm_resource_group.test.name}"
   320  
   321      frontend_ip_configuration {
   322        name = "one-%d"
   323        public_ip_address_id = "${azurerm_public_ip.test.id}"
   324      }
   325  }
   326  
   327  resource "azurerm_lb_probe" "test" {
   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 = 22
   333  }
   334  
   335  resource "azurerm_lb_probe" "test2" {
   336    location = "West US"
   337    resource_group_name = "${azurerm_resource_group.test.name}"
   338    loadbalancer_id = "${azurerm_lb.test.id}"
   339    name = "%s"
   340    port = 80
   341  }
   342  `, rInt, rInt, rInt, rInt, probeName, probe2Name)
   343  }
   344  
   345  func testAccAzureRMLoadBalancerProbe_multipleProbesUpdate(rInt int, probeName, probe2Name string) string {
   346  	return fmt.Sprintf(`
   347  resource "azurerm_resource_group" "test" {
   348      name = "acctestrg-%d"
   349      location = "West US"
   350  }
   351  
   352  resource "azurerm_public_ip" "test" {
   353      name = "test-ip-%d"
   354      location = "West US"
   355      resource_group_name = "${azurerm_resource_group.test.name}"
   356      public_ip_address_allocation = "static"
   357  }
   358  
   359  resource "azurerm_lb" "test" {
   360      name = "arm-test-loadbalancer-%d"
   361      location = "West US"
   362      resource_group_name = "${azurerm_resource_group.test.name}"
   363  
   364      frontend_ip_configuration {
   365        name = "one-%d"
   366        public_ip_address_id = "${azurerm_public_ip.test.id}"
   367      }
   368  }
   369  
   370  resource "azurerm_lb_probe" "test" {
   371    location = "West US"
   372    resource_group_name = "${azurerm_resource_group.test.name}"
   373    loadbalancer_id = "${azurerm_lb.test.id}"
   374    name = "%s"
   375    port = 22
   376  }
   377  
   378  resource "azurerm_lb_probe" "test2" {
   379    location = "West US"
   380    resource_group_name = "${azurerm_resource_group.test.name}"
   381    loadbalancer_id = "${azurerm_lb.test.id}"
   382    name = "%s"
   383    port = 8080
   384  }
   385  `, rInt, rInt, rInt, rInt, probeName, probe2Name)
   386  }
   387  
   388  func testAccAzureRMLoadBalancerProbe_updateProtocolBefore(rInt int, probeName string) string {
   389  	return fmt.Sprintf(`
   390  resource "azurerm_resource_group" "test" {
   391      name = "acctestrg-%d"
   392      location = "West US"
   393  }
   394  
   395  resource "azurerm_public_ip" "test" {
   396      name = "test-ip-%d"
   397      location = "West US"
   398      resource_group_name = "${azurerm_resource_group.test.name}"
   399      public_ip_address_allocation = "static"
   400  }
   401  
   402  resource "azurerm_lb" "test" {
   403      name = "arm-test-loadbalancer-%d"
   404      location = "West US"
   405      resource_group_name = "${azurerm_resource_group.test.name}"
   406  
   407      frontend_ip_configuration {
   408        name = "one-%d"
   409        public_ip_address_id = "${azurerm_public_ip.test.id}"
   410      }
   411  }
   412  
   413  resource "azurerm_lb_probe" "test" {
   414    location = "West US"
   415    resource_group_name = "${azurerm_resource_group.test.name}"
   416    loadbalancer_id = "${azurerm_lb.test.id}"
   417    name = "%s"
   418    protocol = "Http"
   419    request_path = "/"
   420    port = 80
   421  }
   422  `, rInt, rInt, rInt, rInt, probeName)
   423  }
   424  
   425  func testAccAzureRMLoadBalancerProbe_updateProtocolAfter(rInt int, probeName string) string {
   426  	return fmt.Sprintf(`
   427  resource "azurerm_resource_group" "test" {
   428      name = "acctestrg-%d"
   429      location = "West US"
   430  }
   431  
   432  resource "azurerm_public_ip" "test" {
   433      name = "test-ip-%d"
   434      location = "West US"
   435      resource_group_name = "${azurerm_resource_group.test.name}"
   436      public_ip_address_allocation = "static"
   437  }
   438  
   439  resource "azurerm_lb" "test" {
   440      name = "arm-test-loadbalancer-%d"
   441      location = "West US"
   442      resource_group_name = "${azurerm_resource_group.test.name}"
   443  
   444      frontend_ip_configuration {
   445        name = "one-%d"
   446        public_ip_address_id = "${azurerm_public_ip.test.id}"
   447      }
   448  }
   449  
   450  resource "azurerm_lb_probe" "test" {
   451    location = "West US"
   452    resource_group_name = "${azurerm_resource_group.test.name}"
   453    loadbalancer_id = "${azurerm_lb.test.id}"
   454    name = "%s"
   455    protocol = "Tcp"
   456    port = 80
   457  }
   458  `, rInt, rInt, rInt, rInt, probeName)
   459  }