github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 testCheckAzureRMLoadBalancerProbeExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
    73  		if !exists {
    74  			return fmt.Errorf("A Probe with name %q cannot be found.", natRuleName)
    75  		}
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testCheckAzureRMLoadBalancerProbeNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  		_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
    84  		if exists {
    85  			return fmt.Errorf("A Probe with name %q has been found.", natRuleName)
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  func testAccAzureRMLoadBalancerProbe_basic(rInt int, probeName string) string {
    93  	return fmt.Sprintf(`
    94  resource "azurerm_resource_group" "test" {
    95      name = "acctestrg-%d"
    96      location = "West US"
    97  }
    98  
    99  resource "azurerm_public_ip" "test" {
   100      name = "test-ip-%d"
   101      location = "West US"
   102      resource_group_name = "${azurerm_resource_group.test.name}"
   103      public_ip_address_allocation = "static"
   104  }
   105  
   106  resource "azurerm_lb" "test" {
   107      name = "arm-test-loadbalancer-%d"
   108      location = "West US"
   109      resource_group_name = "${azurerm_resource_group.test.name}"
   110  
   111      frontend_ip_configuration {
   112        name = "one-%d"
   113        public_ip_address_id = "${azurerm_public_ip.test.id}"
   114      }
   115  }
   116  
   117  resource "azurerm_lb_probe" "test" {
   118    location = "West US"
   119    resource_group_name = "${azurerm_resource_group.test.name}"
   120    loadbalancer_id = "${azurerm_lb.test.id}"
   121    name = "%s"
   122    port = 22
   123  }
   124  `, rInt, rInt, rInt, rInt, probeName)
   125  }
   126  
   127  func testAccAzureRMLoadBalancerProbe_removal(rInt int) string {
   128  	return fmt.Sprintf(`
   129  resource "azurerm_resource_group" "test" {
   130      name = "acctestrg-%d"
   131      location = "West US"
   132  }
   133  
   134  resource "azurerm_public_ip" "test" {
   135      name = "test-ip-%d"
   136      location = "West US"
   137      resource_group_name = "${azurerm_resource_group.test.name}"
   138      public_ip_address_allocation = "static"
   139  }
   140  
   141  resource "azurerm_lb" "test" {
   142      name = "arm-test-loadbalancer-%d"
   143      location = "West US"
   144      resource_group_name = "${azurerm_resource_group.test.name}"
   145  
   146      frontend_ip_configuration {
   147        name = "one-%d"
   148        public_ip_address_id = "${azurerm_public_ip.test.id}"
   149      }
   150  }
   151  `, rInt, rInt, rInt, rInt)
   152  }