From b6fd13d217b817ae220611de3c0b0831e71cec0f Mon Sep 17 00:00:00 2001 From: TommyLike Date: Fri, 12 May 2017 16:48:59 +0800 Subject: [PATCH] Refactor internal context testcases with ddt Use ddt to wrap internal context testcases. Change-Id: I44ad096065d40d31929ee6aad4ca12ddaf6fe80d --- cinder/tests/unit/test_context.py | 46 ++++++++++--------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/cinder/tests/unit/test_context.py b/cinder/tests/unit/test_context.py index 7508f23bfc9..68f89282df4 100644 --- a/cinder/tests/unit/test_context.py +++ b/cinder/tests/unit/test_context.py @@ -13,12 +13,14 @@ # License for the specific language governing permissions and limitations # under the License. +import ddt import mock from cinder import context from cinder import test +@ddt.ddt class ContextTestCase(test.TestCase): def test_request_context_sets_is_admin(self): @@ -98,42 +100,22 @@ class ContextTestCase(test.TestCase): self.assertEqual('user tenant domain user-domain project-domain', ctx.to_dict()["user_identity"]) + @ddt.data(('ec729e9946bc43c39ece6dfa7de70eea', + 'c466a48309794261b64a4f02cfcc3d64'), + ('ec729e9946bc43c39ece6dfa7de70eea', None), + (None, 'c466a48309794261b64a4f02cfcc3d64'), + (None, None)) + @ddt.unpack @mock.patch('cinder.context.CONF') - def test_cinder_internal_context(self, mock_conf): - project_id = 'ec729e9946bc43c39ece6dfa7de70eea' - user_id = 'c466a48309794261b64a4f02cfcc3d64' + def test_cinder_internal_context(self, project_id, user_id, mock_conf): mock_conf.cinder_internal_tenant_project_id = project_id mock_conf.cinder_internal_tenant_user_id = user_id ctx = context.get_internal_tenant_context() - self.assertEqual(user_id, ctx.user_id) - self.assertEqual(project_id, ctx.project_id) - - @mock.patch('cinder.context.CONF') - def test_cinder_internal_context_missing_user(self, mock_conf): - project_id = 'ec729e9946bc43c39ece6dfa7de70eea' - user_id = None - mock_conf.cinder_internal_tenant_project_id = project_id - mock_conf.cinder_internal_tenant_user_id = user_id - ctx = context.get_internal_tenant_context() - self.assertIsNone(ctx) - - @mock.patch('cinder.context.CONF') - def test_cinder_internal_context_missing_project(self, mock_conf): - project_id = None - user_id = 'c466a48309794261b64a4f02cfcc3d64' - mock_conf.cinder_internal_tenant_project_id = project_id - mock_conf.cinder_internal_tenant_user_id = user_id - ctx = context.get_internal_tenant_context() - self.assertIsNone(ctx) - - @mock.patch('cinder.context.CONF') - def test_cinder_internal_context_missing_all(self, mock_conf): - project_id = None - user_id = None - mock_conf.cinder_internal_tenant_project_id = project_id - mock_conf.cinder_internal_tenant_user_id = user_id - ctx = context.get_internal_tenant_context() - self.assertIsNone(ctx) + if project_id is None or user_id is None: + self.assertIsNone(ctx) + else: + self.assertEqual(user_id, ctx.user_id) + self.assertEqual(project_id, ctx.project_id) def test_request_context_no_roles(self): ctxt = context.RequestContext('111',