"""Loggly connector tests.""" import importlib import logging from flexmock import flexmock from owslogger import logger from datalytics import config from datalytics.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(handler, cls) for handler in loggly.app_logger.handlers]) def test_loggly_enabled(): """Test DSNHandler is in logger handlers if LOGGER_DSN is set.""" flexmock(config).should_receive('LOGGER_DSN').and_return('not_empty_dsn') assert _has_handler(logger.DSNHandler) assert not _has_handler(logging.StreamHandler) def test_loggly_disabled(): """Test StreamHandler is in logger handlers if LOGGER_DSN is not set.""" flexmock(config).should_receive('LOGGER_DSN').and_return('') assert _has_handler(logging.StreamHandler) assert not _has_handler(logger.DSNHandler) 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): """Module level teardown 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)