Merge "Execute error in _detect_cna_card"

This commit is contained in:
Zuul 2018-01-26 21:47:15 +00:00 committed by Gerrit Code Review
commit a3463dc57d
2 changed files with 21 additions and 14 deletions

View File

@ -14,6 +14,7 @@
import os
from oslo_concurrency import processutils
from oslo_config import cfg
from oslo_log import log
@ -27,13 +28,18 @@ CONF = cfg.CONF
def _detect_cna_card():
addr_path = '/sys/class/net'
for net_dev in os.listdir(addr_path):
link_path = '{}/{}/device/driver/module'.format(addr_path, net_dev)
try:
link_command = 'readlink {}/{}/device/driver/module'.format(
addr_path, net_dev)
out = utils.execute(link_command.split())
if not out or out[1]:
continue
except OSError:
out = utils.execute('readlink', '-v', link_path)
except OSError as e:
LOG.warning('Something went wrong when readlink for '
'interface %(device)s. Error: %(error)s',
{'device': net_dev, 'error': e})
continue
except processutils.ProcessExecutionError as e:
LOG.debug('Get driver for interface %(device)s failed. '
'Error: %(error)s',
{'device': net_dev, 'error': e})
continue
driver_name = os.path.basename(out[0].strip())
if driver_name == 'i40e':

View File

@ -15,6 +15,7 @@
import os
import mock
from oslo_concurrency import processutils
from oslo_config import cfg
from ironic_python_agent import hardware
@ -38,9 +39,9 @@ class TestIntelCnaHardwareManager(base.IronicAgentTest):
@mock.patch.object(utils, 'execute', autospec=True)
def test_detect_cna_card(self, mock_execute, mock_listdir):
def mock_return_execute(*args, **kwargs):
if 'eth0' in args[0][1]:
if 'eth0' in args[2]:
return '/foo/bar/fake', ''
if 'eth1' in args[0][1]:
if 'eth1' in args[2]:
return '/foo/bar/i40e', ''
mock_listdir.return_value = ['eth0', 'eth1']
@ -51,11 +52,11 @@ class TestIntelCnaHardwareManager(base.IronicAgentTest):
@mock.patch.object(utils, 'execute', autospec=True)
def test_detect_cna_card_execute_error(self, mock_execute, mock_listdir):
def mock_return_execute(*args, **kwargs):
if 'eth0' in args[0][1]:
if 'eth0' in args[2]:
return '/foo/bar/fake', ''
if 'eth1' in args[0][1]:
return '', 'fake error'
if 'eth2' in args[0][1]:
if 'eth1' in args[2]:
raise processutils.ProcessExecutionError('fake')
if 'eth2' in args[2]:
raise OSError('fake')
mock_listdir.return_value = ['eth0', 'eth1', 'eth2']
@ -66,9 +67,9 @@ class TestIntelCnaHardwareManager(base.IronicAgentTest):
@mock.patch.object(utils, 'execute', autospec=True)
def test_detect_cna_card_no_i40e_driver(self, mock_execute, mock_listdir):
def mock_return_execute(*args, **kwargs):
if 'eth0' in args[0][1]:
if 'eth0' in args[2]:
return '/foo/bar/fake1', ''
if 'eth1' in args[0][1]:
if 'eth1' in args[2]:
return '/foo/bar/fake2', ''
mock_listdir.return_value = ['eth0', 'eth1']