From aa500fbb31d2f7129c048460e57a3dff229cabc8 Mon Sep 17 00:00:00 2001 From: liwenjian Date: Fri, 8 Sep 2023 17:14:38 +0800 Subject: [PATCH] Fixed an error when caching multiple images in aggregate Because in the process of judging whether the image id is Duplicate,there is only deduplication without sorting, so no duplicate image error is judged as duplicate and an error "Duplicate images in request" is reported. Now it is changed to sort after deduplication and then compare. Two unit test cases were added to verify the fix Related-Bug: #2034702 Change-Id: I300a3e29ba56584f4c99d534a6cf8ee7dc0ed4b7 --- nova/api/openstack/compute/aggregates.py | 2 +- .../api/openstack/compute/test_aggregates.py | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/compute/aggregates.py b/nova/api/openstack/compute/aggregates.py index 4b4e31b34868..45bd904ec032 100644 --- a/nova/api/openstack/compute/aggregates.py +++ b/nova/api/openstack/compute/aggregates.py @@ -289,7 +289,7 @@ class AggregateController(wsgi.Controller): for image_req in body.get('cache'): image_ids.append(image_req['id']) - if image_ids != list(set(image_ids)): + if sorted(image_ids) != sorted(list(set(image_ids))): raise exc.HTTPBadRequest( explanation=_('Duplicate images in request')) diff --git a/nova/tests/unit/api/openstack/compute/test_aggregates.py b/nova/tests/unit/api/openstack/compute/test_aggregates.py index 21d644f0bed3..891388590d47 100644 --- a/nova/tests/unit/api/openstack/compute/test_aggregates.py +++ b/nova/tests/unit/api/openstack/compute/test_aggregates.py @@ -738,3 +738,28 @@ class AggregateTestCaseV21(test.NoDBTestCase): version='2.81') self.assertRaises(exc.HTTPBadRequest, self.controller.images, req, 'foo', body=body) + + def test_images_with_duplicate_id(self): + body = {"cache": [{"id": "faae1bd3-c848-41d6-b4dd-97d5b8be8b7e"}, + {"id": "faae1bd3-c848-41d6-b4dd-97d5b8be8b7e"}]} + req = fakes.HTTPRequest.blank('/v2/os-aggregates', + use_admin_context=True, + version='2.81') + self.assertRaises(exc.HTTPBadRequest, self.controller.images, + req, '1', body=body) + + def test_images_with_disorder_id(self): + body = {"cache": [{"id": "faae1bd3-c848-41d6-b4dd-97d5b8be8b7e"}, + {"id": "290de658-cf55-4cce-b025-9a1a9f93676a"}, + {"id": "896f7f54-4e4e-4c21-a2b7-47cff4e99ab0"}, + {"id": "d982bb82-04a0-4e9b-b40e-470f20a7b5d1"}]} + req = fakes.HTTPRequest.blank('/v2/os-aggregates', + use_admin_context=True, + version='2.81') + context = req.environ['nova.context'] + with mock.patch.object(self.controller.api, + 'get_aggregate') as mock_get: + with mock.patch.object(self.controller.conductor_tasks, + 'cache_images'): + self.controller.images(req, '1', body=body) + mock_get.assert_called_once_with(context, '1')