]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_pool.py
import quincy beta 17.1.0
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_pool.py
1 # -*- coding: utf-8 -*-
2 # pylint: disable=protected-access
3 import time
4
5 try:
6 import mock
7 except ImportError:
8 import unittest.mock as mock
9
10 from .. import mgr
11 from ..controllers.pool import Pool
12 from ..controllers.task import Task
13 from ..tests import ControllerTestCase
14 from ..tools import NotificationQueue, TaskManager
15
16
17 class MockTask(object):
18 percentages = []
19
20 def set_progress(self, percentage):
21 self.percentages.append(percentage)
22
23
24 class PoolControllerTest(ControllerTestCase):
25 @classmethod
26 def setup_server(cls):
27 cls.setup_controllers([Pool, Task])
28
29 @mock.patch('dashboard.services.progress.get_progress_tasks')
30 @mock.patch('dashboard.controllers.pool.Pool._get')
31 @mock.patch('dashboard.services.ceph_service.CephService.send_command')
32 def test_creation(self, send_command, _get, get_progress_tasks):
33 _get.side_effect = [{
34 'pool_name': 'test-pool',
35 'pg_num': 64,
36 'pg_num_target': 63,
37 'pg_placement_num': 64,
38 'pg_placement_num_target': 63
39 }, {
40 'pool_name': 'test-pool',
41 'pg_num': 64,
42 'pg_num_target': 64,
43 'pg_placement_num': 64,
44 'pg_placement_num_target': 64
45 }]
46 NotificationQueue.start_queue()
47 TaskManager.init()
48
49 def _send_cmd(*args, **kwargs): # pylint: disable=unused-argument
50 time.sleep(3)
51
52 send_command.side_effect = _send_cmd
53 get_progress_tasks.return_value = [], []
54
55 self._task_post('/api/pool', {
56 'pool': 'test-pool',
57 'pool_type': 1,
58 'pg_num': 64
59 }, 10)
60 self.assertStatus(201)
61 self.assertEqual(_get.call_count, 2)
62 NotificationQueue.stop()
63
64 @mock.patch('dashboard.controllers.pool.Pool._get')
65 def test_wait_for_pgs_without_waiting(self, _get):
66 _get.side_effect = [{
67 'pool_name': 'test-pool',
68 'pg_num': 32,
69 'pg_num_target': 32,
70 'pg_placement_num': 32,
71 'pg_placement_num_target': 32
72 }]
73 Pool._wait_for_pgs('test-pool')
74 self.assertEqual(_get.call_count, 1)
75
76 @mock.patch('dashboard.controllers.pool.Pool._get')
77 def test_wait_for_pgs_with_waiting(self, _get):
78 task = MockTask()
79 orig_method = TaskManager.current_task
80 TaskManager.current_task = mock.MagicMock()
81 TaskManager.current_task.return_value = task
82 _get.side_effect = [{
83 'pool_name': 'test-pool',
84 'pg_num': 64,
85 'pg_num_target': 32,
86 'pg_placement_num': 64,
87 'pg_placement_num_target': 64
88 }, {
89 'pool_name': 'test-pool',
90 'pg_num': 63,
91 'pg_num_target': 32,
92 'pg_placement_num': 62,
93 'pg_placement_num_target': 32
94 }, {
95 'pool_name': 'test-pool',
96 'pg_num': 48,
97 'pg_num_target': 32,
98 'pg_placement_num': 48,
99 'pg_placement_num_target': 32
100 }, {
101 'pool_name': 'test-pool',
102 'pg_num': 48,
103 'pg_num_target': 32,
104 'pg_placement_num': 33,
105 'pg_placement_num_target': 32
106 }, {
107 'pool_name': 'test-pool',
108 'pg_num': 33,
109 'pg_num_target': 32,
110 'pg_placement_num': 32,
111 'pg_placement_num_target': 32
112 }, {
113 'pool_name': 'test-pool',
114 'pg_num': 32,
115 'pg_num_target': 32,
116 'pg_placement_num': 32,
117 'pg_placement_num_target': 32
118 }]
119 Pool._wait_for_pgs('test-pool')
120 self.assertEqual(_get.call_count, 6)
121 self.assertEqual(task.percentages, [0, 5, 50, 73, 98])
122 TaskManager.current_task = orig_method
123
124 @mock.patch('dashboard.controllers.osd.CephService.get_pool_list_with_stats')
125 @mock.patch('dashboard.controllers.osd.CephService.get_pool_list')
126 def test_pool_list(self, get_pool_list, get_pool_list_with_stats):
127 get_pool_list.return_value = [{
128 'type': 3,
129 'crush_rule': 1,
130 'application_metadata': {
131 'test_key': 'test_metadata'
132 },
133 'pool_name': 'test_name'
134 }]
135 mgr.get.side_effect = lambda key: {
136 'osd_map_crush': {
137 'rules': [{
138 'rule_id': 1,
139 'rule_name': 'test-rule'
140 }]
141 }
142 }[key]
143 Pool._pool_list()
144 mgr.get.assert_called_with('osd_map_crush')
145 self.assertEqual(get_pool_list.call_count, 1)
146 # with stats
147 get_pool_list_with_stats.return_value = get_pool_list.return_value
148 Pool._pool_list(attrs='type', stats='True')
149 self.assertEqual(get_pool_list_with_stats.call_count, 1)
150
151 @mock.patch('dashboard.controllers.pool.Pool._get')
152 @mock.patch('dashboard.services.ceph_service.CephService.send_command')
153 def test_set_pool_name(self, send_command, _get):
154 _get.return_value = {
155 'options': {
156 'compression_min_blob_size': '1'
157 },
158 'application_metadata': ['data1', 'data2']
159 }
160
161 def _send_cmd(*args, **kwargs): # pylint: disable=unused-argument
162 pass
163
164 send_command.side_effect = _send_cmd
165 NotificationQueue.start_queue()
166 TaskManager.init()
167 self._task_put('/api/pool/test-pool', {
168 "flags": "ec_overwrites",
169 "application_metadata": ['data3', 'data2'],
170 "configuration": "test-conf",
171 "compression_mode": 'unset',
172 'compression_min_blob_size': '1',
173 'compression_max_blob_size': '1',
174 'compression_required_ratio': '1',
175 'pool': 'test-pool',
176 'pg_num': 64
177 })
178 NotificationQueue.stop()
179 self.assertEqual(_get.call_count, 1)
180 self.assertEqual(send_command.call_count, 10)