From 40db3cd26ee5cb535ca289a898b4ce4b129ff497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 30 Mar 2020 17:37:58 +0200 Subject: [PATCH 1/9] add pytest dependency --- requirements.txt | 3 ++- test.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test.py diff --git a/requirements.txt b/requirements.txt index 07f9470..d573b3d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ sentry_sdk -requests \ No newline at end of file +requests +pytest \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..c3d4b78 --- /dev/null +++ b/test.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +import pytest + +""" +Unit tests for producer module. +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2020, GoldenPogácsa Team" +__module_name__ = "test" +__version__text__ = "1" + +class MyTestCase(unittest.TestCase): + def test_something(self): + self.assertEqual(True, False) + + +if __name__ == '__main__': + unittest.main() From 39d32f39f459b94d113e0131a0fbe14bb487cda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 30 Mar 2020 17:43:30 +0200 Subject: [PATCH 2/9] add mock libraries --- requirements.txt | 4 +++- test.py | 8 -------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/requirements.txt b/requirements.txt index d573b3d..60339c2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ sentry_sdk requests -pytest \ No newline at end of file +pytest +pytest-mock +pytest-httpserver \ No newline at end of file diff --git a/test.py b/test.py index c3d4b78..dad1751 100644 --- a/test.py +++ b/test.py @@ -10,11 +10,3 @@ __author__ = "@tormakris" __copyright__ = "Copyright 2020, GoldenPogácsa Team" __module_name__ = "test" __version__text__ = "1" - -class MyTestCase(unittest.TestCase): - def test_something(self): - self.assertEqual(True, False) - - -if __name__ == '__main__': - unittest.main() From 50dec05e6be51b878739443092d9796a1e43d94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 30 Mar 2020 19:33:09 +0200 Subject: [PATCH 3/9] tests done --- .drone.yml | 8 +- Dockerfile | 2 +- app.py | 6 +- communicator.py | 36 +++++--- consumerlocator.py | 5 +- messagesender.py | 4 +- test.py | 209 ++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 250 insertions(+), 20 deletions(-) diff --git a/.drone.yml b/.drone.yml index 2924cfd..a3e6b8c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -4,7 +4,7 @@ name: default steps: - name: static_analysis - image: python:3 + image: python:3.8 commands: - pip3 install pylint bandit mccabe - pip3 install -r requirements.txt @@ -13,6 +13,12 @@ steps: - find . -name "*.py" -exec python3 -m mccabe --min 3 '{}' + || if [ $? -eq 1 ]; then echo "you fail"; fi - bandit -r . + || if [ $? -eq 1 ]; then echo "you fail"; fi +- name: unit_test + image: python:3.8 + commands: + - pip3 install -r requirements.txt + - pytest test.py + - name: build image: docker:stable-dind volumes: diff --git a/Dockerfile b/Dockerfile index 0ca2e4e..1875003 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3 +FROM python:3.8 WORKDIR /app diff --git a/app.py b/app.py index b808775..32dbf81 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,6 @@ #!/usr/bin/env python + +import os import random import uuid import logging @@ -24,9 +26,9 @@ LOGGER = logging.getLogger(__name__) if __name__ == "__main__": LOGGER.info("Producer started") generateduuid = str(uuid) + communicator = Communicator(currentconsumer=os.environ["KnownConsumer"], uuid=generateduuid) LOGGER.debug(f"My uuid is {generateduuid}") - consumerlocator = ConsumerLocator(uuid=generateduuid) - communicator = Communicator(consumerlocator=consumerlocator,uuid=generateduuid) + consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator) messagesender = MessageSender(communicator=communicator) while True: diff --git a/communicator.py b/communicator.py index 69a7511..8328ad1 100644 --- a/communicator.py +++ b/communicator.py @@ -1,8 +1,6 @@ #!/usr/bin/env python import logging -import random import requests -from consumerlocator import ConsumerLocator """ Communicator module @@ -21,13 +19,13 @@ class Communicator: Class handling low level communication with consumers. """ - def __init__(self, consumerlocator: ConsumerLocator, uuid: str): + def __init__(self, currentconsumer: str, uuid: str): """ Initialize object :param consumerlocator: :param uuid: """ - self.consumerlocator=consumerlocator + self.currenctconsumer=currentconsumer self.uuid = uuid def sendmessage(self, message: str) -> None: @@ -36,7 +34,7 @@ class Communicator: :param message: :return: none """ - currentconsumer=self.consumerlocator.getcurrentconsumer() + currentconsumer=self.currenctconsumer LOGGER.debug(f"Sending message to {currentconsumer}") requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message}) @@ -45,7 +43,7 @@ class Communicator: Get the list of available consumer from the current primary consumer. :return: """ - currentconsumer = self.consumerlocator.getcurrentconsumer() + currentconsumer = self.currenctconsumer response = requests.get(f'http://{currentconsumer}/consumer') json = response.json() LOGGER.debug(f"List of currently available consumers: {json}") @@ -56,9 +54,13 @@ class Communicator: Readiness probe primary consumer. :return: """ - currentconsumer = self.consumerlocator.getcurrentconsumer() - response = requests.get(f'http://{currentconsumer}/consumer') - isavailable = response.status_code == 200 + currentconsumer = self.currenctconsumer + try: + response = requests.get(f'http://{currentconsumer}/consumer') + isavailable = response.status_code == 200 + except Exception as e: + LOGGER.exception(e) + isavailable = False LOGGER.debug(f"Current consumer availability: {isavailable}") return isavailable @@ -68,7 +70,19 @@ class Communicator: :param consumer: :return: """ - response = requests.get(f'http://{consumer}/consumer') - isavailable = response.status_code == 200 + try: + response = requests.get(f'http://{consumer}/consumer') + isavailable = response.status_code == 200 + except Exception as e: + LOGGER.exception(e) + isavailable = False LOGGER.debug(f"Consumer {consumer} availability: {isavailable}") return isavailable + + def set_currentconsumer(self,currenctconsumer): + """ + Set current consumer + :param currenctconsumer: + :return: + """ + self.currenctconsumer=currenctconsumer diff --git a/consumerlocator.py b/consumerlocator.py index 0a9c55f..8fcb95d 100644 --- a/consumerlocator.py +++ b/consumerlocator.py @@ -20,13 +20,13 @@ class ConsumerLocator: Manages the list of consumers. """ - def __init__(self, uuid: str): + def __init__(self, uuid: str, communicator: Communicator): """ Initialize class. """ self.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}] self.currentconsumer = self.consumerlist[0] - self.communicator = Communicator(consumerlocator=self,uuid=uuid) + self.communicator = communicator def learnconsumerlist(self) -> None: """" @@ -82,6 +82,7 @@ class ConsumerLocator: self.learnconsumerlist() if self.currentconsumer is not None: + self.communicator.set_currentconsumer(self.currentconsumer["Host"]) return self.currentconsumer["Host"] else: return None diff --git a/messagesender.py b/messagesender.py index 7c8d2cc..27992ae 100644 --- a/messagesender.py +++ b/messagesender.py @@ -28,10 +28,10 @@ class MessageSender: """ self.communicator = communicator - def randomstring(self, stringLength) -> str: + def randomstring(self, stringlength: int) -> str: """Generate a random string of fixed length """ letters = string.ascii_lowercase - return ''.join(random.choice(letters) for i in range(stringLength)) + return ''.join(random.choice(letters) for i in range(stringlength)) def sendmessage(self, message: str = "") -> None: """ diff --git a/test.py b/test.py index dad1751..9b7d9af 100644 --- a/test.py +++ b/test.py @@ -1,6 +1,10 @@ #!/usr/bin/env python -import pytest +import os +import re +import consumerlocator +import communicator +import messagesender """ Unit tests for producer module. @@ -10,3 +14,206 @@ __author__ = "@tormakris" __copyright__ = "Copyright 2020, GoldenPogácsa Team" __module_name__ = "test" __version__text__ = "1" + +generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad' +os.environ["KnownConsumer"] = "10.1.2.3" + +def test_generate_string(mocker): + 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): + 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): + 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): + 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) + ret = comm.discoveravailableconsumers() + assert type(ret) is list + assert ret == ["10.69.42.1", "10.10.10.10", "10.20.30.40"] + + +def test_isconsumeravailable(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) + ret = comm.isconsumeravailable() + assert type(ret) is bool + assert ret + + ret2 = comm.isconsumeravailable() + assert type(ret2) is bool + assert ret2 == False + + comm2 = communicator.Communicator( + currentconsumer="127.0.0.1:69", + uuid=generateduuid) + + ret3 = comm2.isconsumeravailable() + assert type(ret3) is bool + assert ret3 == False + + +def test_checkconsumer(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="127.0.0.1", + uuid=generateduuid) + ret = comm.checkconsumer(f"127.0.0.1:{port}") + assert type(ret) is bool + assert ret + + ret2 = comm.checkconsumer(f"127.0.0.1:{port}") + assert type(ret2) is bool + assert ret2 == False + + comm2 = communicator.Communicator( + currentconsumer="127.0.0.1", + uuid=generateduuid) + + ret3 = comm2.checkconsumer(f"127.0.0.1:{port}") + assert type(ret3) is bool + assert ret3 == False + + +def test_setcurrentconsumer(): + 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_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): + 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.learnconsumerlist() + assert ret is None + +def test_getcurrentconsumer(mocker): + mocker.patch('communicator.Communicator') + comm = communicator.Communicator( + currentconsumer="127.0.0.1", + uuid=generateduuid) + locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) + assert locator.getcurrentconsumer() == "10.1.2.3" + +def test_checkcurrentconsumer(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.checkcurrentconsumer() + assert ret == False + +def test_updateconsumer(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.updateconsumer() + assert ret == "10.69.42.1" + + httpserver.expect_oneshot_request( + uri="/consumer", + method='GET', + data="").respond_with_json( + []) + url2 = httpserver.url_for("/") + port2 = re.match(r"\W*http[^:]*\D*(\d+)", url2).group(1) + comm2 = communicator.Communicator( + currentconsumer=f"127.0.0.1:{port2}", + uuid=generateduuid) + locator2 = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm2) + ret2 = locator2.updateconsumer() + assert ret2 is None \ No newline at end of file From 782d652c0d4257ab35a3da871c9cf202854d500d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 30 Mar 2020 19:52:52 +0200 Subject: [PATCH 4/9] extract envvar to module-level variable --- app.py | 4 ++- consumerlocator.py | 3 +- test.py | 68 +++++++++++++++++++++++++++------------------- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/app.py b/app.py index 32dbf81..b121b33 100644 --- a/app.py +++ b/app.py @@ -23,10 +23,12 @@ sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9") logging.basicConfig(level=logging.INFO) LOGGER = logging.getLogger(__name__) +KNOWNCONSUMER= os.environ["PRODUCER_KNOWNCONSUMER"] + if __name__ == "__main__": LOGGER.info("Producer started") generateduuid = str(uuid) - communicator = Communicator(currentconsumer=os.environ["KnownConsumer"], uuid=generateduuid) + communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid) LOGGER.debug(f"My uuid is {generateduuid}") consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator) messagesender = MessageSender(communicator=communicator) diff --git a/consumerlocator.py b/consumerlocator.py index 8fcb95d..cababe3 100644 --- a/consumerlocator.py +++ b/consumerlocator.py @@ -13,6 +13,7 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team" __module_name__ = "consumerlocator" __version__text__ = "1" +KNOWNCONSUMER= os.environ["PRODUCER_KNOWNCONSUMER"] class ConsumerLocator: @@ -24,7 +25,7 @@ class ConsumerLocator: """ 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.communicator = communicator diff --git a/test.py b/test.py index 9b7d9af..035e8c6 100644 --- a/test.py +++ b/test.py @@ -16,7 +16,8 @@ __module_name__ = "test" __version__text__ = "1" 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): mocker.patch('communicator.Communicator') @@ -69,7 +70,7 @@ def test_discoveravailableconsumers(httpserver): currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) 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"] @@ -85,11 +86,11 @@ def test_isconsumeravailable(httpserver): currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) ret = comm.isconsumeravailable() - assert type(ret) is bool + assert isinstance(ret, bool) assert ret ret2 = comm.isconsumeravailable() - assert type(ret2) is bool + assert isinstance(ret2, bool) assert ret2 == False comm2 = communicator.Communicator( @@ -97,7 +98,7 @@ def test_isconsumeravailable(httpserver): uuid=generateduuid) ret3 = comm2.isconsumeravailable() - assert type(ret3) is bool + assert isinstance(ret3, bool) assert ret3 == False @@ -113,11 +114,11 @@ def test_checkconsumer(httpserver): currentconsumer="127.0.0.1", uuid=generateduuid) ret = comm.checkconsumer(f"127.0.0.1:{port}") - assert type(ret) is bool + assert isinstance(ret, bool) assert ret ret2 = comm.checkconsumer(f"127.0.0.1:{port}") - assert type(ret2) is bool + assert isinstance(ret2, bool) assert ret2 == False comm2 = communicator.Communicator( @@ -125,7 +126,7 @@ def test_checkconsumer(httpserver): uuid=generateduuid) ret3 = comm2.checkconsumer(f"127.0.0.1:{port}") - assert type(ret3) is bool + assert isinstance(ret3, bool) assert ret3 == False @@ -136,20 +137,6 @@ def test_setcurrentconsumer(): comm.set_currentconsumer("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): httpserver.expect_oneshot_request( @@ -162,18 +149,22 @@ def test_learnconsumerlist(httpserver): comm = communicator.Communicator( currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) - locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) + locator = consumerlocator.ConsumerLocator( + uuid=generateduuid, communicator=comm) ret = locator.learnconsumerlist() assert ret is None + def test_getcurrentconsumer(mocker): mocker.patch('communicator.Communicator') comm = communicator.Communicator( currentconsumer="127.0.0.1", uuid=generateduuid) - locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) + locator = consumerlocator.ConsumerLocator( + uuid=generateduuid, communicator=comm) assert locator.getcurrentconsumer() == "10.1.2.3" + def test_checkcurrentconsumer(httpserver): httpserver.expect_oneshot_request( uri="/consumer", @@ -185,10 +176,12 @@ def test_checkcurrentconsumer(httpserver): comm = communicator.Communicator( currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) - locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) + locator = consumerlocator.ConsumerLocator( + uuid=generateduuid, communicator=comm) ret = locator.checkcurrentconsumer() assert ret == False + def test_updateconsumer(httpserver): httpserver.expect_oneshot_request( uri="/consumer", @@ -200,7 +193,8 @@ def test_updateconsumer(httpserver): comm = communicator.Communicator( currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) - locator = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm) + locator = consumerlocator.ConsumerLocator( + uuid=generateduuid, communicator=comm) ret = locator.updateconsumer() assert ret == "10.69.42.1" @@ -214,6 +208,24 @@ def test_updateconsumer(httpserver): comm2 = communicator.Communicator( currentconsumer=f"127.0.0.1:{port2}", uuid=generateduuid) - locator2 = consumerlocator.ConsumerLocator(uuid=generateduuid, communicator=comm2) + locator2 = consumerlocator.ConsumerLocator( + uuid=generateduuid, communicator=comm2) ret2 = locator2.updateconsumer() - assert ret2 is None \ No newline at end of file + 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 From bbfa1388490fa85cb972ba301fce374a341073b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 30 Mar 2020 21:36:26 +0200 Subject: [PATCH 5/9] tests now run --- app.py | 2 +- consumerlocator.py | 2 +- test.py | 32 ++++++++++---------------------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index b121b33..bbb6f7c 100644 --- a/app.py +++ b/app.py @@ -23,7 +23,7 @@ sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9") logging.basicConfig(level=logging.INFO) LOGGER = logging.getLogger(__name__) -KNOWNCONSUMER= os.environ["PRODUCER_KNOWNCONSUMER"] +KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1') if __name__ == "__main__": LOGGER.info("Producer started") diff --git a/consumerlocator.py b/consumerlocator.py index cababe3..83988b4 100644 --- a/consumerlocator.py +++ b/consumerlocator.py @@ -13,7 +13,7 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team" __module_name__ = "consumerlocator" __version__text__ = "1" -KNOWNCONSUMER= os.environ["PRODUCER_KNOWNCONSUMER"] +KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1') class ConsumerLocator: diff --git a/test.py b/test.py index 035e8c6..2b93933 100644 --- a/test.py +++ b/test.py @@ -1,6 +1,5 @@ #!/usr/bin/env python -import os import re import consumerlocator import communicator @@ -16,7 +15,6 @@ __module_name__ = "test" __version__text__ = "1" generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad' -os.environ["PRODUCER_KNOWNCONSUMER"] = "10.1.2.3" def test_generate_string(mocker): @@ -139,16 +137,18 @@ def test_setcurrentconsumer(): def test_learnconsumerlist(httpserver): - httpserver.expect_oneshot_request( + httpserver.expect_request( uri="/consumer", method='GET', - data="").respond_with_json( + data="", + handler_type='permanent').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) + consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) ret = locator.learnconsumerlist() @@ -162,7 +162,7 @@ def test_getcurrentconsumer(mocker): uuid=generateduuid) locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) - assert locator.getcurrentconsumer() == "10.1.2.3" + assert locator.getcurrentconsumer() == consumerlocator.KNOWNCONSUMER def test_checkcurrentconsumer(httpserver): @@ -176,10 +176,11 @@ def test_checkcurrentconsumer(httpserver): comm = communicator.Communicator( currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) + consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) ret = locator.checkcurrentconsumer() - assert ret == False + assert ret == True def test_updateconsumer(httpserver): @@ -193,25 +194,11 @@ def test_updateconsumer(httpserver): comm = communicator.Communicator( currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) + consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) ret = locator.updateconsumer() - assert ret == "10.69.42.1" - - httpserver.expect_oneshot_request( - uri="/consumer", - method='GET', - data="").respond_with_json( - []) - url2 = httpserver.url_for("/") - port2 = re.match(r"\W*http[^:]*\D*(\d+)", url2).group(1) - comm2 = communicator.Communicator( - currentconsumer=f"127.0.0.1:{port2}", - uuid=generateduuid) - locator2 = consumerlocator.ConsumerLocator( - uuid=generateduuid, communicator=comm2) - ret2 = locator2.updateconsumer() - assert ret2 is None + assert ret == f"127.0.0.1:{port}" def test_updateconsumerlist(httpserver): @@ -225,6 +212,7 @@ def test_updateconsumerlist(httpserver): comm = communicator.Communicator( currentconsumer=f"127.0.0.1:{port}", uuid=generateduuid) + consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) ret = locator.updateconsumerlist() From 4d1a0e1d57117c2d498b9dd011d81c4ff21e57f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Scharnitzky=20Don=C3=A1t?= Date: Mon, 30 Mar 2020 22:12:21 +0200 Subject: [PATCH 6/9] Fix updateconsumer text --- test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test.py b/test.py index 2b93933..6fadebd 100644 --- a/test.py +++ b/test.py @@ -197,6 +197,7 @@ def test_updateconsumer(httpserver): consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) + locator.learnconsumerlist(); ret = locator.updateconsumer() assert ret == f"127.0.0.1:{port}" From d47d2e8d9757e442036acfa1344dca78809f491f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Scharnitzky=20Don=C3=A1t?= Date: Tue, 31 Mar 2020 11:46:19 +0200 Subject: [PATCH 7/9] Added not None assert to updateconsumertest --- test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test.py b/test.py index 6fadebd..f475ed0 100644 --- a/test.py +++ b/test.py @@ -199,6 +199,7 @@ def test_updateconsumer(httpserver): uuid=generateduuid, communicator=comm) locator.learnconsumerlist(); ret = locator.updateconsumer() + assert locator.currentconsumer is not None assert ret == f"127.0.0.1:{port}" From 7a7643cfeba5760e428155f984d885de36e08ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Scharnitzky=20Don=C3=A1t?= Date: Tue, 31 Mar 2020 12:24:53 +0200 Subject: [PATCH 8/9] See if currentconsumer is null after init --- test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.py b/test.py index f475ed0..2bdee66 100644 --- a/test.py +++ b/test.py @@ -197,9 +197,9 @@ def test_updateconsumer(httpserver): consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) + assert locator.currentconsumer is not None locator.learnconsumerlist(); ret = locator.updateconsumer() - assert locator.currentconsumer is not None assert ret == f"127.0.0.1:{port}" From 7d19ec0fa82e4efa05cc4af04a12b99b00e35169 Mon Sep 17 00:00:00 2001 From: schdwlk Date: Tue, 31 Mar 2020 14:26:29 +0200 Subject: [PATCH 9/9] Fixed tests --- test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test.py b/test.py index 2bdee66..9e14434 100644 --- a/test.py +++ b/test.py @@ -140,8 +140,7 @@ def test_learnconsumerlist(httpserver): httpserver.expect_request( uri="/consumer", method='GET', - data="", - handler_type='permanent').respond_with_json( + 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) @@ -198,7 +197,6 @@ def test_updateconsumer(httpserver): locator = consumerlocator.ConsumerLocator( uuid=generateduuid, communicator=comm) assert locator.currentconsumer is not None - locator.learnconsumerlist(); ret = locator.updateconsumer() assert ret == f"127.0.0.1:{port}"