mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-08 15:13:56 -06:00
b869e1682a
If an error occurred which prevented the lb sub resources being written to state then the next apply would fail as the resources would already exist in the API. go test -c ./builtin/providers/azurerm -o ./builtin/providers/azurerm/test-azurerm TestAccAzureRMLoadBalancerBackEndAddressPool_reapply TestAccAzureRMLoadBalancerBackEndAddressPool_removal TestAccAzureRMLoadBalancerNatPool_basic TestAccAzureRMLoadBalancerBackEndAddressPool_basic TestAccAzureRMLoadBalancerNatRule_basic TestAccAzureRMLoadBalancerNatPool_reapply TestAccAzureRMLoadBalancerNatPool_removal TestAccAzureRMLoadBalancerNatPool_update TestAccAzureRMLoadBalancerProbe_basic TestAccAzureRMLoadBalancerNatRule_removal TestAccAzureRMLoadBalancerNatRule_update TestAccAzureRMLoadBalancerNatRule_reapply TestAccAzureRMLoadBalancerProbe_removal TestAccAzureRMLoadBalancerProbe_reapply TestAccAzureRMLoadBalancerProbe_update TestAccAzureRMLoadBalancerRule_basic TestAccAzureRMLoadBalancerRule_inconsistentReads TestAccAzureRMLoadBalancerRule_removal TestAccAzureRMLoadBalancerProbe_updateProtocol TestAccAzureRMLoadBalancer_basic TestAccAzureRMLoadBalancerRule_update TestAccAzureRMLoadBalancerRule_reapply TestAccAzureRMLoadBalancer_frontEndConfig TestAccAzureRMLoadBalancer_tags
326 lines
9.6 KiB
Go
326 lines
9.6 KiB
Go
package azurerm
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccAzureRMLoadBalancerNatRule_basic(t *testing.T) {
|
|
var lb network.LoadBalancer
|
|
ri := acctest.RandInt()
|
|
natRuleName := fmt.Sprintf("NatRule-%d", ri)
|
|
|
|
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
|
|
natRule_id := fmt.Sprintf(
|
|
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatRules/%s",
|
|
subscriptionID, ri, ri, natRuleName)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
|
|
resource.TestCheckResourceAttr(
|
|
"azurerm_lb_nat_rule.test", "id", natRule_id),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAzureRMLoadBalancerNatRule_removal(t *testing.T) {
|
|
var lb network.LoadBalancer
|
|
ri := acctest.RandInt()
|
|
natRuleName := fmt.Sprintf("NatRule-%d", ri)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
|
|
),
|
|
},
|
|
{
|
|
Config: testAccAzureRMLoadBalancerNatRule_removal(ri),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName, &lb),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAzureRMLoadBalancerNatRule_update(t *testing.T) {
|
|
var lb network.LoadBalancer
|
|
ri := acctest.RandInt()
|
|
natRuleName := fmt.Sprintf("NatRule-%d", ri)
|
|
natRule2Name := fmt.Sprintf("NatRule-%d", acctest.RandInt())
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAzureRMLoadBalancerNatRule_multipleRules(ri, natRuleName, natRule2Name),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb),
|
|
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3390"),
|
|
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3390"),
|
|
),
|
|
},
|
|
{
|
|
Config: testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(ri, natRuleName, natRule2Name),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb),
|
|
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3391"),
|
|
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3391"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAzureRMLoadBalancerNatRule_reapply(t *testing.T) {
|
|
var lb network.LoadBalancer
|
|
ri := acctest.RandInt()
|
|
natRuleName := fmt.Sprintf("NatRule-%d", ri)
|
|
|
|
deleteNatRuleState := func(s *terraform.State) error {
|
|
return s.Remove("azurerm_lb_nat_rule.test")
|
|
}
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
|
|
deleteNatRuleState,
|
|
),
|
|
ExpectNonEmptyPlan: true,
|
|
},
|
|
{
|
|
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
_, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
|
|
if !exists {
|
|
return fmt.Errorf("A NAT Rule with name %q cannot be found.", natRuleName)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
_, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
|
|
if exists {
|
|
return fmt.Errorf("A NAT Rule with name %q has been found.", natRuleName)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancerNatRule_basic(rInt int, natRuleName string) string {
|
|
return fmt.Sprintf(`
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
name = "test-ip-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
public_ip_address_allocation = "static"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
frontend_ip_configuration {
|
|
name = "one-%d"
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
}
|
|
}
|
|
|
|
resource "azurerm_lb_nat_rule" "test" {
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
name = "%s"
|
|
protocol = "Tcp"
|
|
frontend_port = 3389
|
|
backend_port = 3389
|
|
frontend_ip_configuration_name = "one-%d"
|
|
}
|
|
|
|
`, rInt, rInt, rInt, rInt, natRuleName, rInt)
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancerNatRule_removal(rInt int) string {
|
|
return fmt.Sprintf(`
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
name = "test-ip-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
public_ip_address_allocation = "static"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
frontend_ip_configuration {
|
|
name = "one-%d"
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
}
|
|
}
|
|
`, rInt, rInt, rInt, rInt)
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancerNatRule_multipleRules(rInt int, natRuleName, natRule2Name string) string {
|
|
return fmt.Sprintf(`
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
name = "test-ip-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
public_ip_address_allocation = "static"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
frontend_ip_configuration {
|
|
name = "one-%d"
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
}
|
|
}
|
|
|
|
resource "azurerm_lb_nat_rule" "test" {
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
name = "%s"
|
|
protocol = "Tcp"
|
|
frontend_port = 3389
|
|
backend_port = 3389
|
|
frontend_ip_configuration_name = "one-%d"
|
|
}
|
|
|
|
resource "azurerm_lb_nat_rule" "test2" {
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
name = "%s"
|
|
protocol = "Tcp"
|
|
frontend_port = 3390
|
|
backend_port = 3390
|
|
frontend_ip_configuration_name = "one-%d"
|
|
}
|
|
|
|
`, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt)
|
|
}
|
|
|
|
func testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(rInt int, natRuleName, natRule2Name string) string {
|
|
return fmt.Sprintf(`
|
|
resource "azurerm_resource_group" "test" {
|
|
name = "acctestrg-%d"
|
|
location = "West US"
|
|
}
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
name = "test-ip-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
public_ip_address_allocation = "static"
|
|
}
|
|
|
|
resource "azurerm_lb" "test" {
|
|
name = "arm-test-loadbalancer-%d"
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
frontend_ip_configuration {
|
|
name = "one-%d"
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
}
|
|
}
|
|
|
|
resource "azurerm_lb_nat_rule" "test" {
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
name = "%s"
|
|
protocol = "Tcp"
|
|
frontend_port = 3389
|
|
backend_port = 3389
|
|
frontend_ip_configuration_name = "one-%d"
|
|
}
|
|
|
|
resource "azurerm_lb_nat_rule" "test2" {
|
|
location = "West US"
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
name = "%s"
|
|
protocol = "Tcp"
|
|
frontend_port = 3391
|
|
backend_port = 3391
|
|
frontend_ip_configuration_name = "one-%d"
|
|
}
|
|
|
|
`, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt)
|
|
}
|