]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: test_pool: fix pool unit tests
authorRicardo Dias <rdias@suse.com>
Tue, 16 Jul 2019 14:45:45 +0000 (15:45 +0100)
committerRicardo Dias <rdias@suse.com>
Wed, 17 Jul 2019 14:20:11 +0000 (15:20 +0100)
Signed-off-by: Ricardo Dias <rdias@suse.com>
src/pybind/mgr/dashboard/tests/__init__.py
src/pybind/mgr/dashboard/tests/test_pool.py

index ee2c4912d38a942f086dc454b0e3879acb4e842c..dddcac4e2e299f1835787110f430dc293df37fd7 100644 (file)
@@ -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)
index 54fef139826482a62b59e6d5a93b83e6937c19f4..16d7d7b12a641e9880eb5407f0f9f3f82e162553 100644 (file)
@@ -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