Catch socket.gaierror in graphite driver

If you specify a name that can't be looked up in DNS, socket.connect()
throws socket.gaierror. We should catch and log that situation properly.
This commit is contained in:
Dan Smith 2016-02-14 15:39:24 -08:00
parent 64430f26f3
commit 366595fd90
2 changed files with 12 additions and 0 deletions

View file

@ -98,6 +98,8 @@ class GraphiteFeeder(threading.Thread):
_LOGGER.debug('Sending to graphite: %s', lines)
try:
self._send_to_graphite('\n'.join(lines))
except socket.gaierror:
_LOGGER.error('Unable to connect to host %s', self._host)
except socket.error:
_LOGGER.exception('Failed to send data to graphite')

View file

@ -132,6 +132,16 @@ class TestGraphite(unittest.TestCase):
actual = mock_send.call_args_list[0][0][0].split('\n')
self.assertEqual(sorted(expected), sorted(actual))
@mock.patch('time.time')
def test_send_to_graphite_errors(self, mock_time):
mock_time.return_value = 12345
state = ha.State('domain.entity', STATE_ON, {'foo': 1.0})
with mock.patch.object(self.gf, '_send_to_graphite') as mock_send:
mock_send.side_effect = socket.error
self.gf._report_attributes('entity', state)
mock_send.side_effect = socket.gaierror
self.gf._report_attributes('entity', state)
@mock.patch('socket.socket')
def test_send_to_graphite(self, mock_socket):
self.gf._send_to_graphite('foo')