def test_save(self, cgsnapshot_update): cgsnapshot = objects.CGSnapshot._from_db_object( self.context, objects.CGSnapshot(), fake_cgsnapshot) cgsnapshot.status = 'active' cgsnapshot.save() cgsnapshot_update.assert_called_once_with(self.context, cgsnapshot.id, {'status': 'active'})
def _from_db_object(context, snapshot, db_snapshot, expected_attrs=None): if expected_attrs is None: expected_attrs = [] for name, field in snapshot.fields.items(): if name in Snapshot.OPTIONAL_FIELDS: continue value = db_snapshot.get(name) if isinstance(field, fields.IntegerField): value = value if value is not None else 0 setattr(snapshot, name, value) if 'volume' in expected_attrs: 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: raise exception.MetadataAbsent() snapshot.metadata = { item['key']: item['value'] for item in metadata } snapshot._context = context snapshot.obj_reset_changes() return snapshot
def test_obj_load_attr(self, mock_vol_get_all_by_group, mock_cgsnap_get_all_by_group): consistencygroup = objects.ConsistencyGroup._from_db_object( self.context, objects.ConsistencyGroup(), fake_consistencygroup) # Test cgsnapshots lazy-loaded field cgsnapshots_objs = [ objects.CGSnapshot(context=self.context, id=i) for i in [fake.cgsnapshot_id, fake.cgsnapshot2_id, fake.cgsnapshot3_id] ] cgsnapshots = objects.CGSnapshotList(context=self.context, objects=cgsnapshots_objs) mock_cgsnap_get_all_by_group.return_value = cgsnapshots self.assertEqual(cgsnapshots, consistencygroup.cgsnapshots) mock_cgsnap_get_all_by_group.assert_called_once_with( self.context, consistencygroup.id) # Test volumes lazy-loaded field volume_objs = [ objects.Volume(context=self.context, id=i) for i in [fake.volume_id, fake.volume2_id, fake.volume3_id] ] volumes = objects.VolumeList(context=self.context, objects=volume_objs) mock_vol_get_all_by_group.return_value = volumes self.assertEqual(volumes, consistencygroup.volumes) mock_vol_get_all_by_group.assert_called_once_with( self.context, consistencygroup.id)
def test_save_with_consistencygroup(self, cgsnapshot_update, cgsnapshot_cg_update): consistencygroup = objects.ConsistencyGroup._from_db_object( self.context, objects.ConsistencyGroup(), fake_consistencygroup) cgsnapshot = objects.CGSnapshot._from_db_object( self.context, objects.CGSnapshot(), fake_cgsnapshot) cgsnapshot.name = 'foobar' cgsnapshot.consistencygroup = consistencygroup self.assertEqual({'name': 'foobar', 'consistencygroup': consistencygroup}, cgsnapshot.obj_get_changes()) self.assertRaises(exception.ObjectActionError, cgsnapshot.save)
def test_save_with_cgsnapshots(self): consistencygroup = objects.ConsistencyGroup._from_db_object( self.context, objects.ConsistencyGroup(), fake_consistencygroup) cgsnapshots_objs = [objects.CGSnapshot(context=self.context, id=i) for i in [3, 4, 5]] cgsnapshots = objects.CGSnapshotList(objects=cgsnapshots_objs) consistencygroup.name = 'foobar' consistencygroup.cgsnapshots = cgsnapshots self.assertEqual({'name': 'foobar', 'cgsnapshots': cgsnapshots}, consistencygroup.obj_get_changes()) self.assertRaises(exception.ObjectActionError, consistencygroup.save)
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)
def test_destroy(self, cgsnapshot_destroy, utcnow_mock): cgsnapshot_destroy.return_value = { 'status': 'deleted', 'deleted': True, 'deleted_at': utcnow_mock.return_value } cgsnapshot = objects.CGSnapshot(context=self.context, id=fake.CGSNAPSHOT_ID) cgsnapshot.destroy() self.assertTrue(cgsnapshot_destroy.called) admin_context = cgsnapshot_destroy.call_args[0][0] self.assertTrue(admin_context.is_admin) self.assertTrue(cgsnapshot.deleted) self.assertEqual('deleted', cgsnapshot.status) self.assertEqual(utcnow_mock.return_value.replace(tzinfo=pytz.UTC), cgsnapshot.deleted_at)
def create_cgsnapshot(ctxt, consistencygroup_id, name='test_cgsnapshot', description='this is a test cgsnapshot', status='creating', **kwargs): """Create a cgsnapshot object in the DB.""" cgsnap = objects.CGSnapshot(ctxt) cgsnap.user_id = ctxt.user_id or 'fake_user_id' cgsnap.project_id = ctxt.project_id or 'fake_project_id' cgsnap.status = status cgsnap.name = name cgsnap.description = description cgsnap.consistencygroup_id = consistencygroup_id for key in kwargs: setattr(cgsnap, key, kwargs[key]) cgsnap.create() return cgsnap
def test_obj_load_attr(self, snapshotlist_get_for_cgs, consistencygroup_get_by_id): cgsnapshot = objects.CGSnapshot._from_db_object( self.context, objects.CGSnapshot(), fake_cgsnapshot) # Test consistencygroup lazy-loaded field consistencygroup = objects.ConsistencyGroup(context=self.context, id=2) consistencygroup_get_by_id.return_value = consistencygroup self.assertEqual(consistencygroup, cgsnapshot.consistencygroup) consistencygroup_get_by_id.assert_called_once_with( self.context, cgsnapshot.consistencygroup_id) # Test snapshots lazy-loaded field snapshots_objs = [objects.Snapshot(context=self.context, id=i) for i in [3, 4, 5]] snapshots = objects.SnapshotList(context=self.context, objects=snapshots_objs) snapshotlist_get_for_cgs.return_value = snapshots self.assertEqual(snapshots, cgsnapshot.snapshots) snapshotlist_get_for_cgs.assert_called_once_with( self.context, cgsnapshot.id)
def _create_cgsnapshot(self, context, group, name, description): volumes = self.db.volume_get_all_by_group(context.elevated(), group.id) if not volumes: msg = _("Consistency group is empty. No cgsnapshot " "will be created.") raise exception.InvalidConsistencyGroup(reason=msg) options = { 'consistencygroup_id': group.id, 'user_id': context.user_id, 'project_id': context.project_id, 'status': "creating", 'name': name, 'description': description } cgsnapshot = None cgsnapshot_id = None try: cgsnapshot = objects.CGSnapshot(context, **options) cgsnapshot.create() cgsnapshot_id = cgsnapshot.id snap_name = cgsnapshot.name snap_desc = cgsnapshot.description self.volume_api.create_snapshots_in_db(context, volumes, snap_name, snap_desc, True, cgsnapshot_id) except Exception: with excutils.save_and_reraise_exception(): try: if cgsnapshot: cgsnapshot.destroy() finally: LOG.error( _LE("Error occurred when creating cgsnapshot" " %s."), cgsnapshot_id) self.volume_rpcapi.create_cgsnapshot(context, cgsnapshot) return cgsnapshot
def _create_cgsnapshot(self, group_id, volume_id, size='0'): """Create a cgsnapshot object.""" cgsnap = objects.CGSnapshot(self.context) cgsnap.user_id = fake.USER_ID cgsnap.project_id = fake.PROJECT_ID cgsnap.consistencygroup_id = group_id cgsnap.status = "creating" cgsnap.create() # Create a snapshot object snap = objects.Snapshot(context.get_admin_context()) snap.volume_size = size snap.user_id = fake.USER_ID snap.project_id = fake.PROJECT_ID snap.volume_id = volume_id snap.status = "available" snap.cgsnapshot_id = cgsnap.id snap.create() return cgsnap, snap
def create_cgsnapshot(self, context, group, name, description): group.assert_not_frozen() options = { 'consistencygroup_id': group.id, 'user_id': context.user_id, 'project_id': context.project_id, 'status': "creating", 'name': name, 'description': description } cgsnapshot = None cgsnapshot_id = None try: cgsnapshot = objects.CGSnapshot(context, **options) cgsnapshot.create() cgsnapshot_id = cgsnapshot.id snap_name = cgsnapshot.name snap_desc = cgsnapshot.description with group.obj_as_admin(): self.volume_api.create_snapshots_in_db(context, group.volumes, snap_name, snap_desc, cgsnapshot_id) except Exception: with excutils.save_and_reraise_exception(): try: # If the cgsnapshot has been created if cgsnapshot.obj_attr_is_set('id'): cgsnapshot.destroy() finally: LOG.error( _LE("Error occurred when creating cgsnapshot" " %s."), cgsnapshot_id) self.volume_rpcapi.create_cgsnapshot(context, cgsnapshot) return cgsnapshot
def _create_cgsnapshot(self, group_id, volume_ids, size='0'): """Create a cgsnapshot object.""" cgsnap = objects.CGSnapshot(self.context) cgsnap.user_id = fake.USER_ID cgsnap.project_id = fake.PROJECT_ID cgsnap.consistencygroup_id = group_id cgsnap.status = "creating" cgsnap.create() # Create snapshot list for volume_id in volume_ids: snaps = [] snap = objects.Snapshot(context.get_admin_context()) snap.volume_size = size snap.user_id = fake.USER_ID snap.project_id = fake.PROJECT_ID snap.volume_id = volume_id snap.status = fields.SnapshotStatus.AVAILABLE snap.cgsnapshot_id = cgsnap.id snap.create() snaps.append(snap) return cgsnap, snaps
def fake_cgsnapshot_obj(context, **updates): expected_attrs = updates.pop('expected_attrs', None) return objects.CGSnapshot._from_db_object(context, objects.CGSnapshot(), fake_db_cgsnapshot(**updates), expected_attrs=expected_attrs)
def test_create_with_id_except_exception(self): cgsnapshot = objects.CGSnapshot(context=self.context, **{'id': fake.CONSISTENCY_GROUP_ID}) self.assertRaises(exception.ObjectActionError, cgsnapshot.create)
def test_create(self, cgsnapshot_create): fake_cgsnap = fake_cgsnapshot.copy() del fake_cgsnap['id'] cgsnapshot = objects.CGSnapshot(context=self.context, **fake_cgsnap) cgsnapshot.create() self._compare(self, fake_cgsnapshot, cgsnapshot)
def test_destroy(self, cgsnapshot_destroy): cgsnapshot = objects.CGSnapshot(context=self.context, id=1) cgsnapshot.destroy() self.assertTrue(cgsnapshot_destroy.called) admin_context = cgsnapshot_destroy.call_args[0][0] self.assertTrue(admin_context.is_admin)
def test_create_with_id_except_exception(self): cgsnapshot = objects.CGSnapshot(context=self.context, **{'id': fake.consistency_group_id}) self.assertRaises(exception.ObjectActionError, cgsnapshot.create)