Merge "NVMe-oF: read mdstat in Python"
This commit is contained in:
@@ -1261,29 +1261,20 @@ class NVMeOFConnector(base.BaseLinuxConnector):
|
|||||||
except Exception:
|
except Exception:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def get_md_name(self, device_name: str) -> Optional[str]:
|
@staticmethod
|
||||||
get_md_cmd = (
|
def get_md_name(device_name: str) -> Optional[str]:
|
||||||
'cat /proc/mdstat | grep ' + device_name +
|
|
||||||
' | awk \'{print $1;}\'')
|
|
||||||
cmd = ['bash', '-c', get_md_cmd]
|
|
||||||
LOG.debug("[!] cmd = %s", cmd)
|
|
||||||
cmd_output = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
lines, err = self._execute(
|
with open('/proc/mdstat', 'r') as f:
|
||||||
*cmd, run_as_root=True, root_helper=self._root_helper)
|
lines = [line.split(' ')[0]
|
||||||
|
for line in f
|
||||||
|
if device_name in line]
|
||||||
|
|
||||||
for line in lines.split('\n'):
|
if lines:
|
||||||
cmd_output = line
|
return lines[0]
|
||||||
break
|
except Exception as exc:
|
||||||
|
LOG.debug("[!] Could not find md name for %s in mdstat: %s",
|
||||||
|
device_name, exc)
|
||||||
|
|
||||||
LOG.debug("[!] cmd_output = %s", cmd_output)
|
|
||||||
if err:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return cmd_output
|
|
||||||
except putils.ProcessExecutionError as ex:
|
|
||||||
LOG.warning("[!] Could not run cmd: %s", ex)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def stop_and_assemble_raid(self,
|
def stop_and_assemble_raid(self,
|
||||||
|
|||||||
@@ -62,6 +62,13 @@ Node SN Model Namespace Usage Format FW Rev
|
|||||||
/dev/nvme0n2 AB12345 s123 12683 0.00 B / 1.07 GB 512 B + 0 B 2.1.0.0
|
/dev/nvme0n2 AB12345 s123 12683 0.00 B / 1.07 GB 512 B + 0 B 2.1.0.0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
md_stat_contents = """
|
||||||
|
Personalities : [raid0]
|
||||||
|
md0 : active raid0 nvme0n1[4] nvme1n1[3] nvme2n1[2] nvme3n1[1]
|
||||||
|
20508171264 blocks super 1.2 level 5, 512k chunk, algorithm 2 [4/4] [UUUU]
|
||||||
|
unused devices: <none>
|
||||||
|
""" # noqa
|
||||||
|
|
||||||
|
|
||||||
@ddt.ddt
|
@ddt.ddt
|
||||||
class UtilityMethodsTestCase(test_base.TestCase):
|
class UtilityMethodsTestCase(test_base.TestCase):
|
||||||
@@ -1701,29 +1708,20 @@ class NVMeOFConnectorTestCase(test_connector.ConnectorTestCase):
|
|||||||
self.assertEqual(args[1], cmd[1])
|
self.assertEqual(args[1], cmd[1])
|
||||||
self.assertEqual(args[2], cmd[2])
|
self.assertEqual(args[2], cmd[2])
|
||||||
|
|
||||||
@mock.patch.object(executor.Executor, '_execute')
|
def test_get_md_name(self):
|
||||||
def test_get_md_name(self, mock_execute):
|
mock_open = mock.mock_open(read_data=md_stat_contents)
|
||||||
mock_execute.return_value = ('nvme1' + "\n", "")
|
with mock.patch('builtins.open', mock_open):
|
||||||
result = self.connector.get_md_name(NVME_DEVICE_PATH)
|
result = self.connector.get_md_name(os.path.basename(NVME_NS_PATH))
|
||||||
self.assertEqual('nvme1', result)
|
self.assertEqual('md0', result)
|
||||||
get_md_cmd = 'cat /proc/mdstat | grep /dev/nvme1 | awk \'{print $1;}\''
|
mock_open.assert_called_once_with('/proc/mdstat', 'r')
|
||||||
cmd = ['bash', '-c', get_md_cmd]
|
mock_fd = mock_open.return_value.__enter__.return_value
|
||||||
args, kwargs = mock_execute.call_args
|
mock_fd.__iter__.assert_called_once_with()
|
||||||
self.assertEqual(args[0], cmd[0])
|
|
||||||
self.assertEqual(args[1], cmd[1])
|
|
||||||
self.assertEqual(args[2], cmd[2])
|
|
||||||
|
|
||||||
@mock.patch.object(executor.Executor, '_execute')
|
@mock.patch.object(builtins, 'open', side_effect=Exception)
|
||||||
def test_get_md_name_err(self, mock_execute):
|
def test_get_md_name_err(self, mock_open):
|
||||||
mock_execute.side_effect = putils.ProcessExecutionError()
|
result = self.connector.get_md_name(os.path.basename(NVME_NS_PATH))
|
||||||
result = self.connector.get_md_name(NVME_DEVICE_PATH)
|
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
get_md_cmd = 'cat /proc/mdstat | grep /dev/nvme1 | awk \'{print $1;}\''
|
mock_open.assert_called_once_with('/proc/mdstat', 'r')
|
||||||
cmd = ['bash', '-c', get_md_cmd]
|
|
||||||
args, kwargs = mock_execute.call_args
|
|
||||||
self.assertEqual(args[0], cmd[0])
|
|
||||||
self.assertEqual(args[1], cmd[1])
|
|
||||||
self.assertEqual(args[2], cmd[2])
|
|
||||||
|
|
||||||
@mock.patch.object(executor.Executor, '_execute')
|
@mock.patch.object(executor.Executor, '_execute')
|
||||||
def test_is_device_in_raid(self, mock_execute):
|
def test_is_device_in_raid(self, mock_execute):
|
||||||
|
|||||||
Reference in New Issue
Block a user