summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2019-01-09 09:03:10 +0000
committerGerrit Code Review <review@openstack.org>2019-01-09 09:03:10 +0000
commitbb48fb398d049988a9ff7c999c4ac90602587b40 (patch)
treeabb15959bdcc4812e78a4d387021d1cc98da23c4
parent6d37fa99405b60a27c1fa8b7177e253b48d16d82 (diff)
parent857c239e97483c7212d4672a31e4f58462d22edd (diff)
Merge "Remove redundant code and improve test coverage"
-rw-r--r--freezer_api/storage/driver.py76
-rw-r--r--freezer_api/storage/elastic.py33
-rw-r--r--freezer_api/storage/elasticv2.py3
-rw-r--r--freezer_api/tests/unit/test_elastic.py52
4 files changed, 40 insertions, 124 deletions
diff --git a/freezer_api/storage/driver.py b/freezer_api/storage/driver.py
deleted file mode 100644
index 28186ef..0000000
--- a/freezer_api/storage/driver.py
+++ /dev/null
@@ -1,76 +0,0 @@
1"""
2(c) Copyright 2014,2015 Hewlett-Packard Development Company, L.P.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16"""
17
18from freezer_api.storage import elastic
19
20from oslo_config import cfg
21from oslo_log import log
22from oslo_utils import importutils
23
24
25CONF = cfg.CONF
26LOG = log.getLogger(__name__)
27
28
29# storage backend options to be registered
30_OPTS = [
31 cfg.StrOpt("backend",
32 help="Database backend section name. This section "
33 "will be loaded by the proper driver to connect to "
34 "the database."
35 ),
36 cfg.StrOpt('driver',
37 default='freezer_api.storage.elasticv2.ElasticSearchEngineV2',
38 help="Database driver to be used."
39 )
40]
41
42
43def register_storage_opts():
44 """Register storage configuration options"""
45 opt_group = cfg.OptGroup(name='storage',
46 title='Freezer Storage Engine')
47 CONF.register_group(opt_group)
48 CONF.register_opts(_OPTS, group=opt_group)
49
50
51def get_db(driver=None):
52 """Automatically loads the database driver to be used."""
53 storage = CONF.get('storage')
54 if not driver:
55 driver = storage['driver']
56 driver_instance = importutils.import_object(
57 driver,
58 backend=storage['backend']
59 )
60
61 return driver_instance
62
63
64def get_storage_opts():
65 """Returns a dict that contains list of options for db backend"""
66 opts = {"storage": _OPTS}
67 opts.update(_get_elastic_opts())
68 return opts
69
70
71def _get_elastic_opts(backend=None):
72 """Return Opts for elasticsearch driver"""
73 if not backend:
74 backend = "elasticsearch"
75 es = elastic.ElasticSearchEngine(backend=backend)
76 return {backend: es.get_opts()}
diff --git a/freezer_api/storage/elastic.py b/freezer_api/storage/elastic.py
index 8743f9d..080d063 100644
--- a/freezer_api/storage/elastic.py
+++ b/freezer_api/storage/elastic.py
@@ -280,34 +280,6 @@ class SessionTypeManager(TypeManager):
280 280
281class ElasticSearchEngine(object): 281class ElasticSearchEngine(object):
282 282
283 _OPTS = [
284 cfg.ListOpt('hosts',
285 default=['http://127.0.0.1:9200'],
286 help='specify the storage hosts'),
287 cfg.StrOpt('index',
288 default='freezer',
289 help='specify the name of the elasticsearch index'),
290 cfg.IntOpt('timeout',
291 default=60,
292 help='specify the connection timeout'),
293 cfg.IntOpt('retries',
294 default=20,
295 help='number of retries to allow before raising and error'),
296 cfg.BoolOpt('use_ssl',
297 default=False,
298 help='explicitly turn on SSL'),
299 cfg.BoolOpt('verify_certs',
300 default=False,
301 help='turn on SSL certs verification'),
302 cfg.StrOpt('ca_certs',
303 help='path to CA certs on disk'),
304 cfg.IntOpt('number_of_replicas',
305 default=0,
306 help='Number of replicas for elk cluster. Default is 0. '
307 'Use 0 for no replicas. This should be set to (number '
308 'of node in the ES cluter -1).')
309 ]
310
311 def __init__(self, backend): 283 def __init__(self, backend):
312 """backend: name of the section in the config file to load 284 """backend: name of the section in the config file to load
313 elasticsearch opts 285 elasticsearch opts
@@ -319,8 +291,6 @@ class ElasticSearchEngine(object):
319 self.job_manager = None 291 self.job_manager = None
320 self.action_manager = None 292 self.action_manager = None
321 self.session_manager = None 293 self.session_manager = None
322 # register elasticsearch opts
323 CONF.register_opts(self._OPTS, group=backend)
324 self.conf = dict(CONF.get(backend)) 294 self.conf = dict(CONF.get(backend))
325 self.backend = backend 295 self.backend = backend
326 self._validate_opts() 296 self._validate_opts()
@@ -332,9 +302,6 @@ class ElasticSearchEngine(object):
332 raise Exception("File not found: ca_certs file ({0}) not " 302 raise Exception("File not found: ca_certs file ({0}) not "
333 "found".format(self.conf.get('ca_certs'))) 303 "found".format(self.conf.get('ca_certs')))
334 304
335 def get_opts(self):
336 return self._OPTS
337
338 def init(self, index='freezer', **kwargs): 305 def init(self, index='freezer', **kwargs):
339 self.index = index 306 self.index = index
340 self.es = elasticsearch.Elasticsearch(**kwargs) 307 self.es = elasticsearch.Elasticsearch(**kwargs)
diff --git a/freezer_api/storage/elasticv2.py b/freezer_api/storage/elasticv2.py
index b254c89..c9c38be 100644
--- a/freezer_api/storage/elasticv2.py
+++ b/freezer_api/storage/elasticv2.py
@@ -341,9 +341,6 @@ class ElasticSearchEngineV2(object):
341 raise Exception("File not found: ca_certs file ({0}) not " 341 raise Exception("File not found: ca_certs file ({0}) not "
342 "found".format(self.conf.get('ca_certs'))) 342 "found".format(self.conf.get('ca_certs')))
343 343
344 def get_opts(self):
345 return self._OPTS
346
347 def init(self, index='freezer', **kwargs): 344 def init(self, index='freezer', **kwargs):
348 self.index = index 345 self.index = index
349 self.es = elasticsearch.Elasticsearch(**kwargs) 346 self.es = elasticsearch.Elasticsearch(**kwargs)
diff --git a/freezer_api/tests/unit/test_elastic.py b/freezer_api/tests/unit/test_elastic.py
index bdb9073..a886873 100644
--- a/freezer_api/tests/unit/test_elastic.py
+++ b/freezer_api/tests/unit/test_elastic.py
@@ -21,11 +21,15 @@ import unittest
21import elasticsearch 21import elasticsearch
22import mock 22import mock
23from mock import patch 23from mock import patch
24from oslo_config import cfg
24 25
25from freezer_api.common import exceptions 26from freezer_api.common import exceptions
27from freezer_api.db.elasticsearch.driver import ElasticSearchDB
26from freezer_api.storage import elastic 28from freezer_api.storage import elastic
27from freezer_api.tests.unit import common 29from freezer_api.tests.unit import common
28 30
31CONF = cfg.CONF
32
29 33
30class TypeManager(unittest.TestCase): 34class TypeManager(unittest.TestCase):
31 def setUp(self): 35 def setUp(self):
@@ -651,13 +655,17 @@ class SessionTypeManager(unittest.TestCase):
651 session_update_doc={'status': 'sleepy'}) 655 session_update_doc={'status': 'sleepy'})
652 656
653 657
654class TestElasticSearchEngine_backup(unittest.TestCase): 658class TestElasticSearchEngine_backup(unittest.TestCase, ElasticSearchDB):
655 @patch('freezer_api.storage.elastic.logging') 659 @patch('freezer_api.storage.elastic.logging')
656 @patch('freezer_api.storage.elastic.elasticsearch') 660 @patch('freezer_api.storage.elastic.elasticsearch')
657 def setUp(self, mock_logging, mock_elasticsearch): 661 def setUp(self, mock_logging, mock_elasticsearch):
662 backend = 'elasticsearch'
663 grp = cfg.OptGroup(backend)
664 CONF.register_group(grp)
665 CONF.register_opts(self._ES_OPTS, group=backend)
658 mock_elasticsearch.Elasticsearch.return_value = mock.Mock() 666 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
659 kwargs = {'hosts': 'http://elasticservaddr:1997'} 667 kwargs = {'hosts': 'http://elasticservaddr:1997'}
660 self.eng = elastic.ElasticSearchEngine(backend='elasticsearch') 668 self.eng = elastic.ElasticSearchEngine(backend=backend)
661 self.eng.init(index='freezer', **kwargs) 669 self.eng.init(index='freezer', **kwargs)
662 self.eng.backup_manager = mock.Mock() 670 self.eng.backup_manager = mock.Mock()
663 671
@@ -758,13 +766,17 @@ class TestElasticSearchEngine_backup(unittest.TestCase):
758 backup_id=common.fake_data_0_backup_id) 766 backup_id=common.fake_data_0_backup_id)
759 767
760 768
761class TestElasticSearchEngine_client(unittest.TestCase): 769class TestElasticSearchEngine_client(unittest.TestCase, ElasticSearchDB):
762 @patch('freezer_api.storage.elastic.logging') 770 @patch('freezer_api.storage.elastic.logging')
763 @patch('freezer_api.storage.elastic.elasticsearch') 771 @patch('freezer_api.storage.elastic.elasticsearch')
764 def setUp(self, mock_logging, mock_elasticsearch): 772 def setUp(self, mock_logging, mock_elasticsearch):
773 backend = 'elasticsearch'
774 grp = cfg.OptGroup(backend)
775 CONF.register_group(grp)
776 CONF.register_opts(self._ES_OPTS, group=backend)
765 mock_elasticsearch.Elasticsearch.return_value = mock.Mock() 777 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
766 kwargs = {'hosts': 'http://elasticservaddr:1997'} 778 kwargs = {'hosts': 'http://elasticservaddr:1997'}
767 self.eng = elastic.ElasticSearchEngine(backend="elasticsearch") 779 self.eng = elastic.ElasticSearchEngine(backend=backend)
768 self.eng.init(index='freezer', **kwargs) 780 self.eng.init(index='freezer', **kwargs)
769 self.eng.client_manager = mock.Mock() 781 self.eng.client_manager = mock.Mock()
770 782
@@ -871,13 +883,17 @@ class TestElasticSearchEngine_client(unittest.TestCase):
871 client_id=common.fake_client_info_0['client_id']) 883 client_id=common.fake_client_info_0['client_id'])
872 884
873 885
874class TestElasticSearchEngine_job(unittest.TestCase): 886class TestElasticSearchEngine_job(unittest.TestCase, ElasticSearchDB):
875 @patch('freezer_api.storage.elastic.logging') 887 @patch('freezer_api.storage.elastic.logging')
876 @patch('freezer_api.storage.elastic.elasticsearch') 888 @patch('freezer_api.storage.elastic.elasticsearch')
877 def setUp(self, mock_elasticsearch, mock_logging): 889 def setUp(self, mock_elasticsearch, mock_logging):
890 backend = 'elasticsearch'
891 grp = cfg.OptGroup(backend)
892 CONF.register_group(grp)
893 CONF.register_opts(self._ES_OPTS, group=backend)
878 mock_elasticsearch.Elasticsearch.return_value = mock.Mock() 894 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
879 kwargs = {'hosts': 'http://elasticservaddr:1997'} 895 kwargs = {'hosts': 'http://elasticservaddr:1997'}
880 self.eng = elastic.ElasticSearchEngine(backend="elasticsearch") 896 self.eng = elastic.ElasticSearchEngine(backend=backend)
881 self.eng.init(index='freezer', **kwargs) 897 self.eng.init(index='freezer', **kwargs)
882 self.eng.job_manager = mock.Mock() 898 self.eng.job_manager = mock.Mock()
883 899
@@ -1019,13 +1035,17 @@ class TestElasticSearchEngine_job(unittest.TestCase):
1019 self.assertEqual(3, res) 1035 self.assertEqual(3, res)
1020 1036
1021 1037
1022class TestElasticSearchEngine_action(unittest.TestCase): 1038class TestElasticSearchEngine_action(unittest.TestCase, ElasticSearchDB):
1023 @patch('freezer_api.storage.elastic.logging') 1039 @patch('freezer_api.storage.elastic.logging')
1024 @patch('freezer_api.storage.elastic.elasticsearch') 1040 @patch('freezer_api.storage.elastic.elasticsearch')
1025 def setUp(self, mock_elasticsearch, mock_logging): 1041 def setUp(self, mock_elasticsearch, mock_logging):
1042 backend = 'elasticsearch'
1043 grp = cfg.OptGroup(backend)
1044 CONF.register_group(grp)
1045 CONF.register_opts(self._ES_OPTS, group=backend)
1026 mock_elasticsearch.Elasticsearch.return_value = mock.Mock() 1046 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1027 kwargs = {'hosts': 'http://elasticservaddr:1997'} 1047 kwargs = {'hosts': 'http://elasticservaddr:1997'}
1028 self.eng = elastic.ElasticSearchEngine(backend="elasticsearch") 1048 self.eng = elastic.ElasticSearchEngine(backend=backend)
1029 self.eng.init(index='freezer', **kwargs) 1049 self.eng.init(index='freezer', **kwargs)
1030 self.eng.action_manager = mock.Mock() 1050 self.eng.action_manager = mock.Mock()
1031 1051
@@ -1176,13 +1196,17 @@ class TestElasticSearchEngine_action(unittest.TestCase):
1176 # self.assertEqual(res, 3) 1196 # self.assertEqual(res, 3)
1177 1197
1178 1198
1179class TestElasticSearchEngine_session(unittest.TestCase): 1199class TestElasticSearchEngine_session(unittest.TestCase, ElasticSearchDB):
1180 @patch('freezer_api.storage.elastic.logging') 1200 @patch('freezer_api.storage.elastic.logging')
1181 @patch('freezer_api.storage.elastic.elasticsearch') 1201 @patch('freezer_api.storage.elastic.elasticsearch')
1182 def setUp(self, mock_elasticsearch, mock_logging): 1202 def setUp(self, mock_elasticsearch, mock_logging):
1203 backend = 'elasticsearch'
1204 grp = cfg.OptGroup(backend)
1205 CONF.register_group(grp)
1206 CONF.register_opts(self._ES_OPTS, group=backend)
1183 mock_elasticsearch.Elasticsearch.return_value = mock.Mock() 1207 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1184 kwargs = {'hosts': 'http://elasticservaddr:1997'} 1208 kwargs = {'hosts': 'http://elasticservaddr:1997'}
1185 self.eng = elastic.ElasticSearchEngine(backend="elasticsearch") 1209 self.eng = elastic.ElasticSearchEngine(backend=backend)
1186 self.eng.init(index='freezer', **kwargs) 1210 self.eng.init(index='freezer', **kwargs)
1187 self.eng.session_manager = mock.Mock() 1211 self.eng.session_manager = mock.Mock()
1188 1212
@@ -1333,14 +1357,18 @@ class TestElasticSearchEngine_session(unittest.TestCase):
1333 self.assertEqual(3, res) 1357 self.assertEqual(3, res)
1334 1358
1335 1359
1336class TestElasticSearchEngine(unittest.TestCase): 1360class TestElasticSearchEngine(unittest.TestCase, ElasticSearchDB):
1337 1361
1338 @patch('freezer_api.storage.elastic.logging') 1362 @patch('freezer_api.storage.elastic.logging')
1339 @patch('freezer_api.storage.elastic.elasticsearch') 1363 @patch('freezer_api.storage.elastic.elasticsearch')
1340 def setUp(self, mock_elasticsearch, mock_logging): 1364 def setUp(self, mock_elasticsearch, mock_logging):
1365 backend = 'elasticsearch'
1366 grp = cfg.OptGroup(backend)
1367 CONF.register_group(grp)
1368 CONF.register_opts(self._ES_OPTS, group=backend)
1341 mock_elasticsearch.Elasticsearch.return_value = mock.Mock() 1369 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1342 kwargs = {'hosts': 'http://elasticservaddr:1997'} 1370 kwargs = {'hosts': 'http://elasticservaddr:1997'}
1343 self.eng = elastic.ElasticSearchEngine(backend="elasticsearch") 1371 self.eng = elastic.ElasticSearchEngine(backend=backend)
1344 self.eng.init(index='freezer', **kwargs) 1372 self.eng.init(index='freezer', **kwargs)
1345 1373
1346 def test_raise_validate_opts_when_ca_certs_file_not_exist(self): 1374 def test_raise_validate_opts_when_ca_certs_file_not_exist(self):