From 4d8faf1ef0d37616357860ef194033f089a3afee Mon Sep 17 00:00:00 2001 From: Fabian Mettler Date: Tue, 25 Jun 2024 08:46:05 +0200 Subject: [PATCH] internal/network/forward: Add network forward resource tests Signed-off-by: Fabian Mettler (cherry picked from commit 676d193fb1b69f78e9ebc8ac9c3d719836bc3424) Signed-off-by: Din Music --- .../network/resource_network_forward_test.go | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 internal/network/resource_network_forward_test.go diff --git a/internal/network/resource_network_forward_test.go b/internal/network/resource_network_forward_test.go new file mode 100644 index 00000000..3e12452c --- /dev/null +++ b/internal/network/resource_network_forward_test.go @@ -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) +}