This repository has been archived on 2020-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
producer/test.py

297 lines
8.8 KiB
Python
Raw Normal View History

2020-03-30 17:37:58 +02:00
#!/usr/bin/env python
"""
Unit tests for the producer module.
"""
2020-03-30 19:33:09 +02:00
import re
import consumerlocator
import communicator
import messagesender
2020-04-17 16:47:27 +02:00
import redisconnector
2020-03-30 17:37:58 +02:00
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "test"
__version__text__ = "1"
2020-03-30 19:33:09 +02:00
generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad'
2020-03-30 19:33:09 +02:00
def test_generate_string(mocker):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`messagesender.MessageSender.randomstring`.
:param mocker: patches the :class:`communicator.Communicator`.
2020-04-06 20:48:36 +02:00
"""
2020-03-30 19:33:09 +02:00
mocker.patch('communicator.Communicator')
comm = communicator.Communicator(
currentconsumer="localhost",
uuid=generateduuid)
mess = messagesender.MessageSender(communicator=comm)
msg = mess.randomstring(stringlength=32)
assert isinstance(msg, str)
assert len(msg) == 32
def test_sendmessage(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`communicator.Communicator.sendmessage`.
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
2020-03-30 19:33:09 +02:00
httpserver.expect_oneshot_request(
uri="/log",
method='POST',
data="{\"uuid\": \"c959ad81-58f9-4445-aab4-8f3d68aee1ad\", \"message\": \"SENDING\"}").respond_with_json(
{
"test": "ok"})
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid)
mess = "SENDING"
ret = comm.sendmessage(message=mess)
assert ret is None
def test_send_message(mocker):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`messagesender.MessageSender.sendmessage`.
:param mocker: patches the :class:`communicator.Communicator`.
2020-04-06 20:48:36 +02:00
:return: None
"""
2020-03-30 19:33:09 +02:00
mocker.patch('communicator.Communicator')
comm = communicator.Communicator(
currentconsumer="127.0.0.1",
uuid=generateduuid)
mess = messagesender.MessageSender(communicator=comm)
messa = "SENDING"
msg = mess.sendmessage(message=messa)
assert msg is None
def test_discoveravailableconsumers(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`communicator.Communicator.discoveravailableconsumers`
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
2020-03-30 19:33:09 +02:00
httpserver.expect_oneshot_request(
2020-04-01 02:23:38 +02:00
uri="/consumers",
2020-03-30 19:33:09 +02:00
method='GET',
data="").respond_with_json(
["10.69.42.1", "10.10.10.10", "10.20.30.40"])
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid)
ret = comm.discoveravailableconsumers()
assert isinstance(ret, list)
2020-03-30 19:33:09 +02:00
assert ret == ["10.69.42.1", "10.10.10.10", "10.20.30.40"]
def test_isconsumeravailable(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`communicator.Communicator.isconsumeravailable`.
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
2020-03-30 19:33:09 +02:00
httpserver.expect_oneshot_request(
2020-04-01 02:23:38 +02:00
uri="/consumers",
2020-03-30 19:33:09 +02:00
method='GET',
data="").respond_with_json(
["10.69.42.1", "10.10.10.10", "10.20.30.40"])
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid)
ret = comm.isconsumeravailable()
assert isinstance(ret, bool)
2020-03-30 19:33:09 +02:00
assert ret
ret2 = comm.isconsumeravailable()
assert isinstance(ret2, bool)
2020-03-30 19:33:09 +02:00
assert ret2 == False
comm2 = communicator.Communicator(
currentconsumer="127.0.0.1:69",
uuid=generateduuid)
ret3 = comm2.isconsumeravailable()
assert isinstance(ret3, bool)
2020-03-30 19:33:09 +02:00
assert ret3 == False
def test_checkconsumer(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`communicator.Communicator.checkconsumer`.
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
2020-03-30 19:33:09 +02:00
httpserver.expect_oneshot_request(
2020-04-01 02:23:38 +02:00
uri="/consumers",
2020-03-30 19:33:09 +02:00
method='GET',
data="").respond_with_json(
["10.69.42.1", "10.10.10.10", "10.20.30.40"])
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer="127.0.0.1",
uuid=generateduuid)
ret = comm.checkconsumer(f"127.0.0.1:{port}")
assert isinstance(ret, bool)
2020-03-30 19:33:09 +02:00
assert ret
ret2 = comm.checkconsumer(f"127.0.0.1:{port}")
assert isinstance(ret2, bool)
2020-03-30 19:33:09 +02:00
assert ret2 == False
comm2 = communicator.Communicator(
currentconsumer="127.0.0.1",
uuid=generateduuid)
ret3 = comm2.checkconsumer(f"127.0.0.1:{port}")
assert isinstance(ret3, bool)
2020-03-30 19:33:09 +02:00
assert ret3 == False
def test_setcurrentconsumer():
2020-04-06 20:48:36 +02:00
"""
Tests :func:`communicator.Communicator.set_currentconsumer`
2020-04-06 20:48:36 +02:00
:return: None
"""
2020-03-30 19:33:09 +02:00
comm = communicator.Communicator(
currentconsumer="127.0.0.1",
uuid=generateduuid)
comm.set_currentconsumer("10.69.42.1")
assert comm.currenctconsumer == "10.69.42.1"
def test_learnconsumerlist(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`consumerlocator.ConsumerLocator.learnconsumerlist`
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
2020-03-30 21:36:26 +02:00
httpserver.expect_request(
2020-04-01 02:23:38 +02:00
uri="/consumers",
2020-03-30 19:33:09 +02:00
method='GET',
2020-03-31 14:26:29 +02:00
data="").respond_with_json(
2020-03-30 19:33:09 +02:00
["10.69.42.1", "10.10.10.10", "10.20.30.40"])
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid)
2020-03-30 21:36:26 +02:00
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator(
2020-04-17 16:47:27 +02:00
uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
2020-03-30 19:33:09 +02:00
ret = locator.learnconsumerlist()
assert ret is None
2020-03-30 19:33:09 +02:00
def test_getcurrentconsumer(mocker):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`consumerlocator.ConsumerLocator.getcurrentconsumer`
:param mocker: patches the :class:`communicator.Communicator`.
2020-04-06 20:48:36 +02:00
:return: None
"""
2020-03-30 19:33:09 +02:00
mocker.patch('communicator.Communicator')
comm = communicator.Communicator(
currentconsumer="127.0.0.1",
uuid=generateduuid)
locator = consumerlocator.ConsumerLocator(
2020-04-17 16:47:27 +02:00
uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
2020-03-30 21:36:26 +02:00
assert locator.getcurrentconsumer() == consumerlocator.KNOWNCONSUMER
2020-03-30 19:33:09 +02:00
2020-03-30 19:33:09 +02:00
def test_checkcurrentconsumer(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`consumerlocator.ConsumerLocator.checkcurrentconsumer`
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
2020-03-30 19:33:09 +02:00
httpserver.expect_oneshot_request(
2020-04-01 02:23:38 +02:00
uri="/consumers",
2020-03-30 19:33:09 +02:00
method='GET',
data="").respond_with_json(
["10.69.42.1", "10.10.10.10", "10.20.30.40"])
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid)
2020-03-30 21:36:26 +02:00
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator(
2020-04-17 16:47:27 +02:00
uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
2020-03-30 19:33:09 +02:00
ret = locator.checkcurrentconsumer()
2020-03-30 21:36:26 +02:00
assert ret == True
2020-03-30 19:33:09 +02:00
2020-03-30 19:33:09 +02:00
def test_updateconsumer(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`consumerlocator.ConsumerLocator.updateconsumer`
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
2020-03-30 19:33:09 +02:00
httpserver.expect_oneshot_request(
2020-04-01 02:23:38 +02:00
uri="/consumers",
2020-03-30 19:33:09 +02:00
method='GET',
data="").respond_with_json(
["10.69.42.1", "10.10.10.10", "10.20.30.40"])
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid)
2020-03-30 21:36:26 +02:00
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator(
2020-04-17 16:47:27 +02:00
uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
assert locator.currentconsumer is not None
2020-03-30 19:33:09 +02:00
ret = locator.updateconsumer()
2020-03-30 21:36:26 +02:00
assert ret == f"127.0.0.1:{port}"
def test_updateconsumerlist(httpserver):
2020-04-06 20:48:36 +02:00
"""
Tests :func:`consumerlocator.ConsumerLocator.updateconsumerlist`
2020-04-06 20:48:36 +02:00
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request(
2020-04-01 02:23:38 +02:00
uri="/consumers",
method='GET',
data="").respond_with_json(
["10.69.42.1", "10.10.10.10", "10.20.30.40"])
url = httpserver.url_for("/")
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid)
2020-03-30 21:36:26 +02:00
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator(
2020-04-17 16:47:27 +02:00
uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
ret = locator.updateconsumerlist()
assert ret is None