diff options
| author | Jenkins <jenkins@review.openstack.org> | 2015-09-02 22:17:44 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2015-09-02 22:17:44 +0000 |
| commit | 87223a0f94971eac4d9e14631aefb821839e4c2b (patch) | |
| tree | 55e4a3884781009cf37851aa0177901cdb0a63c0 /ironic_python_agent | |
| parent | b30efc1c7d2f9a63efe08dce5d31fb6c3e2b74d2 (diff) | |
| parent | c014549804096a29bafe5cc95b11a60f0cffc453 (diff) | |
| download | ironic-python-agent-87223a0f94971eac4d9e14631aefb821839e4c2b.tar.gz | |
Merge "Refactor list_block_devices to its own function"
Diffstat (limited to 'ironic_python_agent')
| -rw-r--r-- | ironic_python_agent/hardware.py | 74 | ||||
| -rw-r--r-- | ironic_python_agent/tests/unit/test_hardware.py | 14 |
2 files changed, 52 insertions, 36 deletions
diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py index 0b21b6cd..bf90e5cf 100644 --- a/ironic_python_agent/hardware.py +++ b/ironic_python_agent/hardware.py @@ -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.""" diff --git a/ironic_python_agent/tests/unit/test_hardware.py b/ironic_python_agent/tests/unit/test_hardware.py index 12033899..ceac031f 100644 --- a/ironic_python_agent/tests/unit/test_hardware.py +++ b/ironic_python_agent/tests/unit/test_hardware.py @@ -396,10 +396,20 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual(hardware_info['interfaces'], self.hardware.list_network_interfaces()) + @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_block_device(self, mocked_execute): + def test_list_all_block_device(self, mocked_execute): mocked_execute.return_value = (BLK_DEVICE_TEMPLATE, '') - devices = self.hardware.list_block_devices() + devices = hardware.list_all_block_devices() expected_devices = [ hardware.BlockDevice(name='/dev/sda', model='TinyUSB Drive', |
