Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/velbustcp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ async def stop(self):
await self.__bridge.stop()


async def main_async(args=None):
"""Main asynchronous method."""
async def main_async(args=None) -> int:
"""Main asynchronous method.

Returns:
int: The process exit code, non-zero if the bridge failed.
"""
parser = argparse.ArgumentParser(description="Velbus communication")
parser.add_argument("--settings", help="Settings file", required=False)
args = parser.parse_args()
Expand All @@ -63,6 +67,7 @@ async def main_async(args=None):

# Create main class
main = Main()
exit_code = 0

try:
loop = asyncio.get_event_loop()
Expand All @@ -74,17 +79,19 @@ async def main_async(args=None):

except Exception as e:
logger.exception(e)
exit_code = 1

finally:
await main.stop()

logger.info("Shutdown")
return exit_code


# entrypoint for the snap
def main(args=None):
def main(args=None) -> int:
"""Main method."""
asyncio.run(main_async(args))
return asyncio.run(main_async(args))


if __name__ == '__main__':
Expand Down
36 changes: 28 additions & 8 deletions src/velbustcp/lib/connection/tcp/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import asyncio
import errno
import logging
from typing import Any, List
from typing import Any, List, Optional

from velbustcp.lib.connection.tcp.clientconnection import ClientConnection
from velbustcp.lib.packet.packetparser import PacketParser
from velbustcp.lib.signals import on_tcp_receive, on_client_close

CONNECTION_LOST_ERRNOS = {errno.EHOSTUNREACH, errno.ENETUNREACH, errno.ETIMEDOUT}


def is_connection_lost(e: Optional[BaseException]) -> bool:
return isinstance(e, ConnectionError) or (isinstance(e, OSError) and e.errno in CONNECTION_LOST_ERRNOS)


class Client:

Expand Down Expand Up @@ -51,9 +58,16 @@ async def stop(self) -> None:
self.__is_active = False
self.__logger.info("Closing client connection for %s", self.address())
self.__connection.writer.close()
await self.__connection.writer.wait_closed()
self.__received_packets.clear()
on_client_close.send(self)

try:
await self.__connection.writer.wait_closed()
except OSError as e:
# wait_closed re-raises the error the transport was closed with, e.g. when the peer reset
# the connection. The connection is gone either way, so still clean up below.
self.__logger.info("Connection for %s was not closed cleanly: %s", self.address(), e)
finally:
self.__received_packets.clear()
on_client_close.send(self)

