"""Loggly connector tests.""" import importlib import logging from flexmock import flexmock from owslogger import logger from availability import config from availability.connectors import loggly def _has_handler(cls): """Check if an instance of cls is present in app_logger.handlers.""" importlib.reload(loggly) importlib.reload(logger) importlib.reload(logging) return any([isinstance(h, cls) for h in loggly.app_logger.handlers]) def test_loggly_enabled(): """Test DSNHandler is in logger handlers if logger name is set.""" flexmock(config).should_receive('DAEMON_NAME') assert _has_handler(logging.StreamHandler) def test_current_logger_correlation_id(): """Test correlation_id is assigned to logging adapter. Assert correlation_id is assigned to logging adapter when using get_current_logger(correlation_id) function. """ correlation_id = 'test_correlation_id' current_logger = loggly.get_current_logger(correlation_id) assert 'correlation_id' in current_logger.extra assert current_logger.extra['correlation_id'] == correlation_id def teardown_function(module): """Tear down handler.""" # Remove all logging handlers. for h in loggly.app_logger.handlers: loggly.app_logger.removeHandler(h) # Reload modules we've mocked. importlib.reload(config)