def test_create(self, group_snapshot_create):
     fake_group_snap = fake_group_snapshot.copy()
     del fake_group_snap['id']
     group_snapshot = objects.GroupSnapshot(context=self.context,
                                            **fake_group_snap)
     group_snapshot.create()
     self._compare(self, fake_group_snapshot, group_snapshot)
Beispiel #2
0
    def create_group_snapshot(self, context, group, name, description):
        options = {'group_id': group.id,
                   'user_id': context.user_id,
                   'project_id': context.project_id,
                   'status': "creating",
                   'name': name,
                   'description': description}

        group_snapshot = None
        group_snapshot_id = None
        try:
            group_snapshot = objects.GroupSnapshot(context, **options)
            group_snapshot.create()
            group_snapshot_id = group_snapshot.id

            snap_name = group_snapshot.name
            snap_desc = group_snapshot.description
            with group.obj_as_admin():
                self.volume_api.create_snapshots_in_db(
                    context, group.volumes, snap_name, snap_desc,
                    None, group_snapshot_id)

        except Exception:
            with excutils.save_and_reraise_exception():
                try:
                    # If the group_snapshot has been created
                    if group_snapshot.obj_attr_is_set('id'):
                        group_snapshot.destroy()
                finally:
                    LOG.error(_LE("Error occurred when creating group_snapshot"
                                  " %s."), group_snapshot_id)

        self.volume_rpcapi.create_group_snapshot(context, group_snapshot)

        return group_snapshot
 def test_save(self, group_snapshot_update):
     group_snapshot = objects.GroupSnapshot._from_db_object(
         self.context, objects.GroupSnapshot(), fake_group_snapshot)
     group_snapshot.status = 'active'
     group_snapshot.save()
     group_snapshot_update.assert_called_once_with(self.context,
                                                   group_snapshot.id,
                                                   {'status': 'active'})
Beispiel #4
0
 def test_save_with_group(self, group_snapshot_update,
                          group_snapshot_cg_update):
     group = objects.Group._from_db_object(
         self.context, objects.Group(), fake_group)
     group_snapshot = objects.GroupSnapshot._from_db_object(
         self.context, objects.GroupSnapshot(), fake_group_snapshot)
     group_snapshot.name = 'foobar'
     group_snapshot.group = group
     self.assertEqual({'name': 'foobar',
                       'group': group},
                      group_snapshot.obj_get_changes())
     self.assertRaises(exception.ObjectActionError, group_snapshot.save)
 def test_destroy(self, group_snapshot_destroy, utcnow_mock):
     group_snapshot_destroy.return_value = {
         'status': 'deleted',
         'deleted': True,
         'deleted_at': utcnow_mock.return_value
     }
     group_snapshot = objects.GroupSnapshot(context=self.context,
                                            id=fake.GROUP_SNAPSHOT_ID)
     group_snapshot.destroy()
     self.assertTrue(group_snapshot_destroy.called)
     admin_context = group_snapshot_destroy.call_args[0][0]
     self.assertTrue(admin_context.is_admin)
     self.assertTrue(group_snapshot.deleted)
     self.assertEqual('deleted', group_snapshot.status)
     self.assertEqual(utcnow_mock.return_value.replace(tzinfo=pytz.UTC),
                      group_snapshot.deleted_at)
