Add a helper to bring network interfaces up on a site, and to retrieve configured IP addresses. Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au> --- 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 <david(a)gibson.dropbear.id.au> +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