From cee739a4a86fdb78747d81f89bd7c3e2cc2e55d4 Mon Sep 17 00:00:00 2001 From: Cao Shufeng Date: Mon, 12 Sep 2016 06:27:03 -0400 Subject: [PATCH] Save volume_type/group_type uuid into db when creating group When creating groups, if volume type name or group type name is passed, cinder-api will try to save the name rather uuid into the database. It will make a foreign key constraint fails. After this change, we always save uuid into database. Change-Id: Ib333130325fc12a4753c7a128e823e992e8c8682 Closes-Bug: #1622476 --- cinder/group/api.py | 4 +-- cinder/tests/unit/group/test_groups_api.py | 30 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cinder/group/api.py b/cinder/group/api.py index 8772f7a11f4..0328da99ddc 100644 --- a/cinder/group/api.py +++ b/cinder/group/api.py @@ -151,8 +151,8 @@ class API(base.Base): 'status': c_fields.GroupStatus.CREATING, 'name': name, 'description': description, - 'volume_type_ids': volume_types, - 'group_type_id': group_type} + 'volume_type_ids': [t['id'] for t in req_volume_types], + 'group_type_id': req_group_type['id']} group = None try: group = objects.Group(context=context, **kwargs) diff --git a/cinder/tests/unit/group/test_groups_api.py b/cinder/tests/unit/group/test_groups_api.py index 90ff43c8d98..efad4ca89f0 100644 --- a/cinder/tests/unit/group/test_groups_api.py +++ b/cinder/tests/unit/group/test_groups_api.py @@ -138,6 +138,36 @@ class GroupAPITestCase(test.TestCase): mock_group_type_get.assert_called_once_with(self.ctxt, "fake-grouptype-name") + @mock.patch('cinder.group.api.API._cast_create_group') + @mock.patch('cinder.group.api.API.update_quota') + @mock.patch('cinder.db.group_type_get_by_name') + @mock.patch('cinder.db.volume_types_get_by_name_or_id') + @mock.patch('cinder.group.api.check_policy') + def test_create_with_multi_types(self, mock_policy, mock_volume_types_get, + mock_group_type_get, + mock_update_quota, + mock_cast_create_group): + volume_types = [{'id': fake.VOLUME_TYPE_ID}, + {'id': fake.VOLUME_TYPE2_ID}] + mock_volume_types_get.return_value = volume_types + mock_group_type_get.return_value = {'id': fake.GROUP_TYPE_ID} + volume_type_names = ['fake-volume-type1', 'fake-volume-type2'] + name = "test_group" + description = "this is a test group" + + group = self.group_api.create(self.ctxt, name, description, + "fake-grouptype-name", + volume_type_names, + availability_zone='nova') + self.assertEqual(group["volume_type_ids"], + [t['id'] for t in volume_types]) + self.assertEqual(group["group_type_id"], fake.GROUP_TYPE_ID) + + mock_group_type_get.assert_called_once_with(self.ctxt, + "fake-grouptype-name") + mock_volume_types_get.assert_called_once_with(mock.ANY, + volume_type_names) + @mock.patch('cinder.volume.rpcapi.VolumeAPI.update_group') @mock.patch('cinder.db.volume_get_all_by_generic_group') @mock.patch('cinder.group.api.API._cast_create_group')