update os-brick to pass python3 tests

This patch adds the py34 tests for tox.ini and updates some
unit tests, exception and some code to support passing the
py34 tests.

Change-Id: Ib50b988966c24d97117472c771e63dd796af5f59
Closes-Bug: 1469338
This commit is contained in:
Walter A. Boring IV 2015-06-26 23:41:51 +00:00
parent 3f08612338
commit 6cc5f4a107
8 changed files with 42 additions and 23 deletions

View File

@ -37,6 +37,7 @@ class BrickException(Exception):
def __init__(self, message=None, **kwargs):
self.kwargs = kwargs
self.kwargs['message'] = message
if 'code' not in self.kwargs:
try:
@ -44,7 +45,11 @@ class BrickException(Exception):
except AttributeError:
pass
if not message:
for k, v in self.kwargs.items():
if isinstance(v, Exception):
self.kwargs[k] = six.text_type(v)
if self._should_format():
try:
message = self.message % kwargs
@ -53,7 +58,7 @@ class BrickException(Exception):
# log the issue and the kwargs
LOG.exception(_LE("Exception in string format operation. "
"msg='%s'"), self.message)
for name, value in kwargs.iteritems():
for name, value in kwargs.items():
LOG.error(_LE("%(name)s: %(value)s"), {'name': name,
'value': value})
@ -68,6 +73,9 @@ class BrickException(Exception):
def __unicode__(self):
return six.text_type(self.msg)
def _should_format(self):
return self.kwargs['message'] is None or '%(message)' in self.message
class NotFound(BrickException):
message = _("Resource could not be found.")

View File

@ -556,8 +556,8 @@ class ISCSIConnector(InitiatorConnector):
# As discovery result may contain other targets' iqns, extract targets
# to be disconnected whose block devices are already deleted here.
ips_iqns = []
entries = map(lambda x: x.lstrip('ip-').split('-lun-')[0],
self._get_iscsi_devices())
entries = [device.lstrip('ip-').split('-lun-')[0]
for device in self._get_iscsi_devices()]
for ip, iqn in all_ips_iqns:
ip_iqn = "%s-iscsi-%s" % (ip.split(",")[0], iqn)
if ip_iqn not in entries:

View File

@ -66,6 +66,8 @@ class RemoteFsClient(object):
"""Return a string that represents hash of base_str
(in a hex format).
"""
if isinstance(base_str, six.text_type):
base_str = base_str.encode('utf-8')
return hashlib.md5(base_str).hexdigest()
def get_mount_point(self, device_name):

View File

