From 4ea25fa280261eb02b4689e419153e3fb8a28e4f Mon Sep 17 00:00:00 2001 From: Ivan Kolodyazhny Date: Sun, 21 Jun 2015 11:33:05 +0300 Subject: [PATCH] Fix Cinder Objects unit tests Due to the import order all tests passed on CI. But you can not run cinder.unit.objects.test_backup only. This patch fixes it adding base class and invokes objects.register_all(). Change-Id: If6168bce88489840b5d25b97be1f285737d943df --- cinder/tests/unit/objects/__init__.py | 10 ++++ cinder/tests/unit/objects/test_backup.py | 6 +-- cinder/tests/unit/objects/test_snapshot.py | 57 +++++++++++----------- cinder/tests/unit/objects/test_volume.py | 6 +-- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/cinder/tests/unit/objects/__init__.py b/cinder/tests/unit/objects/__init__.py index e69de29bb..b62125f9a 100644 --- a/cinder/tests/unit/objects/__init__.py +++ b/cinder/tests/unit/objects/__init__.py @@ -0,0 +1,10 @@ +from oslo_versionedobjects.tests import test_objects + +from cinder import objects + + +class BaseObjectsTestCase(test_objects._LocalTest): + def setUp(self): + super(BaseObjectsTestCase, self).setUp() + # Import cinder objects for test cases + objects.register_all() diff --git a/cinder/tests/unit/objects/test_backup.py b/cinder/tests/unit/objects/test_backup.py index 03de72a61..e6f08f6d4 100644 --- a/cinder/tests/unit/objects/test_backup.py +++ b/cinder/tests/unit/objects/test_backup.py @@ -13,10 +13,10 @@ # under the License. import mock -from oslo_versionedobjects.tests import test_objects from cinder import objects from cinder.tests.unit import fake_volume +from cinder.tests.unit import objects as test_objects fake_backup = { @@ -31,7 +31,7 @@ fake_backup = { } -class TestBackup(test_objects._LocalTest): +class TestBackup(test_objects.BaseObjectsTestCase): @staticmethod def _compare(test, db, obj): for field, value in db.items(): @@ -65,7 +65,7 @@ class TestBackup(test_objects._LocalTest): backup_destroy.assert_called_once_with(self.context, '1') -class TestBackupList(test_objects._LocalTest): +class TestBackupList(test_objects.BaseObjectsTestCase): @mock.patch('cinder.db.backup_get_all', return_value=[fake_backup]) def test_get_all(self, backup_get_all): backups = objects.BackupList.get_all(self.context) diff --git a/cinder/tests/unit/objects/test_snapshot.py b/cinder/tests/unit/objects/test_snapshot.py index 899da03ab..5b928f8db 100644 --- a/cinder/tests/unit/objects/test_snapshot.py +++ b/cinder/tests/unit/objects/test_snapshot.py @@ -13,11 +13,10 @@ # under the License. import mock -from oslo_versionedobjects.tests import test_objects -from cinder.objects import snapshot as snapshot_obj -from cinder.objects import volume as volume_obj +from cinder import objects from cinder.tests.unit import fake_volume +from cinder.tests.unit import objects as test_objects fake_snapshot = { 'id': '1', @@ -30,7 +29,7 @@ fake_snapshot = { } -class TestSnapshot(test_objects._LocalTest): +class TestSnapshot(test_objects.BaseObjectsTestCase): @staticmethod def _compare(test, db, obj): for field, value in db.items(): @@ -39,11 +38,11 @@ class TestSnapshot(test_objects._LocalTest): @mock.patch('cinder.db.snapshot_metadata_get', return_value={}) @mock.patch('cinder.db.snapshot_get', return_value=fake_snapshot) def test_get_by_id(self, snapshot_get, snapshot_metadata_get): - snapshot = snapshot_obj.Snapshot.get_by_id(self.context, 1) + snapshot = objects.Snapshot.get_by_id(self.context, 1) self._compare(self, fake_snapshot, snapshot) def test_reset_changes(self): - snapshot = snapshot_obj.Snapshot() + snapshot = objects.Snapshot() snapshot.metadata = {'key1': 'value1'} self.assertEqual({}, snapshot._orig_metadata) snapshot.obj_reset_changes(['metadata']) @@ -51,7 +50,7 @@ class TestSnapshot(test_objects._LocalTest): @mock.patch('cinder.db.snapshot_create', return_value=fake_snapshot) def test_create(self, snapshot_create): - snapshot = snapshot_obj.Snapshot(context=self.context) + snapshot = objects.Snapshot(context=self.context) snapshot.create() self.assertEqual(fake_snapshot['id'], snapshot.id) self.assertEqual(fake_snapshot['volume_id'], snapshot.volume_id) @@ -59,14 +58,14 @@ class TestSnapshot(test_objects._LocalTest): @mock.patch('cinder.db.snapshot_create', return_value=dict(provider_id='1111-aaaa', **fake_snapshot)) def test_create_with_provider_id(self, snapshot_create): - snapshot = snapshot_obj.Snapshot(context=self.context) + snapshot = objects.Snapshot(context=self.context) snapshot.create() self.assertEqual('1111-aaaa', snapshot.provider_id) @mock.patch('cinder.db.snapshot_update') def test_save(self, snapshot_update): - snapshot = snapshot_obj.Snapshot._from_db_object( - self.context, snapshot_obj.Snapshot(), fake_snapshot) + snapshot = objects.Snapshot._from_db_object( + self.context, objects.Snapshot(), fake_snapshot) snapshot.display_name = 'foobar' snapshot.save() snapshot_update.assert_called_once_with(self.context, snapshot.id, @@ -77,8 +76,8 @@ class TestSnapshot(test_objects._LocalTest): @mock.patch('cinder.db.snapshot_update') def test_save_with_metadata(self, snapshot_update, snapshot_metadata_update): - snapshot = snapshot_obj.Snapshot._from_db_object( - self.context, snapshot_obj.Snapshot(), fake_snapshot) + snapshot = objects.Snapshot._from_db_object( + self.context, objects.Snapshot(), fake_snapshot) snapshot.display_name = 'foobar' snapshot.metadata = {'key1': 'value1'} self.assertEqual({'display_name': 'foobar', @@ -93,13 +92,13 @@ class TestSnapshot(test_objects._LocalTest): @mock.patch('cinder.db.snapshot_destroy') def test_destroy(self, snapshot_destroy): - snapshot = snapshot_obj.Snapshot(context=self.context, id=1) + snapshot = objects.Snapshot(context=self.context, id=1) snapshot.destroy() snapshot_destroy.assert_called_once_with(self.context, '1') @mock.patch('cinder.db.snapshot_metadata_delete') def test_delete_metadata_key(self, snapshot_metadata_delete): - snapshot = snapshot_obj.Snapshot(self.context, id=1) + snapshot = objects.Snapshot(self.context, id=1) snapshot.metadata = {'key1': 'value1', 'key2': 'value2'} self.assertEqual({}, snapshot._orig_metadata) snapshot.delete_metadata_key(self.context, 'key2') @@ -108,18 +107,18 @@ class TestSnapshot(test_objects._LocalTest): 'key2') def test_obj_fields(self): - volume = volume_obj.Volume(context=self.context, id=2, _name_id=2) - snapshot = snapshot_obj.Snapshot(context=self.context, id=1, - volume=volume) + volume = objects.Volume(context=self.context, id=2, _name_id=2) + snapshot = objects.Snapshot(context=self.context, id=1, + volume=volume) self.assertEqual(['name', 'volume_name'], snapshot.obj_extra_fields) self.assertEqual('snapshot-1', snapshot.name) self.assertEqual('volume-2', snapshot.volume_name) @mock.patch('cinder.objects.volume.Volume.get_by_id') def test_obj_load_attr(self, volume_get_by_id): - snapshot = snapshot_obj.Snapshot._from_db_object( - self.context, snapshot_obj.Snapshot(), fake_snapshot) - volume = volume_obj.Volume(context=self.context, id=2) + snapshot = objects.Snapshot._from_db_object( + self.context, objects.Snapshot(), fake_snapshot) + volume = objects.Volume(context=self.context, id=2) volume_get_by_id.return_value = volume self.assertEqual(volume, snapshot.volume) volume_get_by_id.assert_called_once_with(self.context, @@ -127,8 +126,8 @@ class TestSnapshot(test_objects._LocalTest): @mock.patch('cinder.db.snapshot_data_get_for_project') def test_snapshot_data_get_for_project(self, snapshot_data_get): - snapshot = snapshot_obj.Snapshot._from_db_object( - self.context, snapshot_obj.Snapshot(), fake_snapshot) + snapshot = objects.Snapshot._from_db_object( + self.context, objects.Snapshot(), fake_snapshot) volume_type_id = mock.sentinel.volume_type_id snapshot.snapshot_data_get_for_project(self.context, self.project_id, @@ -138,7 +137,7 @@ class TestSnapshot(test_objects._LocalTest): volume_type_id) -class TestSnapshotList(test_objects._LocalTest): +class TestSnapshotList(test_objects.BaseObjectsTestCase): @mock.patch('cinder.db.snapshot_metadata_get', return_value={}) @mock.patch('cinder.objects.volume.Volume.get_by_id') @mock.patch('cinder.db.snapshot_get_all', return_value=[fake_snapshot]) @@ -147,7 +146,7 @@ class TestSnapshotList(test_objects._LocalTest): fake_volume_obj = fake_volume.fake_volume_obj(self.context) volume_get_by_id.return_value = fake_volume_obj - snapshots = snapshot_obj.SnapshotList.get_all(self.context) + snapshots = objects.SnapshotList.get_all(self.context) self.assertEqual(1, len(snapshots)) TestSnapshot._compare(self, fake_snapshot, snapshots[0]) @@ -160,7 +159,7 @@ class TestSnapshotList(test_objects._LocalTest): fake_volume_obj = fake_volume.fake_volume_obj(self.context) volume_get_by_id.return_value = fake_volume_obj - snapshots = snapshot_obj.SnapshotList.get_by_host( + snapshots = objects.SnapshotList.get_by_host( self.context, 'fake-host') self.assertEqual(1, len(snapshots)) TestSnapshot._compare(self, fake_snapshot, snapshots[0]) @@ -174,7 +173,7 @@ class TestSnapshotList(test_objects._LocalTest): fake_volume_obj = fake_volume.fake_volume_obj(self.context) volume_get_by_id.return_value = fake_volume_obj - snapshots = snapshot_obj.SnapshotList.get_all_by_project( + snapshots = objects.SnapshotList.get_all_by_project( self.context, self.project_id) self.assertEqual(1, len(snapshots)) TestSnapshot._compare(self, fake_snapshot, snapshots[0]) @@ -188,7 +187,7 @@ class TestSnapshotList(test_objects._LocalTest): fake_volume_obj = fake_volume.fake_volume_obj(self.context) volume_get_by_id.return_value = fake_volume_obj - snapshots = snapshot_obj.SnapshotList.get_all_for_volume( + snapshots = objects.SnapshotList.get_all_for_volume( self.context, fake_volume_obj.id) self.assertEqual(1, len(snapshots)) TestSnapshot._compare(self, fake_snapshot, snapshots[0]) @@ -202,7 +201,7 @@ class TestSnapshotList(test_objects._LocalTest): fake_volume_obj = fake_volume.fake_volume_obj(self.context) volume_get_by_id.return_value = fake_volume_obj - snapshots = snapshot_obj.SnapshotList.get_active_by_window( + snapshots = objects.SnapshotList.get_active_by_window( self.context, mock.sentinel.begin, mock.sentinel.end) self.assertEqual(1, len(snapshots)) TestSnapshot._compare(self, fake_snapshot, snapshots[0]) @@ -216,7 +215,7 @@ class TestSnapshotList(test_objects._LocalTest): fake_volume_obj = fake_volume.fake_volume_obj(self.context) volume_get_by_id.return_value = fake_volume_obj - snapshots = snapshot_obj.SnapshotList.get_all_for_cgsnapshot( + snapshots = objects.SnapshotList.get_all_for_cgsnapshot( self.context, mock.sentinel.cgsnapshot_id) self.assertEqual(1, len(snapshots)) TestSnapshot._compare(self, fake_snapshot, snapshots[0]) diff --git a/cinder/tests/unit/objects/test_volume.py b/cinder/tests/unit/objects/test_volume.py index 6d8f2dc50..a2f871293 100644 --- a/cinder/tests/unit/objects/test_volume.py +++ b/cinder/tests/unit/objects/test_volume.py @@ -13,13 +13,13 @@ # under the License. import mock -from oslo_versionedobjects.tests import test_objects from cinder import objects from cinder.tests.unit import fake_volume +from cinder.tests.unit import objects as test_objects -class TestVolume(test_objects._LocalTest): +class TestVolume(test_objects.BaseObjectsTestCase): @staticmethod def _compare(test, db, obj): for field, value in db.items(): @@ -65,7 +65,7 @@ class TestVolume(test_objects._LocalTest): self.assertEqual('2', volume.name_id) -class TestVolumeList(test_objects._LocalTest): +class TestVolumeList(test_objects.BaseObjectsTestCase): @mock.patch('cinder.db.volume_get_all') def test_get_all(self, volume_get_all): db_volume = fake_volume.fake_db_volume() -- 2.45.2