Create unit tests #3

Merged
tormakris merged 9 commits from testing into master 2020-03-31 21:49:57 +02:00
3 changed files with 45 additions and 30 deletions
Showing only changes of commit 782d652c0d - Show all commits

4
app.py
View File

@ -23,10 +23,12 @@ sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9")
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
KNOWNCONSUMER= os.environ["PRODUCER_KNOWNCONSUMER"]
if __name__ == "__main__": if __name__ == "__main__":
LOGGER.info("Producer started") LOGGER.info("Producer started")
generateduuid = str(uuid) generateduuid = str(uuid)
communicator = Communicator(currentconsumer=os.environ["KnownConsumer"], uuid=generateduuid) communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid)
LOGGER.debug(f"My uuid is {generateduuid}") LOGGER.debug(f"My uuid is {generateduuid}")
consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator) consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator)
messagesender = MessageSender(communicator=communicator) messagesender = MessageSender(communicator=communicator)

View File

@ -13,6 +13,7 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "consumerlocator" __module_name__ = "consumerlocator"
__version__text__ = "1" __version__text__ = "1"
KNOWNCONSUMER= os.environ["PRODUCER_KNOWNCONSUMER"]
class ConsumerLocator: class ConsumerLocator:
@ -24,7 +25,7 @@ class ConsumerLocator:
""" """
Initialize class. Initialize class.
""" """
self.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}] self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
self.currentconsumer = self.consumerlist[0] self.currentconsumer = self.consumerlist[0]
self.communicator = communicator self.communicator = communicator

66
test.py
View File

@ -16,7 +16,8 @@ __module_name__ = "test"
__version__text__ = "1" __version__text__ = "1"
generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad' generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad'
os.environ["KnownConsumer"] = "10.1.2.3" os.environ["PRODUCER_KNOWNCONSUMER"] = "10.1.2.3"
def test_generate_string(mocker): def test_generate_string(mocker):
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
@ -69,7 +70,7 @@ def test_discoveravailableconsumers(httpserver):
currentconsumer=f"127.0.0.1:{port}", currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid) uuid=generateduuid)
ret = comm.discoveravailableconsumers() ret = comm.discoveravailableconsumers()
assert type(ret) is list assert isinstance(ret, list)
assert ret == ["10.69.42.1", "10.10.10.10", "10.20.30.40"] assert ret == ["10.69.42.1", "10.10.10.10", "10.20.30.40"]
@ -85,11 +86,11 @@ def test_isconsumeravailable(httpserver):
currentconsumer=f"127.0.0.1:{port}", currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid) uuid=generateduuid)
ret = comm.isconsumeravailable() ret = comm.isconsumeravailable()
assert type(ret) is bool assert isinstance(ret, bool)
assert ret assert ret
ret2 = comm.isconsumeravailable() ret2 = comm.isconsumeravailable()
assert type(ret2) is bool assert isinstance(ret2, bool)
assert ret2 == False assert ret2 == False
comm2 = communicator.Communicator( comm2 = communicator.Communicator(
@ -97,7 +98,7 @@ def test_isconsumeravailable(httpserver):
uuid=generateduuid) uuid=generateduuid)
ret3 = comm2.isconsumeravailable() ret3 = comm2.isconsumeravailable()
assert type(ret3) is bool assert isinstance(ret3, bool)
assert ret3 == False assert ret3 == False
@ -113,11 +114,11 @@ def test_checkconsumer(httpserver):
currentconsumer="127.0.0.1", currentconsumer="127.0.0.1",
uuid=generateduuid) uuid=generateduuid)
ret = comm.checkconsumer(f"127.0.0.1:{port}") ret = comm.checkconsumer(f"127.0.0.1:{port}")
assert type(ret) is bool assert isinstance(ret, bool)
assert ret assert ret
ret2 = comm.checkconsumer(f"127.0.0.1:{port}") ret2 = comm.checkconsumer(f"127.0.0.1:{port}")
assert type(ret2) is bool assert isinstance(ret2, bool)
assert ret2 == False assert ret2 == False
comm2 = communicator.Communicator( comm2 = communicator.Communicator(
@ -125,7 +126,7 @@ def test_checkconsumer(httpserver):
uuid=generateduuid) uuid=generateduuid)
ret3 = comm2.checkconsumer(f"127.0.0.1:{port}") ret3 = comm2.checkconsumer(f"127.0.0.1:{port}")
assert type(ret3) is bool assert isinstance(ret3, bool)
assert ret3 == False assert ret3 == False
@ -136,20 +137,6 @@ def test_setcurrentconsumer():
comm.set_currentconsumer("10.69.42.1") comm.set_currentconsumer("10.69.42.1")
assert comm.currenctconsumer == "10.69.42.1" assert comm.currenctconsumer == "10.69.42.1"
def test_updateconsumerlist(httpserver):
httpserver.expect_oneshot_request(
uri="/consumer",
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)
locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm)
ret = locator.updateconsumerlist()
assert ret is None
def test_learnconsumerlist(httpserver): def test_learnconsumerlist(httpserver):
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
@ -162,18 +149,22 @@ def test_learnconsumerlist(httpserver):
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}", currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid) uuid=generateduuid)
locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm)
ret = locator.learnconsumerlist() ret = locator.learnconsumerlist()
assert ret is None assert ret is None
def test_getcurrentconsumer(mocker): def test_getcurrentconsumer(mocker):
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer="127.0.0.1", currentconsumer="127.0.0.1",
uuid=generateduuid) uuid=generateduuid)
locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm)
assert locator.getcurrentconsumer() == "10.1.2.3" assert locator.getcurrentconsumer() == "10.1.2.3"
def test_checkcurrentconsumer(httpserver): def test_checkcurrentconsumer(httpserver):
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumer", uri="/consumer",
@ -185,10 +176,12 @@ def test_checkcurrentconsumer(httpserver):
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}", currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid) uuid=generateduuid)
locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm)
ret = locator.checkcurrentconsumer() ret = locator.checkcurrentconsumer()
assert ret == False assert ret == False
def test_updateconsumer(httpserver): def test_updateconsumer(httpserver):
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumer", uri="/consumer",
@ -200,7 +193,8 @@ def test_updateconsumer(httpserver):
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port}", currentconsumer=f"127.0.0.1:{port}",
uuid=generateduuid) uuid=generateduuid)
locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm)
ret = locator.updateconsumer() ret = locator.updateconsumer()
assert ret == "10.69.42.1" assert ret == "10.69.42.1"
@ -214,6 +208,24 @@ def test_updateconsumer(httpserver):
comm2 = communicator.Communicator( comm2 = communicator.Communicator(
currentconsumer=f"127.0.0.1:{port2}", currentconsumer=f"127.0.0.1:{port2}",
uuid=generateduuid) uuid=generateduuid)
locator2 = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm2) locator2 = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm2)
ret2 = locator2.updateconsumer() ret2 = locator2.updateconsumer()
assert ret2 is None assert ret2 is None
def test_updateconsumerlist(httpserver):
httpserver.expect_oneshot_request(
uri="/consumer",
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)
locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm)
ret = locator.updateconsumerlist()
assert ret is None