Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au> --- 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 <david(a)gibson.dropbear.id.au> 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 <david(a)gibson.dropbear.id.au> + +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