Using from_device_file from Devices

The from_device_file method in pyudev.Device is deprecated since
version 0.18
Using the same method from pyudev.Devices is now recommended.

Increasing minimum version of pyudev to 0.18 and correctly handling
exception raised by from_device_file.

Change-Id: I0637585241c81a36f69aef8017f1134f5c526eb1
This commit is contained in:
Riccardo Pittau 2019-07-08 18:42:16 +02:00
parent e33744ac2d
commit 8c9fd6dffc
4 changed files with 10 additions and 28 deletions

View File

@ -308,9 +308,8 @@ def list_all_block_devices(block_type='disk',
name = os.path.join('/dev', device['KNAME'])
try:
udev = pyudev.Device.from_device_file(context, name)
# pyudev started raising another error in 0.18
except (ValueError, EnvironmentError, pyudev.DeviceNotFoundError) as e:
udev = pyudev.Devices.from_device_file(context, name)
except pyudev.DeviceNotFoundByFileError as e:
LOG.warning("Device %(dev)s is inaccessible, skipping... "
"Error: %(error)s", {'dev': name, 'error': e})
extra = {}

View File

@ -1453,7 +1453,7 @@ class TestGenericHardwareManager(base.IronicAgentTest):
@mock.patch.object(os, 'readlink', autospec=True)
@mock.patch.object(os, 'listdir', autospec=True)
@mock.patch.object(hardware, '_get_device_info', autospec=True)
@mock.patch.object(pyudev.Device, 'from_device_file', autospec=False)
@mock.patch.object(pyudev.Devices, 'from_device_file', autospec=False)
@mock.patch.object(utils, 'execute', autospec=True)
def test_list_all_block_device(self, mocked_execute, mocked_udev,
mocked_dev_vendor, mock_listdir,
@ -1468,7 +1468,7 @@ class TestGenericHardwareManager(base.IronicAgentTest):
mock_listdir.return_value = [os.path.basename(x)
for x in sorted(by_path_map)]
mocked_execute.return_value = (BLK_DEVICE_TEMPLATE, '')
mocked_udev.side_effect = pyudev.DeviceNotFoundError()
mocked_udev.side_effect = pyudev.DeviceNotFoundByFileError()
mocked_dev_vendor.return_value = 'Super Vendor'
devices = hardware.list_all_block_devices()
expected_devices = [
@ -1516,26 +1516,9 @@ class TestGenericHardwareManager(base.IronicAgentTest):
for dev in range(3)]
mock_readlink.assert_has_calls(expected_calls)
@mock.patch.object(os, 'readlink', autospec=True)
@mock.patch.object(os, 'listdir', autospec=True)
@mock.patch.object(hardware, '_get_device_info', autospec=True)
@mock.patch.object(pyudev.Device, 'from_device_file', autospec=False)
@mock.patch.object(utils, 'execute', autospec=True)
def test_list_all_block_device_udev_17(self, mocked_execute, mocked_udev,
mocked_dev_vendor, mocked_listdir,
mocked_readlink):
# test compatibility with pyudev < 0.18
mocked_readlink.return_value = '../../sda'
mocked_listdir.return_value = ['1:0:0:0']
mocked_execute.return_value = (BLK_DEVICE_TEMPLATE, '')
mocked_udev.side_effect = OSError()
mocked_dev_vendor.return_value = 'Super Vendor'
devices = hardware.list_all_block_devices()
self.assertEqual(4, len(devices))
@mock.patch.object(os, 'listdir', autospec=True)
@mock.patch.object(hardware, '_get_device_info', autospec=True)
@mock.patch.object(pyudev.Device, 'from_device_file', autospec=False)
@mock.patch.object(pyudev.Devices, 'from_device_file', autospec=False)
@mock.patch.object(utils, 'execute', autospec=True)
def test_list_all_block_device_hctl_fail(self, mocked_execute, mocked_udev,
mocked_dev_vendor,
@ -1555,7 +1538,7 @@ class TestGenericHardwareManager(base.IronicAgentTest):
@mock.patch.object(os, 'readlink', autospec=True)
@mock.patch.object(os, 'listdir', autospec=True)
@mock.patch.object(hardware, '_get_device_info', autospec=True)
@mock.patch.object(pyudev.Device, 'from_device_file', autospec=False)
@mock.patch.object(pyudev.Devices, 'from_device_file', autospec=False)
@mock.patch.object(utils, 'execute', autospec=True)
def test_list_all_block_device_with_udev(self, mocked_execute, mocked_udev,
mocked_dev_vendor, mocked_listdir,
@ -3058,7 +3041,7 @@ class TestModuleFunctions(base.IronicAgentTest):
@mock.patch.object(hardware, '_get_device_info',
lambda x, y, z: 'FooTastic')
@mock.patch.object(hardware, '_udev_settle', autospec=True)
@mock.patch.object(hardware.pyudev.Device, "from_device_file",
@mock.patch.object(hardware.pyudev.Devices, "from_device_file",
autospec=False)
def test_list_all_block_devices_success(self, mocked_fromdevfile,
mocked_udev, mocked_readlink,
@ -3077,7 +3060,7 @@ class TestModuleFunctions(base.IronicAgentTest):
@mock.patch.object(hardware, '_get_device_info',
lambda x, y, z: 'FooTastic')
@mock.patch.object(hardware, '_udev_settle', autospec=True)
@mock.patch.object(hardware.pyudev.Device, "from_device_file",
@mock.patch.object(hardware.pyudev.Devices, "from_device_file",
autospec=False)
def test_list_all_block_devices_success_raid(self, mocked_fromdevfile,
mocked_udev, mocked_readlink,

View File

@ -69,7 +69,7 @@ python-dateutil==2.7.0
python-mimeparse==1.6.0
python-subunit==1.2.0
pytz==2018.3
pyudev==0.16.1
pyudev==0.18
PyYAML==3.12
reno==2.5.0
repoze.lru==0.7

View File

@ -15,7 +15,7 @@ oslo.utils>=3.33.0 # Apache-2.0
pecan!=1.0.2,!=1.0.3,!=1.0.4,!=1.2,>=1.0.0 # BSD
Pint>=0.5 # BSD
psutil>=3.2.2 # BSD
pyudev>=0.16.1 # LGPLv2.1+
pyudev>=0.18 # LGPLv2.1+
requests>=2.14.2 # Apache-2.0
rtslib-fb>=2.1.65 # Apache-2.0
six>=1.10.0 # MIT