]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Port objects unit tests to Python 3
authorVictor Stinner <vstinner@redhat.com>
Tue, 26 Jan 2016 14:48:34 +0000 (15:48 +0100)
committerVictor Stinner <vstinner@redhat.com>
Mon, 8 Feb 2016 09:47:53 +0000 (10:47 +0100)
* BaseObjectsTestCase._compare(): replace hasattr() with getattr()
  and a white list of expected exceptions. On Python 2, hasattr()
  ignores *all* exceptions and so may hide real bugs.
* Mocks: except a call to obj.__bool__() rather than
  obj.__nonzero__() on Python 3. bool(obj) now calls obj.__bool__()
  on Python 3.
* Replace dict.keys() with list(dict.keys()) to get a list on Python
  3. On Python 3, dict.keys() now returns a view.
* JSON/base64:

  * Replace jsonutils.dumps() with jsonutils.dump_as_bytes() to get
    JSON as bytes
  * Replace base64.encodestring() with oslo_serialization.base64.encode_as_text() to get
    base64 as bytes
  * Replace base64.decodestring() with
    oslo_serialization.base64.decode_as_text() to accept Unicode and
    return Unicode

* tests-py3.txt: add cinder.tests.unit.objects.test_* (11 files)

Partial-Implements: blueprint cinder-python3
Change-Id: I9ce7753eb947bf9a5c91e643f5c65156c8e3f7cc

13 files changed:
cinder/objects/backup.py
cinder/tests/unit/api/contrib/test_backups.py
cinder/tests/unit/objects/__init__.py
cinder/tests/unit/objects/test_backup.py
cinder/tests/unit/objects/test_base.py
cinder/tests/unit/objects/test_cgsnapshot.py
cinder/tests/unit/objects/test_consistencygroup.py
cinder/tests/unit/objects/test_service.py
cinder/tests/unit/objects/test_snapshot.py
cinder/tests/unit/objects/test_volume.py
cinder/tests/unit/objects/test_volume_attachment.py
cinder/tests/unit/objects/test_volume_type.py
tests-py3.txt

index d3df6f05e032d912fd5c37c2b05dfdd6910f3320..6193d118aea5d6011ee7ad1364f08ba6e3fea394 100644 (file)
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import base64
-import binascii
-
 from oslo_config import cfg
 from oslo_log import log as logging
+from oslo_serialization import base64
 from oslo_serialization import jsonutils
 from oslo_utils import versionutils
 from oslo_versionedobjects import fields
-import six
 
 from cinder import db
 from cinder import exception
