From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 407955A026E for ; Wed, 31 May 2023 03:59:04 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4QWC9S45Nhz4x5X; Wed, 31 May 2023 11:58:52 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1685498332; bh=CRqABwnfYvrqj0QeLuKvziimNaCeNwU8VwnDU54d5k0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HIt5X3aVtUUiUV50FCV7BvvWPeL4QfgcpgMahdfbC5n3BqyAriyVdcJkCifeXSqmt ZBu76dFDplNI05F7AHMWFks4n1DU64l1frWdN9YyZ61+5w5/MvZYXvqyUYQdUR2w9e UPTO4c6WmNVbSP+k57r3E0x4xdgCdn7X2ivufsI4= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v3 17/20] avocado/tasst: Helpers for testing NDP behaviour Date: Wed, 31 May 2023 11:58:46 +1000 Message-Id: <20230531015849.3229596-18-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230531015849.3229596-1-david@gibson.dropbear.id.au> References: <20230531015849.3229596-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: A6DH4U5GMZOWXBSNCPXC7HTR7YKFVRJU X-Message-ID-Hash: A6DH4U5GMZOWXBSNCPXC7HTR7YKFVRJU X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Cleber Rosa , jarichte@redhat.com, David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Signed-iff-by: David Gibson --- avocado/tasst/ndp.py | 137 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 avocado/tasst/ndp.py diff --git a/avocado/tasst/ndp.py b/avocado/tasst/ndp.py new file mode 100644 index 0000000..771cba8 --- /dev/null +++ b/avocado/tasst/ndp.py @@ -0,0 +1,137 @@ +#! /usr/bin/python3 + +# SPDX-License-Identifier: GPL-2.0-or-later +# +# tasst - Test A Simple Socket Transport +# library of test helpers for passt & pasta +# +# tasst/ndp.py - Helpers for testing NDP +# +# Copyright Red Hat +# Author: David Gibson + +import contextlib +import ipaddress +import os + +from tasst import Tasst, TasstSubData +from tasst.address import IpiAllocator, TEST_NET6_TASST_A +from tasst.nstool import UnshareSite +from tasst.site import Site +from tasst.typing import typecheck + + +class NdpTasstInfo: + def __init__(self, site, ifname, net, gw): + self.site = typecheck(site, Site) + self.ifname = typecheck(ifname, str) + self.net = typecheck(net, ipaddress.IPv6Network) + self.gw = typecheck(gw, ipaddress.IPv6Address) + + site.require_cmds('ip') + + +class BaseNdpTasst(Tasst): + """ + Test NDP behaviour. + + :avocado: disable + """ + + def setup_ndp(self): + raise NotImplementedError("{} must implement setup_ndp() method".format(type(self).__name__)) + + @contextlib.contextmanager + def check_setup_ndp(self): + with self.setup_ndp() as ndp: + if not isinstance(ndp, NdpTasstInfo): + raise TypeError("{}.setup_ndp() must yield a NdpTasstInfo instance".format(type(self).__name__)) + yield ndp + + def test_addr(self): + with self.check_setup_ndp() as ndp: + # Wait for NDP to do its thing + (addr,) = ndp.site.addr_wait(ndp.ifname, family='inet6', scope='global') + + # The SLAAC address is derived from the guest ns MAC, so + # probably won't exactly match the host address (we need + # DHCPv6 for that). It should be in the right network though. + self.assertEquals(addr.network, ndp.net) + + def test_route(self): + with self.check_setup_ndp() as ndp: + defroutes = ndp.site.routes6(dst='default') + while not defroutes: + defroutes = ndp.site.routes6(dst='default') + + self.assertEquals(len(defroutes), 1) + gateway = ipaddress.ip_address(defroutes[0]['gateway']) + self.assertEquals(gateway, ndp.gw) + + +class MetaNdpTasst(BaseNdpTasst): + """Ugly workaround for + https://github.com/avocado-framework/avocado/issues/5680. + Explicitly apply the "meta" tag to inherited tests + + :avocado: disable + :avocado: tags=meta + + """ + + def test_addr(self): + super().test_addr() + + def test_route(self): + super().test_route() + + +class RadvdNdpTasst(MetaNdpTasst): + timeout = 15.0 + + @contextlib.contextmanager + def setup_ndp(self): + ifname = 'clientif' + router_ifname = 'routerif' + prefix = TEST_NET6_TASST_A + + with UnshareSite(type(self).__name__ + '.client', '-Un') as client, \ + UnshareSite(type(self).__name__ + '.router', '-n', parent=client, sudo=True) as router: + router.require_cmds('radvd') + + client.veth(ifname, router_ifname, router) + + # Configure the simulated router + ipa = IpiAllocator(prefix) + (router_ip6,) = ipa.next_ipis() + + confpath = os.path.join(self.workdir, 'radvd.conf') + pidpath = os.path.join(self.workdir, 'radvd.pid') + open(confpath, 'w').write( + ''' + interface {} {{ + AdvSendAdvert on; + prefix {} {{ + }}; + }}; + '''.format(router_ifname, prefix)) + + router.ifup('lo') + router.ifup('routerif', router_ip6) + + # Configure the client + client.ifup('lo') + client.ifup(ifname) + + # Get the router's link-local-address + (router_ll,) = router.addr_wait(router_ifname, family='inet6', scope='link') + + # Run radvd + router.fg('radvd -c -C {}'.format(confpath)) + with router.bg('radvd -C {} -p {} -n -d 5'.format(confpath, pidpath), sudo=True) as radvd: + yield NdpTasstInfo(client, ifname, prefix, router_ll.ip) + + pid = int(open(pidpath).read()) + router.fg('kill {}'.format(pid)) + status = radvd.wait() + self.assertEquals(status, 0) -- 2.40.1