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..48ef88b --- /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 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 + """ + def setUp(self): + super().setUp() + self.ns1 = UnshareSite(type(self).__name__ + '.1', '-Un') + self.ns2 = UnshareSite(type(self).__name__ + '.2', '-n', + parent=self.ns1, sudo=True) + self.ns1.veth('veth1', 'veth2', self.ns2) + + def tearDown(self): + self.ns2.close() + self.ns1.close() + super().tearDown() + + def test_ifs(self): + self.assertCountEqual(self.ns1.ifs(), ['lo', 'veth1']) + self.assertCountEqual(self.ns2.ifs(), ['lo', 'veth2']) diff --git a/avocado/tasst/nstool.py b/avocado/tasst/nstool.py index 5da2ffc..bfd7cf7 100644 --- a/avocado/tasst/nstool.py +++ b/avocado/tasst/nstool.py @@ -61,6 +61,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