Browse Source

Merge "Remove redundant code and improve test coverage"

tags/7.1.0
Zuul 7 months ago
parent
commit
bb48fb398d

+ 0
- 76
freezer_api/storage/driver.py View File

@@ -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()}

+ 0
- 33
freezer_api/storage/elastic.py View File

@@ -280,34 +280,6 @@ class SessionTypeManager(TypeManager):
280 280
 
281 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 283
     def __init__(self, backend):
312 284
         """backend: name of the section in the config file to load
313 285
         elasticsearch opts
@@ -319,8 +291,6 @@ class ElasticSearchEngine(object):
319 291
         self.job_manager = None
320 292
         self.action_manager = None
321 293
         self.session_manager = None
322
-        # register elasticsearch opts
323
-        CONF.register_opts(self._OPTS, group=backend)
324 294
         self.conf = dict(CONF.get(backend))
325 295
         self.backend = backend
326 296
         self._validate_opts()
@@ -332,9 +302,6 @@ class ElasticSearchEngine(object):
332 302
                 raise Exception("File not found: ca_certs file ({0}) not "
333 303
                                 "found".format(self.conf.get('ca_certs')))
334 304
 
335
-    def get_opts(self):
336
-        return self._OPTS
337
-
338 305
     def init(self, index='freezer', **kwargs):
339 306
         self.index = index
340 307
         self.es = elasticsearch.Elasticsearch(**kwargs)

+ 0
- 3
freezer_api/storage/elasticv2.py View File

@@ -341,9 +341,6 @@ class ElasticSearchEngineV2(object):
341 341
                 raise Exception("File not found: ca_certs file ({0}) not "
342 342
                                 "found".format(self.conf.get('ca_certs')))
343 343
 
344
-    def get_opts(self):
345
-        return self._OPTS
346
-
347 344
     def init(self, index='freezer', **kwargs):
348 345
         self.index = index
349 346
         self.es = elasticsearch.Elasticsearch(**kwargs)

+ 40
- 12
freezer_api/tests/unit/test_elastic.py View File

@@ -21,11 +21,15 @@ import unittest
21 21
 import elasticsearch
22 22
 import mock
23 23
 from mock import patch
24
+from oslo_config import cfg
24 25
 
25 26
 from freezer_api.common import exceptions
27
+from freezer_api.db.elasticsearch.driver import ElasticSearchDB
26 28
 from freezer_api.storage import elastic
27 29
 from freezer_api.tests.unit import common
28 30
 
31
+CONF = cfg.CONF
32
+
29 33
 
30 34
 class TypeManager(unittest.TestCase):
31 35
     def setUp(self):
@@ -651,13 +655,17 @@ class SessionTypeManager(unittest.TestCase):
651 655
                           session_update_doc={'status': 'sleepy'})
652 656
 
653 657
 
654
-class TestElasticSearchEngine_backup(unittest.TestCase):
658
+class TestElasticSearchEngine_backup(unittest.TestCase, ElasticSearchDB):
655 659
     @patch('freezer_api.storage.elastic.logging')
656 660
     @patch('freezer_api.storage.elastic.elasticsearch')
657 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 666
         mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
659 667
         kwargs = {'hosts': 'http://elasticservaddr:1997'}
660
-        self.eng = elastic.ElasticSearchEngine(backend='elasticsearch')
668
+        self.eng = elastic.ElasticSearchEngine(backend=backend)
661 669
         self.eng.init(index='freezer', **kwargs)
662 670
         self.eng.backup_manager = mock.Mock()
663 671
 
@@ -758,13 +766,17 @@ class TestElasticSearchEngine_backup(unittest.TestCase):
758 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 770
     @patch('freezer_api.storage.elastic.logging')
763 771
     @patch('freezer_api.storage.elastic.elasticsearch')
764 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 777
         mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
766 778
         kwargs = {'hosts': 'http://elasticservaddr:1997'}
767
-        self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
779
+        self.eng = elastic.ElasticSearchEngine(backend=backend)
768 780
         self.eng.init(index='freezer', **kwargs)
769 781
         self.eng.client_manager = mock.Mock()
770 782
 
@@ -871,13 +883,17 @@ class TestElasticSearchEngine_client(unittest.TestCase):
871 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 887
     @patch('freezer_api.storage.elastic.logging')
876 888
     @patch('freezer_api.storage.elastic.elasticsearch')
877 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 894
         mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
879 895
         kwargs = {'hosts': 'http://elasticservaddr:1997'}
880
-        self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
896
+        self.eng = elastic.ElasticSearchEngine(backend=backend)
881 897
         self.eng.init(index='freezer', **kwargs)
882 898
         self.eng.job_manager = mock.Mock()
883 899
 
@@ -1019,13 +1035,17 @@ class TestElasticSearchEngine_job(unittest.TestCase):
1019 1035
         self.assertEqual(3, res)
1020 1036
 
1021 1037
 
1022
-class TestElasticSearchEngine_action(unittest.TestCase):
1038
+class TestElasticSearchEngine_action(unittest.TestCase, ElasticSearchDB):
1023 1039
     @patch('freezer_api.storage.elastic.logging')
1024 1040
     @patch('freezer_api.storage.elastic.elasticsearch')
1025 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 1046
         mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1027 1047
         kwargs = {'hosts': 'http://elasticservaddr:1997'}
1028
-        self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
1048
+        self.eng = elastic.ElasticSearchEngine(backend=backend)
1029 1049
         self.eng.init(index='freezer', **kwargs)
1030 1050
         self.eng.action_manager = mock.Mock()
1031 1051
 
@@ -1176,13 +1196,17 @@ class TestElasticSearchEngine_action(unittest.TestCase):
1176 1196
     #     self.assertEqual(res, 3)
1177 1197
 
1178 1198
 
1179
-class TestElasticSearchEngine_session(unittest.TestCase):
1199
+class TestElasticSearchEngine_session(unittest.TestCase, ElasticSearchDB):
1180 1200
     @patch('freezer_api.storage.elastic.logging')
1181 1201
     @patch('freezer_api.storage.elastic.elasticsearch')
1182 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 1207
         mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1184 1208
         kwargs = {'hosts': 'http://elasticservaddr:1997'}
1185
-        self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
1209
+        self.eng = elastic.ElasticSearchEngine(backend=backend)
1186 1210
         self.eng.init(index='freezer', **kwargs)
1187 1211
         self.eng.session_manager = mock.Mock()
1188 1212
 
@@ -1333,14 +1357,18 @@ class TestElasticSearchEngine_session(unittest.TestCase):
1333 1357
         self.assertEqual(3, res)
1334 1358
 
1335 1359
 
1336
-class TestElasticSearchEngine(unittest.TestCase):
1360
+class TestElasticSearchEngine(unittest.TestCase, ElasticSearchDB):
1337 1361
 
1338 1362
     @patch('freezer_api.storage.elastic.logging')
1339 1363
     @patch('freezer_api.storage.elastic.elasticsearch')
1340 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 1369
         mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1342 1370
         kwargs = {'hosts': 'http://elasticservaddr:1997'}
1343
-        self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
1371
+        self.eng = elastic.ElasticSearchEngine(backend=backend)
1344 1372
         self.eng.init(index='freezer', **kwargs)
1345 1373
 
1346 1374
     def test_raise_validate_opts_when_ca_certs_file_not_exist(self):

Loading…
Cancel
Save