From 554f7cd0a6e5e9573bf7b8d371183cb505b0e2cf Mon Sep 17 00:00:00 2001 From: bhagyashris Date: Wed, 18 Sep 2019 19:33:25 +0530 Subject: [PATCH] Remove deprecation warning messages Below deprecation warning message is emitted continuously during each API call due to the introduction of glance_store library in patch [1]. DeprecationWarning: Property 'user' has moved to 'user_id' in version '2.6' and will be removed in version '3.0'" DeprecationWarning: Property 'tenant' has moved to 'project_id' in version '2.6' and will be removed in version '3.0'" There is an issue in glance_store library which is reported in LP bug [2] and it will be fixed by patch [3]. But we are not sure whether this issue will be fixed in Train cycle Moreover, in future we will need to deal and remove `tenant` and `user` attributes from context as these attributes are deprecated from oslo.context in version 2.6 and it's planned to be removed in 3.0. so this patch removes these 'tenant' and 'user' attributes from context. Also, added warnings fixture in the unit test to ensure these attributes are not used anywhere in the code. If used, it will result in test failure. [1] : https://review.opendev.org/#/c/675600 [2] : https://bugs.launchpad.net/glance-store/+bug/1844462 [3] : https://review.opendev.org/#/c/683034 Implements: blueprint tosca-csar-mgmt-driver Change-Id: I0cb320bbf237ebfac72b8e65128901c70cdd6780 --- tacker/context.py | 22 +++++---------------- tacker/tests/unit/base.py | 4 ++++ tacker/tests/unit/fixtures.py | 32 +++++++++++++++++++++++++++++++ tacker/tests/unit/test_context.py | 12 ++++++------ 4 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 tacker/tests/unit/fixtures.py diff --git a/tacker/context.py b/tacker/context.py index e6078b736..f24de4b27 100644 --- a/tacker/context.py +++ b/tacker/context.py @@ -60,17 +60,13 @@ class ContextBase(oslo_context.RequestContext): if self.is_admin is None: self.is_admin = policy.check_is_admin(self) - @property - def project_id(self): - return self.tenant - @property def tenant_id(self): - return self.tenant + return self.project_id @tenant_id.setter def tenant_id(self, tenant_id): - self.tenant = tenant_id + self.project_id = tenant_id @property def tenant_name(self): @@ -80,14 +76,6 @@ class ContextBase(oslo_context.RequestContext): def tenant_name(self, tenant_name): self.project_name = tenant_name - @property - def user_id(self): - return self.user - - @user_id.setter - def user_id(self, user_id): - self.user = user_id - def to_dict(self): context = super(ContextBase, self).to_dict() context.update({ @@ -103,7 +91,7 @@ class ContextBase(oslo_context.RequestContext): @classmethod def from_dict(cls, values): - return cls(user_id=values.get('user_id', values.get('user')), + return cls(user_id=values.get('user_id'), tenant_id=values.get('tenant_id', values.get('project_id')), is_admin=values.get('is_admin'), roles=values.get('roles'), @@ -122,8 +110,8 @@ class ContextBase(oslo_context.RequestContext): # but kept for backwards compatibility. Remove them in Pike # (oslo.context from Ocata release already issues deprecation warnings # for non-standard keys). - values['user'] = self.user_id - values['tenant'] = self.project_id + values['user_id'] = self.user_id + values['project_id'] = self.project_id values['domain'] = self.domain_id values['user_domain'] = self.user_domain_id values['project_domain'] = self.project_domain_id diff --git a/tacker/tests/unit/base.py b/tacker/tests/unit/base.py index 263f13fff..34839750c 100644 --- a/tacker/tests/unit/base.py +++ b/tacker/tests/unit/base.py @@ -19,6 +19,7 @@ from oslo_config import fixture as config_fixture from requests_mock.contrib import fixture as requests_mock_fixture from tacker.tests import base +from tacker.tests.unit import fixtures as tacker_fixtures CONF = cfg.CONF @@ -30,6 +31,9 @@ class TestCase(base.BaseTestCase): self.config_fixture = self.useFixture(config_fixture.Config(CONF)) CONF([], default_config_files=[]) + # Limit the amount of DeprecationWarning messages in the unit test logs + self.useFixture(tacker_fixtures.WarningsFixture()) + def _mock(self, target, new=mock.DEFAULT): patcher = mock.patch(target, new) return patcher.start() diff --git a/tacker/tests/unit/fixtures.py b/tacker/tests/unit/fixtures.py new file mode 100644 index 000000000..c0d4f9274 --- /dev/null +++ b/tacker/tests/unit/fixtures.py @@ -0,0 +1,32 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Fixtures for Tacker unit tests.""" +# NOTE(bhagyashris): This is needed for importing from fixtures. +from __future__ import absolute_import + +import warnings + +import fixtures as pyfixtures + + +class WarningsFixture(pyfixtures.Fixture): + """Filters out warnings during test runs.""" + + def setUp(self): + super(WarningsFixture, self).setUp() + # NOTE(bhagyashris): user/tenant is deprecated in oslo.context + # so don't let anything new use it + warnings.filterwarnings( + 'error', message="Property '.*' has moved to '.*'") + + self.addCleanup(warnings.resetwarnings) diff --git a/tacker/tests/unit/test_context.py b/tacker/tests/unit/test_context.py index 77523fd67..7dc9c324e 100644 --- a/tacker/tests/unit/test_context.py +++ b/tacker/tests/unit/test_context.py @@ -35,8 +35,8 @@ class TestTackerContext(base.BaseTestCase): self.assertEqual('tenant_id', ctx.project_id) self.assertEqual('tenant_id', ctx.tenant_id) self.assertThat(ctx.request_id, matchers.StartsWith('req-')) - self.assertEqual('user_id', ctx.user) - self.assertEqual('tenant_id', ctx.tenant) + self.assertEqual('user_id', ctx.user_id) + self.assertEqual('tenant_id', ctx.project_id) self.assertIsNone(ctx.user_name) self.assertIsNone(ctx.tenant_name) @@ -47,8 +47,8 @@ class TestTackerContext(base.BaseTestCase): self.assertEqual('user_name', ctx.user_name) self.assertEqual('tenant_name', ctx.tenant_name) # Check user/tenant contains its ID even if user/tenant_name is passed - self.assertEqual('user_id', ctx.user) - self.assertEqual('tenant_id', ctx.tenant) + self.assertEqual('user_id', ctx.user_id) + self.assertEqual('tenant_id', ctx.project_id) def test_tacker_context_create_with_request_id(self): ctx = context.Context('user_id', 'tenant_id', request_id='req_id_xxx') @@ -60,8 +60,8 @@ class TestTackerContext(base.BaseTestCase): self.assertEqual('user_id', ctx_dict['user_id']) self.assertEqual('tenant_id', ctx_dict['project_id']) self.assertEqual(ctx.request_id, ctx_dict['request_id']) - self.assertEqual('user_id', ctx_dict['user']) - self.assertEqual('tenant_id', ctx_dict['tenant']) + self.assertEqual('user_id', ctx_dict['user_id']) + self.assertEqual('tenant_id', ctx_dict['project_id']) self.assertIsNone(ctx_dict['user_name']) self.assertIsNone(ctx_dict['tenant_name']) self.assertIsNone(ctx_dict['project_name'])