Added communicator tests
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		@@ -5,6 +5,11 @@ import pytest
 | 
			
		||||
import redis
 | 
			
		||||
import json
 | 
			
		||||
import socket
 | 
			
		||||
import requests
 | 
			
		||||
import requests.exceptions
 | 
			
		||||
import logging
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
REDIS_URL = "redis://localhost/0"
 | 
			
		||||
REDIS_TIMEOUT = 2
 | 
			
		||||
@@ -12,6 +17,10 @@ REDIS_TIMEOUT = 2
 | 
			
		||||
CURRENT_HOSTNAME = "testenv.local"
 | 
			
		||||
CURRENT_IPADDR = "192.168.1.50"
 | 
			
		||||
 | 
			
		||||
LOCAL_UUID = "testuuid1"
 | 
			
		||||
os.environ["LOCAL_UUID"] = LOCAL_UUID
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def redis_super_storage_instance(mocker):
 | 
			
		||||
@@ -26,6 +35,16 @@ def ip_watchdog_instance(mocker, redis_super_storage_instance):
 | 
			
		||||
    yield IPWatchdog(redis_super_storage_instance)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def consumer_communicator_instance(redis_super_storage_instance):
 | 
			
		||||
    yield ConsumerCommunicator(redis_super_storage_instance)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def producer_communicator_instance(redis_super_storage_instance):
 | 
			
		||||
    yield ProducerCommunicator(redis_super_storage_instance)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# ========================================
 | 
			
		||||
# RedisSuperStorage
 | 
			
		||||
# ========================================
 | 
			
		||||
@@ -229,3 +248,198 @@ def test_ipw_is_changed_true(mocker, ip_watchdog_instance):
 | 
			
		||||
    ip_watchdog_instance._redis_store.r.get.assert_called_once_with("current_ip")
 | 
			
		||||
    assert ip_watchdog_instance._redis_store.r.set.called_once_with("current_ip", b"192.168.2.123")
 | 
			
		||||
    socket.gethostbyname.assert_called_once_with(CURRENT_HOSTNAME)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# ========================================
 | 
			
		||||
# Communicators
 | 
			
		||||
# ========================================
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_cc_instantiate(redis_super_storage_instance):
 | 
			
		||||
    cc = ConsumerCommunicator(redis_super_storage_instance)
 | 
			
		||||
 | 
			
		||||
    assert cc._redis_store == redis_super_storage_instance
 | 
			
		||||
    assert isinstance(cc._session, requests.Session)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_pc_instantiate(redis_super_storage_instance):
 | 
			
		||||
    pc = ProducerCommunicator(redis_super_storage_instance)
 | 
			
		||||
 | 
			
		||||
    assert pc._redis_store == redis_super_storage_instance
 | 
			
		||||
    assert isinstance(pc._session, requests.Session)
 | 
			
		||||
 | 
			
		||||
# producer communicator
 | 
			
		||||
 | 
			
		||||
