Skip to content
Snippets Groups Projects
Verified Commit fcbd27f6 authored by Karel Koci's avatar Karel Koci 🤘
Browse files

fixup! nsfarm/setup: implement common WAN configuration setups

parent bca2e1a9
No related merge requests found
Pipeline #92931 passed with warnings with stages
in 1 minute and 1 second
......@@ -39,12 +39,14 @@ class CommonWAN(_Setup):
self._sh.run("/etc/init.d/network restart")
def wait4ping(self, target_ip: str = "172.16.1.1"):
"""Uses ping to wait for network to be ready."""
"""Uses ping to wait for network to be ready.
The default target_ip is the gateway for 'isp-common' container thus pinging up to edge of the Internet.
"""
self._sh.run(f"while ! ping -c1 -w1 '{target_ip}' >/dev/null 2>&1; do true; done")
class DHCP(CommonWAN):
"""Configure WAN interface to obtain IP address from DHCP."""
class DHCPv4(CommonWAN):
"""Configure WAN interface to obtain IPv4 address from DHCP."""
def __init__(self, shell: cli.Shell, interface: str = "wan", restart: bool = True):
super().__init__(shell, interface, restart)
......@@ -55,25 +57,29 @@ class DHCP(CommonWAN):
self._sh.run("while ! ip route | grep -q default; do sleep 1; done")
class StaticIP(CommonWAN):
"""Configure WAN interface to use static IP."""
class StaticIPv4(CommonWAN):
"""Configure WAN interface to use static IPv4."""
def __init__(
self,
shell: cli.Shell,
network: typing.Union[ipaddress.IPv4Interface, ipaddress.IPv6Interface],
gateway: typing.Union[ipaddress.IPv4Address, ipaddress.IPv6Address],
dns: typing.Union[ipaddress.IPv4Address, ipaddress.IPv6Address],
network: ipaddress.IPv4Interface = ipaddress.ip_interface("172.16.1.42/12"),
gateway: ipaddress.IPv4Address = ipaddress.ip_address("172.16.1.1"),
dns: typing.Optional[ipaddress.IPv4Address] = None, # In default uses gateway
interface: str = "wan",
restart: bool = True,
):
"""The dns argument can be None and in such case the gateway is used. This is because it is common that gateway
also provides DNS resolver.
The defaults are appropriate for network access using 'isp-common' container.
"""
super().__init__(shell, interface, restart)
self._config = {
"proto": "static",
"ipadd": str(network.ip),
"netmask": str(network.network),
"ipaddr": str(network.ip),
"netmask": str(network.network.netmask),
"gateway": str(gateway),
"dns": str(dns),
"dns": str(dns if dns is not None else gateway),
}
......
......@@ -238,7 +238,7 @@ def fixture_board_wan(client_board, isp_container):
"""
network = ipaddress.ip_interface("172.16.1.142/24")
uplink = ipaddress.ip_address("172.16.1.1")
with nsfarm.setup.uplink.StaticIP(client_board, network, uplink, uplink) as wan:
with nsfarm.setup.uplink.StaticIPv4(client_board, network, uplink, uplink) as wan:
wan.wait4ping()
yield network.ip
......
......@@ -25,8 +25,8 @@ class TestStatic(common.InternetTests):
"""Configure WAN to use static IP"""
network = ipaddress.ip_interface("172.16.1.42/255.240.0.0")
uplink = ipaddress.ip_address("172.16.1.1")
with nsfarm.lxd.Container(lxd_client, "isp-common", device_map) as container:
with nsfarm.setup.uplink.StaticIP(client_board, network, uplink, uplink) as wan:
with nsfarm.lxd.Container(lxd_client, "isp-common", device_map) as _:
with nsfarm.setup.uplink.StaticIPv4(client_board, network, uplink, uplink) as wan:
wan.wait4ping()
yield client_board
......@@ -39,7 +39,7 @@ class TestDHCP(common.InternetTests):
def fixture_client(self, lxd_client, device_map, client_board):
"""Configure WAN to use DHCP"""
with nsfarm.lxd.Container(lxd_client, "isp-dhcp", device_map) as container:
with nsfarm.setup.uplink.DHCP(client_board) as wan:
with nsfarm.setup.uplink.DHCPv4(client_board) as wan:
wan.wait4route()
yield client_board
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment