Allow to ignore ip command errors

Change-Id: I8d7c77a5f9934b5e835752ce05100f86591c4ec6
This commit is contained in:
Federico Ressi 2020-02-19 12:20:12 +01:00
parent 87c804712d
commit 135de24450
3 changed files with 35 additions and 4 deletions

View File

@ -16,11 +16,15 @@
from __future__ import absolute_import
import netaddr
from oslo_log import log
import tobiko
from tobiko.shell import sh
LOG = log.getLogger(__name__)
class IpError(tobiko.TobikoException):
message = ("Unable to get IP addresses from host "
"(exit_status={exit_status!r}): {error!s}")
@ -66,8 +70,8 @@ def list_ip_addresses(ip_version=None, scope=None, **execute_params):
def list_network_namespaces(**execute_params):
output = execute_ip(['-o', 'netns', 'list'], **execute_params)
namespaces = tobiko.Selection()
output = execute_ip(['-o', 'netns', 'list'], **execute_params)
if output:
for line in output.splitlines():
fields = line.strip().split()
@ -76,10 +80,18 @@ def list_network_namespaces(**execute_params):
return namespaces
def execute_ip(ifconfig_args, **execute_params):
command = ['/sbin/ip'] + ifconfig_args
IP_COMMAND = sh.shell_command(['/sbin/ip'])
def execute_ip(ifconfig_args, ip_command=None, ignore_errors=False,
**execute_params):
if ip_command:
ip_command = sh.shell_command(ip_command)
else:
ip_command = IP_COMMAND
command = ip_command + ifconfig_args
result = sh.execute(command, stdin=False, stdout=True, stderr=True,
expect_exit_status=None, **execute_params)
if result.exit_status:
if not ignore_errors and result.exit_status:
raise IpError(error=result.stderr, exit_status=result.exit_status)
return result.stdout

View File

@ -13,6 +13,7 @@ class NetworkNamespaceFixture(tobiko.SharedFixture):
def setup_fixture(self):
for node in topology.list_openstack_nodes():
network_namespaces = ip.list_network_namespaces(
ignore_errors=True,
ssh_client=node.ssh_client)
if network_namespaces:
self.network_namespace = network_namespaces.first

View File

@ -115,6 +115,15 @@ class IpTest(testtools.TestCase):
def test_list_ip_addresses_with_namespace_and_scope(self):
self.test_list_ip_addresses_with_namespace(scope='global')
def test_list_ip_addresses_with_failing_command(self):
self.assertRaises(ip.IpError, ip.list_ip_addresses,
ip_command=['false'],
ssh_client=self.namespace.ssh_client)
def test_list_ip_addresses_with_ignore_errors(self, **execute_params):
self.test_list_ip_addresses(ignore_errors=True, ip_command='false',
**execute_params)
def test_list_namespaces(self, **execute_params):
namespaces = ip.list_network_namespaces(**execute_params)
self.assertIsInstance(namespaces, list)
@ -133,3 +142,12 @@ class IpTest(testtools.TestCase):
if ssh_client is None:
self.skip('SSH proxy server not configured')
self.test_list_namespaces(ssh_client=ssh_client)
def test_list_namespaces_with_failing_command(self):
self.assertRaises(ip.IpError, ip.list_network_namespaces,
ip_command=['false'],
ssh_client=self.namespace.ssh_client)
def test_list_namespaces_with_ignore_errors(self, **execute_params):
self.test_list_namespaces(ignore_errors=True, ip_command='false',
**execute_params)