diff options
author | gengchc2 <geng.changcai2@zte.com.cn> | 2018-12-29 00:25:10 -0800 |
---|---|---|
committer | gengchc2 <geng.changcai2@zte.com.cn> | 2019-01-08 05:29:23 +0000 |
commit | 857c239e97483c7212d4672a31e4f58462d22edd (patch) | |
tree | 0bb464c3fe3ebf4a20217a7a76e79b34963a7646 | |
parent | df5cca26bb3a100047da354142a8c97c156578cb (diff) |
Remove redundant code and improve test coverage
storage/driver.py is redundant file
get_opts in elasticsearchv2.py is redundant
Change-Id: I2c6d892016508c21fbc782d1628136618342c21f
Notes
Notes (review):
Code-Review+2: gecong <ge.cong@zte.com.cn>
Workflow+1: gecong <ge.cong@zte.com.cn>
Verified+2: Zuul
Submitted-by: Zuul
Submitted-at: Wed, 09 Jan 2019 09:03:10 +0000
Reviewed-on: https://review.openstack.org/627769
Project: openstack/freezer-api
Branch: refs/heads/master
-rw-r--r-- | freezer_api/storage/driver.py | 76 | ||||
-rw-r--r-- | freezer_api/storage/elastic.py | 33 | ||||
-rw-r--r-- | freezer_api/storage/elasticv2.py | 3 | ||||
-rw-r--r-- | freezer_api/tests/unit/test_elastic.py | 52 |
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 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | |||
16 | """ | ||
17 | |||
18 | from freezer_api.storage import elastic | ||
19 | |||
20 | from oslo_config import cfg | ||
21 | from oslo_log import log | ||
22 | from oslo_utils import importutils | ||
23 | |||
24 | |||
25 | CONF = cfg.CONF | ||
26 | LOG = 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 | |||
43 | def 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 | |||
51 | def 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 | |||
64 | def 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 | |||
71 | def _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 | ||
281 | class ElasticSearchEngine(object): | 281 | class 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 | |||
21 | import elasticsearch | 21 | import elasticsearch |
22 | import mock | 22 | import mock |
23 | from mock import patch | 23 | from mock import patch |
24 | from oslo_config import cfg | ||
24 | 25 | ||
25 | from freezer_api.common import exceptions | 26 | from freezer_api.common import exceptions |
27 | from freezer_api.db.elasticsearch.driver import ElasticSearchDB | ||
26 | from freezer_api.storage import elastic | 28 | from freezer_api.storage import elastic |
27 | from freezer_api.tests.unit import common | 29 | from freezer_api.tests.unit import common |
28 | 30 | ||
31 | CONF = cfg.CONF | ||
32 | |||
29 | 33 | ||
30 | class TypeManager(unittest.TestCase): | 34 | class 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 | ||
654 | class TestElasticSearchEngine_backup(unittest.TestCase): | 658 | class 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 | ||
761 | class TestElasticSearchEngine_client(unittest.TestCase): | 769 | class 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 | ||
874 | class TestElasticSearchEngine_job(unittest.TestCase): | 886 | class 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 | ||
1022 | class TestElasticSearchEngine_action(unittest.TestCase): | 1038 | class 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 | ||
1179 | class TestElasticSearchEngine_session(unittest.TestCase): | 1199 | class 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 | ||
1336 | class TestElasticSearchEngine(unittest.TestCase): | 1360 | class 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): |