core/tests/components/fail2ban/test_sensor.py

221 lines
8.7 KiB
Python
Raw Normal View History

"""The tests for local file sensor platform."""
import unittest
from unittest.mock import Mock, patch
from mock_open import MockOpen
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.fail2ban.sensor import (
2019-07-31 19:25:30 +00:00
STATE_ALL_BANS,
STATE_CURRENT_BANS,
BanLogParser,
BanSensor,
)
from homeassistant.setup import setup_component
from tests.common import assert_setup_component, get_test_home_assistant
def fake_log(log_key):
"""Return a fake fail2ban log."""
fake_log_dict = {
2019-07-31 19:25:30 +00:00
"single_ban": (
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 111.111.111.111"
),
2019-07-31 19:25:30 +00:00
"ipv6_ban": (
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 2607:f0d0:1002:51::4"
),
2019-07-31 19:25:30 +00:00
"multi_ban": (
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 111.111.111.111\n"
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 222.222.222.222"
),
2019-07-31 19:25:30 +00:00
"multi_jail": (
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 111.111.111.111\n"
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_two] Ban 222.222.222.222"
),
"unban_all": (
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 111.111.111.111\n"
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Unban 111.111.111.111\n"
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 222.222.222.222\n"
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Unban 222.222.222.222"
),
"unban_one": (
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 111.111.111.111\n"
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Ban 222.222.222.222\n"
"2017-01-01 12:23:35 fail2ban.actions [111]: "
"NOTICE [jail_one] Unban 111.111.111.111"
),
}
return fake_log_dict[log_key]
class TestBanSensor(unittest.TestCase):
"""Test the fail2ban sensor."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
2019-07-31 19:25:30 +00:00
@patch("os.path.isfile", Mock(return_value=True))
def test_setup(self):
"""Test that sensor can be setup."""
2019-07-31 19:25:30 +00:00
config = {"sensor": {"platform": "fail2ban", "jails": ["jail_one"]}}
mock_fh = MockOpen()
2019-07-31 19:25:30 +00:00
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
assert setup_component(self.hass, "sensor", config)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert_setup_component(1, "sensor")
2019-07-31 19:25:30 +00:00
@patch("os.path.isfile", Mock(return_value=True))
def test_multi_jails(self):
"""Test that multiple jails can be set up as sensors.."""
2019-07-31 19:25:30 +00:00
config = {"sensor": {"platform": "fail2ban", "jails": ["jail_one", "jail_two"]}}
mock_fh = MockOpen()
2019-07-31 19:25:30 +00:00
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
assert setup_component(self.hass, "sensor", config)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert_setup_component(2, "sensor")
def test_single_ban(self):
"""Test that log is parsed correctly for single ban."""
log_parser = BanLogParser("/test/fail2ban.log")
2019-07-31 19:25:30 +00:00
sensor = BanSensor("fail2ban", "jail_one", log_parser)
assert sensor.name == "fail2ban jail_one"
mock_fh = MockOpen(read_data=fake_log("single_ban"))
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
sensor.update()
2019-07-31 19:25:30 +00:00
assert sensor.state == "111.111.111.111"
assert sensor.state_attributes[STATE_CURRENT_BANS] == ["111.111.111.111"]
assert sensor.state_attributes[STATE_ALL_BANS] == ["111.111.111.111"]
def test_ipv6_ban(self):
"""Test that log is parsed correctly for IPV6 bans."""
log_parser = BanLogParser("/test/fail2ban.log")
2019-07-31 19:25:30 +00:00
sensor = BanSensor("fail2ban", "jail_one", log_parser)
assert sensor.name == "fail2ban jail_one"
mock_fh = MockOpen(read_data=fake_log("ipv6_ban"))
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
sensor.update()
2019-07-31 19:25:30 +00:00
assert sensor.state == "2607:f0d0:1002:51::4"
assert sensor.state_attributes[STATE_CURRENT_BANS] == ["2607:f0d0:1002:51::4"]
assert sensor.state_attributes[STATE_ALL_BANS] == ["2607:f0d0:1002:51::4"]
def test_multiple_ban(self):
"""Test that log is parsed correctly for multiple ban."""
log_parser = BanLogParser("/test/fail2ban.log")
2019-07-31 19:25:30 +00:00
sensor = BanSensor("fail2ban", "jail_one", log_parser)
assert sensor.name == "fail2ban jail_one"
mock_fh = MockOpen(read_data=fake_log("multi_ban"))
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
sensor.update()
2019-07-31 19:25:30 +00:00
assert sensor.state == "222.222.222.222"
assert sensor.state_attributes[STATE_CURRENT_BANS] == [
"111.111.111.111",
"222.222.222.222",
]
assert sensor.state_attributes[STATE_ALL_BANS] == [
"111.111.111.111",
"222.222.222.222",
]
def test_unban_all(self):
"""Test that log is parsed correctly when unbanning."""
log_parser = BanLogParser("/test/fail2ban.log")
2019-07-31 19:25:30 +00:00
sensor = BanSensor("fail2ban", "jail_one", log_parser)
assert sensor.name == "fail2ban jail_one"
mock_fh = MockOpen(read_data=fake_log("unban_all"))
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
sensor.update()
2019-07-31 19:25:30 +00:00
assert sensor.state == "None"
assert sensor.state_attributes[STATE_CURRENT_BANS] == []
2019-07-31 19:25:30 +00:00
assert sensor.state_attributes[STATE_ALL_BANS] == [
"111.111.111.111",
"222.222.222.222",
]
def test_unban_one(self):
"""Test that log is parsed correctly when unbanning one ip."""
log_parser = BanLogParser("/test/fail2ban.log")
2019-07-31 19:25:30 +00:00
sensor = BanSensor("fail2ban", "jail_one", log_parser)
assert sensor.name == "fail2ban jail_one"
mock_fh = MockOpen(read_data=fake_log("unban_one"))
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
sensor.update()
2019-07-31 19:25:30 +00:00
assert sensor.state == "222.222.222.222"
assert sensor.state_attributes[STATE_CURRENT_BANS] == ["222.222.222.222"]
assert sensor.state_attributes[STATE_ALL_BANS] == [
"111.111.111.111",
"222.222.222.222",
]
def test_multi_jail(self):
"""Test that log is parsed correctly when using multiple jails."""
log_parser = BanLogParser("/test/fail2ban.log")
2019-07-31 19:25:30 +00:00
sensor1 = BanSensor("fail2ban", "jail_one", log_parser)
sensor2 = BanSensor("fail2ban", "jail_two", log_parser)
assert sensor1.name == "fail2ban jail_one"
assert sensor2.name == "fail2ban jail_two"
mock_fh = MockOpen(read_data=fake_log("multi_jail"))
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
sensor1.update()
sensor2.update()
2019-07-31 19:25:30 +00:00
assert sensor1.state == "111.111.111.111"
assert sensor1.state_attributes[STATE_CURRENT_BANS] == ["111.111.111.111"]
assert sensor1.state_attributes[STATE_ALL_BANS] == ["111.111.111.111"]
assert sensor2.state == "222.222.222.222"
assert sensor2.state_attributes[STATE_CURRENT_BANS] == ["222.222.222.222"]
assert sensor2.state_attributes[STATE_ALL_BANS] == ["222.222.222.222"]
def test_ban_active_after_update(self):
"""Test that ban persists after subsequent update."""
log_parser = BanLogParser("/test/fail2ban.log")
2019-07-31 19:25:30 +00:00
sensor = BanSensor("fail2ban", "jail_one", log_parser)
assert sensor.name == "fail2ban jail_one"
mock_fh = MockOpen(read_data=fake_log("single_ban"))
with patch(
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
):
sensor.update()
2019-07-31 19:25:30 +00:00
assert sensor.state == "111.111.111.111"
sensor.update()
2019-07-31 19:25:30 +00:00
assert sensor.state == "111.111.111.111"
assert sensor.state_attributes[STATE_CURRENT_BANS] == ["111.111.111.111"]
assert sensor.state_attributes[STATE_ALL_BANS] == ["111.111.111.111"]