Skip to content

Commit

Permalink
internal/network/forward: Add network forward resource tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Mettler <[email protected]>
(cherry picked from commit 676d193fb1b69f78e9ebc8ac9c3d719836bc3424)
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
maveonair authored and MusicDin committed Jul 8, 2024
1 parent 01c569f commit 4d8faf1
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions internal/network/resource_network_forward_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package network_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/terraform-lxd/terraform-provider-lxd/internal/acctest"
)

func TestAccNetworkForward_basic(t *testing.T) {
networkName := acctest.GenerateName(1, "")

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccNetworkForward(networkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_network_forward.forward", "listen_address", "10.150.19.10"),
resource.TestCheckResourceAttr("lxd_network_forward.forward", "description", "Network Forward"),
resource.TestCheckResourceAttr("lxd_network_forward.forward", "config.target_address", "10.150.19.111"),
),
},
},
})
}

func TestAccNetworkForward_Ports(t *testing.T) {
networkName := acctest.GenerateName(1, "")

entry1 := map[string]string{
"description": "SSH",
"protocol": "tcp",
"listen_port": "22",
"target_port": "2022",
"target_address": "10.150.19.112",
}

entry2 := map[string]string{
"description": "HTTP",
"protocol": "tcp",
"listen_port": "80",
"target_port": "8080",
"target_address": "10.150.19.112",
}

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccNetworkForward_withPorts(networkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_network_forward.forward", "listen_address", "10.150.19.10"),
resource.TestCheckResourceAttr("lxd_network_forward.forward", "description", "Network Forward"),
resource.TestCheckResourceAttr("lxd_network_forward.forward", "config.target_address", "10.150.19.111"),
resource.TestCheckTypeSetElemNestedAttrs("lxd_network_forward.forward", "ports.*", entry1),
resource.TestCheckTypeSetElemNestedAttrs("lxd_network_forward.forward", "ports.*", entry2),
),
},
},
})
}

func testAccNetworkForward(networkName string) string {
return fmt.Sprintf(`
resource "lxd_network" "forward" {
name = "%s"
config = {
"ipv4.address" = "10.150.19.1/24"
"ipv4.nat" = "true"
"ipv6.address" = "fd42:474b:622d:259d::1/64"
"ipv6.nat" = "true"
}
}
resource "lxd_network_forward" "forward" {
network = lxd_network.forward.name
description = "Network Forward"
listen_address = "10.150.19.10"
config = {
target_address = "10.150.19.111"
}
}
`, networkName)
}

func testAccNetworkForward_withPorts(networkName string) string {
return fmt.Sprintf(`
resource "lxd_network" "forward" {
name = "%s"
config = {
"ipv4.address" = "10.150.19.1/24"
"ipv4.nat" = "true"
"ipv6.address" = "fd42:474b:622d:259d::1/64"
"ipv6.nat" = "true"
}
}
resource "lxd_network_forward" "forward" {
network = lxd_network.forward.name
description = "Network Forward"
listen_address = "10.150.19.10"
config = {
target_address = "10.150.19.111"
}
ports = [
{
description = "SSH"
protocol = "tcp"
listen_port = "22"
target_port = "2022"
target_address = "10.150.19.112"
},
{
description = "HTTP"
protocol = "tcp"
listen_port = "80"
target_port = "8080"
target_address = "10.150.19.112"
}
]
}
`, networkName)
}

0 comments on commit 4d8faf1

Please sign in to comment.