Browse Source

Delete invalid codes and Add a unit test for elastic

1.In _validate_opts:
The following code is True forever, We will delete it.
2. Add unit test for ca_certs in _validate_opts.

Change-Id: I25a9a4aaae6fd5677d07d376f66188dc0b92bc3b
gengchc2 3 months ago
parent
commit
df5cca26bb

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

@@ -327,10 +327,6 @@ class ElasticSearchEngine(object):
327 327
         self.init(**self.conf)
328 328
 
329 329
     def _validate_opts(self):
330
-        if not 'hosts' or 'endpoint' in self.conf.keys():
331
-            raise ValueError("Couldn't find hosts in {0} section".format(
332
-                self.backend)
333
-            )
334 330
         if self.conf.get('ca_certs'):
335 331
             if not os.path.isfile(self.conf.get('ca_certs')):
336 332
                 raise Exception("File not found: ca_certs file ({0}) not "

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

@@ -336,10 +336,6 @@ class ElasticSearchEngineV2(object):
336 336
         self.init(**self.conf)
337 337
 
338 338
     def _validate_opts(self):
339
-        if not 'hosts' or 'endpoint' in self.conf.keys():
340
-            raise ValueError("Couldn't find hosts in {0} section".format(
341
-                self.backend)
342
-            )
343 339
         if self.conf.get('ca_certs'):
344 340
             if not os.path.isfile(self.conf.get('ca_certs')):
345 341
                 raise Exception("File not found: ca_certs file ({0}) not "

+ 16
- 0
freezer_api/tests/unit/test_elastic.py View File

@@ -1331,3 +1331,19 @@ class TestElasticSearchEngine_session(unittest.TestCase):
1331 1331
             session_id=common.fake_session_0['session_id'],
1332 1332
             doc=common.get_fake_session_0())
1333 1333
         self.assertEqual(3, res)
1334
+
1335
+
1336
+class TestElasticSearchEngine(unittest.TestCase):
1337
+
1338
+    @patch('freezer_api.storage.elastic.logging')
1339
+    @patch('freezer_api.storage.elastic.elasticsearch')
1340
+    def setUp(self, mock_elasticsearch, mock_logging):
1341
+        mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1342
+        kwargs = {'hosts': 'http://elasticservaddr:1997'}
1343
+        self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
1344
+        self.eng.init(index='freezer', **kwargs)
1345
+
1346
+    def test_raise_validate_opts_when_ca_certs_file_not_exist(self):
1347
+        self.eng.conf.update({'ca_certs': 'invalid_ca_certs_file'})
1348
+        self.assertRaises(Exception,
1349
+                          self.eng._validate_opts)

+ 20
- 0
freezer_api/tests/unit/test_elasticv2.py View File

@@ -1499,3 +1499,23 @@ class TestElasticSearchEngine_session(unittest.TestCase, ElasticSearchDB):
1499 1499
             session_id=common.fake_session_0['session_id'],
1500 1500
             doc=common.get_fake_session_0())
1501 1501
         self.assertEqual(3, res)
1502
+
1503
+
1504
+class TestElasticSearchEngineV2(unittest.TestCase, ElasticSearchDB):
1505
+
1506
+    @patch('freezer_api.storage.elasticv2.logging')
1507
+    @patch('freezer_api.storage.elasticv2.elasticsearch')
1508
+    def setUp(self, mock_logging, mock_elasticsearch):
1509
+        backend = 'elasticsearch'
1510
+        grp = cfg.OptGroup(backend)
1511
+        CONF.register_group(grp)
1512
+        CONF.register_opts(self._ES_OPTS, group=backend)
1513
+        mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1514
+        kwargs = {'hosts': 'http://elasticservaddr:1997'}
1515
+        self.eng = elastic.ElasticSearchEngineV2(backend=backend)
1516
+        self.eng.init(index='freezer', **kwargs)
1517
+
1518
+    def test_raise_validate_opts_when_ca_certs_file_not_exist(self):
1519
+        self.eng.conf.update({'ca_certs': 'invalid_ca_certs_file'})
1520
+        self.assertRaises(Exception,
1521
+                          self.eng._validate_opts)

Loading…
Cancel
Save