@ -14,7 +14,6 @@
import os.path
import platform
import string
import tempfile
import time
@ -22,6 +21,7 @@ import mock
from oslo_concurrency import processutils as putils
from oslo_log import log as logging
from oslo_service import loopingcall
from oslo_utils import encodeutils
import six
import testtools
@ -109,7 +109,7 @@ class ConnectorTestCase(base.TestCase):
self.cmds = []
def fake_execute(self, *cmd, **kwargs):
self.cmds.append(string.join(cmd))
self.cmds.append(" ".join(cmd))
return "", None
def test_connect_volume(self):
@ -265,7 +265,7 @@ class ISCSIConnectorTestCase(ConnectorTestCase):
iqn = 'iqn.2010-10.org.openstack:%s' % name
vol = {'id': 1, 'name': name}
connection_info = self.iscsi_connection(vol, location, iqn)
for key, value in extra_props.iteritems():
for key, value in extra_props.items():
connection_info['data'][key] = value
device = self.connector.connect_volume(connection_info['data'])
dev_str = '/dev/disk/by-path/ip-%s-iscsi-%s-lun-1' % (location, iqn)
@ -1171,7 +1171,7 @@ class HuaweiStorHyperConnectorTestCase(ConnectorTestCase):
def fake_execute(self, *cmd, **kwargs):
method = cmd[2]
self.cmds.append(string.join(cmd))
self.cmds.append(" ".join(cmd))
if 'attach' == method:
HuaweiStorHyperConnectorTestCase.attached = True
return 'ret_code=0', None
@ -1186,7 +1186,7 @@ class HuaweiStorHyperConnectorTestCase(ConnectorTestCase):
def fake_execute_fail(self, *cmd, **kwargs):
method = cmd[2]
self.cmds.append(string.join(cmd))
self.cmds.append(" ".join(cmd))
if 'attach' == method:
HuaweiStorHyperConnectorTestCase.attached = False
return 'ret_code=330151401', None
@ -1522,18 +1522,19 @@ class RBDConnectorTestCase(ConnectorTestCase):
# Ensure rados is instantiated correctly
mock_rados.Rados.assert_called_once_with(
rados_id=self.user,
rados_id=encodeutils.safe_encode(self.user),
conffile='/etc/ceph/ceph.conf')
# Ensure correct calls to connect to cluster
mock_rados.Rados.return_value.connect.assert_called_once()
mock_rados.Rados.return_value.open_ioctx.assert_called_once_with(
self.pool)
encodeutils.safe_encode(self.pool))
# Ensure rbd image is instantiated correctly
mock_rbd.Image.assert_called_once_with(
mock_rados.Rados.return_value.open_ioctx.return_value,
self.volume, read_only=False, snapshot=None)
encodeutils.safe_encode(self.volume), read_only=False,
snapshot=None)
# Ensure expected object is returned correctly
self.assertTrue(isinstance(device_info['path'],

View File

@ -13,7 +13,6 @@
# under the License.
import os.path
import string
import mock
from oslo_log import log as logging
@ -35,7 +34,7 @@ class LinuxFCTestCase(base.TestCase):
self.lfc = linuxfc.LinuxFibreChannel(None, execute=self.fake_execute)
def fake_execute(self, *cmd, **kwargs):
self.cmds.append(string.join(cmd))
self.cmds.append(" ".join(cmd))
return "", None
def test_rescan_hosts(self):

View File

@ -14,7 +14,6 @@
import os
import os.path
import string
import mock
from oslo_log import log as logging
@ -36,7 +35,7 @@ class LinuxSCSITestCase(base.TestCase):
self.linuxscsi = linuxscsi.LinuxSCSI(None, execute=self.fake_execute)
def fake_execute(self, *cmd, **kwargs):
self.cmds.append(string.join(cmd))
self.cmds.append(" ".join(cmd))
return "", None
def test_echo_scsi_command(self):
@ -132,7 +131,6 @@ class LinuxSCSITestCase(base.TestCase):
self.linuxscsi._execute = fake_execute
info = self.linuxscsi.find_multipath_device('/dev/sde')
LOG.error("info = %s" % info)
self.assertEqual("350002ac20398383d", info["id"])
self.assertEqual("mpath6", info["name"])
@ -167,7 +165,6 @@ class LinuxSCSITestCase(base.TestCase):
self.linuxscsi._execute = fake_execute
info = self.linuxscsi.find_multipath_device('/dev/sde')
LOG.error("info = %s" % info)
self.assertEqual("36005076da00638089c000000000004d5", info["id"])
self.assertEqual("36005076da00638089c000000000004d5", info["name"])
@ -200,7 +197,6 @@ class LinuxSCSITestCase(base.TestCase):
self.linuxscsi._execute = fake_execute
info = self.linuxscsi.find_multipath_device('/dev/sdd')
LOG.error("info = %s" % info)
self.assertEqual("36005076303ffc48e0000000000000101", info["id"])
self.assertEqual("36005076303ffc48e0000000000000101", info["name"])
@ -234,7 +230,6 @@ class LinuxSCSITestCase(base.TestCase):
self.linuxscsi._execute = fake_execute
info = self.linuxscsi.find_multipath_device('/dev/sdd')
LOG.error("info = %s" % info)
self.assertEqual("36005076303ffc48e0000000000000101", info["id"])
self.assertEqual("36005076303ffc48e0000000000000101", info["name"])

View File

@ -93,12 +93,13 @@ class TestRetryDecorator(base.TestCase):
expected_sleep_arg = []
for i in xrange(retries):
for i in range(retries):
if i > 0:
interval *= backoff_rate
expected_sleep_arg.append(float(interval))
mock_sleep.assert_has_calls(map(mock.call, expected_sleep_arg))
mock_sleep.assert_has_calls(
list(map(mock.call, expected_sleep_arg)))
def test_wrong_exception_no_retry(self):

15
tox.ini
View File

@ -1,6 +1,6 @@
[tox]
minversion = 1.6
envlist = py27,pep8
envlist = py27,py34,pep8
skipsdist = True
[testenv]
@ -28,6 +28,19 @@ commands = python setup.py testr --coverage --testr-args='{posargs}'
[testenv:docs]
commands = python setup.py build_sphinx
[testenv:py34]
commands =
find . -type f -name "*.pyc" -delete
python -m testtools.run \
os_brick.tests.test_brick \
os_brick.tests.test_exception \
os_brick.tests.test_utils \
os_brick.tests.initiator.test_connector \
os_brick.tests.initiator.test_linuxfc \
os_brick.tests.initiator.test_linuxrbd \
os_brick.tests.initiator.test_linuxscsi
[flake8]
# H803 skipped on purpose per list discussion.
# E123, E125 skipped as they are invalid PEP-8.