Add missing tests for top-level

This commit is contained in:
Dave Shawley 2019-09-02 21:31:18 -04:00
parent fe4f4a7e38
commit 3c71ae0313

View file

@ -7,6 +7,7 @@ import os
import json
import time
import unittest
import uuid
from tornado import concurrent, httpserver, httputil, ioloop, testing, web
@ -55,16 +56,19 @@ class MockHelper(unittest.TestCase):
@contextlib.contextmanager
def override_environment_variable(name, value):
stash = os.environ.pop(name, None)
if value is not None:
os.environ[name] = value
def override_environment_variable(**env_vars):
stash = {}
for name, value in env_vars.items():
stash[name] = os.environ.pop(name, None)
if value is not None:
os.environ[name] = value
try:
yield
finally:
os.environ.pop(name, None)
if stash is not None:
os.environ[name] = stash
for name, value in stash.items():
os.environ.pop(name, None)
if value is not None:
os.environ[name] = value
class ErrorLoggerTests(testing.AsyncHTTPTestCase):
@ -240,21 +244,21 @@ class RunTests(MockHelper, unittest.TestCase):
def test_that_debug_envvar_enables_debug_flag(self):
create_app = mock.Mock()
with override_environment_variable('DEBUG', '1'):
with override_environment_variable(DEBUG='1'):
sprockets.http.run(create_app)
create_app.assert_called_once_with(debug=True)
self.get_logging_config.assert_called_once_with(True)
def test_that_false_debug_envvar_disables_debug_flag(self):
create_app = mock.Mock()
with override_environment_variable('DEBUG', '0'):
with override_environment_variable(DEBUG='0'):
sprockets.http.run(create_app)
create_app.assert_called_once_with(debug=False)
self.get_logging_config.assert_called_once_with(False)
def test_that_unset_debug_envvar_disables_debug_flag(self):
create_app = mock.Mock()
with override_environment_variable('DEBUG', None):
with override_environment_variable(DEBUG=None):
sprockets.http.run(create_app)
create_app.assert_called_once_with(debug=False)
self.get_logging_config.assert_called_once_with(False)
@ -264,7 +268,7 @@ class RunTests(MockHelper, unittest.TestCase):
self.runner_instance.run.assert_called_once_with(8000, mock.ANY)
def test_that_port_envvar_sets_port_number(self):
with override_environment_variable('PORT', '8888'):
with override_environment_variable(PORT='8888'):
sprockets.http.run(mock.Mock())
self.runner_instance.run.assert_called_once_with(8888, mock.ANY)
@ -642,3 +646,41 @@ class TestCaseTests(unittest.TestCase):
test_case.app.stop.assert_called_once_with(
test_case.io_loop, test_case.shutdown_limit,
test_case.wait_timeout)
class CorrelationFilterTests(unittest.TestCase):
def setUp(self):
super(CorrelationFilterTests, self).setUp()
self.logger = logging.getLogger()
self.record = self.logger.makeRecord(
'name', logging.INFO, 'functionName', 42, 'hello %s',
tuple(['world']), (None, None, None))
self.filter = sprockets.http._CorrelationFilter()
def test_that_correlation_filter_adds_correlation_id(self):
self.filter.filter(self.record)
self.assertTrue(hasattr(self.record, 'correlation-id'))
def test_that_correlation_filter_does_not_overwrite_correlation_id(self):
some_value = str(uuid.uuid4())
setattr(self.record, 'correlation-id', some_value)
self.filter.filter(self.record)
self.assertEqual(getattr(self.record, 'correlation-id'), some_value)
class LoggingConfigurationTests(unittest.TestCase):
def test_that_debug_sets_log_level_to_debug(self):
config = sprockets.http._get_logging_config(True)
self.assertEqual(config['root']['level'], 'DEBUG')
def test_that_not_debug_sets_log_level_to_info(self):
config = sprockets.http._get_logging_config(False)
self.assertEqual(config['root']['level'], 'INFO')
def test_that_format_includes_sd_when_service_and_env_are_set(self):
with override_environment_variable(SERVICE='service',
ENVIRONMENT='whatever'):
config = sprockets.http._get_logging_config(False)
fmt_name = list(config['formatters'].keys())[0]
self.assertIn('service="service" environment="whatever"',
config['formatters'][fmt_name]['format'])