From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202408 header.b=XpnUh+kK; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 810DB5A026E for ; Mon, 26 Aug 2024 04:09:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202408; t=1724638184; bh=ntAga6ZkfEqbjUSQn1EVT+nKSgFnL2SKIe7gqiYdsyQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XpnUh+kKXzog9XimHWajBzGFhFVJgE1p2n+Ryu/LHtJnylG67svAeLlVH78VPSxX6 Ff/fu6pqytZ5iAdPpb5OOiNlHLUpg1G+dPyq/BaW9GOigUbqW1SUmdkuxregLOXjRh 45dftDJUqIdVsojTAfpripsmXTxglZWOutXVP9vLtcD+rnfW65XVnY6S7yRqD2EjhY OMiLNwJ1jJ7xX2mXQicKncLocqkV/YIyaF62ADXaxxBYuy1QkzIlrHFHbC3kciy50L ugwDW2V/Xv2CgCwwZEAv5XHH9RNTXk1ljaaCKn0DoXGhhT/Ry7sWeeaRy2SOD3EcSx rLugmhZEjoqTA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WsYyw60rcz4xCg; Mon, 26 Aug 2024 12:09:44 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v3 12/15] tasst: Helpers for testing NDP behaviour Date: Mon, 26 Aug 2024 12:09:39 +1000 Message-ID: <20240826020942.545155-13-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240826020942.545155-1-david@gibson.dropbear.id.au> References: <20240826020942.545155-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: GDP3SZCATH4JE4234U5UHFIDSGUPGHWK X-Message-ID-Hash: GDP3SZCATH4JE4234U5UHFIDSGUPGHWK 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 , 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 --- test/tasst/__main__.py | 1 + test/tasst/ndp.py | 106 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 test/tasst/ndp.py diff --git a/test/tasst/__main__.py b/test/tasst/__main__.py index 251edae5..92319d46 100644 --- a/test/tasst/__main__.py +++ b/test/tasst/__main__.py @@ -18,6 +18,7 @@ import exeter MODULES = [ 'cmdsite', 'ip', + 'ndp', 'transfer', 'unshare', 'veth', diff --git a/test/tasst/ndp.py b/test/tasst/ndp.py new file mode 100644 index 00000000..0ea2f75e --- /dev/null +++ b/test/tasst/ndp.py @@ -0,0 +1,106 @@ +#! /usr/bin/env avocado-runner-avocado-classless + +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright Red Hat +# Author: David Gibson + +""" +Test A Simple Socket Transport + +ndp.py - Helpers for testing NDP +""" + +import dataclasses +import ipaddress +import os +import tempfile +from typing import Iterator + +import exeter + +from . import cmdsite, ip, unshare, veth + + +@dataclasses.dataclass +class NdpScenario(exeter.Scenario): + client: cmdsite.CmdSite + ifname: str + network: ip.Net + gateway: ip.Addr + + @exeter.scenariotest + def ndp_addr(self) -> None: + # Wait for NDP to do its thing + (addr,) = ip.addr_wait(self.client, self.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. + exeter.assert_eq(addr.network, self.network) + + @exeter.scenariotest + def ndp_route(self) -> None: + defroutes = ip.routes6(self.client, dst='default') + while not defroutes: + defroutes = ip.routes6(self.client, dst='default') + + exeter.assert_eq(len(defroutes), 1) + gw = ipaddress.ip_address(defroutes[0]['gateway']) + exeter.assert_eq(gw, self.gateway) + + +IFNAME = 'clientif' +NETWORK = ip.TEST_NET6_TASST_A +ipa = ip.IpiAllocator(NETWORK) +(ROUTER_IP6,) = ipa.next_ipis() + + +def setup_radvd() -> Iterator[NdpScenario]: + router_ifname = 'routerif' + + with unshare.unshare('client', '-Un') as client, \ + unshare.unshare('router', '-n', + parent=client, privilege=True) as router, \ + tempfile.TemporaryDirectory() as tmpdir, \ + veth.veth(client, IFNAME, router_ifname, router): + + # Configure the simulated router + confpath = os.path.join(tmpdir, 'radvd.conf') + pidfile = os.path.join(tmpdir, 'radvd.pid') + open(confpath, 'w', encoding='UTF-8').write( + f''' + interface {router_ifname} {{ + AdvSendAdvert on; + prefix {NETWORK} {{ + }}; + }}; + ''' + ) + + ip.ifup(router, 'lo') + ip.ifup(router, 'routerif', ROUTER_IP6) + + # Configure the client + ip.ifup(client, 'lo') + ip.ifup(client, IFNAME) + + # Get the router's link-local-address + (router_ll,) = ip.addr_wait(router, router_ifname, + family='inet6', scope='link') + + # Run radvd + router.fg('radvd', '-c', '-C', f'{confpath}') + radvd_cmd = ['radvd', '-C', f'{confpath}', '-n', + '-p', f'{pidfile}', '-d', '5'] + with router.bg(*radvd_cmd, privilege=True) as radvd: + yield NdpScenario(client=client, + ifname=IFNAME, + network=NETWORK, + gateway=router_ll.ip) + radvd.terminate() + + +def selftests() -> None: + NdpScenario.test(setup_radvd) -- 2.46.0