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 791E55A026E for ; Wed, 31 May 2023 03:59:03 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4QWC9S3KYcz4x4h; 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=7+3vvvynZiQnj+3bADT2eJvJGGgsLcllKZxzglWqCsk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p9aOrHGeOxKrmxmG3zenMNl9XcpIY8Bhq6LZjNpmdnDKgImOEfB6cwCwks2+mkGI/ h8rfM+GXt4DTkvsVyH8ppUB22bM/0cIm9KWd48TiiLUUy0utvGZbYMRAIwwbSi7X9e B2o1/2eZdPF8nSvLqK71vumE0IT24kw0/tD25rmQ= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v3 11/20] avocado/tasst: Helper for creating veth devices between namespaces Date: Wed, 31 May 2023 11:58:40 +1000 Message-Id: <20230531015849.3229596-12-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: GRNDDWMCRQZ5V25IQBNFHZP424TH7VIN X-Message-ID-Hash: GRNDDWMCRQZ5V25IQBNFHZP424TH7VIN 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-off-by: David Gibson --- avocado/tasst/meta/__init__.py | 16 +++++++++++++ avocado/tasst/meta/veth.py | 43 ++++++++++++++++++++++++++++++++++ avocado/tasst/nstool.py | 10 ++++++++ 3 files changed, 69 insertions(+) create mode 100644 avocado/tasst/meta/__init__.py create mode 100644 avocado/tasst/meta/veth.py diff --git a/avocado/tasst/meta/__init__.py b/avocado/tasst/meta/__init__.py new file mode 100644 index 0000000..6582554 --- /dev/null +++ b/avocado/tasst/meta/__init__.py @@ -0,0 +1,16 @@ +#! /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/meta - Test pieces of the test infrastructure. +# +# Usually, tests for the test infrastructure should go next to the +# implementation of the thing being tested. Sometimes that's not +# possible (usually because it would cause a circular module +# dependency). In that case those tests can go here. +# +# Copyright Red Hat +# Author: David Gibson diff --git a/avocado/tasst/meta/veth.py b/avocado/tasst/meta/veth.py new file mode 100644 index 0000000..c4cdcf7 --- /dev/null +++ b/avocado/tasst/meta/veth.py @@ -0,0 +1,43 @@ +#! /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/metatest/veth - Test the veth creation helper +# +# These test code from tasst.site, but require additional support from +# tasst.nstool. +# +# Copyright Red Hat +# Author: David Gibson + +import contextlib + +import avocado + +from tasst import Tasst +from tasst.site import REAL_HOST +from tasst.nstool import UnshareSite + + +class VethTasst(Tasst): + """ + Test helpers for creating veths between namespaces + + :avocado: tags=meta + """ + + @contextlib.contextmanager + def setup_veth(self): + with UnshareSite(type(self).__name__ + '.1', '-Un') as ns1: + with UnshareSite(type(self).__name__ + '.2', '-n', + parent=ns1, sudo=True) as ns2: + ns1.veth('veth1', 'veth2', ns2) + yield (ns1, ns2) + + def test_ifs(self): + with self.setup_veth() as (ns1, ns2): + self.assertCountEqual(ns1.ifs(), ['lo', 'veth1']) + self.assertCountEqual(ns2.ifs(), ['lo', 'veth2']) diff --git a/avocado/tasst/nstool.py b/avocado/tasst/nstool.py index 50598a5..6f1d96b 100644 --- a/avocado/tasst/nstool.py +++ b/avocado/tasst/nstool.py @@ -68,6 +68,16 @@ class NsToolSite(Site): def bg(self, cmd, sudo=False, **kwargs): return REAL_HOST.bg(self._nst_cmd(cmd, sudo), **kwargs) + def veth(self, ifname, peername, peer=None): + self.fg('ip link add {} type veth peer name {}'.format(ifname, peername), + sudo=True) + if peer is not None: + if not isinstance(peer, NsToolSite): + raise TypeError + self.fg('ip link set {} netns {}'.format(peername, + peer.relative_pid(self)), + sudo=True) + # Create path for temporary nstool Unix socket # -- 2.40.1