block_device: Make refresh_conn_infos py3 compatible

Also add a simple test ensuring that refresh_connection_info is called
for each DriverVolumeBlockDevice derived device provided.

Related-Bug: #1419577
Partially-Implements: blueprint goal-python35
Change-Id: Ib1ff00e7f4f5b599317d7111c322ce9af8a9a2b1
This commit is contained in:
Lee Yarwood 2016-07-14 11:53:09 +01:00
parent 0b59a2d9dc
commit b83cae02ec
2 changed files with 38 additions and 4 deletions

View File

@ -1052,3 +1052,36 @@ class TestDriverBlockDevice(test.NoDBTestCase):
instance = fake_instance.fake_instance_obj(self.context, **updates)
self.assertIsNone(
driver_block_device._get_volume_create_az_value(instance))
def test_refresh_conn_infos(self):
# Only DriverVolumeBlockDevice derived devices should refresh their
# connection_info during a refresh_conn_infos call.
test_volume = mock.MagicMock(
spec=driver_block_device.DriverVolumeBlockDevice)
test_image = mock.MagicMock(
spec=driver_block_device.DriverImageBlockDevice)
test_snapshot = mock.MagicMock(
spec=driver_block_device.DriverSnapshotBlockDevice)
test_blank = mock.MagicMock(
spec=driver_block_device.DriverBlankBlockDevice)
test_eph = mock.MagicMock(
spec=driver_block_device.DriverEphemeralBlockDevice)
test_swap = mock.MagicMock(
spec=driver_block_device.DriverSwapBlockDevice)
block_device_mapping = [test_volume, test_image, test_eph,
test_snapshot, test_swap, test_blank]
driver_block_device.refresh_conn_infos(block_device_mapping,
mock.sentinel.refresh_context,
mock.sentinel.refresh_instance,
mock.sentinel.refresh_vol_api,
mock.sentinel.refresh_virt_drv)
for test_mock in [test_volume, test_image, test_snapshot, test_blank]:
test_mock.refresh_connection_info.assert_called_once_with(
mock.sentinel.refresh_context,
mock.sentinel.refresh_instance,
mock.sentinel.refresh_vol_api,
mock.sentinel.refresh_virt_drv)
# NOTE(lyarwood): Can't think of a better way of testing this as we
# can't assert_not_called if the method isn't in the spec.
self.assertFalse(hasattr(test_eph, 'refresh_connection_info'))
self.assertFalse(hasattr(test_swap, 'refresh_connection_info'))

View File

@ -14,7 +14,6 @@
import functools
import itertools
import operator
from oslo_log import log as logging
from oslo_serialization import jsonutils
@ -514,9 +513,11 @@ def attach_block_devices(block_device_mapping, *attach_args, **attach_kwargs):
def refresh_conn_infos(block_device_mapping, *refresh_args, **refresh_kwargs):
map(operator.methodcaller('refresh_connection_info',
*refresh_args, **refresh_kwargs),
block_device_mapping)
for device in block_device_mapping:
# NOTE(lyarwood): At present only DriverVolumeBlockDevice derived
# devices provide a refresh_connection_info method.
if hasattr(device, 'refresh_connection_info'):
device.refresh_connection_info(*refresh_args, **refresh_kwargs)
return block_device_mapping