From 8728bf47db5fcaba976fd1880ffa44631e940993 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Tue, 16 Jul 2019 15:45:45 +0100 Subject: [PATCH] mgr/dashboard: test_pool: fix pool unit tests Signed-off-by: Ricardo Dias --- src/pybind/mgr/dashboard/tests/__init__.py | 14 +++++- src/pybind/mgr/dashboard/tests/test_pool.py | 54 ++++++++++++++++----- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/pybind/mgr/dashboard/tests/__init__.py b/src/pybind/mgr/dashboard/tests/__init__.py index ee2c4912d38a9..dddcac4e2e299 100644 --- a/src/pybind/mgr/dashboard/tests/__init__.py +++ b/src/pybind/mgr/dashboard/tests/__init__.py @@ -78,6 +78,8 @@ class CLICommandTestMixin(KVStoreMockMixin): class ControllerTestCase(helper.CPWebCase): + _endpoints_cache = {} + @classmethod def setup_controllers(cls, ctrl_classes, base_url=''): if not isinstance(ctrl_classes, list): @@ -86,7 +88,17 @@ class ControllerTestCase(helper.CPWebCase): endpoint_list = [] for ctrl in ctrl_classes: inst = ctrl() - for endpoint in ctrl.endpoints(): + + # We need to cache the controller endpoints because + # BaseController#endpoints method is not idempontent + # and a controller might be needed by more than one + # unit test. + if ctrl not in cls._endpoints_cache: + ctrl_endpoints = ctrl.endpoints() + cls._endpoints_cache[ctrl] = ctrl_endpoints + + ctrl_endpoints = cls._endpoints_cache[ctrl] + for endpoint in ctrl_endpoints: endpoint.inst = inst endpoint_list.append(endpoint) endpoint_list = sorted(endpoint_list, key=lambda e: e.url) diff --git a/src/pybind/mgr/dashboard/tests/test_pool.py b/src/pybind/mgr/dashboard/tests/test_pool.py index 54fef13982648..16d7d7b12a641 100644 --- a/src/pybind/mgr/dashboard/tests/test_pool.py +++ b/src/pybind/mgr/dashboard/tests/test_pool.py @@ -1,9 +1,12 @@ # -*- coding: utf-8 -*- # pylint: disable=protected-access +import time import mock from . import ControllerTestCase from ..controllers.pool import Pool +from ..controllers.task import Task +from ..tools import NotificationQueue, TaskManager class MockTask(object): @@ -16,17 +19,42 @@ class MockTask(object): class PoolControllerTest(ControllerTestCase): @classmethod def setup_server(cls): + Task._cp_config['tools.authenticate.on'] = False Pool._cp_config['tools.authenticate.on'] = False - cls.setup_controllers([Pool]) + cls.setup_controllers([Pool, Task]) - def test_creation(self): - self._post('/api/pool', { + @mock.patch('dashboard.controllers.pool.Pool._get') + @mock.patch('dashboard.services.ceph_service.CephService.send_command') + def test_creation(self, send_command, _get): + _get.side_effect = [{ + 'pool_name': 'test-pool', + 'pg_num': 64, + 'pg_num_target': 63, + 'pg_placement_num': 64, + 'pg_placement_num_target': 63 + }, { + 'pool_name': 'test-pool', + 'pg_num': 64, + 'pg_num_target': 64, + 'pg_placement_num': 64, + 'pg_placement_num_target': 64 + }] + NotificationQueue.start_queue() + TaskManager.init() + + def _send_cmd(*args, **kwargs): # pylint: disable=unused-argument + time.sleep(3) + + send_command.side_effect = _send_cmd + + self._task_post('/api/pool', { 'pool': 'test-pool', 'pool_type': 1, 'pg_num': 64 - }) - self.assertStatus(202) - self.assertJsonBody({'name': 'pool/create', 'metadata': {'pool_name': 'test-pool'}}) + }, 10) + self.assertStatus(201) + self.assertEqual(_get.call_count, 2) + NotificationQueue.stop() @mock.patch('dashboard.controllers.pool.Pool._get') def test_wait_for_pgs_without_waiting(self, _get): @@ -37,15 +65,15 @@ class PoolControllerTest(ControllerTestCase): 'pg_placement_num': 32, 'pg_placement_num_target': 32 }] - pool = Pool() - pool._wait_for_pgs('test-pool') + Pool._wait_for_pgs('test-pool') self.assertEqual(_get.call_count, 1) @mock.patch('dashboard.controllers.pool.Pool._get') - @mock.patch('dashboard.tools.TaskManager.current_task') - def test_wait_for_pgs_with_waiting(self, taskMock, _get): + def test_wait_for_pgs_with_waiting(self, _get): task = MockTask() - taskMock.return_value = task + orig_method = TaskManager.current_task + TaskManager.current_task = mock.MagicMock() + TaskManager.current_task.return_value = task _get.side_effect = [{ 'pool_name': 'test-pool', 'pg_num': 64, @@ -83,7 +111,7 @@ class PoolControllerTest(ControllerTestCase): 'pg_placement_num': 32, 'pg_placement_num_target': 32 }] - pool = Pool() - pool._wait_for_pgs('test-pool') + Pool._wait_for_pgs('test-pool') self.assertEqual(_get.call_count, 6) self.assertEqual(task.percentages, [0, 5, 50, 73, 98]) + TaskManager.current_task = orig_method -- 2.39.5