From: MichaƂ Dulko Date: Fri, 23 Oct 2015 13:55:49 +0000 (+0200) Subject: Add missing cgsnapshot field to Snapshot object X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=774c8a9dc4cfe559a1d2f3afd2380ea8f9cdd6ee;p=openstack-build%2Fcinder-build.git Add missing cgsnapshot field to Snapshot object 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 --- diff --git a/cinder/objects/snapshot.py b/cinder/objects/snapshot.py index c71311d94..b51b92060 100644 --- a/cinder/objects/snapshot.py +++ b/cinder/objects/snapshot.py @@ -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): diff --git a/cinder/tests/unit/objects/test_snapshot.py b/cinder/tests/unit/objects/test_snapshot.py index aa59c7caf..e0efce88b 100644 --- a/cinder/tests/unit/objects/test_snapshot.py +++ b/cinder/tests/unit/objects/test_snapshot.py @@ -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):