]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add missing cgsnapshot field to Snapshot object
authorMichał Dulko <michal.dulko@intel.com>
Fri, 23 Oct 2015 13:55:49 +0000 (15:55 +0200)
committerMichał Dulko <michal.dulko@intel.com>
Fri, 23 Oct 2015 14:48:20 +0000 (16:48 +0200)
SQLAlchemy model of snapshot specifies its relationship to cgsnapshot.
This wasn't reflected in Snapshot versioned object. The patch adds
cgsnapshot field to Snapshot object as well as required unit tests.

Partial-Bug: 1509012
Change-Id: I285930222c491f83a35fdef78067e9548e4b845a

cinder/objects/snapshot.py
cinder/tests/unit/objects/test_snapshot.py

index c71311d949831871b59c83db7a3ac02e170ce512..b51b9206049461dc982f30e0ca528cad915c7db8 100644 (file)
@@ -26,7 +26,7 @@ from cinder.objects import base
 CONF = cfg.CONF
 # NOTE(thangp): OPTIONAL_FIELDS are fields that would be lazy-loaded. They are
 # typically the relationship in the sqlalchemy object.
-OPTIONAL_FIELDS = ['volume', 'metadata']
+OPTIONAL_FIELDS = ['volume', 'metadata', 'cgsnapshot']
 LOG = logging.getLogger(__name__)
 
 
@@ -60,6 +60,7 @@ class Snapshot(base.CinderPersistentObject, base.CinderObject,
         'provider_auth': fields.StringField(nullable=True),
 
         'volume': fields.ObjectField('Volume', nullable=True),
+        'cgsnapshot': fields.ObjectField('CGSnapshot', nullable=True),
     }
 
     # NOTE(thangp): obj_extra_fields is used to hold properties that are not
@@ -117,6 +118,11 @@ class Snapshot(base.CinderPersistentObject, base.CinderObject,
             volume = objects.Volume(context)
             volume._from_db_object(context, volume, db_snapshot['volume'])
             snapshot.volume = volume
+        if 'cgsnapshot' in expected_attrs:
+            cgsnapshot = objects.CGSnapshot(context)
+            cgsnapshot._from_db_object(context, cgsnapshot,
+                                       db_snapshot['cgsnapshot'])
+            snapshot.cgsnapshot = cgsnapshot
         if 'metadata' in expected_attrs:
             metadata = db_snapshot.get('snapshot_metadata')
             if metadata is None:
@@ -143,6 +149,9 @@ class Snapshot(base.CinderPersistentObject, base.CinderObject,
         if 'volume' in updates:
             raise exception.ObjectActionError(action='create',
                                               reason=_('volume assigned'))
+        if 'cgsnapshot' in updates:
+            raise exception.ObjectActionError(action='create',
+                                              reason=_('cgsnapshot assigned'))
 
         db_snapshot = db.snapshot_create(self._context, updates)
         self._from_db_object(self._context, self, db_snapshot)
@@ -154,6 +163,9 @@ class Snapshot(base.CinderPersistentObject, base.CinderObject,
             if 'volume' in updates:
                 raise exception.ObjectActionError(action='save',
                                                   reason=_('volume changed'))
+            if 'cgsnapshot' in updates:
+                raise exception.ObjectActionError(
+                    action='save', reason=_('cgsnapshot changed'))
 
             if 'metadata' in updates:
                 # Metadata items that are not specified in the
@@ -184,6 +196,10 @@ class Snapshot(base.CinderPersistentObject, base.CinderObject,
             self.volume = objects.Volume.get_by_id(self._context,
                                                    self.volume_id)
 
+        if attrname == 'cgsnapshot':
+            self.cgsnapshot = objects.CGSnapshot.get_by_id(self._context,
+                                                           self.cgsnapshot_id)
+
         self.obj_reset_changes(fields=[attrname])
 
     def delete_metadata_key(self, context, key):
index aa59c7caf3bcd158a4a088b7f76c27b8540ae913..e0efce88b26552e8d497e8120d8f3def117691d1 100644 (file)
@@ -27,7 +27,8 @@ from cinder.tests.unit import objects as test_objects
 LOG = logging.getLogger(__name__)
 
 
-fake_db_snapshot = fake_snapshot.fake_db_snapshot()
+fake_db_snapshot = fake_snapshot.fake_db_snapshot(
+    cgsnapshot_id='fake_cgsnap_id')
 del fake_db_snapshot['metadata']
 del fake_db_snapshot['volume']
 
@@ -133,14 +134,22 @@ class TestSnapshot(test_objects.BaseObjectsTestCase):
         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):
+    @mock.patch('cinder.objects.cgsnapshot.CGSnapshot.get_by_id')
+    def test_obj_load_attr(self, cgsnapshot_get_by_id, volume_get_by_id):
         snapshot = objects.Snapshot._from_db_object(
             self.context, objects.Snapshot(), fake_db_snapshot)
+        # Test volume lazy-loaded field
         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,
                                                  snapshot.volume_id)
+        # Test cgsnapshot lazy-loaded field
+        cgsnapshot = objects.CGSnapshot(context=self.context, id=2)
+        cgsnapshot_get_by_id.return_value = cgsnapshot
+        self.assertEqual(cgsnapshot, snapshot.cgsnapshot)
+        cgsnapshot_get_by_id.assert_called_once_with(self.context,
+                                                     snapshot.cgsnapshot_id)
 
     @mock.patch('cinder.db.snapshot_data_get_for_project')
     def test_snapshot_data_get_for_project(self, snapshot_data_get):