github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule_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 TestAccAzureRMLoadBalancerNatRule_basic(t *testing.T) { 14 var lb network.LoadBalancer 15 ri := acctest.RandInt() 16 natRuleName := fmt.Sprintf("NatRule-%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: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName), 25 Check: resource.ComposeTestCheckFunc( 26 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 27 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccAzureRMLoadBalancerNatRule_removal(t *testing.T) { 35 var lb network.LoadBalancer 36 ri := acctest.RandInt() 37 natRuleName := fmt.Sprintf("NatRule-%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: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName), 46 Check: resource.ComposeTestCheckFunc( 47 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 48 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 49 ), 50 }, 51 { 52 Config: testAccAzureRMLoadBalancerNatRule_removal(ri), 53 Check: resource.ComposeTestCheckFunc( 54 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 55 testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName, &lb), 56 ), 57 }, 58 }, 59 }) 60 } 61 62 func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 63 return func(s *terraform.State) error { 64 _, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName) 65 if !exists { 66 return fmt.Errorf("A NAT Rule with name %q cannot be found.", natRuleName) 67 } 68 69 return nil 70 } 71 } 72 73 func testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 _, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName) 76 if exists { 77 return fmt.Errorf("A NAT Rule with name %q has been found.", natRuleName) 78 } 79 80 return nil 81 } 82 } 83 84 func testAccAzureRMLoadBalancerNatRule_basic(rInt int, natRuleName 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_nat_rule" "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 protocol = "Tcp" 115 frontend_port = 3389 116 backend_port = 3389 117 frontend_ip_configuration_name = "one-%d" 118 } 119 120 `, rInt, rInt, rInt, rInt, natRuleName, rInt) 121 } 122 123 func testAccAzureRMLoadBalancerNatRule_removal(rInt int) string { 124 return fmt.Sprintf(` 125 resource "azurerm_resource_group" "test" { 126 name = "acctestrg-%d" 127 location = "West US" 128 } 129 130 resource "azurerm_public_ip" "test" { 131 name = "test-ip-%d" 132 location = "West US" 133 resource_group_name = "${azurerm_resource_group.test.name}" 134 public_ip_address_allocation = "static" 135 } 136 137 resource "azurerm_lb" "test" { 138 name = "arm-test-loadbalancer-%d" 139 location = "West US" 140 resource_group_name = "${azurerm_resource_group.test.name}" 141 142 frontend_ip_configuration { 143 name = "one-%d" 144 public_ip_address_id = "${azurerm_public_ip.test.id}" 145 } 146 } 147 `, rInt, rInt, rInt, rInt) 148 }