Merge "Refactor list_block_devices to its own function"
This commit is contained in:
commit
87223a0f94
@ -39,6 +39,45 @@ UNIT_CONVERTER.define('MB = []')
|
||||
UNIT_CONVERTER.define('GB = 1024 MB')
|
||||
|
||||
|
||||
def list_all_block_devices():
|
||||
"""List all physical block devices
|
||||
|
||||
The switches we use for lsblk: P for KEY="value" output, b for size output
|
||||
in bytes, d to exclude dependent devices (like md or dm devices), i to
|
||||
ensure ascii characters only, and o to specify the fields we need.
|
||||
|
||||
Broken out as its own function to facilitate custom hardware managers that
|
||||
don't need to subclass GenericHardwareManager.
|
||||
|
||||
:return: A list of BlockDevices
|
||||
"""
|
||||
report = utils.execute('lsblk', '-PbdioKNAME,MODEL,SIZE,ROTA,TYPE',
|
||||
check_exit_code=[0])[0]
|
||||
lines = report.split('\n')
|
||||
|
||||
devices = []
|
||||
for line in lines:
|
||||
device = {}
|
||||
# Split into KEY=VAL pairs
|
||||
vals = shlex.split(line)
|
||||
for key, val in (v.split('=', 1) for v in vals):
|
||||
device[key] = val.strip()
|
||||
# Ignore non disk
|
||||
if device.get('TYPE') != 'disk':
|
||||
continue
|
||||
|
||||
# Ensure all required keys are at least present, even if blank
|
||||
diff = set(['KNAME', 'MODEL', 'SIZE', 'ROTA']) - set(device.keys())
|
||||
if diff:
|
||||
raise errors.BlockDeviceError(
|
||||
'%s must be returned by lsblk.' % diff)
|
||||
devices.append(BlockDevice(name='/dev/' + device['KNAME'],
|
||||
model=device['MODEL'],
|
||||
size=int(device['SIZE']),
|
||||
rotational=bool(int(device['ROTA']))))
|
||||
return devices
|
||||
|
||||
|
||||
class HardwareSupport(object):
|
||||
"""Example priorities for hardware managers.
|
||||
|
||||
@ -336,40 +375,7 @@ class GenericHardwareManager(HardwareManager):
|
||||
return Memory(total=total, physical_mb=physical)
|
||||
|
||||
def list_block_devices(self):
|
||||
"""List all physical block devices
|
||||
|
||||
The switches we use for lsblk: P for KEY="value" output,
|
||||
b for size output in bytes, d to exclude dependant devices
|
||||
(like md or dm devices), i to ensure ascii characters only,
|
||||
and o to specify the fields we need
|
||||
|
||||
:return: A list of BlockDevices
|
||||
"""
|
||||
report = utils.execute('lsblk', '-PbdioKNAME,MODEL,SIZE,ROTA,TYPE',
|
||||
check_exit_code=[0])[0]
|
||||
lines = report.split('\n')
|
||||
|
||||
devices = []
|
||||
for line in lines:
|
||||
device = {}
|
||||
# Split into KEY=VAL pairs
|
||||
vals = shlex.split(line)
|
||||
for key, val in (v.split('=', 1) for v in vals):
|
||||
device[key] = val.strip()
|
||||
# Ignore non disk
|
||||
if device.get('TYPE') != 'disk':
|
||||
continue
|
||||
|
||||
# Ensure all required keys are at least present, even if blank
|
||||
diff = set(['KNAME', 'MODEL', 'SIZE', 'ROTA']) - set(device.keys())
|
||||
if diff:
|
||||
raise errors.BlockDeviceError(
|
||||
'%s must be returned by lsblk.' % diff)
|
||||
devices.append(BlockDevice(name='/dev/' + device['KNAME'],
|
||||
model=device['MODEL'],
|
||||
size=int(device['SIZE']),
|
||||
rotational=bool(int(device['ROTA']))))
|
||||
return devices
|
||||
return list_all_block_devices()
|
||||
|
||||
def _get_device_vendor(self, dev):
|
||||
"""Get the vendor name of a given device."""
|
||||
|
@ -396,10 +396,20 @@ class TestGenericHardwareManager(test_base.BaseTestCase):
|
||||
self.assertEqual(hardware_info['interfaces'],
|
||||
self.hardware.list_network_interfaces())
|
||||
|
||||
@mock.patch.object(utils, 'execute')
|
||||
def test_list_block_device(self, mocked_execute):
|
||||
mocked_execute.return_value = (BLK_DEVICE_TEMPLATE, '')
|
||||
@mock.patch.object(hardware, 'list_all_block_devices')
|
||||
def test_list_block_devices(self, list_mock):
|
||||
device = hardware.BlockDevice('/dev/hdaa', 'small', 65535, False)
|
||||
list_mock.return_value = [device]
|
||||
devices = self.hardware.list_block_devices()
|
||||
|
||||
self.assertEqual([device], devices)
|
||||
|
||||
list_mock.assert_called_once_with()
|
||||
|
||||
@mock.patch.object(utils, 'execute')
|
||||
def test_list_all_block_device(self, mocked_execute):
|
||||
mocked_execute.return_value = (BLK_DEVICE_TEMPLATE, '')
|
||||
devices = hardware.list_all_block_devices()
|
||||
expected_devices = [
|
||||
hardware.BlockDevice(name='/dev/sda',
|
||||
model='TinyUSB Drive',
|
||||
|
Loading…
x
Reference in New Issue
Block a user