def _from_db_object(context, consistencygroup, db_consistencygroup, expected_attrs=None): if expected_attrs is None: expected_attrs = [] for name, field in consistencygroup.fields.items(): if name in OPTIONAL_FIELDS: continue value = db_consistencygroup.get(name) setattr(consistencygroup, name, value) if 'cgsnapshots' in expected_attrs: cgsnapshots = base.obj_make_list( context, objects.CGSnapshotsList(context), objects.CGSnapshot, db_consistencygroup['cgsnapshots']) consistencygroup.cgsnapshots = cgsnapshots if 'volumes' in expected_attrs: volumes = base.obj_make_list( context, objects.VolumeList(context), objects.Volume, db_consistencygroup['volumes']) consistencygroup.cgsnapshots = volumes consistencygroup._context = context consistencygroup.obj_reset_changes() return consistencygroup
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_show_group(self, mock_vol_get_all_by_group, mock_vol_type_get_all_by_group): volume_objs = [ objects.Volume(context=self.ctxt, id=i) for i in [fake.VOLUME_ID] ] volumes = objects.VolumeList(context=self.ctxt, objects=volume_objs) mock_vol_get_all_by_group.return_value = volumes vol_type_objs = [ objects.VolumeType(context=self.ctxt, id=i) for i in [fake.VOLUME_TYPE_ID] ] vol_types = objects.VolumeTypeList(context=self.ctxt, objects=vol_type_objs) mock_vol_type_get_all_by_group.return_value = vol_types req = fakes.HTTPRequest.blank('/v3/%s/groups/%s' % (fake.PROJECT_ID, self.group1.id), version=GROUP_MICRO_VERSION) res_dict = self.controller.show(req, self.group1.id) self.assertEqual(1, len(res_dict)) self.assertEqual('az1', res_dict['group']['availability_zone']) self.assertEqual('this is a test group', res_dict['group']['description']) self.assertEqual('test_group', res_dict['group']['name']) self.assertEqual('creating', res_dict['group']['status']) self.assertEqual([fake.VOLUME_TYPE_ID], res_dict['group']['volume_types'])
def _from_db_object(context, group, db_group, expected_attrs=None): if expected_attrs is None: expected_attrs = [] for name, field in group.fields.items(): if name in Group.OPTIONAL_FIELDS: continue value = db_group.get(name) setattr(group, name, value) if 'volumes' in expected_attrs: volumes = base.obj_make_list(context, objects.VolumeList(context), objects.Volume, db_group['volumes']) group.volumes = volumes if 'volume_types' in expected_attrs: volume_types = base.obj_make_list(context, objects.VolumeTypeList(context), objects.VolumeType, db_group['volume_types']) group.volume_types = volume_types if 'group_snapshots' in expected_attrs: group_snapshots = base.obj_make_list( context, objects.GroupSnapshotList(context), objects.GroupSnapshot, db_group['group_snapshots']) group.group_snapshots = group_snapshots group._context = context group.obj_reset_changes() return group
def fake_volume_api_get_all_by_project(self, context, marker, limit, sort_keys=None, sort_dirs=None, filters=None, viewable_admin_meta=False, offset=None): vol = fake_volume_get(self, context, fake.VOLUME_ID, viewable_admin_meta=viewable_admin_meta) vol_obj = fake_volume.fake_volume_obj(context, **vol) return objects.VolumeList(objects=[vol_obj])
def test_save_with_volumes(self): consistencygroup = objects.ConsistencyGroup._from_db_object( self.context, objects.ConsistencyGroup(), fake_consistencygroup) volumes_objs = [objects.Volume(context=self.context, id=i) for i in [3, 4, 5]] volumes = objects.VolumeList(objects=volumes_objs) consistencygroup.name = 'foobar' consistencygroup.volumes = volumes self.assertEqual({'name': 'foobar', 'volumes': volumes}, consistencygroup.obj_get_changes()) self.assertRaises(exception.ObjectActionError, consistencygroup.save)
def test_save_with_volumes(self): group = objects.Group._from_db_object( self.context, objects.Group(), fake_group) volumes_objs = [objects.Volume(context=self.context, id=i) for i in [fake.VOLUME_ID, fake.VOLUME2_ID, fake.VOLUME3_ID]] volumes = objects.VolumeList(objects=volumes_objs) group.name = 'foobar' group.volumes = volumes self.assertEqual({'name': 'foobar', 'volumes': volumes}, group.obj_get_changes()) self.assertRaises(exception.ObjectActionError, group.save)
def test_obj_load_attr(self, mock_vol_get_all_by_group, mock_vol_type_get_all_by_group): group = objects.Group._from_db_object( self.context, objects.Group(), fake_group) # 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, group.volumes) mock_vol_get_all_by_group.assert_called_once_with(self.context, group.id)
def test_create_from_group(self, group_create): fake_grp = fake_group.copy() del fake_grp['id'] group = objects.Group(context=self.context, **fake_grp) group.create() volumes_objs = [ objects.Volume(context=self.context, id=i) for i in [fake.VOLUME_ID, fake.VOLUME2_ID, fake.VOLUME3_ID] ] volumes = objects.VolumeList(objects=volumes_objs) group.volumes = volumes consistencygroup = objects.ConsistencyGroup() consistencygroup.from_group(group) self.assertEqual(group.id, consistencygroup.id) self.assertEqual(group.name, consistencygroup.name)
def test_show_group_with_list_volume(self, mock_vol_get_all_by_group, mock_vol_type_get_all_by_group): volume_objs = [ objects.Volume(context=self.ctxt, id=i) for i in [fake.VOLUME_ID] ] volumes = objects.VolumeList(context=self.ctxt, objects=volume_objs) mock_vol_get_all_by_group.return_value = volumes vol_type_objs = [ objects.VolumeType(context=self.ctxt, id=i) for i in [fake.VOLUME_TYPE_ID] ] vol_types = objects.VolumeTypeList(context=self.ctxt, objects=vol_type_objs) mock_vol_type_get_all_by_group.return_value = vol_types # If the microversion >= 3.25 and "list_volume=True", "volumes" should # be contained in the response body. req = fakes.HTTPRequest.blank('/v3/%s/groups/%s?list_volume=True' % (fake.PROJECT_ID, self.group1.id), version='3.25') res_dict = self.controller.show(req, self.group1.id) self.assertEqual(1, len(res_dict)) self.assertEqual([fake.VOLUME_ID], res_dict['group']['volumes']) # If the microversion >= 3.25 but "list_volume" is missing, "volumes" # should not be contained in the response body. req = fakes.HTTPRequest.blank('/v3/%s/groups/%s' % (fake.PROJECT_ID, self.group1.id), version='3.25') res_dict = self.controller.show(req, self.group1.id) self.assertEqual(1, len(res_dict)) self.assertIsNone(res_dict['group'].get('volumes', None)) # If the microversion < 3.25, "volumes" should not be contained in the # response body. req = fakes.HTTPRequest.blank('/v3/%s/groups/%s?list_volume=True' % (fake.PROJECT_ID, self.group1.id), version='3.24') res_dict = self.controller.show(req, self.group1.id) self.assertEqual(1, len(res_dict)) self.assertIsNone(res_dict['group'].get('volumes', None))
def test_list_group_with_list_volume(self, version, has_list_volume, mock_vol_get_all_by_group, mock_vol_type_get_all_by_group): volume_objs = [ objects.Volume(context=self.ctxt, id=i) for i in [fake.VOLUME_ID] ] volumes = objects.VolumeList(context=self.ctxt, objects=volume_objs) mock_vol_get_all_by_group.return_value = volumes vol_type_objs = [ objects.VolumeType(context=self.ctxt, id=i) for i in [fake.VOLUME_TYPE_ID] ] vol_types = objects.VolumeTypeList(context=self.ctxt, objects=vol_type_objs) mock_vol_type_get_all_by_group.return_value = vol_types if has_list_volume: req = fakes.HTTPRequest.blank( '/v3/%s/groups/detail?list_volume=True' % fake.PROJECT_ID, version=version) else: req = fakes.HTTPRequest.blank('/v3/%s/groups/detail' % fake.PROJECT_ID, version=version) res_dict = self.controller.detail(req) # If the microversion >= 3.25 and "list_volume=True", "volumes" should # be contained in the response body. Else,"volumes" should not be # contained in the response body. self.assertEqual(3, len(res_dict['groups'])) if (version, has_list_volume) == ('3.25', True): self.assertEqual([fake.VOLUME_ID], res_dict['groups'][0]['volumes']) else: self.assertIsNone(res_dict['groups'][0].get('volumes', None)) # "volumes" should not be contained in the response body when list # groups without detail. res_dict = self.controller.index(req) self.assertIsNone(res_dict['groups'][0].get('volumes', None))
def _from_db_object(cls, context, consistencygroup, db_consistencygroup, expected_attrs=None): if expected_attrs is None: expected_attrs = [] for name, field in consistencygroup.fields.items(): if name in cls.OPTIONAL_FIELDS: continue value = db_consistencygroup.get(name) setattr(consistencygroup, name, value) if 'cgsnapshots' in expected_attrs: cgsnapshots = base.obj_make_list( context, objects.CGSnapshotList(context), objects.CGSnapshot, db_consistencygroup['cgsnapshots']) consistencygroup.cgsnapshots = cgsnapshots if 'volumes' in expected_attrs: volumes = base.obj_make_list(context, objects.VolumeList(context), objects.Volume, db_consistencygroup['volumes']) consistencygroup.volumes = volumes if 'cluster' in expected_attrs: db_cluster = db_consistencygroup.get('cluster') # If this consistency group doesn't belong to a cluster the cluster # field in the ORM instance will have value of None. if db_cluster: consistencygroup.cluster = objects.Cluster(context) objects.Cluster._from_db_object(context, consistencygroup.cluster, db_cluster) else: consistencygroup.cluster = None consistencygroup._context = context consistencygroup.obj_reset_changes() return consistencygroup
def fake_volume_get_all(*args, **kwargs): return objects.VolumeList(objects=[fake_volume_get()])
def fake_volume_get_all_empty(*args, **kwargs): return objects.VolumeList(objects=[])
def fake_volume_get_all(*args, **kwargs): return objects.VolumeList(objects=[fake_volume_api_get(), fake_volume_api_get( volume_id=fake.VOLUME2_ID)])