236 lines
7.4 KiB
Go
236 lines
7.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
|
)
|
|
|
|
func TestAccNetwork_basic(t *testing.T) {
|
|
vlanID1 := getTestVLAN(t)
|
|
vlanID2 := getTestVLAN(t)
|
|
resource.ParallelTest(t, resource.TestCase{
|
|
PreCheck: func() { preCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
// TODO: CheckDestroy: ,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccNetworkConfig(vlanID1, true, nil),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.test", "domain_name", "foo.local"),
|
|
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", strconv.Itoa(vlanID1)),
|
|
resource.TestCheckResourceAttr("unifi_network.test", "igmp_snooping", "true"),
|
|
),
|
|
},
|
|
importStep("unifi_network.test"),
|
|
{
|
|
Config: testAccNetworkConfig(vlanID2, false, nil),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", strconv.Itoa(vlanID2)),
|
|
resource.TestCheckResourceAttr("unifi_network.test", "igmp_snooping", "false"),
|
|
),
|
|
},
|
|
importStep("unifi_network.test"),
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccNetwork_weird_cidr(t *testing.T) {
|
|
vlanID := getTestVLAN(t)
|
|
|
|
resource.ParallelTest(t, resource.TestCase{
|
|
PreCheck: func() { preCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
// TODO: CheckDestroy: ,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccNetworkConfig(vlanID, true, nil),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
// TODO: ...
|
|
),
|
|
},
|
|
importStep("unifi_network.test"),
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccNetwork_dhcp_dns(t *testing.T) {
|
|
vlanID := getTestVLAN(t)
|
|
|
|
resource.ParallelTest(t, resource.TestCase{
|
|
PreCheck: func() { preCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
// TODO: CheckDestroy: ,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccNetworkConfig(vlanID, true, []string{"192.168.1.101"}),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.0", "192.168.1.101"),
|
|
),
|
|
},
|
|
importStep("unifi_network.test"),
|
|
{
|
|
Config: testAccNetworkConfig(vlanID, true, []string{"192.168.1.101", "192.168.1.102"}),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.0", "192.168.1.101"),
|
|
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.1", "192.168.1.102"),
|
|
),
|
|
},
|
|
importStep("unifi_network.test"),
|
|
{
|
|
Config: testAccNetworkConfig(vlanID, true, nil),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckNoResourceAttr("unifi_network.test", "dhcp_dns"),
|
|
),
|
|
},
|
|
{
|
|
Config: testAccNetworkConfig(vlanID, true, []string{"192.168.1.101"}),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.0", "192.168.1.101"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccNetwork_v6(t *testing.T) {
|
|
vlanID1 := getTestVLAN(t)
|
|
vlanID2 := getTestVLAN(t)
|
|
|
|
resource.ParallelTest(t, resource.TestCase{
|
|
PreCheck: func() { preCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
// TODO: CheckDestroy: ,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccNetworkConfigV6(vlanID1, "static", "fd6a:37be:e362::1/64"),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.test", "domain_name", "foo.local"),
|
|
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", strconv.Itoa(vlanID1)),
|
|
resource.TestCheckResourceAttr("unifi_network.test", "ipv6_static_subnet", "fd6a:37be:e362::1/64"),
|
|
),
|
|
},
|
|
importStep("unifi_network.test"),
|
|
{
|
|
Config: testAccNetworkConfigV6(vlanID2, "static", "fd6a:37be:e363::1/64"),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", strconv.Itoa(vlanID2)),
|
|
resource.TestCheckResourceAttr("unifi_network.test", "ipv6_static_subnet", "fd6a:37be:e363::1/64"),
|
|
),
|
|
},
|
|
importStep("unifi_network.test"),
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccNetwork_wan(t *testing.T) {
|
|
resource.ParallelTest(t, resource.TestCase{
|
|
PreCheck: func() { preCheck(t) },
|
|
ProviderFactories: providerFactories,
|
|
// TODO: CheckDestroy: ,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testWanNetworkConfig("WAN", "pppoe", "192.168.1.1", 1, "username", "password"),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_networkgroup", "WAN"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_type", "pppoe"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_ip", "192.168.1.1"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_egress_qos", "1"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_username", "username"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "x_wan_password", "password"),
|
|
),
|
|
},
|
|
importStep("unifi_network.wan_test"),
|
|
{
|
|
Config: testWanNetworkConfig("WAN", "pppoe", "192.168.1.1", 1, "username", "password"),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_networkgroup", "WAN"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_type", "pppoe"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_ip", "192.168.1.1"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_egress_qos", "1"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "wan_username", "username"),
|
|
resource.TestCheckResourceAttr("unifi_network.wan_test", "x_wan_password", "password"),
|
|
),
|
|
},
|
|
importStep("unifi_network.wan_test"),
|
|
},
|
|
})
|
|
}
|
|
|
|
// TODO: ipv6 prefix delegation test
|
|
|
|
func quoteStrings(src []string) []string {
|
|
dst := make([]string, 0, len(src))
|
|
for _, s := range src {
|
|
dst = append(dst, fmt.Sprintf("%q", s))
|
|
}
|
|
return dst
|
|
}
|
|
|
|
func testAccNetworkConfig(vlan int, igmpSnoop bool, dhcpDNS []string) string {
|
|
return fmt.Sprintf(`
|
|
locals {
|
|
subnet = cidrsubnet("10.0.0.0/8", 4, %[1]d)
|
|
vlan_id = %[1]d
|
|
}
|
|
|
|
resource "unifi_network" "test" {
|
|
name = "tfacc"
|
|
purpose = "corporate"
|
|
|
|
subnet = local.subnet
|
|
vlan_id = local.vlan_id
|
|
dhcp_start = cidrhost(local.subnet, 6)
|
|
dhcp_stop = cidrhost(local.subnet, 254)
|
|
dhcp_enabled = true
|
|
domain_name = "foo.local"
|
|
igmp_snooping = %[2]t
|
|
|
|
dhcp_dns = [%[3]s]
|
|
}
|
|
`, vlan, igmpSnoop, strings.Join(quoteStrings(dhcpDNS), ","))
|
|
}
|
|
|
|
func testAccNetworkConfigV6(vlan int, ipv6Type string, ipv6Subnet string) string {
|
|
return fmt.Sprintf(`
|
|
locals {
|
|
subnet = cidrsubnet("10.0.0.0/8", 4, %[1]d)
|
|
vlan_id = %[1]d
|
|
}
|
|
|
|
resource "unifi_network" "test" {
|
|
name = "tfacc"
|
|
purpose = "corporate"
|
|
|
|
subnet = local.subnet
|
|
vlan_id = local.vlan_id
|
|
dhcp_start = cidrhost(local.subnet, 6)
|
|
dhcp_stop = cidrhost(local.subnet, 254)
|
|
dhcp_enabled = true
|
|
domain_name = "foo.local"
|
|
|
|
ipv6_interface_type = "%[2]s"
|
|
ipv6_static_subnet = "%[3]s"
|
|
ipv6_ra_enable = true
|
|
}
|
|
`, vlan, ipv6Type, ipv6Subnet)
|
|
}
|
|
|
|
func testWanNetworkConfig(networkGroup string, wanType string, wanIP string, wanEgressQOS int, wanUsername string, wanPassword string) string {
|
|
return fmt.Sprintf(`
|
|
resource "unifi_network" "wan_test" {
|
|
name = "tfwan"
|
|
purpose = "wan"
|
|
wan_networkgroup = "%s"
|
|
wan_type = "%s"
|
|
wan_ip = "%s"
|
|
wan_egress_qos = %d
|
|
wan_username = "%s"
|
|
x_wan_password = "%s"
|
|
}
|
|
`, networkGroup, wanType, wanIP, wanEgressQOS, wanUsername, wanPassword)
|
|
}
|