2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the StatsD feeder."""
|
2016-02-12 01:04:28 +00:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
2016-09-14 01:22:30 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-02-13 08:08:32 +00:00
|
|
|
import homeassistant.core as ha
|
2016-02-12 01:04:28 +00:00
|
|
|
import homeassistant.components.statsd as statsd
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED
|
2016-02-12 01:04:28 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
2018-10-24 10:10:05 +00:00
|
|
|
import pytest
|
2016-10-27 07:16:23 +00:00
|
|
|
|
2016-02-12 01:04:28 +00:00
|
|
|
|
|
|
|
class TestStatsd(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the StatsD component."""
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-31 15:47:29 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-10-27 07:16:23 +00:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
2016-09-14 01:22:30 +00:00
|
|
|
def test_invalid_config(self):
|
|
|
|
"""Test configuration with defaults."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"statsd": {"host1": "host1"}}
|
2016-09-14 01:22:30 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
with pytest.raises(vol.Invalid):
|
2016-09-14 01:22:30 +00:00
|
|
|
statsd.CONFIG_SCHEMA(None)
|
2018-10-24 10:10:05 +00:00
|
|
|
with pytest.raises(vol.Invalid):
|
2016-09-14 01:22:30 +00:00
|
|
|
statsd.CONFIG_SCHEMA(config)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@mock.patch("statsd.StatsClient")
|
2016-07-03 22:21:18 +00:00
|
|
|
def test_statsd_setup_full(self, mock_connection):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test setup with all data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"statsd": {"host": "host", "port": 123, "rate": 1, "prefix": "foo"}}
|
2016-10-27 07:16:23 +00:00
|
|
|
self.hass.bus.listen = mock.MagicMock()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup_component(self.hass, statsd.DOMAIN, config)
|
|
|
|
assert mock_connection.call_count == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_connection.call_args == mock.call(
|
|
|
|
host="host", port=123, prefix="foo"
|
|
|
|
)
|
2016-07-03 22:21:18 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.hass.bus.listen.called
|
2019-07-31 19:25:30 +00:00
|
|
|
assert EVENT_STATE_CHANGED == self.hass.bus.listen.call_args_list[0][0][0]
|
2016-02-12 01:04:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@mock.patch("statsd.StatsClient")
|
2016-07-03 22:21:18 +00:00
|
|
|
def test_statsd_setup_defaults(self, mock_connection):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test setup with defaults."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"statsd": {"host": "host"}}
|
2016-09-14 01:22:30 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config["statsd"][statsd.CONF_PORT] = statsd.DEFAULT_PORT
|
|
|
|
config["statsd"][statsd.CONF_PREFIX] = statsd.DEFAULT_PREFIX
|
2016-09-14 01:22:30 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
self.hass.bus.listen = mock.MagicMock()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup_component(self.hass, statsd.DOMAIN, config)
|
|
|
|
assert mock_connection.call_count == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_connection.call_args == mock.call(
|
|
|
|
host="host", port=8125, prefix="hass"
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.hass.bus.listen.called
|
2016-02-12 01:04:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@mock.patch("statsd.StatsClient")
|
2016-07-08 06:09:02 +00:00
|
|
|
def test_event_listener_defaults(self, mock_client):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test event listener."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"statsd": {"host": "host", "value_mapping": {"custom": 3}}}
|
2016-09-14 01:22:30 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config["statsd"][statsd.CONF_RATE] = statsd.DEFAULT_RATE
|
2016-09-14 01:22:30 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
self.hass.bus.listen = mock.MagicMock()
|
|
|
|
setup_component(self.hass, statsd.DOMAIN, config)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.hass.bus.listen.called
|
2016-10-27 07:16:23 +00:00
|
|
|
handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
2016-02-12 01:04:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
valid = {"1": 1, "1.0": 1.0, "custom": 3, STATE_ON: 1, STATE_OFF: 0}
|
2016-02-12 01:04:28 +00:00
|
|
|
for in_, out in valid.items():
|
2019-07-31 19:25:30 +00:00
|
|
|
state = mock.MagicMock(state=in_, attributes={"attribute key": 3.2})
|
|
|
|
handler_method(mock.MagicMock(data={"new_state": state}))
|
|
|
|
mock_client.return_value.gauge.assert_has_calls(
|
|
|
|
[mock.call(state.entity_id, out, statsd.DEFAULT_RATE)]
|
|
|
|
)
|
2016-07-08 06:09:02 +00:00
|
|
|
|
|
|
|
mock_client.return_value.gauge.reset_mock()
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert mock_client.return_value.incr.call_count == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.return_value.incr.call_args == mock.call(
|
|
|
|
state.entity_id, rate=statsd.DEFAULT_RATE
|
|
|
|
)
|
2016-07-08 06:09:02 +00:00
|
|
|
mock_client.return_value.incr.reset_mock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for invalid in ("foo", "", object):
|
|
|
|
handler_method(
|
|
|
|
mock.MagicMock(data={"new_state": ha.State("domain.test", invalid, {})})
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not mock_client.return_value.gauge.called
|
|
|
|
assert mock_client.return_value.incr.called
|
2016-07-08 06:09:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@mock.patch("statsd.StatsClient")
|
2016-07-08 06:09:02 +00:00
|
|
|
def test_event_listener_attr_details(self, mock_client):
|
|
|
|
"""Test event listener."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"statsd": {"host": "host", "log_attributes": True}}
|
2016-09-14 01:22:30 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config["statsd"][statsd.CONF_RATE] = statsd.DEFAULT_RATE
|
2016-09-14 01:22:30 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
self.hass.bus.listen = mock.MagicMock()
|
|
|
|
setup_component(self.hass, statsd.DOMAIN, config)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.hass.bus.listen.called
|
2016-10-27 07:16:23 +00:00
|
|
|
handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
2016-07-08 06:09:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
valid = {"1": 1, "1.0": 1.0, STATE_ON: 1, STATE_OFF: 0}
|
2016-07-08 06:09:02 +00:00
|
|
|
for in_, out in valid.items():
|
2019-07-31 19:25:30 +00:00
|
|
|
state = mock.MagicMock(state=in_, attributes={"attribute key": 3.2})
|
|
|
|
handler_method(mock.MagicMock(data={"new_state": state}))
|
|
|
|
mock_client.return_value.gauge.assert_has_calls(
|
|
|
|
[
|
|
|
|
mock.call("%s.state" % state.entity_id, out, statsd.DEFAULT_RATE),
|
|
|
|
mock.call(
|
|
|
|
"%s.attribute_key" % state.entity_id, 3.2, statsd.DEFAULT_RATE
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2016-07-08 06:09:02 +00:00
|
|
|
|
2016-07-03 22:21:18 +00:00
|
|
|
mock_client.return_value.gauge.reset_mock()
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert mock_client.return_value.incr.call_count == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.return_value.incr.call_args == mock.call(
|
|
|
|
state.entity_id, rate=statsd.DEFAULT_RATE
|
|
|
|
)
|
2016-07-03 22:21:18 +00:00
|
|
|
mock_client.return_value.incr.reset_mock()
|
2016-02-12 01:04:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for invalid in ("foo", "", object):
|
|
|
|
handler_method(
|
|
|
|
mock.MagicMock(data={"new_state": ha.State("domain.test", invalid, {})})
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not mock_client.return_value.gauge.called
|
|
|
|
assert mock_client.return_value.incr.called
|