Merge pull request #934 from tahoe-lafs/3564.eliot-log-testing-python-3

Re-enable logging validation on Python 3.

Fixes ticket:3564
This commit is contained in:
Itamar Turner-Trauring 2020-12-22 11:43:36 -05:00 committed by GitHub
commit f9ee4b239f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 8 deletions

0
newsfragments/3564.minor Normal file
View File

View File

@ -111,7 +111,9 @@ install_requires = [
# Eliot is contemplating dropping Python 2 support. Stick to a version we # Eliot is contemplating dropping Python 2 support. Stick to a version we
# know works on Python 2.7. # know works on Python 2.7.
"eliot ~= 1.7", "eliot ~= 1.7 ; python_version < '3.0'",
# On Python 3, we want a new enough version to support custom JSON encoders.
"eliot >= 1.13.0 ; python_version > '3.0'",
# Pyrsistent 0.17.0 (which we use by way of Eliot) has dropped # Pyrsistent 0.17.0 (which we use by way of Eliot) has dropped
# Python 2 entirely; stick to the version known to work for us. # Python 2 entirely; stick to the version known to work for us.

View File

@ -113,4 +113,5 @@ if sys.platform == "win32":
initialize() initialize()
from eliot import to_file from eliot import to_file
to_file(open("eliot.log", "w")) from allmydata.util.jsonbytes import BytesJSONEncoder
to_file(open("eliot.log", "w"), encoder=BytesJSONEncoder)

View File

@ -31,6 +31,9 @@ from twisted.internet.defer import (
maybeDeferred, maybeDeferred,
) )
from ..util.jsonbytes import BytesJSONEncoder
_NAME = Field.for_types( _NAME = Field.for_types(
u"name", u"name",
[str], [str],
@ -61,6 +64,14 @@ def eliot_logged_test(f):
class storage(object): class storage(object):
pass pass
# On Python 3, we want to use our custom JSON encoder when validating
# messages can be encoded to JSON:
if PY3:
capture = lambda f : capture_logging(None, encoder_=BytesJSONEncoder)(f)
else:
capture = lambda f : capture_logging(None)(f)
@wraps(f) @wraps(f)
def run_and_republish(self, *a, **kw): def run_and_republish(self, *a, **kw):
# Unfortunately the only way to get at the global/default logger... # Unfortunately the only way to get at the global/default logger...
@ -85,7 +96,7 @@ def eliot_logged_test(f):
# can finish the test's action. # can finish the test's action.
storage.action.finish() storage.action.finish()
@capture_logging(None) @capture
def run(self, logger): def run(self, logger):
# Record the MemoryLogger for later message extraction. # Record the MemoryLogger for later message extraction.
storage.logger = logger storage.logger = logger
@ -165,9 +176,6 @@ class EliotLoggedRunTest(object):
@eliot_logged_test @eliot_logged_test
def run(self, result=None): def run(self, result=None):
# Workaround for https://github.com/itamarst/eliot/issues/456
if PY3:
self.case.eliot_logger._validate_message = lambda *args, **kwargs: None
return self._run_tests_with_factory( return self._run_tests_with_factory(
self.case, self.case,
self.handlers, self.handlers,

View File

@ -57,11 +57,14 @@ from ..util.eliotutil import (
_parse_destination_description, _parse_destination_description,
_EliotLogging, _EliotLogging,
) )
from ..util.jsonbytes import BytesJSONEncoder
from .common import ( from .common import (
SyncTestCase, SyncTestCase,
AsyncTestCase, AsyncTestCase,
) )
class EliotLoggedTestTests(AsyncTestCase): class EliotLoggedTestTests(AsyncTestCase):
def test_returns_none(self): def test_returns_none(self):
Message.log(hello="world") Message.log(hello="world")
@ -94,7 +97,7 @@ class ParseDestinationDescriptionTests(SyncTestCase):
reactor = object() reactor = object()
self.assertThat( self.assertThat(
_parse_destination_description("file:-")(reactor), _parse_destination_description("file:-")(reactor),
Equals(FileDestination(stdout)), Equals(FileDestination(stdout, encoder=BytesJSONEncoder)),
) )

View File

@ -86,6 +86,9 @@ from twisted.internet.defer import (
) )
from twisted.application.service import Service from twisted.application.service import Service
from .jsonbytes import BytesJSONEncoder
def validateInstanceOf(t): def validateInstanceOf(t):
""" """
Return an Eliot validator that requires values to be instances of ``t``. Return an Eliot validator that requires values to be instances of ``t``.
@ -302,7 +305,7 @@ class _DestinationParser(object):
rotateLength=rotate_length, rotateLength=rotate_length,
maxRotatedFiles=max_rotated_files, maxRotatedFiles=max_rotated_files,
) )
return lambda reactor: FileDestination(get_file()) return lambda reactor: FileDestination(get_file(), BytesJSONEncoder)
_parse_destination_description = _DestinationParser().parse _parse_destination_description = _DestinationParser().parse