Commit d3b265a1 authored by Robin Obůrka's avatar Robin Obůrka

Make flake8 happy

parent d68847f0
#!/usr/bin/env python3
import zmq
import sn
......@@ -14,7 +12,6 @@ class MyBox(sn.SNTerminationBox):
def teardown(self):
print("teardown")
def process(self, msg_type, payload):
print(msg_type, payload)
......
#!/usr/bin/env python3
import zmq
import sn
......@@ -14,7 +12,6 @@ class MyBox(sn.SNPipelineBox):
def teardown(self):
print("teardown")
def process(self, msg_type, payload):
print(msg_type, payload)
......
......@@ -2,8 +2,6 @@
import time
import zmq
import sn
......@@ -16,7 +14,6 @@ class MyBox(sn.SNGeneratorBox):
def teardown(self):
print("teardown")
def process(self):
serial = 0
while True:
......
......@@ -2,13 +2,13 @@
from distutils.core import setup
setup(name = "sn",
version = "0.2",
description = "Sentinel networking library",
author = "CZ.NIC, z.s.p.o.",
author_email = "admin@turris.cz",
url = "https://gitlab.labs.nic.cz/turris/sentinel/sn",
packages = [
setup(name="sn",
version="0.2",
description="Sentinel networking library",
author="CZ.NIC, z.s.p.o.",
author_email="admin@turris.cz",
url="https://gitlab.labs.nic.cz/turris/sentinel/sn",
packages=[
"sn",
],
install_requires=[
......@@ -27,4 +27,4 @@ setup(name = "sn",
"recommonmark",
]
}
)
)
from sn.exceptions import *
from sn.messages import *
from sn.argparser import *
from sn.network import *
from sn.msgloop import SNPipelineBox, SNGeneratorBox, SNTerminationBox
from sn.msgloop import SNMultipleOutputPipelineBox
import sn.logging
from sn.exceptions import * # noqa: F401,F403
from sn.messages import * # noqa: F401,F403
from sn.argparser import * # noqa: F401,F403
from sn.network import * # noqa: F401,F403
from sn.msgloop import SNPipelineBox, SNGeneratorBox, SNTerminationBox # noqa: F401
from sn.msgloop import SNMultipleOutputPipelineBox # noqa: F401
import sn.logging # noqa: F401
......@@ -60,4 +60,5 @@ def log_uncaught(exc_type, exc_value, exc_traceback):
root_logger.exception("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.__excepthook__(exc_type, exc_value, exc_traceback)
sys.excepthook = log_uncaught
......@@ -2,11 +2,12 @@ import re
import msgpack
from .exceptions import *
from .exceptions import InvalidMsgError, InvalidMsgTypeError
SN_MSG_REGEXP = "^([a-z0-9_]+/)*[a-z0-9_]+$"
SN_MSG = re.compile(SN_MSG_REGEXP)
def parse_msg(data):
""" Gets a Sentinel-type ZMQ message and parses message type and its
payload.
......@@ -23,7 +24,6 @@ def parse_msg(data):
except (TypeError, msgpack.exceptions.UnpackException, UnicodeDecodeError):
raise InvalidMsgError("Broken message")
return msg_type, payload
......
......@@ -4,7 +4,7 @@ import socket
import zmq
from .argparser import get_arg_parser
from .exceptions import *
from .exceptions import SockConfigError, UndefinedSocketError
class Resource:
......@@ -57,7 +57,6 @@ class Resource:
self.address = address
self.port = port_number
def check_address(self, address):
try:
if socket.inet_pton(socket.AF_INET, address):
......@@ -79,11 +78,9 @@ class Resource:
return False
def get_connection_string(self):
return "tcp://{}:{}".format(self.address, self.port)
@classmethod
def from_string(cls, arg):
splitted = arg.split(",")
......@@ -93,7 +90,6 @@ class Resource:
return cls(*splitted)
def __eq__(self, other):
if self.name == other.name and \
self.direction == other.direction and \
......@@ -104,7 +100,6 @@ class Resource:
return False
def __ne__(self, other):
return not self.__eq__(other)
......@@ -130,7 +125,6 @@ class Socket:
self.configuration = configuration
self.setup_done = False
def check_resource(self, resource):
if self.name != resource.name:
raise SockConfigError("Putting bad resource to socket")
......@@ -150,7 +144,6 @@ class Socket:
if resource in self.resources:
raise SockConfigError("Resource duplication")
def add_resource(self, resource):
self.check_resource(resource)
......@@ -158,7 +151,6 @@ class Socket:
self.setup_done = True
def build(self, ctx, name, sock_type=None):
if self.name != name:
raise SockConfigError("Name of requested resource is invalid")
......@@ -178,7 +170,6 @@ class Socket:
return socket
def configure(self, socket):
if "ipv6" in self.configuration:
socket.ipv6 = self.configuration["ipv6"]
......@@ -191,7 +182,7 @@ class SN:
resources.
"""
def __init__(self, ctx, argparser=None):
## Gather data
# Gather data
self.context = ctx
if argparser:
......@@ -199,21 +190,18 @@ class SN:
else:
self.args = get_arg_parser().parse_args()
## Build all necessary configuration
# Build all necessary configuration
self.build_global_configuration()
self.parse_resources()
self.build_sockets()
def build_global_configuration(self):
self.global_configuration = {
"ipv6": not self.args.disable_ipv6,
}
def parse_resources(self):
self.resources = [ Resource.from_string(res) for res in self.args.resource ]
self.resources = [Resource.from_string(res) for res in self.args.resource]
def build_sockets(self):
self.sockets = {}
......@@ -224,7 +212,6 @@ class SN:
self.sockets[resource.name].add_resource(resource)
def get_socket(self, *requested_sockets):
""" Gets multiple socket names in 'get_socket(name1, name2,...)'
or 'get_socket((name1, TYPE1), name2, (name3,TYPE3),...)' or any of
......
......@@ -2,7 +2,6 @@ import pytest
from unittest.mock import patch
import sn
def args_from_string(s):
args = ["prog"]
......
import pytest
import argparse
import zmq
import sn
def test_empty_args(zmq_context, empty_args_mock):
with pytest.raises(SystemExit):
ctx = sn.SN(zmq_context)
sn.SN(zmq_context)
def test_undefined_resource(zmq_context, one_resource_mock):
......@@ -15,6 +15,7 @@ def test_undefined_resource(zmq_context, one_resource_mock):
ctx = sn.SN(zmq_context)
assert ctx.get_socket("in")
def test_bad_args(zmq_context, bad_resources_mock):
with pytest.raises((SystemExit, sn.SockConfigError, zmq.error.ZMQError)):
ctx = sn.SN(zmq_context)
......@@ -53,7 +54,7 @@ def test_required_type_decline(zmq_context, one_resource_mock):
def test_verbose(zmq_context, verbose_args_mock):
ctx = sn.SN(zmq_context)
sn.SN(zmq_context)
import logging
......
import sys
sys.path.append("..")
import pytest
import zmq
import sn
sys.path.append("..")
@pytest.fixture
......
......@@ -2,10 +2,12 @@ import pytest
import msgpack
@pytest.fixture
def good_type():
return "sentinel/tests/good_string"
@pytest.fixture(params=[
"sentinel",
"sentinel/tests",
......@@ -21,6 +23,7 @@ def good_type():
def good_types(request):
return request.param
@pytest.fixture(params=[
"/sentinel/tests/broken",
"sentinel/tests/broken/",
......@@ -41,6 +44,7 @@ def good_types(request):
def bad_types(request):
return request.param
@pytest.fixture
def good_payload():
return {
......@@ -50,6 +54,7 @@ def good_payload():
"key4": "ěščřžýáíé",
}
@pytest.fixture
def good_msg(good_type, good_payload):
t = bytes(good_type, encoding="UTF-8")
......@@ -57,6 +62,7 @@ def good_msg(good_type, good_payload):
return (t, p)
@pytest.fixture
def good_long(good_type, good_payload):
t = bytes(good_type, encoding="UTF-8")
......@@ -65,6 +71,7 @@ def good_long(good_type, good_payload):
return (t, p, x)
@pytest.fixture(params=[1, 5, 7, 9, 12, 15, 18, 20])
def broken_msg(request, good_msg):
# Parameters are tested by eye, but it generates several different
......@@ -79,11 +86,13 @@ def broken_msg(request, good_msg):
return (good_msg[0], msg)
@pytest.fixture
def good_type_msg(good_types, good_msg):
t = bytes(good_types, encoding="UTF-8")
return (t, good_msg[1])
@pytest.fixture
def broken_type_msg(bad_types, good_msg):
t = bytes(bad_types, encoding="UTF-8")
......
......@@ -2,27 +2,33 @@ import pytest
import sn
def test_encoding(good_type, good_payload, good_msg):
encoded = sn.encode_msg(good_type, good_payload)
assert encoded == good_msg
def test_empty_type(good_payload):
with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(None, good_payload)
sn.encode_msg(None, good_payload)
def test_empty_payload(good_type):
with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(good_type, None)
sn.encode_msg(good_type, None)
def test_bad_payload(good_type):
with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(good_type, ("I", "am", "a", "tuple"))
sn.encode_msg(good_type, ("I", "am", "a", "tuple"))
def test_good_type(good_types, good_payload):
(t, p) = sn.encode_msg(good_types, good_payload)
assert t
assert p
def test_invalid_type(bad_types, good_payload):
with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.encode_msg(bad_types, good_payload)
......@@ -2,34 +2,41 @@ import pytest
import sn
def test_parse(good_type, good_payload, good_msg):
(t, p) = sn.parse_msg(good_msg)
assert t == good_type
assert p == good_payload
def test_longer(good_type, good_payload, good_long):
(t, p) = sn.parse_msg(good_long)
assert t == good_type
assert p == good_payload
def test_missing_type(good_msg):
with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.parse_msg(good_msg[1])
def test_missing_payload(good_msg):
with pytest.raises(sn.InvalidMsgError):
data = (good_msg[0],)
(t, p) = sn.parse_msg(data)
def test_broken_payload(broken_msg):
with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.parse_msg(broken_msg)
def test_good_type(good_type_msg):
(t, p) = sn.parse_msg(good_type_msg)
assert t
assert p
def test_invalid_type(broken_type_msg):
with pytest.raises(sn.InvalidMsgTypeError):
(t, p) = sn.parse_msg(broken_type_msg)
......@@ -4,6 +4,7 @@ from unittest.mock import patch
import msgpack
def args_from_string(s):
args = ["prog"]
args.extend(s.split(" "))
......@@ -64,7 +65,7 @@ def bad_socket_args_mock(bad_socket_args):
@pytest.fixture
def good_msg():
return build_msg("sentinel/test", { "foo": "bar" })
return build_msg("sentinel/test", {"foo": "bar"})
@pytest.fixture
......
from .conftest import build_msg
import pytest
from unittest.mock import Mock, patch
......@@ -66,7 +64,7 @@ def test_processed_from_generator(out_only_args_mock, send_multipart_mock):
class TestBox(sn.SNGeneratorBox):
def process(self):
for i in range(msg_num):
yield "sentinel/test", { "foo": "bar" }
yield "sentinel/test", {"foo": "bar"}
TestBox("test").run()
......@@ -79,7 +77,7 @@ def test_many_errors_in_row(out_only_args_mock, send_multipart_mock):
class TestBox(sn.SNGeneratorBox):
def process(self):
while True:
yield "šentinel/test", { "foo": "bar" }
yield "šentinel/test", {"foo": "bar"}
tb = TestBox("test")
......@@ -96,8 +94,8 @@ def test_resetable_error_counter(out_only_args_mock, send_multipart_mock):
class TestBox(sn.SNGeneratorBox):
def process(self):
for i in range(10):
yield "šentinel/test", { "foo": "bar" }
yield "sentinel/test", { "foo": "bar" }
yield "šentinel/test", {"foo": "bar"}
yield "sentinel/test", {"foo": "bar"}
TestBox("test").run()
......@@ -107,9 +105,10 @@ def test_resetable_error_counter(out_only_args_mock, send_multipart_mock):
def test_before_first_request_processed(out_only_args_mock, send_multipart_mock):
class TestBox(sn.SNGeneratorBox):
def before_first_request(self):
return "sentinel/test/bfr", { "foo": "bar" }
return "sentinel/test/bfr", {"foo": "bar"}
def process(self):
yield "sentinel/test", { "foo": "bar" }
yield "sentinel/test", {"foo": "bar"}
tb = TestBox("test")
tb.run()
......@@ -124,7 +123,7 @@ def test_before_first_request_processed(out_only_args_mock, send_multipart_mock)
def test_set_signal_handlers(out_only_args_mock, send_multipart_mock):
class TestBox(sn.SNGeneratorBox):
def process(self):
yield "sentinel/test", { "foo": "bar" }
yield "sentinel/test", {"foo": "bar"}
with patch("signal.signal") as signal:
tb = TestBox("test")
......
......@@ -2,6 +2,7 @@ import pytest
import sn
def test_empty_name():
class TestBox(sn.SNPipelineBox):
pass
......
......@@ -2,11 +2,10 @@ import pytest
import zmq
import sn
# This test proof that SN class is really able to generate working sockets.
# More input brutal test is in module argparser.
@pytest.fixture
def socket_binded():
ctx = zmq.Context.instance()
......@@ -17,6 +16,7 @@ def socket_binded():
s.close()
ctx.destroy()
@pytest.fixture
def socket_connected():
ctx = zmq.Context.instance()
......
import pytest
from unittest.mock import patch
import sn
def test_send(zmq_context, socket_binded):
with patch("sys.argv", ["prog", "--resource", "res,connect,PUSH,127.0.0.1,8800"]):
msg = b"ping"
......@@ -16,6 +15,7 @@ def test_send(zmq_context, socket_binded):
s.close()
ctx.context.destroy()
def test_recv(zmq_context, socket_connected):
with patch("sys.argv", ["prog", "--resource", "res,bind,PULL,127.0.0.1,8800"]):
msg = b"ping"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment