Skip to content

Commit

Permalink
Adding unit tests to the asynchronous client
Browse files Browse the repository at this point in the history
  • Loading branch information
amenezes committed Mar 22, 2019
1 parent a2a2bba commit b3435bc
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 12 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[![Build Status](https://travis-ci.org/amenezes/discovery-client.svg?branch=master)](https://travis-ci.org/amenezes/discovery-client)
[![Maintainability](https://api.codeclimate.com/v1/badges/fc7916aab464c8b7d742/maintainability)](https://codeclimate.com/github/amenezes/discovery-client/maintainability)
<!-- [![Test Coverage](https://api.codeclimate.com/v1/badges/fc7916aab464c8b7d742/test_coverage)](https://codeclimate.com/github/amenezes/discovery-client/test_coverage) -->
[![codecov](https://codecov.io/gh/amenezes/discovery-client/branch/master/graph/badge.svg)](https://codecov.io/gh/amenezes/discovery-client)

# discovery-client
Expand Down
191 changes: 191 additions & 0 deletions tests/unit/test_aioclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
"""Test Consul async client module."""
import asyncio
import os
import unittest

import asynctest
from asynctest import CoroutineMock, patch

import consul.aio

from discovery import aioclient


class TestAioClient(asynctest.TestCase):
"""Unit tests to async Consul client."""

def setUp(self):
"""Mock of responses generated by python-consul.
and expected results generated by discovery-client.
"""
self.loop = asyncio.get_event_loop()
self.consul_health_response = (
0, [{'Node': {
'ID': '123456'}}])
self.consul_raw_response = (
0, [{'Node': 'localhost',
'Address': '127.0.0.1',
'ServiceID': '#123',
'ServiceName': 'consul',
'ServicePort': 8300}])
self.myapp_raw_response = (
0, [{'Node': 'localhost',
'ID': '987654',
'Address': '127.0.0.1',
'ServiceID': '#987',
'ServiceName': 'myapp',
'ServicePort': 5000}])
self.fmt_response = [
{
'node': 'localhost',
'address': '127.0.0.1',
'service_id': '#123',
'service_name': 'consul',
'service_port': 8300
},
{
'node': 'localhost',
'address': '127.0.0.1',
'service_id': '#987',
'service_name': 'myapp',
'service_port': 5000
}]

def test_changing_default_timeout(self):
"""Test change the time used to check periodically health status of the Consul connection."""
async def async_test_changing_default_timeout(loop):
os.environ['DEFAULT_TIMEOUT'] = '5'
dc = aioclient.Consul('localhost', 8500, app=loop)

self.assertEqual(dc.DEFAULT_TIMEOUT, 5)
self.assertNotEqual(dc.DEFAULT_TIMEOUT, 30)

self.loop.run_until_complete(
async_test_changing_default_timeout(self.loop)
)

@patch('discovery.aioclient.consul.aio.Consul')
def test_find_services(self, MockAioConsul):
"""Test for localization of a set of services present in the consul's catalog.
Return a list of instances present in the consul's catalog.
"""
async def async_test_find_services(loop):
consul_client = MockAioConsul(consul.aio.Consul)
consul_client.catalog.service = CoroutineMock(
return_value=self.consul_raw_response
)

dc = aioclient.Consul('localhost', 8500, app=loop)
consul_service = await dc.find_service('consul')

self.assertIsInstance(consul_service, dict)
self.assertEqual(consul_service, self.fmt_response[0])

self.loop.run_until_complete(
async_test_find_services(self.loop)
)

@patch('discovery.aioclient.consul.aio.Consul')
def test_find_services_not_on_catalog(self, MockAioConsul):
"""Test for localization of a set of services not present in the consul's catalog.
Return a empty list.
"""
async def async_test_find_services_not_on_catalog(loop):
consul_client = MockAioConsul(consul.aio.Consul)
consul_client.catalog.service = CoroutineMock(
return_value=(0, [])
)

dc = aioclient.Consul('localhost', 8500, app=loop)
response = await dc.find_services('myapp')

self.assertEqual(response, [])

self.loop.run_until_complete(
async_test_find_services_not_on_catalog(self.loop)
)

@patch('discovery.aioclient.consul.aio.Consul')
def test_test_find_service_random(self, MockAioConsul):
"""Test for localization of a service present in the consul's catalog.
Return random instances, when there is more than one registered.
"""
async def async_test_find_services(loop):
consul_client = MockAioConsul(consul.aio.Consul)
consul_client.catalog.service = CoroutineMock(
return_value=self.consul_raw_response
)

dc = aioclient.Consul('localhost', 8500, app=loop)
consul_service = await dc.find_service('consul', method='random')

self.assertIsInstance(consul_service, dict)
self.assertEqual(consul_service, self.fmt_response[0])

self.loop.run_until_complete(
async_test_find_services(self.loop)
)

@patch('discovery.aioclient.consul.aio.Consul')
def test_register(self, MockAioConsul):
"""Test registration of a service in the consul's catalog."""
async def async_test_register(loop):
consul_client = MockAioConsul(consul.aio.Consul)
consul_client.agent.service.register = CoroutineMock()
consul_client.catalog.service = CoroutineMock(
return_value=self.myapp_raw_response
)
consul_client.health.service = CoroutineMock(
return_value=self.consul_health_response
)

dc = aioclient.Consul('localhost', 8500, app=loop)
await dc.register('myapp', 5000)
myapp_service = await dc.find_service('myapp')

self.assertIsInstance(myapp_service, dict)
self.assertEqual(myapp_service, self.fmt_response[1])

self.loop.run_until_complete(
async_test_register(self.loop)
)

@patch('discovery.aioclient.consul.aio.Consul')
def test_deregister(self, MockAioConsul):
"""Test the deregistration of a service present in the consul's catalog."""
async def async_test_deregister(loop):
consul_client = MockAioConsul(consul.aio.Consul)
consul_client.agent.service.register = CoroutineMock()
consul_client.agent.service.deregister = CoroutineMock()
consul_client.catalog.service = CoroutineMock(
return_value=self.myapp_raw_response
)
consul_client.health.service = CoroutineMock(
return_value=self.consul_health_response
)

dc = aioclient.Consul('localhost', 8500, app=loop)
await dc.register('myapp', 5000)
myapp_service = await dc.find_service('myapp')

self.assertIsInstance(myapp_service, dict)
self.assertEqual(myapp_service, self.fmt_response[1])

await dc.deregister()

consul_client.catalog.service = CoroutineMock(return_value=(0, []))

with self.assertRaises(IndexError):
await dc.find_service('myapp')

self.loop.run_until_complete(
async_test_deregister(self.loop)
)


if __name__ == '__main__':
unittest.main()
41 changes: 30 additions & 11 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test Consul standard client module."""

import logging
import os
import unittest
Expand Down Expand Up @@ -68,7 +67,9 @@ def test_find_services(self, MockConsul):
Return a list of instances present in the consul's catalog.
"""
consul_client = MockConsul(consul.Consul)
consul_client.catalog.service = MagicMock(return_value=self.consul_raw_response)
consul_client.catalog.service = MagicMock(
return_value=self.consul_raw_response
)

dc = client.Consul('localhost', 8500)
consul_service = dc.find_services('consul')
Expand Down Expand Up @@ -112,7 +113,9 @@ def test_find_service_rr(self, MockConsul):
one registered.
"""
consul_client = MockConsul(consul.Consul)
consul_client.catalog.service = MagicMock(return_value=self.consul_raw_response)
consul_client.catalog.service = MagicMock(
return_value=self.consul_raw_response
)

dc = client.Consul('localhost', 8500)
consul_service = dc.find_service('consul')
Expand All @@ -127,7 +130,9 @@ def test_find_service_random(self, MockConsul):
Return random instances, when there is more than one registered.
"""
consul_client = MockConsul(consul.Consul)
consul_client.catalog.service = MagicMock(return_value=self.consul_raw_response)
consul_client.catalog.service = MagicMock(
return_value=self.consul_raw_response
)

dc = client.Consul('localhost', 8500)
consul_service = dc.find_service('consul', method='random')
Expand All @@ -140,8 +145,12 @@ def test_register(self, MockConsul):
"""Test registration of a service in the consul's catalog."""
consul_client = MockConsul(consul.Consul)
consul_client.agent.service.register = MagicMock()
consul_client.catalog.service = MagicMock(return_value=self.myapp_raw_response)
consul_client.health.service = MagicMock(return_value=self.consul_health_response)
consul_client.catalog.service = MagicMock(
return_value=self.myapp_raw_response
)
consul_client.health.service = MagicMock(
return_value=self.consul_health_response
)

dc = client.Consul('localhost', 8500)
dc.register('myapp', 5000)
Expand All @@ -154,22 +163,32 @@ def test_register(self, MockConsul):
def test_register_connection_error(self, MockConsul):
"""Failure test to register a service when there is no instance of consul available."""
consul_client = MockConsul(consul.Consul)
consul_client.agent.service.register = MagicMock(side_effect=requests.exceptions.ConnectionError)
consul_client.health.service = MagicMock(side_effect=requests.exceptions.ConnectionError)
consul_client.agent.service.register = MagicMock(
side_effect=requests.exceptions.ConnectionError
)
consul_client.health.service = MagicMock(
side_effect=requests.exceptions.ConnectionError
)

dc = client.Consul('localhost', 8500)
with self.assertLogs() as cm:
logging.getLogger(dc.register('myapp', 5000))
self.assertEqual(cm.output, ['ERROR:root:Failed to connect to discovery...'])
self.assertEqual(
cm.output, ['ERROR:root:Failed to connect to discovery...']
)

@patch('discovery.client.consul.Consul')
def test_deregister(self, MockConsul):
"""Test the deregistration of a service present in the consul's catalog."""
consul_client = MockConsul(consul.Consul)
consul_client.agent.service.register = MagicMock()
consul_client.agent.service.deregister = MagicMock()
consul_client.catalog.service = MagicMock(return_value=self.myapp_raw_response)
consul_client.health.service = MagicMock(return_value=self.consul_health_response)
consul_client.catalog.service = MagicMock(
return_value=self.myapp_raw_response
)
consul_client.health.service = MagicMock(
return_value=self.consul_health_response
)

dc = client.Consul('localhost', 8500)
dc.register('myapp', 5000)
Expand Down

0 comments on commit b3435bc

Please sign in to comment.