Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init_called_twice(mocker):
""" test cf_logging.init can be called only once """
framework = mocker.Mock(Framework)
cf_logging._SETUP_DONE = False
cf_logging.init(framework, level=logging.DEBUG)
cf_logging.init(framework, level=logging.DEBUG)
def _set_up_sanic_logging(app, level=logging.DEBUG):
cf_logging._SETUP_DONE = False
sanic_logging.init(app, level)
def test_log_in_expected_format(log_callback):
""" Test the cf_logger as a standalone """
cf_logging.init(level=logging.DEBUG)
logger, stream = config_logger('cli.test')
log_callback(logger, 'hi')
log_json = JSONDecoder().decode(stream.getvalue())
_, error = JsonValidator(JOB_LOG_SCHEMA).validate(log_json)
assert error == {}
def test_init_called_twice(mocker):
""" test cf_logging.init can be called only once """
framework = mocker.Mock(Framework)
cf_logging._SETUP_DONE = False
cf_logging.init(framework, level=logging.DEBUG)
cf_logging.init(framework, level=logging.DEBUG)
def _set_up_falcon_logging(app, *args):
cf_logging._SETUP_DONE = False
falcon_logging.init(app, logging.DEBUG, *args)
def test_thread_safety():
""" test context keeps separate correlation ID per thread """
class _SampleThread(threading.Thread):
def __init__(self):
super(_SampleThread, self).__init__()
self.correlation_id = str(uuid.uuid1())
self.read_correlation_id = ''
def run(self):
cf_logging.FRAMEWORK.context.set_correlation_id(self.correlation_id)
time.sleep(0.1)
self.read_correlation_id = cf_logging.FRAMEWORK.context.get_correlation_id()
cf_logging.init(level=logging.DEBUG)
thread_one = _SampleThread()
thread_two = _SampleThread()
thread_one.start()
thread_two.start()
thread_one.join()
thread_two.join()
assert thread_one.correlation_id == thread_one.read_correlation_id
assert thread_two.correlation_id == thread_two.read_correlation_id
def test_init_called_twice(mocker):
""" test cf_logging.init can be called only once """
framework = mocker.Mock(Framework)
cf_logging._SETUP_DONE = False
cf_logging.init(framework, level=logging.DEBUG)
cf_logging.init(framework, level=logging.DEBUG)
def test_init_incorrect_framework():
""" test cf_logging.init fails for invalid framework """
cf_logging._SETUP_DONE = False
cf_logging.init({})
def test_set_correlation_id():
""" Test setting correlation_id """
correlation_id = '1234'
cf_logging.init(level=logging.DEBUG)
cf_logging.FRAMEWORK.context.set_correlation_id(correlation_id)
logger, stream = config_logger('cli.test')
logger.info('hi')
log_json = JSONDecoder().decode(stream.getvalue())
_, error = JsonValidator(JOB_LOG_SCHEMA).validate(log_json)
assert error == {}
assert log_json['correlation_id'] == correlation_id
assert cf_logging.FRAMEWORK.context.get_correlation_id() == correlation_id
def test_init(initializers):
""" test constructor with invalid context, request_reader and response_reader """
Framework('django', **initializers)