Compare commits
21 Commits
58c4e296ea
...
master
Author | SHA1 | Date | |
---|---|---|---|
489883b464 | |||
f3e8750ff1 | |||
c50cd6b23a | |||
32f104df7c | |||
957227b715 | |||
6bdcb40220 | |||
0ab07cb1a2 | |||
20a1aa6dfa | |||
f782d167ef | |||
d82cc3910b | |||
ea7982e0ef | |||
926ed41a0f | |||
f015eb2301 | |||
4610c7a42f | |||
112a7fc8c2 | |||
3d4274db86 | |||
02e3dc5267 | |||
f898af4558 | |||
2cb6ef3a6c | |||
c5a3fb2d04
|
|||
22ebf01e20
|
2
.coveragerc
Normal file
2
.coveragerc
Normal file
@ -0,0 +1,2 @@
|
||||
[run]
|
||||
omit=venv/*
|
66
.drone.yml
66
.drone.yml
@ -3,11 +3,25 @@ type: docker
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: restore-cache-with-filesystem
|
||||
image: meltwater/drone-cache
|
||||
settings:
|
||||
backend: "filesystem"
|
||||
restore: true
|
||||
cache_key: "{{ .Repo.Name }}"
|
||||
archive_format: "gzip"
|
||||
filesystem_cache_root: "/tmp/cache"
|
||||
mount:
|
||||
- '.pipcache'
|
||||
volumes:
|
||||
- name: cache
|
||||
path: /tmp/cache
|
||||
|
||||
- name: static_analysis
|
||||
image: python:3.8
|
||||
commands:
|
||||
- pip3 install pylint bandit mccabe
|
||||
- pip3 install -r requirements.txt
|
||||
- pip3 install --cache-dir='./.pipcache' pylint bandit mccabe
|
||||
- pip3 install --cache-dir='./.pipcache' -r requirements.txt
|
||||
- find . -name "*.py" -exec python3 -m py_compile '{}' \;
|
||||
- find . -name "*.py" -exec pylint '{}' + || if [ $? -eq 1 ]; then echo "you fail"; fi
|
||||
- find . -name "*.py" -exec python3 -m mccabe --min 3 '{}' + || if [ $? -eq 1 ]; then echo "you fail"; fi
|
||||
@ -15,9 +29,29 @@ steps:
|
||||
|
||||
- name: unit_test
|
||||
image: python:3.8
|
||||
environment:
|
||||
PRODUCER_REDIS: cache
|
||||
commands:
|
||||
- pip3 install -r requirements.txt
|
||||
- echo -e 'pytest test.py'
|
||||
- pip3 install --cache-dir='./.pipcache' -r requirements.txt
|
||||
- pytest test.py
|
||||
|
||||
- name: coverage
|
||||
image: python:3.8
|
||||
environment:
|
||||
PRODUCER_REDIS: cache
|
||||
commands:
|
||||
- pip3 install --cache-dir='./.pipcache' -r requirements.txt
|
||||
- pip3 install --cache-dir='./.pipcache' coverage pytest
|
||||
- coverage run -m pytest test.py
|
||||
- coverage report -m
|
||||
|
||||
- name: integration_test
|
||||
image: python:3.8
|
||||
environment:
|
||||
PRODUCER_REDIS: cache
|
||||
commands:
|
||||
- pip3 install --cache-dir='./.pipcache' -r requirements.txt
|
||||
- pytest integtest.py
|
||||
|
||||
- name: build-app
|
||||
image: banzaicloud/drone-kaniko
|
||||
@ -35,11 +69,26 @@ steps:
|
||||
- name: make_docs
|
||||
image: python:3.8
|
||||
commands:
|
||||
- pip3 install Sphinx sphinx_rtd_theme
|
||||
- pip3 install -r requirements.txt
|
||||
- pip3 install --cache-dir='./.pipcache' Sphinx sphinx_rtd_theme
|
||||
- pip3 install --cache-dir='./.pipcache' -r requirements.txt
|
||||
- cd docs
|
||||
- make html
|
||||
|
||||
- name: rebuild-cache-with-filesystem
|
||||
image: meltwater/drone-cache
|
||||
pull: true
|
||||
settings:
|
||||
backend: "filesystem"
|
||||
rebuild: true
|
||||
cache_key: "{{ .Repo.Name }}"
|
||||
archive_format: "gzip"
|
||||
filesystem_cache_root: "/tmp/cache"
|
||||
mount:
|
||||
- '.pipcache'
|
||||
volumes:
|
||||
- name: cache
|
||||
path: /tmp/cache
|
||||
|
||||
- name: build_docs
|
||||
image: docker:stable-dind
|
||||
volumes:
|
||||
@ -75,7 +124,12 @@ services:
|
||||
volumes:
|
||||
- name: dockersock
|
||||
path: /var/run
|
||||
- name: cache
|
||||
image: redis
|
||||
|
||||
volumes:
|
||||
- name: dockersock
|
||||
temp: {}
|
||||
- name: cache
|
||||
host:
|
||||
path: "/tmp/cache"
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -131,3 +131,4 @@ dmypy.json
|
||||
#Pycharm
|
||||
.idea/
|
||||
*.iml
|
||||
.coverage
|
@ -10,5 +10,3 @@ clone the main repository and run
|
||||
*docker-compose up.*
|
||||
|
||||
Produced by GoldenPogácsa Inc.
|
||||
|
||||
TODO
|
||||
|
18
app.py
18
app.py
@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Main entry point, This module builds the producer from the submodules.
|
||||
Main entry point, this module builds the producer from the submodules.
|
||||
"""
|
||||
|
||||
import os
|
||||
@ -10,6 +10,7 @@ import uuid
|
||||
import logging
|
||||
import sentry_sdk
|
||||
import time
|
||||
from consumerinformation import ConsumerInformation
|
||||
from communicator import Communicator
|
||||
from consumerlocator import ConsumerLocator
|
||||
from messagesender import MessageSender
|
||||
@ -34,15 +35,22 @@ if __name__ == "__main__":
|
||||
"""
|
||||
This is the producers entry point, initializes all the components (:class:`communicator.Communicator`,
|
||||
:class:`consumerlocator.ConsumerLocator` and :class:`messagesender.MessageSender`) and sends infinite random
|
||||
messages.
|
||||
messages. Basically this is a big loop, learning about consumers ( :func:`consumerlocator.ConsumerLocator.learnconsumerlist` ),
|
||||
updating the current ( :func:`consumerlocator.ConsumerLocator.updateconsumer` ) one and sending the message
|
||||
( :func:`messagesender.MessageSender.sendmessage` ).
|
||||
If the current consumer is unavailable, the update will change to an available one. To not flood the network with
|
||||
infinite data, we some random time.
|
||||
"""
|
||||
LOGGER.info("Producer started")
|
||||
generateduuid = str(uuid.uuid4())
|
||||
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid)
|
||||
redisconnector = RedisConnector()
|
||||
consumerinfomation = ConsumerInformation(redisconnector=redisconnector)
|
||||
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid,
|
||||
consumerinformation=consumerinfomation)
|
||||
LOGGER.debug(f"My uuid is {generateduuid}")
|
||||
messagesender = MessageSender(communicator=communicator, uuid=generateduuid)
|
||||
consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator,
|
||||
redisconnector=RedisConnector())
|
||||
consumerlocator = ConsumerLocator(communicator=communicator,
|
||||
redisconnector=redisconnector)
|
||||
|
||||
while True:
|
||||
consumerlocator.learnconsumerlist()
|
||||
|
@ -7,6 +7,7 @@ Communicator module
|
||||
import logging
|
||||
import requests
|
||||
import requests.exceptions
|
||||
from consumerinformation import ConsumerInformation
|
||||
|
||||
__author__ = "@tormakris"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
@ -16,20 +17,22 @@ __version__text__ = "1"
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Communicator:
|
||||
"""
|
||||
Class handling low level communication with consumers.
|
||||
"""
|
||||
|
||||
def __init__(self, currentconsumer: str, uuid: str):
|
||||
def __init__(self, currentconsumer: str, uuid: str, consumerinformation: ConsumerInformation):
|
||||
"""**Constructor:**
|
||||
Initializes the object.
|
||||
|
||||
:param consumerlocator: the current consumer's IP address as a string
|
||||
:param currentconsumer: the current consumer's IP address as a string
|
||||
:param uuid: string typed UUID.
|
||||
"""
|
||||
self.currenctconsumer=currentconsumer
|
||||
self.currenctconsumer = currentconsumer
|
||||
self.uuid = uuid
|
||||
self.consumerinformation = consumerinformation
|
||||
|
||||
def sendmessage(self, message: str) -> None:
|
||||
"""Send message to the current consumer. Logs the process.
|
||||
@ -37,13 +40,18 @@ class Communicator:
|
||||
:param message: the message of type string that will be sent.
|
||||
:return: None
|
||||
"""
|
||||
currentconsumer=self.currenctconsumer
|
||||
currentconsumer = self.currenctconsumer
|
||||
LOGGER.info(f"Sending message to {currentconsumer}")
|
||||
|
||||
for consumer in self.consumerinformation.getconsumerlist():
|
||||
try:
|
||||
postresponse=requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message}, timeout=5)
|
||||
postresponse = requests.post(f'http://{consumer}/log', json={'uuid': self.uuid, 'message': message},
|
||||
timeout=5)
|
||||
LOGGER.debug(f"Message status code is:{postresponse.status_code}")
|
||||
if postresponse.status_code < 300:
|
||||
return None
|
||||
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
|
||||
LOGGER.exception(e) # Fun fact: ez azt jelenti, hogy elveszett az üzenet... ide valami retry kellene inkább más consumerek felé...
|
||||
LOGGER.warning(f"Could not send message to {consumer}")
|
||||
|
||||
def discoveravailableconsumers(self) -> list:
|
||||
"""Get the list of available consumer from the current primary consumer. Logs the received list.
|
||||
@ -57,8 +65,8 @@ class Communicator:
|
||||
LOGGER.info(f"List of currently available consumers: {json}")
|
||||
return json
|
||||
except Exception as e:
|
||||
LOGGER.error("Could not query available consumer list!")
|
||||
#LOGGER.exception(e)
|
||||
LOGGER.warning("Could not query available consumer list!")
|
||||
# LOGGER.exception(e)
|
||||
return []
|
||||
|
||||
def isconsumeravailable(self) -> bool:
|
||||
@ -71,7 +79,7 @@ class Communicator:
|
||||
response = requests.get(f'http://{currentconsumer}/consumers', timeout=5)
|
||||
isavailable = response.status_code == 200
|
||||
except Exception as e:
|
||||
#LOGGER.exception(e)
|
||||
# LOGGER.exception(e)
|
||||
isavailable = False
|
||||
LOGGER.info(f"Current consumer availability: {isavailable}")
|
||||
return isavailable
|
||||
@ -86,15 +94,15 @@ class Communicator:
|
||||
response = requests.get(f'http://{consumer}/consumers', timeout=5)
|
||||
isavailable = response.status_code == 200
|
||||
except Exception as e:
|
||||
#LOGGER.exception(e)
|
||||
# LOGGER.exception(e)
|
||||
isavailable = False
|
||||
LOGGER.info(f"Consumer {consumer} availability: {isavailable}")
|
||||
return isavailable
|
||||
|
||||
def set_currentconsumer(self,currenctconsumer) -> None:
|
||||
def set_currentconsumer(self, currenctconsumer) -> None:
|
||||
"""Set current consumer
|
||||
|
||||
:param currenctconsumer: the consumer's IP address
|
||||
:return: None
|
||||
"""
|
||||
self.currenctconsumer=currenctconsumer
|
||||
self.currenctconsumer = currenctconsumer
|
||||
|
37
consumerinformation.py
Normal file
37
consumerinformation.py
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Consumer locator module, that manages the list of consumers.
|
||||
"""
|
||||
|
||||
from redisconnector import RedisConnector
|
||||
|
||||
__author__ = "@ricsik52"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
__module_name__ = "consumerlocator"
|
||||
__version__text__ = "1"
|
||||
|
||||
|
||||
class ConsumerInformation:
|
||||
"""
|
||||
Component responsible for providing high level information about consumers
|
||||
"""
|
||||
|
||||
def __init__(self, redisconnector: RedisConnector):
|
||||
"""**Constructor:**
|
||||
Initializes the object.
|
||||
|
||||
:param redisconnector: the :class:'redisconnector.RedisConnector' instance that will be used for Redis connection.
|
||||
"""
|
||||
self.red = redisconnector
|
||||
|
||||
def getconsumerlist(self) -> list:
|
||||
"""
|
||||
Gets the list of currently available consumers.
|
||||
:return:
|
||||
"""
|
||||
toreturn = []
|
||||
for consumer in self.red.consumerlist:
|
||||
if consumer['State']:
|
||||
toreturn.append(consumer['Host'])
|
||||
return toreturn
|
@ -18,19 +18,18 @@ __version__text__ = "1"
|
||||
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER", '10.69.42.1')
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class ConsumerLocator:
|
||||
|
||||
class ConsumerLocator:
|
||||
"""
|
||||
Component responsible for managing the list of consumers. Requires an instance of :class:`communicator.Communicator`
|
||||
"""
|
||||
|
||||
def __init__(self, uuid: str, communicator: Communicator, redisconnector: RedisConnector):
|
||||
def __init__(self, communicator: Communicator, redisconnector: RedisConnector):
|
||||
"""**Constructor:**
|
||||
Initializes the object.
|
||||
|
||||
Gets the known consumer's IP address from the PRODUCER_KNOWNCONSUMER envar.
|
||||
|
||||
:param uuid: Not used
|
||||
:param communicator: the :class:'communicator.Communicator' instance that will be used for the low level communication.
|
||||
"""
|
||||
self.red = redisconnector
|
||||
@ -50,18 +49,18 @@ class ConsumerLocator:
|
||||
if not recievedconsumerlist:
|
||||
return
|
||||
|
||||
|
||||
consumer_list = self.red.consumerlist
|
||||
|
||||
for recconsumer in recievedconsumerlist:
|
||||
contains = False
|
||||
for consumer in self.red.consumerlist:
|
||||
for consumer in consumer_list:
|
||||
if consumer["Host"] == recconsumer:
|
||||
contains = True
|
||||
|
||||
if not contains:
|
||||
LOGGER.info(f"Learned about new consumer at {recconsumer}")
|
||||
consumer_list.append({"Host": recconsumer, "State": True, "LastOk": datetime.datetime.now().timestamp()})
|
||||
consumer_list.append(
|
||||
{"Host": recconsumer, "State": True, "LastOk": datetime.datetime.now().timestamp()})
|
||||
|
||||
self.red.consumerlist = consumer_list
|
||||
self.updateconsumerlist()
|
||||
@ -80,7 +79,8 @@ class ConsumerLocator:
|
||||
for consumer in consumer_list:
|
||||
if not self.communicator.checkconsumer(consumer["Host"]):
|
||||
consumer["State"] = False
|
||||
if datetime.datetime.now() - datetime.datetime.fromtimestamp(consumer["LastOk"]) > datetime.timedelta(hours=1):
|
||||
if datetime.datetime.now() - datetime.datetime.fromtimestamp(consumer["LastOk"]) > datetime.timedelta(
|
||||
seconds=15):
|
||||
removelist.append(consumer)
|
||||
else:
|
||||
consumer["LastOk"] = datetime.datetime.now().timestamp()
|
||||
|
87
integtest.py
Normal file
87
integtest.py
Normal file
@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Integration test for the producer module.
|
||||
"""
|
||||
|
||||
import re
|
||||
from consumerinformation import ConsumerInformation
|
||||
from communicator import Communicator
|
||||
from consumerlocator import ConsumerLocator
|
||||
from messagesender import MessageSender
|
||||
from redisconnector import RedisConnector
|
||||
from pytest_redis import factories
|
||||
|
||||
__author__ = "@dscharnitzky"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
__module_name__ = "integtest"
|
||||
__version__text__ = "1"
|
||||
|
||||
generateduuid = '2fbff1f2-27e7-44c8-88d9-7348cee8c1c3'
|
||||
redis_proc = factories.redis_proc(host='cache', port=6379)
|
||||
redis_db = factories.redisdb('redis_nooproc')
|
||||
|
||||
answer = ""
|
||||
|
||||
|
||||
def answermethod(Request):
|
||||
global answer
|
||||
answer = Request
|
||||
return ""
|
||||
|
||||
|
||||
def test_integration(httpserver):
|
||||
"""
|
||||
Tests the whole system.
|
||||
|
||||
:param httpserver: simple HTTP server
|
||||
"""
|
||||
#Inint
|
||||
httpserver.expect_request(
|
||||
uri="/consumers",
|
||||
method='GET',
|
||||
data="").respond_with_json(
|
||||
["localhost", "localhost", "1.2.3.4"])
|
||||
httpserver.expect_request(
|
||||
uri="/log",
|
||||
method='POST').respond_with_handler(answermethod)
|
||||
url = httpserver.url_for("/")
|
||||
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
|
||||
redisconnector = RedisConnector()
|
||||
consumerinfomation = ConsumerInformation(redisconnector=redisconnector)
|
||||
comm = Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid, consumerinformation=consumerinfomation)
|
||||
messagesender = MessageSender(communicator=comm, uuid=generateduuid)
|
||||
consumerlocator = ConsumerLocator(communicator=comm,
|
||||
redisconnector=redisconnector)
|
||||
#First test method
|
||||
consumerlocator.learnconsumerlist()
|
||||
conslist = consumerlocator.red.consumerlist
|
||||
assert len(conslist) == 3
|
||||
#Mock in port numbers
|
||||
consrepllist = []
|
||||
for consumer in conslist:
|
||||
newconsumer = {"Host": consumer["Host"]+":"+port, "State": consumer["State"], "LastOk": consumer["LastOk"]}
|
||||
consrepllist.append(newconsumer)
|
||||
consumerlocator.red.consumerlist = consrepllist
|
||||
conslist = consumerlocator.red.consumerlist
|
||||
#Second test method call
|
||||
consumerlocator.updateconsumerlist()
|
||||
conslist = consumerlocator.red.consumerlist
|
||||
error = False
|
||||
for consumer in conslist:
|
||||
if consumer["Host"] == "localhost:"+port and consumer["State"] is True:
|
||||
pass
|
||||
elif consumer["Host"] == "1.2.3.4:"+port and consumer["State"] is False:
|
||||
pass
|
||||
elif consumer["Host"] == "10.69.42.1:"+port and consumer["State"] is False:
|
||||
pass
|
||||
else:
|
||||
error = True
|
||||
assert error is False
|
||||
#Third test method call
|
||||
consumerlocator.updateconsumer()
|
||||
assert consumerlocator.red.currentconsumer["Host"] == "localhost:"+port
|
||||
messagesender.sendmessage()
|
||||
assert answer != ""
|
86
test.py
86
test.py
@ -9,6 +9,7 @@ import consumerlocator
|
||||
import communicator
|
||||
import messagesender
|
||||
import redisconnector
|
||||
import consumerinformation
|
||||
from pytest_redis import factories
|
||||
|
||||
__author__ = "@tormakris"
|
||||
@ -17,8 +18,8 @@ __module_name__ = "test"
|
||||
__version__text__ = "1"
|
||||
|
||||
generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad'
|
||||
redis_proc = factories.redis_proc(port=6379)
|
||||
redis_db = factories.redisdb('redis_db')
|
||||
redis_proc = factories.redis_proc(host='cache', port=6379)
|
||||
redis_db = factories.redisdb('redis_nooproc')
|
||||
|
||||
|
||||
def test_generate_string(mocker):
|
||||
@ -28,9 +29,11 @@ def test_generate_string(mocker):
|
||||
:param mocker: patches the :class:`communicator.Communicator`.
|
||||
"""
|
||||
mocker.patch('communicator.Communicator')
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer="localhost",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
mess = messagesender.MessageSender(communicator=comm, uuid=generateduuid)
|
||||
msg = mess.randomstring(stringlength=32)
|
||||
assert isinstance(msg, str)
|
||||
@ -52,9 +55,13 @@ def test_sendmessage(httpserver):
|
||||
"test": "ok"})
|
||||
url = httpserver.url_for("/")
|
||||
port = re.match(r"\W*http[^:]*\D*(\d+)", url).group(1)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
mess = "SENDING"
|
||||
ret = comm.sendmessage(message=mess)
|
||||
assert ret is None
|
||||
@ -68,9 +75,13 @@ def test_send_message(mocker):
|
||||
:return: None
|
||||
"""
|
||||
mocker.patch('communicator.Communicator')
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1", "State": True, "LastOk": "1589479202"}]
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer="127.0.0.1",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
mess = messagesender.MessageSender(communicator=comm, uuid=generateduuid)
|
||||
messa = "SENDING"
|
||||
msg = mess.sendmessage(message=messa)
|
||||
@ -91,9 +102,13 @@ def test_discoveravailableconsumers(httpserver):
|
||||
["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)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
ret = comm.discoveravailableconsumers()
|
||||
assert isinstance(ret, list)
|
||||
assert ret == ["10.69.42.1", "10.10.10.10", "10.20.30.40"]
|
||||
@ -113,9 +128,13 @@ def test_isconsumeravailable(httpserver):
|
||||
["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)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
ret = comm.isconsumeravailable()
|
||||
assert isinstance(ret, bool)
|
||||
assert ret
|
||||
@ -126,7 +145,7 @@ def test_isconsumeravailable(httpserver):
|
||||
|
||||
comm2 = communicator.Communicator(
|
||||
currentconsumer="127.0.0.1:69",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
|
||||
ret3 = comm2.isconsumeravailable()
|
||||
assert isinstance(ret3, bool)
|
||||
@ -147,9 +166,13 @@ def test_checkconsumer(httpserver):
|
||||
["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)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer="127.0.0.1",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
ret = comm.checkconsumer(f"127.0.0.1:{port}")
|
||||
assert isinstance(ret, bool)
|
||||
assert ret
|
||||
@ -160,8 +183,7 @@ def test_checkconsumer(httpserver):
|
||||
|
||||
comm2 = communicator.Communicator(
|
||||
currentconsumer="127.0.0.1",
|
||||
uuid=generateduuid)
|
||||
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
ret3 = comm2.checkconsumer(f"127.0.0.1:{port}")
|
||||
assert isinstance(ret3, bool)
|
||||
assert ret3 == False
|
||||
@ -173,9 +195,11 @@ def test_setcurrentconsumer():
|
||||
|
||||
:return: None
|
||||
"""
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer="127.0.0.1",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
comm.set_currentconsumer("10.69.42.1")
|
||||
assert comm.currenctconsumer == "10.69.42.1"
|
||||
|
||||
@ -194,12 +218,16 @@ def test_learnconsumerlist(httpserver):
|
||||
["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)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm,
|
||||
communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
ret = locator.learnconsumerlist()
|
||||
assert ret is None
|
||||
@ -213,11 +241,13 @@ def test_getcurrentconsumer(mocker):
|
||||
:return: None
|
||||
"""
|
||||
mocker.patch('communicator.Communicator')
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer="127.0.0.1",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm,
|
||||
communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
assert locator.getcurrentconsumer() == consumerlocator.KNOWNCONSUMER
|
||||
|
||||
@ -236,13 +266,17 @@ def test_checkcurrentconsumer(httpserver):
|
||||
["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)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm,
|
||||
communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
ret = locator.checkcurrentconsumer()
|
||||
assert ret == True
|
||||
|
||||
@ -261,14 +295,18 @@ def test_updateconsumer(httpserver):
|
||||
["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)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
redisconn = redisconnector.RedisConnector()
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm,
|
||||
communicator=comm,
|
||||
redisconnector=redisconn)
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
assert redisconn.currentconsumer is not None
|
||||
ret = locator.updateconsumer()
|
||||
assert ret == f"127.0.0.1:{port}"
|
||||
@ -288,12 +326,16 @@ def test_updateconsumerlist(httpserver):
|
||||
["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)
|
||||
redisconnect = redisconnector.RedisConnector()
|
||||
consumerinfo = consumerinformation.ConsumerInformation(redisconnector=redisconnect)
|
||||
comm = communicator.Communicator(
|
||||
currentconsumer=f"127.0.0.1:{port}",
|
||||
uuid=generateduuid)
|
||||
uuid=generateduuid, consumerinformation=consumerinfo)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm,
|
||||
communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
redisconnect.currentconsumer = {"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}
|
||||
redisconnect.consumerlist = [{"Host": f"127.0.0.1:{port}", "State": True, "LastOk": "1589479202"}]
|
||||
ret = locator.updateconsumerlist()
|
||||
assert ret is None
|
||||
|
Reference in New Issue
Block a user