Beispiel #6
0
def create_group_snapshot(ctxt,
                          group_id,
                          group_type_id=None,
                          name='test_group_snapshot',
                          description='this is a test group snapshot',
                          status='creating',
                          recursive_create_if_needed=True,
                          return_vo=True,
                          **kwargs):
    """Create a group snapshot object in the DB."""
    values = {
        'user_id': ctxt.user_id or fake.USER_ID,
        'project_id': ctxt.project_id or fake.PROJECT_ID,
        'status': status,
        'name': name,
        'description': description,
        'group_id': group_id,
        'group_type_id': group_type_id}
    values.update(kwargs)

    if recursive_create_if_needed and group_id:
        create_grp = False
        try:
            objects.Group.get_by_id(ctxt,
                                    group_id)
            create_vol = not db.volume_get_all_by_generic_group(
                ctxt, group_id)
        except exception.GroupNotFound:
            create_grp = True
            create_vol = True
        if create_grp:
            create_group(ctxt, id=group_id, group_type_id=group_type_id)
        if create_vol:
            create_volume(ctxt, group_id=group_id)

    if not return_vo:
        return db.group_snapshot_create(ctxt, values)
    else:
        group_snapshot = objects.GroupSnapshot(ctxt)
        new_id = values.pop('id', None)
        group_snapshot.update(values)
        group_snapshot.create()
        if new_id and new_id != group_snapshot.id:
            db.group_snapshot_update(ctxt, group_snapshot.id, {'id': new_id})
            group_snapshot = objects.GroupSnapshot.get_by_id(ctxt, new_id)
        return group_snapshot
 def test_obj_load_attr(self, snapshotlist_get_for_cgs, group_get_by_id):
     group_snapshot = objects.GroupSnapshot._from_db_object(
         self.context, objects.GroupSnapshot(), fake_group_snapshot)
     # Test group lazy-loaded field
     group = objects.Group(context=self.context, id=fake.GROUP_ID)
     group_get_by_id.return_value = group
     self.assertEqual(group, group_snapshot.group)
     group_get_by_id.assert_called_once_with(self.context,
                                             group_snapshot.group_id)
     # Test snapshots lazy-loaded field
     snapshots_objs = [
         objects.Snapshot(context=self.context, id=i)
         for i in [fake.SNAPSHOT_ID, fake.SNAPSHOT2_ID, fake.SNAPSHOT3_ID]
     ]
     snapshots = objects.SnapshotList(context=self.context,
                                      objects=snapshots_objs)
     snapshotlist_get_for_cgs.return_value = snapshots
     self.assertEqual(snapshots, group_snapshot.snapshots)
     snapshotlist_get_for_cgs.assert_called_once_with(
         self.context, group_snapshot.id)
Beispiel #8
0
    def _from_db_object(cls,
                        context,
                        snapshot,
                        db_snapshot,
                        expected_attrs=None):
        if expected_attrs is None:
            expected_attrs = []
        for name, field in snapshot.fields.items():
            if name in cls.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 snapshot.cgsnapshot_id and 'cgsnapshot' in expected_attrs:
            cgsnapshot = objects.CGSnapshot(context)
            cgsnapshot._from_db_object(context, cgsnapshot,
                                       db_snapshot['cgsnapshot'])
            snapshot.cgsnapshot = cgsnapshot
        if snapshot.group_snapshot_id and 'group_snapshot' in expected_attrs:
            group_snapshot = objects.GroupSnapshot(context)
            group_snapshot._from_db_object(context, group_snapshot,
                                           db_snapshot['group_snapshot'])
            snapshot.group_snapshot = group_snapshot

        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
Beispiel #9
0
    def _create_group_snapshot(self, group_id, volume_ids, size='0'):
        """Create a group_snapshot object."""
        grpsnap = objects.GroupSnapshot(self.context)
        grpsnap.user_id = fake.USER_ID
        grpsnap.project_id = fake.PROJECT_ID
        grpsnap.group_id = group_id
        grpsnap.status = fields.GroupStatus.CREATING
        grpsnap.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.group_snapshot_id = grpsnap.id
            snap.create()
            snaps.append(snap)

        return grpsnap, snaps
Beispiel #10
0
 def test_create_with_id_except_exception(self):
     group_snapshot = objects.GroupSnapshot(context=self.context,
                                            **{'id': fake.GROUP_ID})
     self.assertRaises(exception.ObjectActionError, group_snapshot.create)
Beispiel #11
0
def fake_group_snapshot_obj(context, **updates):
    return objects.GroupSnapshot._from_db_object(
        context, objects.GroupSnapshot(), fake_db_group_snapshot(**updates))