async def send(self, data: bytearray) -> None:
"""Sends data to the client.
Expand Down Expand Up @@ -110,8 +124,11 @@ async def __handle_authorization(self) -> bool:

return self.__connection.authorization_key == data.decode("utf-8").strip()

except Exception:
self.__logger.exception("Exception during authorization for %s", self.address())
except Exception as e:
if is_connection_lost(e):
self.__logger.info("Connection lost during authorization for %s: %s", self.address(), e)
else:
self.__logger.exception("Exception during authorization for %s", self.address())

return False

Expand All @@ -124,8 +141,11 @@ async def __handle_packets(self) -> None:
while self.is_active():
try:
data = await self.__connection.reader.read(1024)
except Exception:
self.__logger.exception("Exception during packet receiving")
except Exception as e:
if is_connection_lost(e):
self.__logger.info("Connection lost with client %s: %s", self.address(), e)
else:
self.__logger.exception("Exception during packet receiving")
return

if not data:
Expand Down
12 changes: 9 additions & 3 deletions src/velbustcp/lib/connection/tcp/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import ssl
import logging
from typing import List, Optional
from velbustcp.lib.connection.tcp.client import Client
from velbustcp.lib.connection.tcp.client import Client, is_connection_lost
from velbustcp.lib.connection.tcp.clientconnection import ClientConnection
from velbustcp.lib.settings.network import NetworkSettings
from velbustcp.lib.signals import on_client_close
Expand Down Expand Up @@ -110,5 +110,11 @@ async def send(self, data: bytearray) -> None:
if self.__logger.isEnabledFor(logging.DEBUG): # pragma: no cover
self.__logger.debug("[TCP OUT] %s", " ".join(hex(x) for x in data))

tasks = [client.send(data) for client in self.__clients]
await asyncio.gather(*tasks)
clients = list(self.__clients)
results = await asyncio.gather(*(client.send(data) for client in clients), return_exceptions=True)

for client, result in zip(clients, results):
if is_connection_lost(result):
self.__logger.warning("Connection lost while sending to client %s: %s", client.address(), result)
elif isinstance(result, Exception):
self.__logger.error("Could not send data to client %s", client.address(), exc_info=result)
6 changes: 4 additions & 2 deletions src/velbustcp/lib/settings/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict # noqa: F401

LOGGING_TYPES = ["debug", "info"]
LOGGING_OUTPUT = ["syslog", "stream"]
LOGGING_OUTPUT = ["syslog", "syslog-local", "stream"]


class LoggingSettings():
Expand All @@ -26,7 +26,9 @@ def parse(settings_dict):
if "output" in settings_dict:

if (not settings_dict["output"] in LOGGING_OUTPUT):
raise ValueError("Provided option logging.output incorrect, expected 'syslog' or 'stream', got '{0}'".format(settings_dict["output"]))
raise ValueError(
"Provided option logging.output incorrect, expected 'syslog', 'syslog-local' or 'stream', got '{0}'".format(settings_dict["output"])
)

settings.output = settings_dict["output"]

Expand Down
19 changes: 16 additions & 3 deletions src/velbustcp/lib/util/util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import logging
import logging.handlers
import os
import socket
from typing import List
import serial
import serial.tools.list_ports
from velbustcp.lib.settings.logging import LoggingSettings
from velbustcp.lib.consts import PRODUCT_IDS


SYSLOG_SOCKETS = ["/dev/log", "/var/run/syslog"]


def str2bool(v: str) -> bool:
return str(v).lower() in ["true", "yes", "y", "t", "1"]

Expand All @@ -20,8 +25,6 @@ def setup_logging(settings: LoggingSettings) -> logging.Logger:

logger = logging.getLogger(settings.name)

print(settings.type)

# Set type
if settings.type == "debug":
logger.setLevel(logging.DEBUG)
Expand All @@ -30,12 +33,22 @@ def setup_logging(settings: LoggingSettings) -> logging.Logger:

# Set handler
handler: logging.Handler
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s')
if settings.output == "syslog":
handler = logging.handlers.SysLogHandler()
elif settings.output == "syslog-local":
if not hasattr(socket, "AF_UNIX"):
raise ValueError("Logging output 'syslog-local' is not supported on this platform")

address = next((path for path in SYSLOG_SOCKETS if os.path.exists(path)), SYSLOG_SOCKETS[0])
handler = logging.handlers.SysLogHandler(address=address)
handler.ident = "velbustcp: "
handler.append_nul = False
# The local syslog adds its own timestamp
formatter = logging.Formatter('%(levelname)s - %(module)s - %(message)s')
else:
handler = logging.StreamHandler()

formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

Expand Down
73 changes: 73 additions & 0 deletions tests/velbustcp/lib/connection/tcp/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import errno
import logging
import pytest
from velbustcp.lib.connection.tcp.client import Client
from pytest_mock import MockFixture, MockerFixture
Expand Down Expand Up @@ -239,3 +241,74 @@ async def test_client_send_not_own_packet(mocker: MockerFixture):

await client.stop()
await task


CONNECTION_LOST_ERRORS = [
ConnectionResetError(errno.ECONNRESET, "Connection reset by peer"),
OSError(errno.EHOSTUNREACH, "No route to host"),
OSError(errno.ENETUNREACH, "Network is unreachable"),
OSError(errno.ETIMEDOUT, "Connection timed out"),
]


@pytest.mark.asyncio
@pytest.mark.parametrize("error", CONNECTION_LOST_ERRORS)
async def test_connection_reset(mocker: MockerFixture, caplog: pytest.LogCaptureFixture, error: OSError):
# Create connection where the peer is gone, which is also what wait_closed reports
conn = get_mock_connection(mocker)
conn.reader.read = mocker.AsyncMock(side_effect=error)
conn.writer.wait_closed = mocker.AsyncMock(side_effect=error)
conn.should_authorize = False

closed = []

def handle_client_close(sender, **kwargs):
closed.append(sender)

on_client_close.connect(handle_client_close)

# Must not raise, and must still report the client as closed
client = Client(conn)
await client.start()

assert not client.is_active()
assert client in closed
assert not [r for r in caplog.records if r.levelno >= logging.ERROR]


@pytest.mark.asyncio
@pytest.mark.parametrize("error", CONNECTION_LOST_ERRORS)
async def test_auth_connection_reset(mocker: MockerFixture, caplog: pytest.LogCaptureFixture, error: OSError):
conn = get_mock_connection(mocker)
conn.reader.read = mocker.AsyncMock(side_effect=error)
conn.writer.wait_closed = mocker.AsyncMock(side_effect=error)
conn.should_authorize = True
conn.authorization_key = "velbus"

closed = []

def handle_client_close(sender, **kwargs):
closed.append(sender)

on_client_close.connect(handle_client_close)

client = Client(conn)
await client.start()

assert not client.is_active()
assert client in closed
assert not [r for r in caplog.records if r.levelno >= logging.ERROR]


@pytest.mark.asyncio
async def test_unexpected_os_error(mocker: MockerFixture, caplog: pytest.LogCaptureFixture):
# OSErrors that do not indicate a lost connection must still be reported as errors
conn = get_mock_connection(mocker)
conn.reader.read = mocker.AsyncMock(side_effect=OSError(errno.EBADF, "Bad file descriptor"))
conn.should_authorize = False

client = Client(conn)
await client.start()

assert not client.is_active()
assert [r for r in caplog.records if r.levelno >= logging.ERROR]
29 changes: 29 additions & 0 deletions tests/velbustcp/lib/connection/tcp/test_network.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import errno
import pytest
from pytest_mock import MockFixture

Expand Down Expand Up @@ -54,3 +55,31 @@ async def test_send_not_active(mocker: MockFixture):
# Assert
spy.assert_called_once()
assert not spy.spy_return


@pytest.mark.asyncio
@pytest.mark.parametrize("error", [
ConnectionResetError(104, "Connection reset by peer"),
OSError(errno.EHOSTUNREACH, "No route to host"),
RuntimeError("Thrown on purpose"),
])
async def test_send_client_failure(mocker: MockFixture, error: Exception):

# Arrange
network = Network(options=NetworkSettings())
mocker.patch.object(network, 'is_active', return_value=True)

failing = mocker.Mock()
failing.send = mocker.AsyncMock(side_effect=error)
healthy = mocker.Mock()
healthy.send = mocker.AsyncMock()
network._Network__clients = [failing, healthy] # type: ignore[attr-defined]

data = bytearray([0x0F])

# Act, must not raise
await network.send(data)

# Assert, the healthy client still got the data
failing.send.assert_awaited_once_with(data)
healthy.send.assert_awaited_once_with(data)
36 changes: 36 additions & 0 deletions tests/velbustcp/lib/util/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import logging.handlers

import pytest

from velbustcp.lib.settings.logging import LoggingSettings
from velbustcp.lib.util.util import setup_logging, str2bool

Expand Down Expand Up @@ -43,3 +45,37 @@ def test_output_syslog():
logger = setup_logging(settings)
assert logger.name == settings.name
assert isinstance(logger.handlers[0], logging.handlers.SysLogHandler)
assert logger.handlers[0].address == ("localhost", logging.handlers.SYSLOG_UDP_PORT)


@pytest.mark.parametrize("existing,expected", [
(["/dev/log", "/var/run/syslog"], "/dev/log"),
(["/var/run/syslog"], "/var/run/syslog"),
([], "/dev/log"),
])
def test_output_syslog_local(mocker, existing, expected):
mocker.patch("velbustcp.lib.util.util.socket", spec=["AF_UNIX"])
mocker.patch("velbustcp.lib.util.util.os.path.exists", side_effect=lambda path: path in existing)
handler_class = mocker.patch("velbustcp.lib.util.util.logging.handlers.SysLogHandler")
handler_class.return_value = logging.NullHandler()

settings = LoggingSettings()
settings.name = "test-logger-syslog-local-" + expected
settings.output = "syslog-local"

logger = setup_logging(settings)
handler_class.assert_called_once_with(address=expected)
assert logger.handlers[0].ident == "velbustcp: "
assert logger.handlers[0].append_nul is False


def test_output_syslog_local_unsupported(mocker):

mocker.patch("velbustcp.lib.util.util.socket", spec=[])

settings = LoggingSettings()
settings.name = "test-logger-syslog-local-unsupported"
settings.output = "syslog-local"

with pytest.raises(ValueError):
setup_logging(settings)
Loading