Allow to ignore ip command errors
Change-Id: I8d7c77a5f9934b5e835752ce05100f86591c4ec6
This commit is contained in:
parent
87c804712d
commit
135de24450
@ -16,11 +16,15 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import netaddr
|
import netaddr
|
||||||
|
from oslo_log import log
|
||||||
|
|
||||||
import tobiko
|
import tobiko
|
||||||
from tobiko.shell import sh
|
from tobiko.shell import sh
|
||||||
|
|
||||||
|
|
||||||
|
LOG = log.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class IpError(tobiko.TobikoException):
|
class IpError(tobiko.TobikoException):
|
||||||
message = ("Unable to get IP addresses from host "
|
message = ("Unable to get IP addresses from host "
|
||||||
"(exit_status={exit_status!r}): {error!s}")
|
"(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):
|
def list_network_namespaces(**execute_params):
|
||||||
output = execute_ip(['-o', 'netns', 'list'], **execute_params)
|
|
||||||
namespaces = tobiko.Selection()
|
namespaces = tobiko.Selection()
|
||||||
|
output = execute_ip(['-o', 'netns', 'list'], **execute_params)
|
||||||
if output:
|
if output:
|
||||||
for line in output.splitlines():
|
for line in output.splitlines():
|
||||||
fields = line.strip().split()
|
fields = line.strip().split()
|
||||||
@ -76,10 +80,18 @@ def list_network_namespaces(**execute_params):
|
|||||||
return namespaces
|
return namespaces
|
||||||
|
|
||||||
|
|
||||||
def execute_ip(ifconfig_args, **execute_params):
|
IP_COMMAND = sh.shell_command(['/sbin/ip'])
|
||||||
command = ['/sbin/ip'] + ifconfig_args
|
|
||||||
|
|
||||||
|
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,
|
result = sh.execute(command, stdin=False, stdout=True, stderr=True,
|
||||||
expect_exit_status=None, **execute_params)
|
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)
|
raise IpError(error=result.stderr, exit_status=result.exit_status)
|
||||||
return result.stdout
|
return result.stdout
|
||||||
|
@ -13,6 +13,7 @@ class NetworkNamespaceFixture(tobiko.SharedFixture):
|
|||||||
def setup_fixture(self):
|
def setup_fixture(self):
|
||||||
for node in topology.list_openstack_nodes():
|
for node in topology.list_openstack_nodes():
|
||||||
network_namespaces = ip.list_network_namespaces(
|
network_namespaces = ip.list_network_namespaces(
|
||||||
|
ignore_errors=True,
|
||||||
ssh_client=node.ssh_client)
|
ssh_client=node.ssh_client)
|
||||||
if network_namespaces:
|
if network_namespaces:
|
||||||
self.network_namespace = network_namespaces.first
|
self.network_namespace = network_namespaces.first
|
||||||
|
@ -115,6 +115,15 @@ class IpTest(testtools.TestCase):
|
|||||||
def test_list_ip_addresses_with_namespace_and_scope(self):
|
def test_list_ip_addresses_with_namespace_and_scope(self):
|
||||||
self.test_list_ip_addresses_with_namespace(scope='global')
|
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):
|
def test_list_namespaces(self, **execute_params):
|
||||||
namespaces = ip.list_network_namespaces(**execute_params)
|
namespaces = ip.list_network_namespaces(**execute_params)
|
||||||
self.assertIsInstance(namespaces, list)
|
self.assertIsInstance(namespaces, list)
|
||||||
@ -133,3 +142,12 @@ class IpTest(testtools.TestCase):
|
|||||||
if ssh_client is None:
|
if ssh_client is None:
|
||||||
self.skip('SSH proxy server not configured')
|
self.skip('SSH proxy server not configured')
|
||||||
self.test_list_namespaces(ssh_client=ssh_client)
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user