self.assertIsNone(dev)
+class GetDiskOfPartitionTestCase(test.TestCase):
+ def test_devpath_is_diskpath(self):
+ devpath = '/some/path'
+ st_mock = mock.Mock()
+ output = utils._get_disk_of_partition(devpath, st_mock)
+ self.assertEqual('/some/path', output[0])
+ self.assertIs(st_mock, output[1])
+
+ with mock.patch('os.stat') as mock_stat:
+ devpath = '/some/path'
+ output = utils._get_disk_of_partition(devpath)
+ mock_stat.assert_called_once_with(devpath)
+ self.assertEqual(devpath, output[0])
+ self.assertIs(mock_stat.return_value, output[1])
+
+ @mock.patch('os.stat', side_effect=OSError)
+ def test_stat_oserror(self, mock_stat):
+ st_mock = mock.Mock()
+ devpath = '/some/path1'
+ output = utils._get_disk_of_partition(devpath, st_mock)
+ mock_stat.assert_called_once_with('/some/path')
+ self.assertEqual(devpath, output[0])
+ self.assertIs(st_mock, output[1])
+
+ @mock.patch('stat.S_ISBLK', return_value=True)
+ @mock.patch('os.stat')
+ def test_diskpath_is_block_device(self, mock_stat, mock_isblk):
+ st_mock = mock.Mock()
+ devpath = '/some/path1'
+ output = utils._get_disk_of_partition(devpath, st_mock)
+ self.assertEqual('/some/path', output[0])
+ self.assertEqual(mock_stat.return_value, output[1])
+
+ @mock.patch('stat.S_ISBLK', return_value=False)
+ @mock.patch('os.stat')
+ def test_diskpath_is_not_block_device(self, mock_stat, mock_isblk):
+ st_mock = mock.Mock()
+ devpath = '/some/path1'
+ output = utils._get_disk_of_partition(devpath, st_mock)
+ self.assertEqual(devpath, output[0])
+ self.assertEqual(st_mock, output[1])
+
+
class MonkeyPatchTestCase(test.TestCase):
"""Unit test for utils.monkey_patch()."""
def setUp(self):
for '/dev/disk1p1' ('p' is prepended to the partition number if the disk
name ends with numbers).
"""
- if st is None:
- st = os.stat(devpath)
diskpath = re.sub('(?:(?<=\d)p)?\d+$', '', devpath)
if diskpath != devpath:
try:
- st = os.stat(diskpath)
- if stat.S_ISBLK(st.st_mode):
- return (diskpath, st)
+ st_disk = os.stat(diskpath)
+ if stat.S_ISBLK(st_disk.st_mode):
+ return (diskpath, st_disk)
except OSError:
pass
# devpath is not a partition
+ if st is None:
+ st = os.stat(devpath)
return (devpath, st)