public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: Cleber Rosa <crosa@redhat.com>,
	David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v3 13/15] tasst: Helpers for testing DHCP & DHCPv6 behaviour
Date: Mon, 26 Aug 2024 12:09:40 +1000	[thread overview]
Message-ID: <20240826020942.545155-14-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240826020942.545155-1-david@gibson.dropbear.id.au>

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 test/tasst/__main__.py |   1 +
 test/tasst/dhcp.py     | 190 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 191 insertions(+)
 create mode 100644 test/tasst/dhcp.py

diff --git a/test/tasst/__main__.py b/test/tasst/__main__.py
index 92319d46..f94001e7 100644
--- a/test/tasst/__main__.py
+++ b/test/tasst/__main__.py
@@ -17,6 +17,7 @@ import exeter
 
 MODULES = [
     'cmdsite',
+    'dhcp',
     'ip',
     'ndp',
     'transfer',
diff --git a/test/tasst/dhcp.py b/test/tasst/dhcp.py
new file mode 100644
index 00000000..9231b086
--- /dev/null
+++ b/test/tasst/dhcp.py
@@ -0,0 +1,190 @@
+#! /usr/bin/env avocado-runner-avocado-classless
+
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright Red Hat
+# Author: David Gibson <david@gibson.dropbear.id.au>
+
+"""
+Test A Simple Socket Transport
+
+dhcp.py - Helpers for testing DHCP & DHCPv6
+"""
+
+import contextlib
+import dataclasses
+import ipaddress
+import os
+import tempfile
+from typing import Iterator, Literal
+
+import exeter
+
+from . import cmdsite, ip, unshare, veth
+
+
+DHCLIENT = '/sbin/dhclient'
+
+
+def _dhclient(site: cmdsite.CmdSite, ifname: str, ipv: Literal['4', '6']) \
+        -> Iterator[None]:
+    with tempfile.TemporaryDirectory() as tmpdir:
+        pidfile = os.path.join(tmpdir, 'dhclient.pid')
+        leasefile = os.path.join(tmpdir, 'dhclient.leases')
+
+        # We need '-nc' because we may be running with
+        # capabilities but not UID 0.  Without -nc dhclient drops
+        # capabilities before invoking dhclient-script, so it's
+        # unable to actually configure the interface
+        opts = [f'-{ipv}', '-v', '-nc', '-pf', f'{pidfile}',
+                '-lf', f'{leasefile}', f'{ifname}']
+        site.fg(f'{DHCLIENT}', *opts, privilege=True)
+        yield
+        site.fg(f'{DHCLIENT}', '-x', '-pf', f'{pidfile}', privilege=True)
+
+
+@contextlib.contextmanager
+def dhclient4(site: cmdsite.CmdSite, ifname: str) -> Iterator[None]:
+    yield from _dhclient(site, ifname, '4')
+
+
+@contextlib.contextmanager
+def dhclient6(site: cmdsite.CmdSite, ifname: str) -> Iterator[None]:
+    yield from _dhclient(site, ifname, '6')
+
+
+@dataclasses.dataclass
+class Dhcp4Scenario(exeter.Scenario):
+    client: cmdsite.CmdSite
+    ifname: str
+    addr: ip.Addr
+    gateway: ip.Addr
+    mtu: int
+
+    @exeter.scenariotest
+    def dhcp_addr(self) -> None:
+        with dhclient4(self.client, self.ifname):
+            (actual_addr,) = ip.addrs(self.client, self.ifname,
+                                      family='inet', scope='global')
+            exeter.assert_eq(actual_addr.ip, self.addr)
+
+    @exeter.scenariotest
+    def dhcp_route(self) -> None:
+        with dhclient4(self.client, self.ifname):
+            (defroute,) = ip.routes4(self.client, dst='default')
+            exeter.assert_eq(ipaddress.ip_address(defroute['gateway']),
+                             self.gateway)
+
+    @exeter.scenariotest
+    def dhcp_mtu(self) -> None:
+        with dhclient4(self.client, self.ifname):
+            exeter.assert_eq(ip.mtu(self.client, self.ifname), self.mtu)
+
+
+DHCPD = 'dhcpd'
+IFNAME = 'clientif'
+
+SUBNET4 = ip.TEST_NET_1
+ipa4 = ip.IpiAllocator(SUBNET4)
+(SERVER_IP4,) = ipa4.next_ipis()
+(CLIENT_IP4,) = ipa4.next_ipis()
+
+
+@contextlib.contextmanager
+def setup_dhcpd_common(ifname: str, server_ifname: str) \
+        -> Iterator[tuple[cmdsite.CmdSite, cmdsite.CmdSite, str]]:
+    with unshare.unshare('client', '-Un') as client, \
+         unshare.unshare('server', '-n',
+                         parent=client, privilege=True) as server, \
+         veth.veth(client, ifname, server_ifname, server), \
+         tempfile.TemporaryDirectory() as tmpdir:
+        yield (client, server, tmpdir)
+
+
+def setup_dhcpd4() -> Iterator[Dhcp4Scenario]:
+    server_ifname = 'serverif'
+
+    with setup_dhcpd_common(IFNAME, server_ifname) as (client, server, tmpdir):
+        # Configure dhcpd
+        confpath = os.path.join(tmpdir, 'dhcpd.conf')
+        open(confpath, 'w', encoding='UTF-8').write(
+            f'''subnet {SUBNET4.network_address} netmask {SUBNET4.netmask} {{
+            option routers {SERVER_IP4.ip};
+            range {CLIENT_IP4.ip} {CLIENT_IP4.ip};
+            }}'''
+        )
+        pidfile = os.path.join(tmpdir, 'dhcpd.pid')
+        leasepath = os.path.join(tmpdir, 'dhcpd.leases')
+        open(leasepath, 'wb').write(b'')
+
+        ip.ifup(server, 'lo')
+        ip.ifup(server, server_ifname, SERVER_IP4)
+
+        opts = ['-f', '-d', '-4', '-cf', f'{confpath}',
+                '-lf', f'{leasepath}', '-pf', f'{pidfile}']
+        server.fg(f'{DHCPD}', '-t', *opts)  # test config
+        with server.bg(f'{DHCPD}', *opts, privilege=True,
+                       check=False) as dhcpd:
+            # Configure the client
+            ip.ifup(client, 'lo')
+            yield Dhcp4Scenario(client=client, ifname=IFNAME,
+                                addr=CLIENT_IP4.ip,
+                                gateway=SERVER_IP4.ip, mtu=1500)
+            dhcpd.terminate()
+
+
+@dataclasses.dataclass
+class Dhcp6Scenario(exeter.Scenario):
+    client: cmdsite.CmdSite
+    ifname: str
+    addr: ip.Addr
+
+    @exeter.scenariotest
+    def dhcp6_addr(self) -> None:
+        with dhclient6(self.client, self.ifname):
+            addrs = [a.ip for a in ip.addrs(self.client, self.ifname,
+                                            family='inet6',
+                                            scope='global')]
+            assert self.addr in addrs  # Might also have a SLAAC address
+
+
+SUBNET6 = ip.TEST_NET6_TASST_A
+ipa6 = ip.IpiAllocator(SUBNET6)
+(SERVER_IP6,) = ipa6.next_ipis()
+(CLIENT_IP6,) = ipa6.next_ipis()
+
+
+def setup_dhcpd6() -> Iterator[Dhcp6Scenario]:
+    server_ifname = 'serverif'
+
+    with setup_dhcpd_common(IFNAME, server_ifname) as (client, server, tmpdir):
+        # Sort out link local addressing
+        ip.ifup(server, 'lo')
+        ip.ifup(server, server_ifname, SERVER_IP6)
+        ip.ifup(client, 'lo')
+        ip.ifup(client, IFNAME)
+        ip.addr_wait(server, server_ifname, family='inet6', scope='link')
+
+        # Configure the DHCP server
+        confpath = os.path.join(tmpdir, 'dhcpd.conf')
+        open(confpath, 'w', encoding='UTF-8').write(
+            f'''subnet6 {SUBNET6} {{
+            range6 {CLIENT_IP6.ip} {CLIENT_IP6.ip};
+            }}''')
+        pidfile = os.path.join(tmpdir, 'dhcpd.pid')
+        leasepath = os.path.join(tmpdir, 'dhcpd.leases')
+        open(leasepath, 'wb').write(b'')
+
+        opts = ['-f', '-d', '-6', '-cf', f'{confpath}',
+                '-lf', f'{leasepath}', '-pf', f'{pidfile}']
+        server.fg(f'{DHCPD}', '-t', *opts)  # test config
+        with server.bg(f'{DHCPD}', *opts, privilege=True,
+                       check=False) as dhcpd:
+            yield Dhcp6Scenario(client=client, ifname=IFNAME,
+                                addr=CLIENT_IP6.ip)
+            dhcpd.terminate()
+
+
+def selftests() -> None:
+    Dhcp4Scenario.test(setup_dhcpd4)
+    Dhcp6Scenario.test(setup_dhcpd6)
-- 
@@ -0,0 +1,190 @@
+#! /usr/bin/env avocado-runner-avocado-classless
+
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright Red Hat
+# Author: David Gibson <david@gibson.dropbear.id.au>
+
+"""
+Test A Simple Socket Transport
+
+dhcp.py - Helpers for testing DHCP & DHCPv6
+"""
+
+import contextlib
+import dataclasses
+import ipaddress
+import os
+import tempfile
+from typing import Iterator, Literal
+
+import exeter
+
+from . import cmdsite, ip, unshare, veth
+
+
+DHCLIENT = '/sbin/dhclient'
+
+
+def _dhclient(site: cmdsite.CmdSite, ifname: str, ipv: Literal['4', '6']) \
+        -> Iterator[None]:
+    with tempfile.TemporaryDirectory() as tmpdir:
+        pidfile = os.path.join(tmpdir, 'dhclient.pid')
+        leasefile = os.path.join(tmpdir, 'dhclient.leases')
+
+        # We need '-nc' because we may be running with
+        # capabilities but not UID 0.  Without -nc dhclient drops
+        # capabilities before invoking dhclient-script, so it's
+        # unable to actually configure the interface
+        opts = [f'-{ipv}', '-v', '-nc', '-pf', f'{pidfile}',
+                '-lf', f'{leasefile}', f'{ifname}']
+        site.fg(f'{DHCLIENT}', *opts, privilege=True)
+        yield
+        site.fg(f'{DHCLIENT}', '-x', '-pf', f'{pidfile}', privilege=True)
+
+
+@contextlib.contextmanager
+def dhclient4(site: cmdsite.CmdSite, ifname: str) -> Iterator[None]:
+    yield from _dhclient(site, ifname, '4')
+
+
+@contextlib.contextmanager
+def dhclient6(site: cmdsite.CmdSite, ifname: str) -> Iterator[None]:
+    yield from _dhclient(site, ifname, '6')
+
+
+@dataclasses.dataclass
+class Dhcp4Scenario(exeter.Scenario):
+    client: cmdsite.CmdSite
+    ifname: str
+    addr: ip.Addr
+    gateway: ip.Addr
+    mtu: int
+
+    @exeter.scenariotest
+    def dhcp_addr(self) -> None:
+        with dhclient4(self.client, self.ifname):
+            (actual_addr,) = ip.addrs(self.client, self.ifname,
+                                      family='inet', scope='global')
+            exeter.assert_eq(actual_addr.ip, self.addr)
+
+    @exeter.scenariotest
+    def dhcp_route(self) -> None:
+        with dhclient4(self.client, self.ifname):
+            (defroute,) = ip.routes4(self.client, dst='default')
+            exeter.assert_eq(ipaddress.ip_address(defroute['gateway']),
+                             self.gateway)
+
+    @exeter.scenariotest
+    def dhcp_mtu(self) -> None:
+        with dhclient4(self.client, self.ifname):
+            exeter.assert_eq(ip.mtu(self.client, self.ifname), self.mtu)
+
+
+DHCPD = 'dhcpd'
+IFNAME = 'clientif'
+
+SUBNET4 = ip.TEST_NET_1
+ipa4 = ip.IpiAllocator(SUBNET4)
+(SERVER_IP4,) = ipa4.next_ipis()
+(CLIENT_IP4,) = ipa4.next_ipis()
+
+
+@contextlib.contextmanager
+def setup_dhcpd_common(ifname: str, server_ifname: str) \
+        -> Iterator[tuple[cmdsite.CmdSite, cmdsite.CmdSite, str]]:
+    with unshare.unshare('client', '-Un') as client, \
+         unshare.unshare('server', '-n',
+                         parent=client, privilege=True) as server, \
+         veth.veth(client, ifname, server_ifname, server), \
+         tempfile.TemporaryDirectory() as tmpdir:
+        yield (client, server, tmpdir)
+
+
+def setup_dhcpd4() -> Iterator[Dhcp4Scenario]:
+    server_ifname = 'serverif'
+
+    with setup_dhcpd_common(IFNAME, server_ifname) as (client, server, tmpdir):
+        # Configure dhcpd
+        confpath = os.path.join(tmpdir, 'dhcpd.conf')
+        open(confpath, 'w', encoding='UTF-8').write(
+            f'''subnet {SUBNET4.network_address} netmask {SUBNET4.netmask} {{
+            option routers {SERVER_IP4.ip};
+            range {CLIENT_IP4.ip} {CLIENT_IP4.ip};
+            }}'''
+        )
+        pidfile = os.path.join(tmpdir, 'dhcpd.pid')
+        leasepath = os.path.join(tmpdir, 'dhcpd.leases')
+        open(leasepath, 'wb').write(b'')
+
+        ip.ifup(server, 'lo')
+        ip.ifup(server, server_ifname, SERVER_IP4)
+
+        opts = ['-f', '-d', '-4', '-cf', f'{confpath}',
+                '-lf', f'{leasepath}', '-pf', f'{pidfile}']
+        server.fg(f'{DHCPD}', '-t', *opts)  # test config
+        with server.bg(f'{DHCPD}', *opts, privilege=True,
+                       check=False) as dhcpd:
+            # Configure the client
+            ip.ifup(client, 'lo')
+            yield Dhcp4Scenario(client=client, ifname=IFNAME,
+                                addr=CLIENT_IP4.ip,
+                                gateway=SERVER_IP4.ip, mtu=1500)
+            dhcpd.terminate()
+
+
+@dataclasses.dataclass
+class Dhcp6Scenario(exeter.Scenario):
+    client: cmdsite.CmdSite
+    ifname: str
+    addr: ip.Addr
+
+    @exeter.scenariotest
+    def dhcp6_addr(self) -> None:
+        with dhclient6(self.client, self.ifname):
+            addrs = [a.ip for a in ip.addrs(self.client, self.ifname,
+                                            family='inet6',
+                                            scope='global')]
+            assert self.addr in addrs  # Might also have a SLAAC address
+
+
+SUBNET6 = ip.TEST_NET6_TASST_A
+ipa6 = ip.IpiAllocator(SUBNET6)
+(SERVER_IP6,) = ipa6.next_ipis()
+(CLIENT_IP6,) = ipa6.next_ipis()
+
+
+def setup_dhcpd6() -> Iterator[Dhcp6Scenario]:
+    server_ifname = 'serverif'
+
+    with setup_dhcpd_common(IFNAME, server_ifname) as (client, server, tmpdir):
+        # Sort out link local addressing
+        ip.ifup(server, 'lo')
+        ip.ifup(server, server_ifname, SERVER_IP6)
+        ip.ifup(client, 'lo')
+        ip.ifup(client, IFNAME)
+        ip.addr_wait(server, server_ifname, family='inet6', scope='link')
+
+        # Configure the DHCP server
+        confpath = os.path.join(tmpdir, 'dhcpd.conf')
+        open(confpath, 'w', encoding='UTF-8').write(
+            f'''subnet6 {SUBNET6} {{
+            range6 {CLIENT_IP6.ip} {CLIENT_IP6.ip};
+            }}''')
+        pidfile = os.path.join(tmpdir, 'dhcpd.pid')
+        leasepath = os.path.join(tmpdir, 'dhcpd.leases')
+        open(leasepath, 'wb').write(b'')
+
+        opts = ['-f', '-d', '-6', '-cf', f'{confpath}',
+                '-lf', f'{leasepath}', '-pf', f'{pidfile}']
+        server.fg(f'{DHCPD}', '-t', *opts)  # test config
+        with server.bg(f'{DHCPD}', *opts, privilege=True,
+                       check=False) as dhcpd:
+            yield Dhcp6Scenario(client=client, ifname=IFNAME,
+                                addr=CLIENT_IP6.ip)
+            dhcpd.terminate()
+
+
+def selftests() -> None:
+    Dhcp4Scenario.test(setup_dhcpd4)
+    Dhcp6Scenario.test(setup_dhcpd6)
-- 
2.46.0


  parent reply	other threads:[~2024-08-26  2:09 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-26  2:09 [PATCH v3 00/15] RFC: Proof-of-concept based exeter+Avocado tests David Gibson
2024-08-26  2:09 ` [PATCH v3 01/15] test: run static checkers with Avocado and JSON definitions David Gibson
2024-08-26  2:09 ` [PATCH v3 02/15] test: Adjust how we invoke tests with run_avocado David Gibson
2024-08-26  2:09 ` [PATCH v3 03/15] test: Extend make targets to run Avocado tests David Gibson
2024-08-26  2:09 ` [PATCH v3 04/15] test: Exeter based static tests David Gibson
2024-08-26  2:09 ` [PATCH v3 05/15] tasst: Support library and linters for tests in Python David Gibson
2024-08-26  2:09 ` [PATCH v3 06/15] tasst/cmdsite: Base helpers for running shell commands in various places David Gibson
2024-08-26  2:09 ` [PATCH v3 07/15] test: Add exeter+Avocado based build tests David Gibson
2024-08-26  2:09 ` [PATCH v3 08/15] tasst/unshare: Add helpers to run commands in Linux unshared namespaces David Gibson
2024-08-26  2:09 ` [PATCH v3 09/15] tasst/ip: Helpers for configuring IPv4 and IPv6 David Gibson
2024-08-26  2:09 ` [PATCH v3 10/15] tasst/veth: Helpers for constructing veth devices between namespaces David Gibson
2024-08-26  2:09 ` [PATCH v3 11/15] tasst: Helpers to test transferring data between sites David Gibson
2024-08-26  2:09 ` [PATCH v3 12/15] tasst: Helpers for testing NDP behaviour David Gibson
2024-08-26  2:09 ` David Gibson [this message]
2024-08-26  2:09 ` [PATCH v3 14/15] tasst: Helpers to construct a simple network environment for tests David Gibson
2024-08-26  2:09 ` [PATCH v3 15/15] avocado: Convert basic pasta tests David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240826020942.545155-14-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=crosa@redhat.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).