patch logger info and other code fixes for test_monitor.py
# -*- coding: utf-8 -*-
-
-import pytest
-
from unittest.mock import MagicMock
from unittest.mock import patch
+import pytest
+
import aiohttp
from foglamp.services.core.service_registry.monitor import Monitor
from foglamp.services.core.service_registry.service_registry import ServiceRegistry
-from foglamp.services.core.service_registry import exceptions as service_registry_exceptions
from foglamp.common.service_record import ServiceRecord
__author__ = "Ashwin Gopalakrishnan"
@@ -17,16 +15,17 @@ __version__ = "${VERSION}"
@pytest.allure.feature("unit")
-@pytest.allure.story("core", "monitor")
+@pytest.allure.story("services", "core", "service-registry")
class TestMonitor:
- @pytest.fixture
- def reset_service_registry(self):
- del ServiceRegistry._registry[:]
- yield
- del ServiceRegistry._registry[:]
+
+ def setup_method(self):
+ ServiceRegistry._registry = []
+
+ def teardown_method(self):
+ ServiceRegistry._registry = []
@pytest.mark.asyncio
- async def test__monitor_good_uptime(self, reset_service_registry):
+ async def test__monitor_good_uptime(self):
async def async_mock(return_value):
return return_value
# used to mock client session context manager
@@ -49,8 +48,14 @@ class TestMonitor:
class TestMonitorException(Exception):
pass
# register a service
+ with patch.object(ServiceRegistry._logger, 'info') as log_info:
s_id_1 = ServiceRegistry.register(
'sname1', 'Storage', 'saddress1', 1, 1, 'protocol1')
+ assert 1 == log_info.call_count
+ args, kwargs = log_info.call_args
+ assert args[0].startswith('Registered service instance id=')
+ assert args[0].endswith(': <sname1, type=Storage, protocol=protocol1, address=saddress1, service port=1, '
+ 'management port=1, status=1>')
monitor = Monitor()
monitor._sleep_interval = Monitor._DEFAULT_SLEEP_INTERVAL
monitor._max_attempts = Monitor._DEFAULT_MAX_ATTEMPTS
@@ -58,15 +63,16 @@ class TestMonitor:
# throw the TestMonitorException when sleep is called (end of infinite loop)
with patch.object(Monitor, '_sleep', side_effect=TestMonitorException()):
with patch.object(aiohttp.ClientSession, 'get', return_value=AsyncSessionContextManagerMock()):
- with pytest.raises(TestMonitorException) as excinfo:
+ with pytest.raises(Exception) as excinfo:
await monitor._monitor_loop()
+ assert excinfo.type is TestMonitorException
# service is good, so it should remain in the service registry
assert len(ServiceRegistry.get(idx=s_id_1)) is 1
assert ServiceRegistry.get(idx=s_id_1)[0]._status is ServiceRecord.Status.Running
@pytest.mark.skip(reason="To be taken up after monitor.py->L#82 exception FIXME is attended to.")
@pytest.mark.asyncio
- async def test__monitor_exceed_attempts(self, reset_service_registry):
+ async def test__monitor_exceed_attempts(self):
async def async_mock(return_value):
return return_value
# used to mock client session context manager
@@ -100,7 +106,8 @@ class TestMonitor:
sleep_side_effect_list.append(TestMonitorException())
with patch.object(Monitor, '_sleep', side_effect=sleep_side_effect_list):
with patch.object(aiohttp.ClientSession, 'get', return_value=AsyncSessionContextManagerMock()):
- with pytest.raises(TestMonitorException) as excinfo:
+ with pytest.raises(Exception) as excinfo:
await monitor._monitor_loop()
+ assert excinfo.type is TestMonitorException
assert ServiceRegistry.get(idx=s_id_1)[0]._status is ServiceRecord.Status.Failed
