github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/builtin/providers/azurerm/resource_arm_loadbalancer_probe_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/arm/network"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMLoadBalancerProbe_basic(t *testing.T) {
    14  	var lb network.LoadBalancer
    15  	ri := acctest.RandInt()
    16  	probeName := fmt.Sprintf("probe-%d", ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    27  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureRMLoadBalancerProbe_removal(t *testing.T) {
    35  	var lb network.LoadBalancer
    36  	ri := acctest.RandInt()
    37  	probeName := fmt.Sprintf("probe-%d", ri)
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    43  		Steps: []resource.TestStep{
    44  			{
    45  				Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    48  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
    49  				),
    50  			},
    51  			{
    52  				Config: testAccAzureRMLoadBalancerProbe_removal(ri),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    55  					testCheckAzureRMLoadBalancerProbeNotExists(probeName, &lb),
    56  				),
    57  			},
    58  		},
    59  	})
    60  }
    61  
    62  func testCheckAzureRMLoadBalancerProbeExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
    65  		if !exists {
    66  			return fmt.Errorf("A Probe with name %q cannot be found.", natRuleName)
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  func testCheckAzureRMLoadBalancerProbeNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
    76  		if exists {
    77  			return fmt.Errorf("A Probe with name %q has been found.", natRuleName)
    78  		}
    79  
    80  		return nil
    81  	}
    82  }
    83  
    84  func testAccAzureRMLoadBalancerProbe_basic(rInt int, probeName string) string {
    85  	return fmt.Sprintf(`
    86  resource "azurerm_resource_group" "test" {
    87      name = "acctestrg-%d"
    88      location = "West US"
    89  }
    90  
    91  resource "azurerm_public_ip" "test" {
    92      name = "test-ip-%d"
    93      location = "West US"
    94      resource_group_name = "${azurerm_resource_group.test.name}"
    95      public_ip_address_allocation = "static"
    96  }
    97  
    98  resource "azurerm_lb" "test" {
    99      name = "arm-test-loadbalancer-%d"
   100      location = "West US"
   101      resource_group_name = "${azurerm_resource_group.test.name}"
   102  
   103      frontend_ip_configuration {
   104        name = "one-%d"
   105        public_ip_address_id = "${azurerm_public_ip.test.id}"
   106      }
   107  }
   108  
   109  resource "azurerm_lb_probe" "test" {
   110    location = "West US"
   111    resource_group_name = "${azurerm_resource_group.test.name}"
   112    loadbalancer_id = "${azurerm_lb.test.id}"
   113    name = "%s"
   114    port = 22
   115  }
   116  `, rInt, rInt, rInt, rInt, probeName)
   117  }
   118  
   119  func testAccAzureRMLoadBalancerProbe_removal(rInt int) string {
   120  	return fmt.Sprintf(`
   121  resource "azurerm_resource_group" "test" {
   122      name = "acctestrg-%d"
   123      location = "West US"
   124  }
   125  
   126  resource "azurerm_public_ip" "test" {
   127      name = "test-ip-%d"
   128      location = "West US"
   129      resource_group_name = "${azurerm_resource_group.test.name}"
   130      public_ip_address_allocation = "static"
   131  }
   132  
   133  resource "azurerm_lb" "test" {
   134      name = "arm-test-loadbalancer-%d"
   135      location = "West US"
   136      resource_group_name = "${azurerm_resource_group.test.name}"
   137  
   138      frontend_ip_configuration {
   139        name = "one-%d"
   140        public_ip_address_id = "${azurerm_public_ip.test.id}"
   141      }
   142  }
   143  `, rInt, rInt, rInt, rInt)
   144  }