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

Make flake8 happy

parent d68847f0
#!/usr/bin/env python3 #!/usr/bin/env python3
import zmq
import sn import sn
...@@ -14,7 +12,6 @@ class MyBox(sn.SNTerminationBox): ...@@ -14,7 +12,6 @@ class MyBox(sn.SNTerminationBox):
def teardown(self): def teardown(self):
print("teardown") print("teardown")
def process(self, msg_type, payload): def process(self, msg_type, payload):
print(msg_type, payload) print(msg_type, payload)
......
#!/usr/bin/env python3 #!/usr/bin/env python3
import zmq
import sn import sn
...@@ -14,7 +12,6 @@ class MyBox(sn.SNPipelineBox): ...@@ -14,7 +12,6 @@ class MyBox(sn.SNPipelineBox):
def teardown(self): def teardown(self):
print("teardown") print("teardown")
def process(self, msg_type, payload): def process(self, msg_type, payload):
print(msg_type, payload) print(msg_type, payload)
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
import time import time
import zmq
import sn import sn
...@@ -16,7 +14,6 @@ class MyBox(sn.SNGeneratorBox): ...@@ -16,7 +14,6 @@ class MyBox(sn.SNGeneratorBox):
def teardown(self): def teardown(self):
print("teardown") print("teardown")
def process(self): def process(self):
serial = 0 serial = 0
while True: while True:
......
...@@ -2,29 +2,29 @@ ...@@ -2,29 +2,29 @@
from distutils.core import setup from distutils.core import setup
setup(name = "sn", setup(name="sn",
version = "0.2", version="0.2",
description = "Sentinel networking library", description="Sentinel networking library",
author = "CZ.NIC, z.s.p.o.", author="CZ.NIC, z.s.p.o.",
author_email = "admin@turris.cz", author_email="admin@turris.cz",
url = "https://gitlab.labs.nic.cz/turris/sentinel/sn", url="https://gitlab.labs.nic.cz/turris/sentinel/sn",
packages = [ packages=[
"sn", "sn",
], ],
install_requires=[ install_requires=[
"msgpack", "msgpack",
"zmq", "zmq",
], ],
extras_require={ extras_require={
"tests": [ "tests": [
"pytest", "pytest",
"coverage", "coverage",
"pytest-cov", "pytest-cov",
], ],
"docs": [ "docs": [
"Sphinx", "Sphinx",
"sphinx-rtd-theme", "sphinx-rtd-theme",
"recommonmark", "recommonmark",
] ]
} }
) )
from sn.exceptions import * from sn.exceptions import * # noqa: F401,F403
from sn.messages import * from sn.messages import * # noqa: F401,F403
from sn.argparser import * from sn.argparser import * # noqa: F401,F403
from sn.network import * from sn.network import * # noqa: F401,F403
from sn.msgloop import SNPipelineBox, SNGeneratorBox, SNTerminationBox from sn.msgloop import SNPipelineBox, SNGeneratorBox, SNTerminationBox # noqa: F401
from sn.msgloop import SNMultipleOutputPipelineBox from sn.msgloop import SNMultipleOutputPipelineBox # noqa: F401
import sn.logging import sn.logging # noqa: F401
...@@ -16,14 +16,14 @@ def get_arg_parser(): ...@@ -16,14 +16,14 @@ def get_arg_parser():
action='append', action='append',
required=True, required=True,
help='resource format: sockname,[connect/bind],sock_type,ip_address,port' help='resource format: sockname,[connect/bind],sock_type,ip_address,port'
) )
parser.add_argument('--disable-ipv6', parser.add_argument('--disable-ipv6',
action='store_true', action='store_true',
help='Disable IPv6 mode of ZMQ sockets' help='Disable IPv6 mode of ZMQ sockets'
) )
parser.add_argument('-v', '--verbose', parser.add_argument('-v', '--verbose',
nargs=0, nargs=0,
action=EnableVerbose, action=EnableVerbose,
help="Enables debug mode in logger" help="Enables debug mode in logger"
) )
return parser return parser
...@@ -60,4 +60,5 @@ def log_uncaught(exc_type, exc_value, exc_traceback): ...@@ -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)) root_logger.exception("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.__excepthook__(exc_type, exc_value, exc_traceback) sys.__excepthook__(exc_type, exc_value, exc_traceback)
sys.excepthook = log_uncaught sys.excepthook = log_uncaught
...@@ -2,11 +2,12 @@ import re ...@@ -2,11 +2,12 @@ import re
import msgpack import msgpack
from .exceptions import * from .exceptions import InvalidMsgError, InvalidMsgTypeError
SN_MSG_REGEXP = "^([a-z0-9_]+/)*[a-z0-9_]+$" SN_MSG_REGEXP = "^([a-z0-9_]+/)*[a-z0-9_]+$"
SN_MSG = re.compile(SN_MSG_REGEXP) SN_MSG = re.compile(SN_MSG_REGEXP)
def parse_msg(data): def parse_msg(data):
""" Gets a Sentinel-type ZMQ message and parses message type and its """ Gets a Sentinel-type ZMQ message and parses message type and its
payload. payload.
...@@ -23,7 +24,6 @@ def parse_msg(data): ...@@ -23,7 +24,6 @@ def parse_msg(data):
except (TypeError, msgpack.exceptions.UnpackException, UnicodeDecodeError): except (TypeError, msgpack.exceptions.UnpackException, UnicodeDecodeError):
raise InvalidMsgError("Broken message") raise InvalidMsgError("Broken message")
return msg_type, payload return msg_type, payload
......
...@@ -4,7 +4,7 @@ import socket ...@@ -4,7 +4,7 @@ import socket
import zmq import zmq
from .argparser import get_arg_parser from .argparser import get_arg_parser
from .exceptions import * from .exceptions import SockConfigError, UndefinedSocketError
class Resource: class Resource:
...@@ -57,7 +57,6 @@ class Resource: ...@@ -57,7 +57,6 @@ class Resource:
self.address = address self.address = address
self.port = port_number self.port = port_number
def check_address(self, address): def check_address(self, address):
try: try:
if socket.inet_pton(socket.AF_INET, address): if socket.inet_pton(socket.AF_INET, address):
...@@ -79,11 +78,9 @@ class Resource: ...@@ -79,11 +78,9 @@ class Resource:
return False return False
def get_connection_string(self): def get_connection_string(self):
return "tcp://{}:{}".format(self.address, self.port) return "tcp://{}:{}".format(self.address, self.port)
@classmethod @classmethod
def from_string(cls, arg): def from_string(cls, arg):
splitted = arg.split(",") splitted = arg.split(",")
...@@ -93,7 +90,6 @@ class Resource: ...@@ -93,7 +90,6 @@ class Resource:
return cls(*splitted) return cls(*splitted)
def __eq__(self, other): def __eq__(self, other):
if self.name == other.name and \ if self.name == other.name and \
self.direction == other.direction and \ self.direction == other.direction and \
...@@ -104,7 +100,6 @@ class Resource: ...@@ -104,7 +100,6 @@ class Resource:
return False return False
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
...@@ -130,7 +125,6 @@ class Socket: ...@@ -130,7 +125,6 @@ class Socket:
self.configuration = configuration self.configuration = configuration
self.setup_done = False self.setup_done = False
def check_resource(self, resource): def check_resource(self, resource):
if self.name != resource.name: if self.name != resource.name:
raise SockConfigError("Putting bad resource to socket") raise SockConfigError("Putting bad resource to socket")
...@@ -150,7 +144,6 @@ class Socket: ...@@ -150,7 +144,6 @@ class Socket:
if resource in self.resources: if resource in self.resources:
raise SockConfigError("Resource duplication") raise SockConfigError("Resource duplication")
def add_resource(self, resource): def add_resource(self, resource):
self.check_resource(resource) self.check_resource(resource)
...@@ -158,7 +151,6 @@ class Socket: ...@@ -158,7 +151,6 @@ class Socket:
self.setup_done = True self.setup_done = True
def build(self, ctx, name, sock_type=None): def build(self, ctx, name, sock_type=None):
if self.name != name: if self.name != name:
raise SockConfigError("Name of requested resource is invalid") raise SockConfigError("Name of requested resource is invalid")
...@@ -178,7 +170,6 @@ class Socket: ...@@ -178,7 +170,6 @@ class Socket:
return socket return socket
def configure(self, socket): def configure(self, socket):
if "ipv6" in self.configuration: if "ipv6" in self.configuration:
socket.ipv6 = self.configuration["ipv6"] socket.ipv6 = self.configuration["ipv6"]
...@@ -191,7 +182,7 @@ class SN: ...@@ -191,7 +182,7 @@ class SN:
resources. resources.
""" """
def __init__(self, ctx, argparser=None): def __init__(self, ctx, argparser=None):
## Gather data # Gather data
self.context = ctx self.context = ctx
if argparser: if argparser:
...@@ -199,21 +190,18 @@ class SN: ...@@ -199,21 +190,18 @@ class SN:
else: else:
self.args = get_arg_parser().parse_args() self.args = get_arg_parser().parse_args()
## Build all necessary configuration # Build all necessary configuration
self.build_global_configuration() self.build_global_configuration()
self.parse_resources() self.parse_resources()
self.build_sockets() self.build_sockets()
def build_global_configuration(self): def build_global_configuration(self):
self.global_configuration = { self.global_configuration = {
"ipv6": not self.args.disable_ipv6, "ipv6": not self.args.disable_ipv6,
} }
def parse_resources(self): 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): def build_sockets(self):
self.sockets = {} self.sockets = {}
...@@ -224,7 +212,6 @@ class SN: ...@@ -224,7 +212,6 @@ class SN:
self.sockets[resource.name].add_resource(resource) self.sockets[resource.name].add_resource(resource)
def get_socket(self, *requested_sockets): def get_socket(self, *requested_sockets):
""" Gets multiple socket names in 'get_socket(name1, name2,...)' """ Gets multiple socket names in 'get_socket(name1, name2,...)'
or 'get_socket((name1, TYPE1), name2, (name3,TYPE3),...)' or any of or 'get_socket((name1, TYPE1), name2, (name3,TYPE3),...)' or any of
......
...@@ -2,7 +2,6 @@ import pytest ...@@ -2,7 +2,6 @@ import pytest
from unittest.mock import patch from unittest.mock import patch
import sn
def args_from_string(s): def args_from_string(s):
args = ["prog"] args = ["prog"]
...@@ -44,11 +43,11 @@ def empty_args_mock(): ...@@ -44,11 +43,11 @@ def empty_args_mock():
"--resource res,connect,PUSH,*,8800", "--resource res,connect,PUSH,*,8800",
"--resource res,conn,PUSH,127.0.0.1,8800", "--resource res,conn,PUSH,127.0.0.1,8800",
"--resource res,connect,PUSH,localhost,8800" "--resource res,connect,PUSH,localhost,8800"
" --resource res,connect,PUSH,localhost,8800", " --resource res,connect,PUSH,localhost,8800",
"--resource res,bind,PULL,localhost,8800" "--resource res,bind,PULL,localhost,8800"
" --resource res,bind,PULL,localhost,8800", " --resource res,bind,PULL,localhost,8800",
"--resource res,bind,PULL,localhost,8800" "--resource res,bind,PULL,localhost,8800"
" --resource res,bind,PUSH,localhost,8800", " --resource res,bind,PUSH,localhost,8800",
]) ])
def bad_resources_mock(request): def bad_resources_mock(request):
...@@ -80,13 +79,13 @@ def bind_resources_mock(request): ...@@ -80,13 +79,13 @@ def bind_resources_mock(request):
@pytest.fixture(params=[ @pytest.fixture(params=[
"--resource res1,connect,PUSH,localhost,8800" "--resource res1,connect,PUSH,localhost,8800"
" --resource res2,connect,PUSH,localhost,8800", " --resource res2,connect,PUSH,localhost,8800",
"--resource res1,connect,PUSH,localhost,8800" "--resource res1,connect,PUSH,localhost,8800"
" --resource res1,connect,PUSH,localhost,8801" " --resource res1,connect,PUSH,localhost,8801"
" --resource res2,connect,PUB,localhost,8802", " --resource res2,connect,PUB,localhost,8802",
"--resource res1,connect,PUB,localhost,8800" "--resource res1,connect,PUB,localhost,8800"
" --resource res1,connect,PUB,localhost,8801" " --resource res1,connect,PUB,localhost,8801"
" --resource res2,connect,PUB,localhost,8802", " --resource res2,connect,PUB,localhost,8802",
]) ])
def multisock_resources_mock(request): def multisock_resources_mock(request):
with patch("sys.argv", args_from_string(request.param)) as m: with patch("sys.argv", args_from_string(request.param)) as m:
......
import pytest import pytest
import argparse
import zmq import zmq
import sn import sn
def test_empty_args(zmq_context, empty_args_mock): def test_empty_args(zmq_context, empty_args_mock):
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
ctx = sn.SN(zmq_context) sn.SN(zmq_context)
def test_undefined_resource(zmq_context, one_resource_mock): def test_undefined_resource(zmq_context, one_resource_mock):
...@@ -15,6 +15,7 @@ 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) ctx = sn.SN(zmq_context)
assert ctx.get_socket("in") assert ctx.get_socket("in")
def test_bad_args(zmq_context, bad_resources_mock): def test_bad_args(zmq_context, bad_resources_mock):
with pytest.raises((SystemExit, sn.SockConfigError, zmq.error.ZMQError)): with pytest.raises((SystemExit, sn.SockConfigError, zmq.error.ZMQError)):
ctx = sn.SN(zmq_context) ctx = sn.SN(zmq_context)
...@@ -53,7 +54,7 @@ def test_required_type_decline(zmq_context, one_resource_mock): ...@@ -53,7 +54,7 @@ def test_required_type_decline(zmq_context, one_resource_mock):
def test_verbose(zmq_context, verbose_args_mock): def test_verbose(zmq_context, verbose_args_mock):
ctx = sn.SN(zmq_context) sn.SN(zmq_context)
import logging import logging
......
import sys import sys
sys.path.append("..")
import pytest import pytest
import zmq import zmq
import sn
sys.path.append("..")
@pytest.fixture @pytest.fixture
......
...@@ -2,10 +2,12 @@ import pytest ...@@ -2,10 +2,12 @@ import pytest
import msgpack import msgpack
@pytest.fixture @pytest.fixture
def good_type(): def good_type():
return "sentinel/tests/good_string" return "sentinel/tests/good_string"
@pytest.fixture(params=[ @pytest.fixture(params=[
"sentinel", "sentinel",
"sentinel/tests", "sentinel/tests",
...@@ -21,6 +23,7 @@ def good_type(): ...@@ -21,6 +23,7 @@ def good_type():
def good_types(request): def good_types(request):
return request.param return request.param
@pytest.fixture(params=[ @pytest.fixture(params=[
"/sentinel/tests/broken", "/sentinel/tests/broken",
"sentinel/tests/broken/", "sentinel/tests/broken/",
...@@ -41,6 +44,7 @@ def good_types(request): ...@@ -41,6 +44,7 @@ def good_types(request):
def bad_types(request): def bad_types(request):
return request.param return request.param
@pytest.fixture @pytest.fixture
def good_payload(): def good_payload():
return { return {
...@@ -50,6 +54,7 @@ def good_payload(): ...@@ -50,6 +54,7 @@ def good_payload():
"key4": "ěščřžýáíé", "key4": "ěščřžýáíé",
} }
@pytest.fixture @pytest.fixture
def good_msg(good_type, good_payload): def good_msg(good_type, good_payload):
t = bytes(good_type, encoding="UTF-8") t = bytes(good_type, encoding="UTF-8")
...@@ -57,6 +62,7 @@ def good_msg(good_type, good_payload): ...@@ -57,6 +62,7 @@ def good_msg(good_type, good_payload):
return (t, p) return (t, p)
@pytest.fixture @pytest.fixture
def good_long(good_type, good_payload): def good_long(good_type, good_payload):
t = bytes(good_type, encoding="UTF-8") t = bytes(good_type, encoding="UTF-8")
...@@ -65,6 +71,7 @@ def good_long(good_type, good_payload): ...@@ -65,6 +71,7 @@ def good_long(good_type, good_payload):
return (t, p, x) return (t, p, x)
@pytest.fixture(params=[1, 5, 7, 9, 12, 15, 18, 20]) @pytest.fixture(params=[1, 5, 7, 9, 12, 15, 18, 20])
def broken_msg(request, good_msg): def broken_msg(request, good_msg):
# Parameters are tested by eye, but it generates several different # Parameters are tested by eye, but it generates several different
...@@ -79,11 +86,13 @@ def broken_msg(request, good_msg): ...@@ -79,11 +86,13 @@ def broken_msg(request, good_msg):
return (good_msg[0], msg) return (good_msg[0], msg)
@pytest.fixture @pytest.fixture
def good_type_msg(good_types, good_msg): def good_type_msg(good_types, good_msg):
t = bytes(good_types, encoding="UTF-8") t = bytes(good_types, encoding="UTF-8")
return (t, good_msg[1]) return (t, good_msg[1])
@pytest.fixture @pytest.fixture
def broken_type_msg(bad_types, good_msg): def broken_type_msg(bad_types, good_msg):
t = bytes(bad_types, encoding="UTF-8") t = bytes(bad_types, encoding="UTF-8")
......
...@@ -2,27 +2,33 @@ import pytest ...@@ -2,27 +2,33 @@ import pytest
import sn import sn
def test_encoding(good_type, good_payload, good_msg): def test_encoding(good_type, good_payload, good_msg):
encoded = sn.encode_msg(good_type, good_payload) encoded = sn.encode_msg(good_type, good_payload)
assert encoded == good_msg assert encoded == good_msg
def test_empty_type(good_payload): def test_empty_type(good_payload):
with pytest.raises(sn.InvalidMsgError): with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(None, good_payload) sn.encode_msg(None, good_payload)
def test_empty_payload(good_type): def test_empty_payload(good_type):
with pytest.raises(sn.InvalidMsgError): with pytest.raises(sn.InvalidMsgError):
encoded = sn.encode_msg(good_type, None) sn.encode_msg(good_type, None)
def test_bad_payload(good_type): def test_bad_payload(good_type):
with pytest.raises(sn.InvalidMsgError): 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): def test_good_type(good_types, good_payload):
(t, p) = sn.encode_msg(good_types, good_payload) (t, p) = sn.encode_msg(good_types, good_payload)
assert t assert t
assert p assert p
def test_invalid_type(bad_types, good_payload): def test_invalid_type(bad_types, good_payload):
with pytest.raises(sn.InvalidMsgError): with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.encode_msg(bad_types, good_payload) (t, p) = sn.encode_msg(bad_types, good_payload)
...@@ -2,34 +2,41 @@ import pytest ...@@ -2,34 +2,41 @@ import pytest
import sn import sn
def test_parse(good_type, good_payload, good_msg): def test_parse(good_type, good_payload, good_msg):
(t, p) = sn.parse_msg(good_msg) (t, p) = sn.parse_msg(good_msg)
assert t == good_type assert t == good_type
assert p == good_payload assert p == good_payload
def test_longer(good_type, good_payload, good_long): def test_longer(good_type, good_payload, good_long):
(t, p) = sn.parse_msg(good_long) (t, p) = sn.parse_msg(good_long)
assert t == good_type assert t == good_type
assert p == good_payload assert p == good_payload
def test_missing_type(good_msg): def test_missing_type(good_msg):
with pytest.raises(sn.InvalidMsgError): with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.parse_msg(good_msg[1]) (t, p) = sn.parse_msg(good_msg[1])
def test_missing_payload(good_msg): def test_missing_payload(good_msg):
with pytest.raises(sn.InvalidMsgError): with pytest.raises(sn.InvalidMsgError):
data = (good_msg[0],) data = (good_msg[0],)
(t, p) = sn.parse_msg(data) (t, p) = sn.parse_msg(data)
def test_broken_payload(broken_msg): def test_broken_payload(broken_msg):
with pytest.raises(sn.InvalidMsgError): with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.parse_msg(broken_msg) (t, p) = sn.parse_msg(broken_msg)
def test_good_type(good_type_msg): def test_good_type(good_type_msg):
(t, p) = sn.parse_msg(good_type_msg) (t, p) = sn.parse_msg(good_type_msg)
assert t assert t
assert p assert p
def test_invalid_type(broken_type_msg): def test_invalid_type(broken_type_msg):