Skip to content

Commit c050c96

Browse files
author
Alexander Maretskiy
committed
Fix issue #14. Watcher got exception if starting with dead Elastic
1 parent 726671b commit c050c96

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

availability/storage.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,27 @@
2525
NUMBER_OF_SHARDS = 2
2626

2727

28+
class StorageException(Exception):
29+
pass
30+
31+
2832
def get_elasticsearch(check_availability=False):
2933
"""Return Elasticsearch instance.
3034
3135
:param check_availability: check if nodes are available
32-
:returns: Elasticsearch or None on failure
33-
:rtype: elasticsearch.Elasticsearch
36+
:returns: elasticsearch.Elasticsearch
37+
:raises: StorageException
3438
"""
3539
nodes = config.get_config()["backend"]["connection"]
3640
try:
3741
es = elasticsearch.Elasticsearch(nodes)
3842
if check_availability:
3943
es.info()
4044
except Exception as e:
41-
LOG.warning(
42-
"Failed to query Elasticsearch nodes %s: %s"
43-
% (nodes, str(e)))
44-
raise
45+
mesg = ("Failed to query Elasticsearch nodes %s: %s"
46+
% (nodes, str(e)))
47+
LOG.error(mesg)
48+
raise StorageException(mesg)
4549
return es
4650

4751

availability/watcher.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ def main(period=None):
146146
LOG.error("Unexpected backend: %(type)s" % backend)
147147
return 1
148148

149-
if not storage.get_elasticsearch(check_availability=True):
150-
LOG.error("Failed to set up Elasticsearch")
149+
try:
150+
storage.get_elasticsearch(check_availability=True)
151+
except storage.StorageException:
151152
return 1
152153

153154
LOG.info("Start watching with period %s seconds" % period)

tests/unit/test_storage.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,15 @@ def test_ensure_es_index_exists(
6262
@mock.patch("availability.storage.LOG")
6363
def test_get_elasticsearch(self, mock_log, mock_config, mock_elastic):
6464
mock_es = mock.Mock()
65-
mock_es.indices.exists.side_effect = ValueError
6665
mock_elastic.return_value = mock_es
6766
mock_config.get_config.return_value = (
6867
{"backend": {"connection": "nodes"}})
69-
self.assertFalse(mock_es.info.called)
7068
self.assertEqual(mock_es, storage.get_elasticsearch())
69+
self.assertFalse(mock_es.info.called)
7170

72-
mock_es.indices.exists.side_effect = None
7371
mock_elastic.reset_mock()
74-
self.assertEqual(mock_es, storage.get_elasticsearch(
75-
check_availability=True))
72+
mock_es.info.side_effect = ValueError
73+
self.assertRaises(storage.StorageException,
74+
storage.get_elasticsearch, check_availability=True)
7675
mock_elastic.assert_called_once_with("nodes")
7776
mock_es.info.assert_called_once_with()

tests/unit/test_watcher.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mock
1717
import requests
1818

19+
from availability import storage
1920
from availability import watcher
2021
from tests.unit import test
2122

@@ -134,13 +135,13 @@ def test_watch_services(self, mock_log, mock_queue, mock_thread,
134135
self.assertEqual(calls, mock_thread.mock_calls)
135136

136137
@mock.patch("availability.watcher.schedule")
137-
@mock.patch("availability.watcher.storage")
138+
@mock.patch("availability.watcher.storage.get_elasticsearch")
138139
@mock.patch("availability.watcher.watch_services")
139140
@mock.patch("availability.watcher.config.get_config")
140141
@mock.patch("availability.watcher.time")
141142
@mock.patch("availability.watcher.LOG")
142143
def test_main(self, mock_log, mock_time, mock_get_config,
143-
mock_watch_services, mock_storage, mock_schedule):
144+
mock_watch_services, mock_get_elastic, mock_schedule):
144145
class BreakInfinityCicle(Exception):
145146
pass
146147

@@ -166,19 +167,18 @@ class BreakInfinityCicle(Exception):
166167
backend = {"type": "elastic", "connection": "foo_conn"}
167168
mock_get_config.return_value = {"regions": ["foo_region"],
168169
"backend": backend}
169-
mock_storage.get_elasticsearch.return_value = None
170+
mock_get_elastic.side_effect = storage.StorageException
170171
self.assertEqual(1, watcher.main())
171-
mock_storage.get_elasticsearch.assert_called_once_with(
172+
mock_get_elastic.assert_called_once_with(
172173
check_availability=True)
173174

174-
mock_storage.reset_mock()
175-
mock_storage.get_elasticsearch.return_value = mock.Mock()
175+
mock_get_elastic.reset_mock()
176+
mock_get_elastic.side_effect = None
176177

177178
self.assertRaises(BreakInfinityCicle, watcher.main)
178179
self.assertEqual([mock.call(1)] * 4, mock_time.sleep.mock_calls)
179180
mock_schedule.every.assert_called_once_with(60)
180-
mock_storage.get_elasticsearch.assert_called_once_with(
181-
check_availability=True)
181+
mock_get_elastic.assert_called_once_with(check_availability=True)
182182

183183
mock_get_config.return_value = {"regions": ["foo_region"],
184184
"backend": backend, "period": 42}

0 commit comments

Comments
 (0)