def test_pc_push_ip_update(requests_mock, producer_communicator_instance):
 | 
			
		||||
    producer_communicator_instance._redis_store.r.keys.side_effect = lambda a: [
 | 
			
		||||
        b"producer_uuid1",
 | 
			
		||||
        b"producer_uuid2",
 | 
			
		||||
        b"producer_uuid3"
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    data = {
 | 
			
		||||
        b"producer_uuid1": b"127.0.0.1",
 | 
			
		||||
        b"producer_uuid2": b"127.0.0.2",
 | 
			
		||||
        b"producer_uuid3": b"127.0.0.3",
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    producer_communicator_instance._redis_store.r.get.side_effect = lambda a: data[a]
 | 
			
		||||
 | 
			
		||||
    first = requests_mock.post("http://127.0.0.1/ip")
 | 
			
		||||
    second = requests_mock.post("http://127.0.0.2/ip")
 | 
			
		||||
    third = requests_mock.post("http://127.0.0.3/ip")
 | 
			
		||||
 | 
			
		||||
    producer_communicator_instance.push_ip_update(CURRENT_IPADDR)
 | 
			
		||||
 | 
			
		||||
    assert first.call_count == 1
 | 
			
		||||
    assert second.call_count == 1
 | 
			
		||||
    assert third.call_count == 1
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json() == second.last_request.json() == third.last_request.json()
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json()['ip'] == CURRENT_IPADDR
 | 
			
		||||
    assert first.last_request.json()['uuid'] == LOCAL_UUID
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_pc_push_ip_update_error_logged(mocker, requests_mock, producer_communicator_instance):
 | 
			
		||||
    mocker.patch("logging.warning")
 | 
			
		||||
 | 
			
		||||
    producer_communicator_instance._redis_store.r.keys.side_effect = lambda a: [
 | 
			
		||||
        b"producer_uuid1",
 | 
			
		||||
        b"producer_uuid2",
 | 
			
		||||
        b"producer_uuid3"
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    data = {
 | 
			
		||||
        b"producer_uuid1": b"127.0.0.1",
 | 
			
		||||
        b"producer_uuid2": b"127.0.0.2",
 | 
			
		||||
        b"producer_uuid3": b"127.0.0.3",
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    producer_communicator_instance._redis_store.r.get.side_effect = lambda a: data[a]
 | 
			
		||||
 | 
			
		||||
    first = requests_mock.post("http://127.0.0.1/ip")
 | 
			
		||||
    second = requests_mock.post("http://127.0.0.2/ip", exc=requests.exceptions.ConnectTimeout)
 | 
			
		||||
    third = requests_mock.post("http://127.0.0.3/ip")
 | 
			
		||||
 | 
			
		||||
    producer_communicator_instance.push_ip_update(CURRENT_IPADDR)
 | 
			
		||||
 | 
			
		||||
    assert first.call_count == 1
 | 
			
		||||
    assert second.call_count == 1
 | 
			
		||||
    assert third.call_count == 1
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json() == third.last_request.json()
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json()['ip'] == CURRENT_IPADDR
 | 
			
		||||
    assert first.last_request.json()['uuid'] == LOCAL_UUID
 | 
			
		||||
 | 
			
		||||
    logging.warning.assert_called_once()
 | 
			
		||||
 | 
			
		||||
# customer communicator
 | 
			
		||||
 | 
			
		||||
def test_cc_targeted_sync(requests_mock, consumer_communicator_instance):
 | 
			
		||||
    a = requests_mock.post("http://127.0.0.2/sync", json={"uuid": "testasdasdasd"})
 | 
			
		||||
 | 
			
		||||
    consumer_communicator_instance.targeted_snyc("127.0.0.2")
 | 
			
		||||
 | 
			
		||||
    assert a.called
 | 
			
		||||
    assert a.last_request.json() == {'uuid': LOCAL_UUID}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_cc_targeted_sync_error_logged(mocker, requests_mock, consumer_communicator_instance):
 | 
			
		||||
    mocker.patch("logging.error")
 | 
			
		||||
 | 
			
		||||
    requests_mock.post("http://127.0.0.2/sync", exc=requests.exceptions.ConnectTimeout)
 | 
			
		||||
 | 
			
		||||
    consumer_communicator_instance.targeted_snyc("127.0.0.2")
 | 
			
		||||
 | 
			
		||||
    logging.error.assert_called_once()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_cc_sync_all(requests_mock, consumer_communicator_instance):
 | 
			
		||||
    consumer_communicator_instance._redis_store.r.keys.side_effect = lambda a: [
 | 
			
		||||
        b"consumer_uuid1",
 | 
			
		||||
        b"consumer_uuid2",
 | 
			
		||||
        b"consumer_uuid3"
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    data = {
 | 
			
		||||
        b"consumer_uuid1": json.dumps({
 | 
			
		||||
            "uuid": "consumer_uuid1",
 | 
			
		||||
            "ip": "127.0.0.1",
 | 
			
		||||
            "last_seen": 123
 | 
			
		||||
        }).encode("utf-8"),
 | 
			
		||||
        b"consumer_uuid2": json.dumps({
 | 
			
		||||
            "uuid": "consumer_uuid2",
 | 
			
		||||
            "ip": "127.0.0.2",
 | 
			
		||||
            "last_seen": 1234
 | 
			
		||||
        }).encode("utf-8"),
 | 
			
		||||
        b"consumer_uuid3": json.dumps({
 | 
			
		||||
            "uuid": "consumer_uuid3",
 | 
			
		||||
            "ip": "127.0.0.3",
 | 
			
		||||
            "last_seen": 1235
 | 
			
		||||
        }).encode("utf-8")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    first = requests_mock.post("http://127.0.0.1/sync", json={"uuid": "consumer_uuid1"})
 | 
			
		||||
    second = requests_mock.post("http://127.0.0.2/sync", json={"uuid": "consumer_uuid2"})
 | 
			
		||||
    third = requests_mock.post("http://127.0.0.3/sync", json={"uuid": "consumer_uuid3"})
 | 
			
		||||
 | 
			
		||||
    consumer_communicator_instance._redis_store.r.get.side_effect = lambda a: data[a]
 | 
			
		||||
 | 
			
		||||
    consumer_communicator_instance.sync_all()
 | 
			
		||||
 | 
			
		||||
    assert first.called
 | 
			
		||||
    assert second.called
 | 
			
		||||
    assert third.called
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json() == second.last_request.json() == third.last_request.json()
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json()['uuid'] == LOCAL_UUID
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_cc_sync_all_error_logged(mocker, requests_mock, consumer_communicator_instance):
 | 
			
		||||
    mocker.patch("logging.error")
 | 
			
		||||
 | 
			
		||||
    consumer_communicator_instance._redis_store.r.keys.side_effect = lambda a: [
 | 
			
		||||
        b"consumer_uuid1",
 | 
			
		||||
        b"consumer_uuid2",
 | 
			
		||||
        b"consumer_uuid3"
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    data = {
 | 
			
		||||
        b"consumer_uuid1": json.dumps({
 | 
			
		||||
            "uuid": "consumer_uuid1",
 | 
			
		||||
            "ip": "127.0.0.1",
 | 
			
		||||
            "last_seen": 123
 | 
			
		||||
        }).encode("utf-8"),
 | 
			
		||||
        b"consumer_uuid2": json.dumps({
 | 
			
		||||
            "uuid": "consumer_uuid2",
 | 
			
		||||
            "ip": "127.0.0.2",
 | 
			
		||||
            "last_seen": 1234
 | 
			
		||||
        }).encode("utf-8"),
 | 
			
		||||
        b"consumer_uuid3": json.dumps({
 | 
			
		||||
            "uuid": "consumer_uuid3",
 | 
			
		||||
            "ip": "127.0.0.3",
 | 
			
		||||
            "last_seen": 1235
 | 
			
		||||
        }).encode("utf-8")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    first = requests_mock.post("http://127.0.0.1/sync", json={"uuid": "consumer_uuid1"})
 | 
			
		||||
    second = requests_mock.post("http://127.0.0.2/sync", exc=requests.exceptions.ConnectTimeout)
 | 
			
		||||
    third = requests_mock.post("http://127.0.0.3/sync", json={"uuid": "consumer_uuid3"})
 | 
			
		||||
 | 
			
		||||
    consumer_communicator_instance._redis_store.r.get.side_effect = lambda a: data[a]
 | 
			
		||||
 | 
			
		||||
    consumer_communicator_instance.sync_all()
 | 
			
		||||
 | 
			
		||||
    assert first.called
 | 
			
		||||
    assert second.called
 | 
			
		||||
    assert third.called
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json() == third.last_request.json()
 | 
			
		||||
 | 
			
		||||
    assert first.last_request.json()['uuid'] == LOCAL_UUID
 | 
			
		||||
 | 
			
		||||
    logging.error.assert_called_once()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user