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 23ECD5A0291 for ; Tue, 16 May 2023 04:01:49 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4QKzxd25rtz4y1N; Tue, 16 May 2023 12:01:41 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1684202501; bh=aMpgqfK+KKvuvTryqeG8qXAa/b7L44M+GtXBkUv70zY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b5e1NlPe1n/xYBOy1PawRJ3Msg+bPCFepv7bSt7QbqPhlD6i/yZFxcElft1XVADuV aeo7R0iWUCX/sFyJadTsYAcrOqmE+4RoHeMjI+qqAJvA7KOJCMtj0jisCdR8/Xusa+ V3zCSZhUV+wCM+pbrlYapvYVKXth/WeJOUbhZMOQ= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 11/21] avocado/tasst: Add ifup and network address helpers to Site Date: Tue, 16 May 2023 12:01:25 +1000 Message-Id: <20230516020135.1901256-12-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230516020135.1901256-1-david@gibson.dropbear.id.au> References: <20230516020135.1901256-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: GKHRH5VDZM6PWSUIG2R3KG24OL36KI4O X-Message-ID-Hash: GKHRH5VDZM6PWSUIG2R3KG24OL36KI4O 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: jarichte@redhat.com, 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: 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 | 1 + avocado/tasst/site.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/avocado/tasst/nstool.py b/avocado/tasst/nstool.py index 96ed2bd..5da2ffc 100644 --- a/avocado/tasst/nstool.py +++ b/avocado/tasst/nstool.py @@ -107,6 +107,7 @@ class IsolatedNetTasst(BaseSiteTasst): """ def subsetup(self, site): + site.ifup('lo') Tasst.subsetup(self, IsolatedNetTasst, site) BaseSiteTasst.subsetup(self, site) diff --git a/avocado/tasst/site.py b/avocado/tasst/site.py index 841efbe..5a63b76 100644 --- a/avocado/tasst/site.py +++ b/avocado/tasst/site.py @@ -10,6 +10,7 @@ # Copyright Red Hat # Author: David Gibson +import ipaddress import json import avocado @@ -59,6 +60,28 @@ class Site: 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 BaseSiteTasst(Tasst): """ @@ -105,6 +128,12 @@ class BaseSiteTasst(Tasst): site = self.get_subsetup(BaseSiteTasst) self.assertIn('lo', site.ifs()) + def test_lo_addrs(self): + site = self.get_subsetup(BaseSiteTasst) + expected = [ipaddress.ip_interface(a) + for a in ['127.0.0.1/8', '::1/128']] + self.assertCountEqual(site.addrs('lo'), expected) + # Represents the host on which the tests are running, as opposed to # some simulated host created by the tests -- 2.40.1