From cf418b753a5cf1762d431b317d32e73bb96174af Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Thu, 4 Mar 2021 10:51:24 -0500 Subject: [PATCH] All tests pass on Python 3. --- src/allmydata/storage_client.py | 2 ++ src/allmydata/test/test_client.py | 11 ++++++----- src/allmydata/util/eliotutil.py | 10 +++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/allmydata/storage_client.py b/src/allmydata/storage_client.py index 75c554562..b59a7f1bc 100644 --- a/src/allmydata/storage_client.py +++ b/src/allmydata/storage_client.py @@ -37,6 +37,7 @@ from __future__ import unicode_literals from future.utils import PY2 if PY2: from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401 +from six import ensure_text import re, time, hashlib @@ -198,6 +199,7 @@ class StorageFarmBroker(service.MultiService): # doesn't really matter but it makes the logging behavior more # predictable and easier to test (and at least one test does depend on # this sorted order). + servers = {ensure_text(key): value for (key, value) in servers.items()} for (server_id, server) in sorted(servers.items()): try: storage_server = self._make_storage_server( diff --git a/src/allmydata/test/test_client.py b/src/allmydata/test/test_client.py index 249be7c38..fdebf4ac2 100644 --- a/src/allmydata/test/test_client.py +++ b/src/allmydata/test/test_client.py @@ -1,3 +1,4 @@ +from future.utils import PY2 from past.builtins import unicode import os, sys @@ -23,7 +24,6 @@ from hypothesis.strategies import ( ) from eliot.testing import ( - capture_logging, assertHasAction, ) from twisted.trial import unittest @@ -64,6 +64,7 @@ from allmydata.util import ( encodingutil, configutil, ) +from allmydata.util.eliotutil import capture_logging from allmydata.util.fileutil import abspath_expanduser_unicode from allmydata.interfaces import IFilesystemNode, IFileNode, \ IImmutableFileNode, IMutableFileNode, IDirectoryNode @@ -798,7 +799,7 @@ class StaticServers(Fixture): for (serverid, announcement) in self._server_details }, - })) + }).encode("utf-8")) class StorageClients(SyncTestCase): @@ -847,7 +848,7 @@ class StorageClients(SyncTestCase): succeeded( AfterPreprocessing( get_known_server_details, - Equals([(serverid, announcement)]), + Equals([(serverid.encode("utf-8"), announcement)]), ), ), ) @@ -874,7 +875,7 @@ class StorageClients(SyncTestCase): self.useFixture( StaticServers( self.basedir, - [(serverid, announcement), + [(serverid.encode("ascii"), announcement), # Along with a "bad" server announcement. Order in this list # doesn't matter, yaml serializer and Python dicts are going # to shuffle everything around kind of randomly. @@ -891,7 +892,7 @@ class StorageClients(SyncTestCase): AfterPreprocessing( get_known_server_details, # It should have the good server details. - Equals([(serverid, announcement)]), + Equals([(serverid.encode("utf-8"), announcement)]), ), ), ) diff --git a/src/allmydata/util/eliotutil.py b/src/allmydata/util/eliotutil.py index 9e3cdd3e1..5d144eb1d 100644 --- a/src/allmydata/util/eliotutil.py +++ b/src/allmydata/util/eliotutil.py @@ -32,7 +32,7 @@ from six import ensure_text from sys import ( stdout, ) -from functools import wraps +from functools import wraps, partial from logging import ( INFO, Handler, @@ -66,6 +66,7 @@ from eliot.twisted import ( DeferredContext, inline_callbacks, ) +from eliot.testing import capture_logging as eliot_capture_logging from twisted.python.usage import ( UsageError, @@ -326,3 +327,10 @@ def log_call_deferred(action_type): return DeferredContext(d).addActionFinish() return logged_f return decorate_log_call_deferred + +# On Python 3, encoding bytes to JSON doesn't work, so we have a custom JSON +# encoder we want to use when validating messages. +if PY2: + capture_logging = eliot_capture_logging +else: + capture_logging = partial(eliot_capture_logging, encoder_=BytesJSONEncoder)