From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 642B95A026D for ; Wed, 31 May 2023 03:59:02 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4QWC9S3FMSz4x4X; 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=4cAD6yaO+WykiwCmECjgA1fWnMW9rMM6+zJNyfyJ+RA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VmFtHpfNwStjr1dY6vOpuhyFS/+hAoSj4MoWRVe23YEjDnTe4GWxnU0t9RPgvXMVU ySYBqliA9AswiGjIt8q8JReSssMJ5oCGVPEzhZ4zccrVfZHUaMrJdDQT44yfdGbgc6 XrHjIWreTdwFIQbmvJIXylzZkxGQLfQm+lD/XNdA= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v3 10/20] avocado/tasst: Add ifup and network address helpers to Site Date: Wed, 31 May 2023 11:58:39 +1000 Message-Id: <20230531015849.3229596-11-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: SB2SRQFLXEBKNKV5DB46SN7ABTK7IG24 X-Message-ID-Hash: SB2SRQFLXEBKNKV5DB46SN7ABTK7IG24 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: Add a helper to bring network interfaces up on a site, and to retrieve configured IP addresses. Signed-off-by: David Gibson --- avocado/tasst/nstool.py | 11 +++++++++-- avocado/tasst/site.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/avocado/tasst/nstool.py b/avocado/tasst/nstool.py index 020e8a0..50598a5 100644 --- a/avocado/tasst/nstool.py +++ b/avocado/tasst/nstool.py @@ -118,8 +118,11 @@ class UserNetNsTasst(IsolatedSiteTasst): :avocado: tags=meta """ + @contextlib.contextmanager def setup_site(self): - return UnshareSite(type(self).__name__, '-Ucn') + with UnshareSite(type(self).__name__, '-Ucn') as ns: + ns.ifup('lo') + yield ns def test_userns(self): REAL_HOST.require_cmds('capsh') @@ -142,6 +145,7 @@ class NestedNsTasst(IsolatedSiteTasst): name = type(self).__name__ with UnshareSite(name + '.userns', '-Uc') as userns: with UnshareSite(name + '.netns', '-n', parent=userns, sudo=True) as netns: + netns.ifup('lo') yield netns @@ -152,8 +156,11 @@ class PidNsTasst(IsolatedSiteTasst): :avocado: tags=meta """ + @contextlib.contextmanager def setup_site(self): - return UnshareSite(type(self).__name__, '-Upfn') + with UnshareSite(type(self).__name__, '-Upfn') as ns: + ns.ifup('lo') + yield ns def test_relative_pid(self): with self.setup_site() as site: diff --git a/avocado/tasst/site.py b/avocado/tasst/site.py index 900e945..3cb3721 100644 --- a/avocado/tasst/site.py +++ b/avocado/tasst/site.py @@ -11,6 +11,7 @@ # Author: David Gibson import contextlib +import ipaddress import json import avocado @@ -56,6 +57,28 @@ class Site(contextlib.AbstractContextManager): info = json.loads(self.output('ip -j link show')) return [i['ifname'] for i in info] + def ifup(self, ifname): + self.require_cmds('ip') + self.fg('ip link set {} up'.format(ifname), sudo=True) + + def addrinfos(self, ifname, **filter): + self.require_cmds('ip') + info = json.loads(self.output('ip -j addr show {}'.format(ifname))) + assert len(info) == 1 # We specified a specific interface + + ais = [ai for ai in info[0]['addr_info']] + for key, value in filter.items(): + ais = [ai for ai in ais if key in ai and ai[key] == value] + + return ais + + def addrs(self, ifname, **filter): + self.require_cmds('ip') + # Return just the parsed, non-tentative addresses + return [ipaddress.ip_interface('{}/{}'.format(ai['local'], ai['prefixlen'])) + for ai in self.addrinfos(ifname, **filter) + if not 'tentative' in ai] + class SiteTasst(Tasst): """ @@ -100,6 +123,12 @@ class SiteTasst(Tasst): with self.setup_site() as site: self.assertIn('lo', site.ifs()) + def test_lo_addrs(self): + with self.setup_site() as site: + expected = [ipaddress.ip_interface(a) + for a in ['127.0.0.1/8', '::1/128']] + self.assertCountEqual(site.addrs('lo'), expected) + class IsolatedSiteTasst(SiteTasst): """ -- 2.40.1