@@ -136,8 +133,8 @@ class Backup(base.CinderPersistentObject, base.CinderObject,
         :raises: InvalidInput
         """
         try:
-            return jsonutils.loads(base64.decodestring(backup_url))
-        except binascii.Error:
+            return jsonutils.loads(base64.decode_as_text(backup_url))
+        except TypeError:
             msg = _("Can't decode backup record.")
         except ValueError:
             msg = _("Can't parse backup record.")
@@ -153,10 +150,8 @@ class Backup(base.CinderPersistentObject, base.CinderObject,
         # We must update kwargs instead of record to ensure we don't overwrite
         # "real" data from the backup
         kwargs.update(record)
-        retval = jsonutils.dumps(kwargs)
-        if six.PY3:
-            retval = retval.encode('utf-8')
-        return base64.encodestring(retval)
+        retval = jsonutils.dump_as_bytes(kwargs)
+        return base64.encode_as_text(retval)
 
 
 @base.CinderObjectRegistry.register
index 36290ecb70669ba91a5441d72f453c60fca5d0a2..d12017e76e93dfa8299d6407738578b90e36eefe 100644 (file)
@@ -23,6 +23,7 @@ from xml.dom import minidom
 import ddt
 import mock
 from oslo_utils import timeutils
+import six
 import webob
 
 # needed for stubs to work
@@ -1792,6 +1793,8 @@ class BackupsAPITestCase(test.TestCase):
         _mock_list_services.return_value = [backup_service]
 
         req = webob.Request.blank('/v2/fake/backups/import_record')
+        if six.PY2:
+            backup_url = backup_url.encode('utf-8')
         req.body = ('<backup-record backup_service="%(backup_service)s" '
                     'backup_url="%(backup_url)s"/>') \
             % {'backup_url': backup_url,
index c697b391d197fa4b82222b8d6012ffd34070214f..7a5066ca9358bb677224d60296c66dd248791ecb 100644 (file)
@@ -15,6 +15,7 @@
 from oslo_utils import timeutils
 
 from cinder import context
+from cinder import exception
 from cinder.objects import base as obj_base
 from cinder import test
 
@@ -36,7 +37,12 @@ class BaseObjectsTestCase(test.TestCase):
     @staticmethod
     def _compare(test, db, obj):
         for field, value in db.items():
-            if not hasattr(obj, field):
+            try:
+                getattr(obj, field)
+            except (AttributeError, exception.CinderException,
+                    NotImplementedError):
+                # NotImplementedError: ignore "Cannot load 'projects' in the
+                # base class" error
                 continue
 
             if field in ('modified_at', 'created_at',
index bb3901aa00b33015766b0529481fe5f38c66eded..4b9a48bc7d519ea426379d23f10e0fe7f01df427 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import mock
+import six
 
 from cinder.db.sqlalchemy import models
 from cinder import exception
@@ -164,8 +165,12 @@ class TestBackup(test_objects.BaseObjectsTestCase):
         # for that field
         backup.refresh()
         self._compare(self, db_backup2, backup)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         backup_get.assert_has_calls([mock.call(self.context, '1'),
-                                     mock.call.__nonzero__(),
+                                     call_bool,
                                      mock.call(self.context, '1')])
 
 
index 7f53518ef462554ecd390458071f97c6b9b41730..70e2da3020325cbe43e057261a4dbd28878eb6a3 100644 (file)
@@ -173,7 +173,7 @@ class TestCinderObjectConditionalUpdate(test.TestCase):
         self.assertEqual(status, volume.status)
         self.assertEqual(size, volume.size)
         dirty = volume.cinder_obj_get_changes()
-        self.assertEqual(list(dirty_keys), dirty.keys())
+        self.assertEqual(list(dirty_keys), list(dirty.keys()))
         for key, value in kwargs.items():
             self.assertEqual(value, getattr(volume, key))
 
index 5d8cc97f5d3489f0ef6feb1b4a9db203ca25295e..086b2743e12b0f0c4c5d316d62138b69d2571d7f 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import mock
+import six
 
 from cinder import exception
 from cinder import objects
@@ -123,8 +124,12 @@ class TestCGSnapshot(test_objects.BaseObjectsTestCase):
         # value for that field
         cgsnapshot.refresh()
         self._compare(self, db_cgsnapshot2, cgsnapshot)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         cgsnapshot_get.assert_has_calls([mock.call(self.context, '1'),
-                                         mock.call.__nonzero__(),
+                                         call_bool,
                                          mock.call(self.context, '1')])
 
 
index c5125c031b578422927d9a470dca76cbf08f2939..1ed731afe42f1bdb5b18763564e41c4b16bea7fb 100644 (file)
@@ -13,6 +13,7 @@
 # under the License.
 
 import mock
+import six
 
 from cinder import exception
 from cinder import objects
@@ -162,8 +163,12 @@ class TestConsistencyGroup(test_objects.BaseObjectsTestCase):
         # new value for that field
         cg.refresh()
         self._compare(self, db_cg2, cg)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         consistencygroup_get.assert_has_calls([mock.call(self.context, '1'),
-                                               mock.call.__nonzero__(),
+                                               call_bool,
                                                mock.call(self.context, '1')])
 
 
index 923cffc2173486cc8a897eaea76ddc0d26bf30f9..e7b3eec321a4c37ecfa46b6f8bf50e13dbbedf67 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import mock
+import six
 
 from cinder import objects
 from cinder.tests.unit import fake_service
@@ -93,8 +94,12 @@ class TestService(test_objects.BaseObjectsTestCase):
         # new value for that field
         service.refresh()
         self._compare(self, db_service2, service)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         service_get.assert_has_calls([mock.call(self.context, 123),
-                                      mock.call.__nonzero__(),
+                                      call_bool,
                                       mock.call(self.context, 123)])
 
     @mock.patch('cinder.db.service_get_all_by_binary')
index 021e1b83f080993fb1e4562a77072feed2eec43d..fa74b8174db9bf6a2d2863406547543e456dd7b6 100644 (file)
@@ -14,6 +14,7 @@
 
 import copy
 import mock
+import six
 
 from oslo_log import log as logging
 
@@ -183,8 +184,12 @@ class TestSnapshot(test_objects.BaseObjectsTestCase):
         # value for that field
         snapshot.refresh()
         self._compare(self, db_snapshot2, snapshot)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         snapshot_get.assert_has_calls([mock.call(self.context, '1'),
-                                       mock.call.__nonzero__(),
+                                       call_bool,
                                        mock.call(self.context, '1')])
 
 
index d9f1dc2ec537815434bf87c0367e7cf703dcbd39..e2af749f52862afdd66a215e552cfeaa7db91589 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import mock
+import six
 
 from cinder import context
 from cinder import exception
@@ -291,8 +292,12 @@ class TestVolume(test_objects.BaseObjectsTestCase):
         # for that field
         volume.refresh()
         self._compare(self, db_volume2, volume)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         volume_get.assert_has_calls([mock.call(self.context, '1'),
-                                     mock.call.__nonzero__(),
+                                     call_bool,
                                      mock.call(self.context, '1')])
 
     def test_metadata_aliases(self):
index ed6325eeff181aff91bfad58705c81ab436f5db4..8b7fb78b4ad05f6007a5c0c0df5a480a8e3c0354 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import mock
+import six
 
 from cinder import objects
 from cinder.tests.unit import fake_volume
@@ -52,8 +53,12 @@ class TestVolumeAttachment(test_objects.BaseObjectsTestCase):
         # new value for that field
         attachment.refresh()
         self._compare(self, db_attachment2, attachment)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         attachment_get.assert_has_calls([mock.call(self.context, '1'),
-                                         mock.call.__nonzero__(),
+                                         call_bool,
                                          mock.call(self.context, '1')])
 
 
index be207557964bbbb0cbaea44209706330cd64aadd..e7484301d12115eb489136d0bbf274d7fa10e790 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import mock
+import six
 
 from cinder import objects
 from cinder.tests.unit import fake_volume
@@ -86,8 +87,12 @@ class TestVolumeType(test_objects.BaseObjectsTestCase):
         # value for that field
         volume_type.refresh()
         self._compare(self, db_type2, volume_type)
+        if six.PY3:
+            call_bool = mock.call.__bool__()
+        else:
+            call_bool = mock.call.__nonzero__()
         volume_type_get.assert_has_calls([mock.call(self.context, '1'),
-                                          mock.call.__nonzero__(),
+                                          call_bool,
                                           mock.call(self.context, '1')])
 
 
index 2e21480c19884be38cef395957daf97dc570baf5..b275f5734671284af19d4b634d966d38e158f57c 100644 (file)
@@ -36,6 +36,17 @@ cinder.tests.unit.keymgr.test_key
 cinder.tests.unit.keymgr.test_key_mgr
 cinder.tests.unit.keymgr.test_mock_key_mgr
 cinder.tests.unit.keymgr.test_not_implemented_key_mgr
+cinder.tests.unit.objects.test_backup
+cinder.tests.unit.objects.test_base
+cinder.tests.unit.objects.test_cgsnapshot
+cinder.tests.unit.objects.test_consistencygroup
+cinder.tests.unit.objects.test_fields
+cinder.tests.unit.objects.test_objects
+cinder.tests.unit.objects.test_service
+cinder.tests.unit.objects.test_snapshot
+cinder.tests.unit.objects.test_volume
+cinder.tests.unit.objects.test_volume_attachment
+cinder.tests.unit.objects.test_volume_type
 cinder.tests.unit.scheduler.test_allocated_capacity_weigher
 cinder.tests.unit.scheduler.test_capacity_weigher
 cinder.tests.unit.scheduler.test_chance_weigher