class ControllerTestCase(helper.CPWebCase):
+ _endpoints_cache = {}
+
@classmethod
def setup_controllers(cls, ctrl_classes, base_url=''):
if not isinstance(ctrl_classes, list):
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)
# -*- 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):
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):
'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,
'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