Add some tests for splunk
This also fixes issue #1214, and I think another bug. The splunk code will just take the value of state.state and try to serialize it to json if it can't make it into a number. It did this before I generalized that code. Since json.dumps() will fail on most anything complicated, I think the right thing to do is *not* try to do that.pull/1209/head
parent
76df759f4c
commit
0a904acd4d
|
@ -50,7 +50,7 @@ def setup(hass, config):
|
||||||
uri_scheme = "https://"
|
uri_scheme = "https://"
|
||||||
else:
|
else:
|
||||||
uri_scheme = "http://"
|
uri_scheme = "http://"
|
||||||
event_collector = uri_scheme + host + ":" + port + \
|
event_collector = uri_scheme + host + ":" + str(port) + \
|
||||||
"/services/collector/event"
|
"/services/collector/event"
|
||||||
headers = {'Authorization': 'Splunk ' + token}
|
headers = {'Authorization': 'Splunk ' + token}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ def setup(hass, config):
|
||||||
try:
|
try:
|
||||||
_state = state_helper.state_as_number(state)
|
_state = state_helper.state_as_number(state)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_state = state.state
|
return
|
||||||
|
|
||||||
json_body = [
|
json_body = [
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,93 @@
|
||||||
|
"""
|
||||||
|
tests.components.test_splunk
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests splunk component.
|
||||||
|
"""
|
||||||
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
import homeassistant.components.splunk as splunk
|
||||||
|
from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
|
||||||
|
|
||||||
|
|
||||||
|
class TestSplunk(unittest.TestCase):
|
||||||
|
def test_setup_config_full(self):
|
||||||
|
config = {
|
||||||
|
'splunk': {
|
||||||
|
'host': 'host',
|
||||||
|
'port': 123,
|
||||||
|
'token': 'secret',
|
||||||
|
'use_ssl': 'False',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hass = mock.MagicMock()
|
||||||
|
self.assertTrue(splunk.setup(hass, config))
|
||||||
|
self.assertTrue(hass.bus.listen.called)
|
||||||
|
self.assertEqual(EVENT_STATE_CHANGED,
|
||||||
|
hass.bus.listen.call_args_list[0][0][0])
|
||||||
|
|
||||||
|
def test_setup_config_defaults(self):
|
||||||
|
config = {
|
||||||
|
'splunk': {
|
||||||
|
'host': 'host',
|
||||||
|
'token': 'secret',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hass = mock.MagicMock()
|
||||||
|
self.assertTrue(splunk.setup(hass, config))
|
||||||
|
self.assertTrue(hass.bus.listen.called)
|
||||||
|
self.assertEqual(EVENT_STATE_CHANGED,
|
||||||
|
hass.bus.listen.call_args_list[0][0][0])
|
||||||
|
|
||||||
|
|
||||||
|
def _setup(self, mock_requests):
|
||||||
|
self.mock_post = mock_requests.post
|
||||||
|
self.mock_request_exception = Exception
|
||||||
|
mock_requests.exceptions.RequestException = self.mock_request_exception
|
||||||
|
config = {
|
||||||
|
'splunk': {
|
||||||
|
'host': 'host',
|
||||||
|
'token': 'secret',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.hass = mock.MagicMock()
|
||||||
|
splunk.setup(self.hass, config)
|
||||||
|
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
||||||
|
|
||||||
|
@mock.patch.object(splunk, 'requests')
|
||||||
|
@mock.patch('json.dumps')
|
||||||
|
def test_event_listener(self, mock_dump, mock_requests):
|
||||||
|
mock_dump.side_effect = lambda x: x
|
||||||
|
self._setup(mock_requests)
|
||||||
|
|
||||||
|
valid = {'1': 1,
|
||||||
|
'1.0': 1.0,
|
||||||
|
STATE_ON: 1,
|
||||||
|
STATE_OFF: 0}
|
||||||
|
for in_, out in valid.items():
|
||||||
|
state = mock.MagicMock(state=in_,
|
||||||
|
domain='fake',
|
||||||
|
object_id='entity',
|
||||||
|
attributes={})
|
||||||
|
event = mock.MagicMock(data={'new_state': state},
|
||||||
|
time_fired=12345)
|
||||||
|
body = [{
|
||||||
|
'domain': 'fake',
|
||||||
|
'entity_id': 'entity',
|
||||||
|
'attributes': {},
|
||||||
|
'time': '12345',
|
||||||
|
'value': out,
|
||||||
|
}]
|
||||||
|
payload = {'host': 'http://host:8088/services/collector/event',
|
||||||
|
'event': body}
|
||||||
|
self.handler_method(event)
|
||||||
|
self.mock_post.assert_called_once_with(
|
||||||
|
payload['host'], data=payload,
|
||||||
|
headers={'Authorization': 'Splunk secret'})
|
||||||
|
self.mock_post.reset_mock()
|
||||||
|
|
||||||
|
for invalid in ('foo', '', object):
|
||||||
|
state = mock.MagicMock(state=invalid)
|
||||||
|
self.handler_method(mock.MagicMock(data={'new_state': state}))
|
||||||
|
self.assertFalse(self.mock_post.called)
|
Loading…
Reference in New Issue