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