diff --git a/nova/scheduler/utils.py b/nova/scheduler/utils.py index 25a436dd4592..c6d7d053dc7c 100644 --- a/nova/scheduler/utils.py +++ b/nova/scheduler/utils.py @@ -1014,11 +1014,12 @@ def claim_resources(ctx, client, spec_obj, instance_uuid, alloc_req, project_id = spec_obj.project_id - # NOTE(jaypipes): So, the RequestSpec doesn't store the user_id, - # only the project_id, so we need to grab the user information from - # the context. Perhaps we should consider putting the user ID in - # the spec object? - user_id = ctx.user_id + # We didn't start storing the user_id in the RequestSpec until Rocky so + # if it's not set on an old RequestSpec, use the user_id from the context. + if 'user_id' in spec_obj and spec_obj.user_id: + user_id = spec_obj.user_id + else: + user_id = ctx.user_id # NOTE(gibi): this could raise AllocationUpdateFailed which means there is # a serious issue with the instance_uuid as a consumer. Every caller of diff --git a/nova/tests/unit/scheduler/test_utils.py b/nova/tests/unit/scheduler/test_utils.py index 439469aede63..c96da46fa326 100644 --- a/nova/tests/unit/scheduler/test_utils.py +++ b/nova/tests/unit/scheduler/test_utils.py @@ -881,8 +881,8 @@ class TestUtils(test.NoDBTestCase): call the placement client to claim resources for the instance. """ mock_is_rebuild.return_value = False - ctx = mock.Mock(user_id=uuids.user_id) - spec_obj = mock.Mock(project_id=uuids.project_id) + ctx = nova_context.RequestContext(user_id=uuids.user_id) + spec_obj = objects.RequestSpec(project_id=uuids.project_id) instance_uuid = uuids.instance alloc_req = mock.sentinel.alloc_req mock_client.claim_resources.return_value = True @@ -896,6 +896,16 @@ class TestUtils(test.NoDBTestCase): consumer_generation=None) self.assertTrue(res) + # Now do it again but with RequestSpec.user_id set. + spec_obj.user_id = uuids.spec_user_id + mock_client.reset_mock() + utils.claim_resources(ctx, mock_client, spec_obj, instance_uuid, + alloc_req) + mock_client.claim_resources.assert_called_once_with( + ctx, uuids.instance, mock.sentinel.alloc_req, uuids.project_id, + uuids.spec_user_id, allocation_request_version=None, + consumer_generation=None) + @mock.patch('nova.scheduler.client.report.SchedulerReportClient') @mock.patch('nova.scheduler.utils.request_is_rebuild') def test_claim_resouces_for_policy_check(self, mock_is_rebuild,