]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/tests/__init__.py
5e5a541fbdf8270611610ce8517fe30e50622e75
2 from __future__
import absolute_import
8 if 'UNITTEST' in os
.environ
:
10 # Mock ceph_module. Otherwise every module that is involved in a testcase and imports it will
11 # raise an ImportError
16 from unittest
import mock
26 * self.set_store() populates self._store
27 * self.set_module_option() populates self._store[module_name]
28 * self.get(thing) comes from self._store['_ceph_get' + thing]
32 def mock_store_get(self
, kind
, key
, default
):
33 if not hasattr(self
, '_store'):
35 return self
._store
.get(f
'mock_store/{kind}/{key}', default
)
37 def mock_store_set(self
, kind
, key
, value
):
38 if not hasattr(self
, '_store'):
40 k
= f
'mock_store/{kind}/{key}'
45 self
._store
[k
] = value
47 def mock_store_preifx(self
, kind
, prefix
):
48 if not hasattr(self
, '_store'):
50 full_prefix
= f
'mock_store/{kind}/{prefix}'
51 kind_len
= len(f
'mock_store/{kind}/')
53 k
[kind_len
:]: v
for k
, v
in self
._store
.items()
54 if k
.startswith(full_prefix
)
57 def _ceph_get_store(self
, k
):
58 return self
.mock_store_get('store', k
, None)
60 def _ceph_set_store(self
, k
, v
):
61 self
.mock_store_set('store', k
, v
)
63 def _ceph_get_store_prefix(self
, prefix
):
64 return self
.mock_store_preifx('store', prefix
)
66 def _ceph_get_module_option(self
, module
, key
, localized_prefix
= None):
68 _
, val
, _
= self
.check_mon_command({
69 'prefix': 'config get',
71 'key': f
'mgr/{module}/{key}'
73 except FileNotFoundError
:
75 mo
= [o
for o
in self
.MODULE_OPTIONS
if o
['name'] == key
]
81 'bool': lambda s
: bool(s
) and s
!= 'false' and s
!= 'False',
83 }[mo
[0].get('type', 'str')]
87 return val
if val
is not None else ''
89 def _ceph_set_module_option(self
, module
, key
, val
):
90 _
, _
, _
= self
.check_mon_command({
91 'prefix': 'config set',
93 'name': f
'mgr/{module}/{key}',
98 def _ceph_get(self
, data_name
):
99 return self
.mock_store_get('_ceph_get', data_name
, mock
.MagicMock())
101 def _ceph_send_command(self
, res
, svc_type
, svc_id
, command
, tag
):
102 cmd
= json
.loads(command
)
104 # Mocking the config store is handy sometimes:
106 who
= cmd
['who'].split('.')
107 whos
= ['global'] + ['.'.join(who
[:i
+1]) for i
in range(len(who
))]
108 for attepmt
in reversed(whos
):
109 val
= self
.mock_store_get('config', f
'{attepmt}/{cmd["key"]}', None)
115 self
.mock_store_set('config', f
'{cmd["who"]}/{cmd["name"]}', cmd
['value'])
120 for prefix
, value
in self
.mock_store_preifx('config', '').items():
121 section
, name
= prefix
.split('/', 1)
130 if cmd
['prefix'] == 'config get':
132 elif cmd
['prefix'] == 'config set':
134 elif cmd
['prefix'] == 'config dump':
136 elif hasattr(self
, '_mon_command_mock_' + cmd
['prefix'].replace(' ', '_')):
137 a
= getattr(self
, '_mon_command_mock_' + cmd
['prefix'].replace(' ', '_'))
140 res
.complete(0, outb
, '')
144 return logging
.getLogger(__name__
)
147 def _logger(self
, _
):
150 def __init__(self
, *args
):
151 if not hasattr(self
, '_store'):
155 if self
.__class
__.__name
__ not in M_classes
:
156 # call those only once.
157 self
._register
_commands
('')
158 self
._register
_options
('')
159 M_classes
.add(self
.__class
__.__name
__)
161 super(M
, self
).__init
__()
162 self
._ceph
_get
_version
= mock
.Mock()
163 self
._ceph
_get
_option
= mock
.MagicMock()
164 self
._ceph
_get
_context
= mock
.MagicMock()
165 self
._ceph
_register
_client
= mock
.MagicMock()
166 self
._ceph
_set
_health
_checks
= mock
.MagicMock()
167 self
._configure
_logging
= lambda *_
: None
168 self
._unconfigure
_logging
= mock
.MagicMock()
169 self
._ceph
_log
= mock
.MagicMock()
170 self
._ceph
_dispatch
_remote
= lambda *_
: None
171 self
._ceph
_get
_mgr
_id
= mock
.MagicMock()
176 cm
.BaseMgrStandbyModule
= M
177 sys
.modules
['ceph_module'] = cm
179 def mock_ceph_modules():
180 class MockRadosError(Exception):
181 def __init__(self
, message
, errno
=None):
182 super(MockRadosError
, self
).__init
__(message
)
186 msg
= super(MockRadosError
, self
).__str
__()
187 if self
.errno
is None:
189 return '[errno {0}] {1}'.format(self
.errno
, msg
)
193 'rados': mock
.MagicMock(Error
=MockRadosError
, OSError=MockRadosError
),
195 'cephfs': mock
.Mock(),