From 39c279e2edc8657a8925fa2126a98f6a85e402a3 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Thu, 25 May 2017 10:10:46 -0400 Subject: [PATCH] Have nova.context use super from_context The nova.context should be a super set of oslo.context in all cases, especially after we've deserialized on the other side of the RPC bus. Our current code actually has a completely decoupled from_dict, which means that as fields are added to oslo.context we don't pick up any of those, and end up with a potentially broken and fragmented context on the workers. This fixes that by using the parent constructor for all the fields we can, and only explicitly load in a few fields that we also need. It also simplifies the testing so that we're just testing that our extra fields end up in the context, and not exact matching everything in the context, as oslo.context may add important things over time. Change-Id: Ie683adb36d5e2a736ddbf714524c9c18f3c0d69c --- nova/context.py | 14 +++----- nova/tests/unit/test_context.py | 63 ++------------------------------- 2 files changed, 8 insertions(+), 69 deletions(-) diff --git a/nova/context.py b/nova/context.py index 42fff61e8e..6f59107695 100644 --- a/nova/context.py +++ b/nova/context.py @@ -204,21 +204,17 @@ class RequestContext(context.RequestContext): @classmethod def from_dict(cls, values): - return cls( + return super(RequestContext, cls).from_dict( + values, user_id=values.get('user_id'), - user=values.get('user'), project_id=values.get('project_id'), - tenant=values.get('tenant'), - is_admin=values.get('is_admin'), + # TODO(sdague): oslo.context has show_deleted, if + # possible, we should migrate to that in the future so we + # don't need to be different here. read_deleted=values.get('read_deleted', 'no'), - roles=values.get('roles'), remote_address=values.get('remote_address'), timestamp=values.get('timestamp'), - request_id=values.get('request_id'), - auth_token=values.get('auth_token'), quota_class=values.get('quota_class'), - user_name=values.get('user_name'), - project_name=values.get('project_name'), service_catalog=values.get('service_catalog'), instance_lock_checked=values.get('instance_lock_checked', False), ) diff --git a/nova/tests/unit/test_context.py b/nova/tests/unit/test_context.py index bd48bc01c4..984b9c6c9f 100644 --- a/nova/tests/unit/test_context.py +++ b/nova/tests/unit/test_context.py @@ -198,66 +198,9 @@ class ContextTestCase(test.NoDBTestCase): 'user_id': 111, 'user_identity': '111 222 - - -', 'user_name': None} - self.assertEqual(expected_values, values2) - - def test_convert_from_dict_to_dict_version_2_4_x(self): - # fake dict() created with oslo.context 2.4.x, Missing is_admin_project - # key - values = {'user': '111', - 'user_id': '111', - 'tenant': '222', - 'project_id': '222', - 'domain': None, 'project_domain': None, - 'auth_token': None, - 'resource_uuid': None, 'read_only': False, - 'user_identity': '111 222 - - -', - 'instance_lock_checked': False, - 'user_name': None, 'project_name': None, - 'timestamp': '2015-03-02T20:03:59.416299', - 'remote_address': None, 'quota_class': None, - 'is_admin': True, - 'service_catalog': [], - 'read_deleted': 'no', 'show_deleted': False, - 'roles': [], - 'request_id': 'req-956637ad-354a-4bc5-b969-66fd1cc00f50', - 'user_domain': None} - ctx = context.RequestContext.from_dict(values) - self.assertEqual('111', ctx.user) - self.assertEqual('222', ctx.tenant) - self.assertEqual('111', ctx.user_id) - self.assertEqual('222', ctx.project_id) - # to_dict() will add is_admin_project - values.update({'is_admin_project': True}) - values2 = ctx.to_dict() - self.assertEqual(values, values2) - - def test_convert_from_dict_then_to_dict(self): - values = {'user': '111', - 'user_id': '111', - 'tenant': '222', - 'project_id': '222', - 'domain': None, 'project_domain': None, - 'auth_token': None, - 'resource_uuid': None, 'read_only': False, - 'user_identity': '111 222 - - -', - 'instance_lock_checked': False, - 'user_name': None, 'project_name': None, - 'timestamp': '2015-03-02T20:03:59.416299', - 'remote_address': None, 'quota_class': None, - 'is_admin': True, - 'is_admin_project': True, - 'service_catalog': [], - 'read_deleted': 'no', 'show_deleted': False, - 'roles': [], - 'request_id': 'req-956637ad-354a-4bc5-b969-66fd1cc00f50', - 'user_domain': None} - ctx = context.RequestContext.from_dict(values) - self.assertEqual('111', ctx.user) - self.assertEqual('222', ctx.tenant) - self.assertEqual('111', ctx.user_id) - self.assertEqual('222', ctx.project_id) - values2 = ctx.to_dict() - self.assertEqual(values, values2) + for k, v in expected_values.items(): + self.assertIn(k, values2) + self.assertEqual(values2[k], v) @mock.patch.object(context.policy, 'authorize') def test_can(self, mock_authorize):