mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Rewrite fail2ban unittest tests to pytest style test functions (#41606)
This commit is contained in:
parent
2146dd1268
commit
a28f347b2b
@ -1,18 +1,14 @@
|
|||||||
"""The tests for local file sensor platform."""
|
"""The tests for local file sensor platform."""
|
||||||
import unittest
|
|
||||||
|
|
||||||
from mock_open import MockOpen
|
|
||||||
|
|
||||||
from homeassistant.components.fail2ban.sensor import (
|
from homeassistant.components.fail2ban.sensor import (
|
||||||
STATE_ALL_BANS,
|
STATE_ALL_BANS,
|
||||||
STATE_CURRENT_BANS,
|
STATE_CURRENT_BANS,
|
||||||
BanLogParser,
|
BanLogParser,
|
||||||
BanSensor,
|
BanSensor,
|
||||||
)
|
)
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import Mock, patch
|
from tests.async_mock import Mock, mock_open, patch
|
||||||
from tests.common import assert_setup_component, get_test_home_assistant
|
from tests.common import assert_setup_component
|
||||||
|
|
||||||
|
|
||||||
def fake_log(log_key):
|
def fake_log(log_key):
|
||||||
@ -60,158 +56,140 @@ def fake_log(log_key):
|
|||||||
return fake_log_dict[log_key]
|
return fake_log_dict[log_key]
|
||||||
|
|
||||||
|
|
||||||
class TestBanSensor(unittest.TestCase):
|
@patch("os.path.isfile", Mock(return_value=True))
|
||||||
"""Test the fail2ban sensor."""
|
async def test_setup(hass):
|
||||||
|
"""Test that sensor can be setup."""
|
||||||
|
config = {"sensor": {"platform": "fail2ban", "jails": ["jail_one"]}}
|
||||||
|
mock_fh = mock_open()
|
||||||
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert_setup_component(1, "sensor")
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.addCleanup(self.hass.stop)
|
|
||||||
|
|
||||||
@patch("os.path.isfile", Mock(return_value=True))
|
@patch("os.path.isfile", Mock(return_value=True))
|
||||||
def test_setup(self):
|
async def test_multi_jails(hass):
|
||||||
"""Test that sensor can be setup."""
|
"""Test that multiple jails can be set up as sensors.."""
|
||||||
config = {"sensor": {"platform": "fail2ban", "jails": ["jail_one"]}}
|
config = {"sensor": {"platform": "fail2ban", "jails": ["jail_one", "jail_two"]}}
|
||||||
mock_fh = MockOpen()
|
mock_fh = mock_open()
|
||||||
with patch(
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
):
|
await hass.async_block_till_done()
|
||||||
assert setup_component(self.hass, "sensor", config)
|
assert_setup_component(2, "sensor")
|
||||||
self.hass.block_till_done()
|
|
||||||
assert_setup_component(1, "sensor")
|
|
||||||
|
|
||||||
@patch("os.path.isfile", Mock(return_value=True))
|
|
||||||
def test_multi_jails(self):
|
|
||||||
"""Test that multiple jails can be set up as sensors.."""
|
|
||||||
config = {"sensor": {"platform": "fail2ban", "jails": ["jail_one", "jail_two"]}}
|
|
||||||
mock_fh = MockOpen()
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
|
|
||||||
):
|
|
||||||
assert setup_component(self.hass, "sensor", config)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
assert_setup_component(2, "sensor")
|
|
||||||
|
|
||||||
def test_single_ban(self):
|
async def test_single_ban(hass):
|
||||||
"""Test that log is parsed correctly for single ban."""
|
"""Test that log is parsed correctly for single ban."""
|
||||||
log_parser = BanLogParser("/test/fail2ban.log")
|
log_parser = BanLogParser("/test/fail2ban.log")
|
||||||
sensor = BanSensor("fail2ban", "jail_one", log_parser)
|
sensor = BanSensor("fail2ban", "jail_one", log_parser)
|
||||||
assert sensor.name == "fail2ban jail_one"
|
assert sensor.name == "fail2ban jail_one"
|
||||||
mock_fh = MockOpen(read_data=fake_log("single_ban"))
|
mock_fh = mock_open(read_data=fake_log("single_ban"))
|
||||||
with patch(
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
"homeassistant.components.fail2ban.sensor.open", mock_fh, create=True
|
sensor.update()
|
||||||
):
|
|
||||||
sensor.update()
|
|
||||||
|
|
||||||
|
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"]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_ipv6_ban(hass):
|
||||||
|
"""Test that log is parsed correctly for IPV6 bans."""
|
||||||
|
log_parser = BanLogParser("/test/fail2ban.log")
|
||||||
|
sensor = BanSensor("fail2ban", "jail_one", log_parser)
|
||||||
|
assert sensor.name == "fail2ban jail_one"
|
||||||
|
mock_fh = mock_open(read_data=fake_log("ipv6_ban"))
|
||||||
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
|
sensor.update()
|
||||||
|
|
||||||
|
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"]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_multiple_ban(hass):
|
||||||
|
"""Test that log is parsed correctly for multiple ban."""
|
||||||
|
log_parser = BanLogParser("/test/fail2ban.log")
|
||||||
|
sensor = BanSensor("fail2ban", "jail_one", log_parser)
|
||||||
|
assert sensor.name == "fail2ban jail_one"
|
||||||
|
mock_fh = mock_open(read_data=fake_log("multi_ban"))
|
||||||
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
|
sensor.update()
|
||||||
|
|
||||||
|
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",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_unban_all(hass):
|
||||||
|
"""Test that log is parsed correctly when unbanning."""
|
||||||
|
log_parser = BanLogParser("/test/fail2ban.log")
|
||||||
|
sensor = BanSensor("fail2ban", "jail_one", log_parser)
|
||||||
|
assert sensor.name == "fail2ban jail_one"
|
||||||
|
mock_fh = mock_open(read_data=fake_log("unban_all"))
|
||||||
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
|
sensor.update()
|
||||||
|
|
||||||
|
assert sensor.state == "None"
|
||||||
|
assert sensor.state_attributes[STATE_CURRENT_BANS] == []
|
||||||
|
assert sensor.state_attributes[STATE_ALL_BANS] == [
|
||||||
|
"111.111.111.111",
|
||||||
|
"222.222.222.222",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_unban_one(hass):
|
||||||
|
"""Test that log is parsed correctly when unbanning one ip."""
|
||||||
|
log_parser = BanLogParser("/test/fail2ban.log")
|
||||||
|
sensor = BanSensor("fail2ban", "jail_one", log_parser)
|
||||||
|
assert sensor.name == "fail2ban jail_one"
|
||||||
|
mock_fh = mock_open(read_data=fake_log("unban_one"))
|
||||||
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
|
sensor.update()
|
||||||
|
|
||||||
|
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",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_multi_jail(hass):
|
||||||
|
"""Test that log is parsed correctly when using multiple jails."""
|
||||||
|
log_parser = BanLogParser("/test/fail2ban.log")
|
||||||
|
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 = mock_open(read_data=fake_log("multi_jail"))
|
||||||
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
|
sensor1.update()
|
||||||
|
sensor2.update()
|
||||||
|
|
||||||
|
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"]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_ban_active_after_update(hass):
|
||||||
|
"""Test that ban persists after subsequent update."""
|
||||||
|
log_parser = BanLogParser("/test/fail2ban.log")
|
||||||
|
sensor = BanSensor("fail2ban", "jail_one", log_parser)
|
||||||
|
assert sensor.name == "fail2ban jail_one"
|
||||||
|
mock_fh = mock_open(read_data=fake_log("single_ban"))
|
||||||
|
with patch("homeassistant.components.fail2ban.sensor.open", mock_fh, create=True):
|
||||||
|
sensor.update()
|
||||||
assert sensor.state == "111.111.111.111"
|
assert sensor.state == "111.111.111.111"
|
||||||
assert sensor.state_attributes[STATE_CURRENT_BANS] == ["111.111.111.111"]
|
sensor.update()
|
||||||
assert sensor.state_attributes[STATE_ALL_BANS] == ["111.111.111.111"]
|
assert sensor.state == "111.111.111.111"
|
||||||
|
assert sensor.state_attributes[STATE_CURRENT_BANS] == ["111.111.111.111"]
|
||||||
def test_ipv6_ban(self):
|
assert sensor.state_attributes[STATE_ALL_BANS] == ["111.111.111.111"]
|
||||||
"""Test that log is parsed correctly for IPV6 bans."""
|
|
||||||
log_parser = BanLogParser("/test/fail2ban.log")
|
|
||||||
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()
|
|
||||||
|
|
||||||
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")
|
|
||||||
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()
|
|
||||||
|
|
||||||
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")
|
|
||||||
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()
|
|
||||||
|
|
||||||
assert sensor.state == "None"
|
|
||||||
assert sensor.state_attributes[STATE_CURRENT_BANS] == []
|
|
||||||
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")
|
|
||||||
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()
|
|
||||||
|
|
||||||
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")
|
|
||||||
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()
|
|
||||||
|
|
||||||
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")
|
|
||||||
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()
|
|
||||||
assert sensor.state == "111.111.111.111"
|
|
||||||
sensor.update()
|
|
||||||
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"]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user