Merge pull request #865 from tahoe-lafs/3479.test-node-python-3

Port test_node.py to Python 3

Fixes ticket:3479
This commit is contained in:
Itamar Turner-Trauring 2020-10-28 08:39:19 -04:00 committed by GitHub
commit 91c055a821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 56 additions and 22 deletions

0
3479.minor Normal file
View File

View File

@ -626,7 +626,7 @@ def storage_enabled(config):
:return bool: ``True`` if storage is enabled, ``False`` otherwise.
"""
return config.get_config(b"storage", b"enabled", True, boolean=True)
return config.get_config("storage", "enabled", True, boolean=True)
def anonymous_storage_enabled(config):
@ -640,7 +640,7 @@ def anonymous_storage_enabled(config):
"""
return (
storage_enabled(config) and
config.get_config(b"storage", b"anonymous", True, boolean=True)
config.get_config("storage", "anonymous", True, boolean=True)
)
@ -782,7 +782,7 @@ class _Client(node.Node, pollmixin.PollMixin):
vk_string = ed25519.string_from_verifying_key(self._node_public_key)
vk_bytes = remove_prefix(vk_string, ed25519.PUBLIC_KEY_PREFIX)
seed = base32.b2a(vk_bytes)
self.config.write_config_file("permutation-seed", seed+"\n")
self.config.write_config_file("permutation-seed", seed+b"\n", mode="wb")
return seed.strip()
def get_anonymous_storage_server(self):
@ -1026,7 +1026,7 @@ class _Client(node.Node, pollmixin.PollMixin):
c = ControlServer()
c.setServiceParent(self)
control_url = self.control_tub.registerReference(c)
self.config.write_private_config("control.furl", control_url + b"\n")
self.config.write_private_config("control.furl", control_url + "\n")
def init_helper(self):
self.helper = Helper(self.config.get_config_path("helper"),

View File

@ -1,3 +1,5 @@
from past.builtins import unicode
import time
from zope.interface import implementer
from twisted.application import service

View File

@ -360,14 +360,16 @@ class _Config(object):
"""
privname = os.path.join(self._basedir, "private", name)
try:
value = fileutil.read(privname)
value = fileutil.read(privname, mode="r")
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist"
if default is _None:
raise MissingConfigEntry("The required configuration file %s is missing."
% (quote_output(privname),))
if isinstance(default, (bytes, unicode)):
if isinstance(default, bytes):
default = unicode(default, "utf-8")
if isinstance(default, unicode):
value = default
else:
value = default()
@ -379,19 +381,21 @@ class _Config(object):
config file that resides within the subdirectory named 'private'), and
return it.
"""
if isinstance(value, unicode):
value = value.encode("utf-8")
privname = os.path.join(self._basedir, "private", name)
with open(privname, "wb") as f:
f.write(value)
def get_private_config(self, name, default=_None):
"""Read the (string) contents of a private config file (which is a
"""Read the (native string) contents of a private config file (a
config file that resides within the subdirectory named 'private'),
and return it. Return a default, or raise an error if one was not
given.
"""
privname = os.path.join(self._basedir, "private", name)
try:
return fileutil.read(privname).strip()
return fileutil.read(privname, mode="r").strip()
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist"
@ -549,9 +553,12 @@ def _convert_tub_port(s):
:returns: a proper Twisted endpoint string like (`tcp:X`) is `s`
is a bare number, or returns `s` as-is
"""
if re.search(r'^\d+$', s):
return "tcp:{}".format(int(s))
return s
us = s
if isinstance(s, bytes):
us = s.decode("utf-8")
if re.search(r'^\d+$', us):
return "tcp:{}".format(int(us))
return us
def _tub_portlocation(config):
@ -749,7 +756,7 @@ class Node(service.MultiService):
if self.tub is not None:
self.nodeid = b32decode(self.tub.tubID.upper()) # binary format
self.short_nodeid = b32encode(self.nodeid).lower()[:8] # for printing
self.config.write_config_file("my_nodeid", b32encode(self.nodeid).lower() + "\n")
self.config.write_config_file("my_nodeid", b32encode(self.nodeid).lower() + b"\n", mode="wb")
self.tub.setServiceParent(self)
else:
self.nodeid = self.short_nodeid = None
@ -852,7 +859,7 @@ class Node(service.MultiService):
self.config.get_config_path("log_gatherer.furl"))
incident_dir = self.config.get_config_path("logs", "incidents")
foolscap.logging.log.setLogDir(incident_dir.encode(get_filesystem_encoding()))
foolscap.logging.log.setLogDir(incident_dir)
twlog.msg("Foolscap logging initialized")
twlog.msg("Note to developers: twistd.log does not receive very much.")
twlog.msg("Use 'flogtool tail -c NODEDIR/private/logport.furl' instead")

View File

@ -1,3 +1,15 @@
"""
Ported to Python 3.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
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
import base64
import os
import stat
@ -145,7 +157,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
def test_tahoe_cfg_utf8(self):
basedir = "test_node/test_tahoe_cfg_utf8"
fileutil.make_dirs(basedir)
f = open(os.path.join(basedir, 'tahoe.cfg'), 'wt')
f = open(os.path.join(basedir, 'tahoe.cfg'), 'wb')
f.write(u"\uFEFF[node]\n".encode('utf-8'))
f.write(u"nickname = \u2621\n".encode('utf-8'))
f.close()
@ -194,8 +206,8 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
config = read_config(basedir, "portnum")
self.assertEqual(
config.items("node"),
[(b"nickname", b"foo"),
(b"timeout.disconnect", b"12"),
[("nickname", "foo"),
("timeout.disconnect", "12"),
],
)
@ -372,7 +384,7 @@ class TestMissingPorts(unittest.TestCase):
with get_addr, alloc_port:
tubport, tublocation = _tub_portlocation(config)
self.assertEqual(tubport, "tcp:777")
self.assertEqual(tublocation, "tcp:LOCAL:777")
self.assertEqual(tublocation, b"tcp:LOCAL:777")
def test_parsing_defaults(self):
"""
@ -394,7 +406,7 @@ class TestMissingPorts(unittest.TestCase):
with get_addr, alloc_port:
tubport, tublocation = _tub_portlocation(config)
self.assertEqual(tubport, "tcp:999")
self.assertEqual(tublocation, "tcp:LOCAL:999")
self.assertEqual(tublocation, b"tcp:LOCAL:999")
def test_parsing_location_complex(self):
"""
@ -417,7 +429,7 @@ class TestMissingPorts(unittest.TestCase):
with get_addr, alloc_port:
tubport, tublocation = _tub_portlocation(config)
self.assertEqual(tubport, "tcp:999")
self.assertEqual(tublocation, "tcp:HOST:888,tcp:LOCAL:999")
self.assertEqual(tublocation, b"tcp:HOST:888,tcp:LOCAL:999")
def test_parsing_all_disabled(self):
"""
@ -545,7 +557,7 @@ enabled = true
class FakeTub(object):
def __init__(self):
self.tubID = base64.b32encode("foo")
self.tubID = base64.b32encode(b"foo")
self.listening_ports = []
def setOption(self, name, value): pass
def removeAllConnectionHintHandlers(self): pass

View File

@ -413,6 +413,16 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase):
f.write(b"foobar")
f.close()
def test_write(self):
"""fileutil.write() can write both unicode and bytes."""
path = self.mktemp()
fileutil.write(path, b"abc")
with open(path, "rb") as f:
self.assertEqual(f.read(), b"abc")
fileutil.write(path, u"def \u1234")
with open(path, "rb") as f:
self.assertEqual(f.read(), u"def \u1234".encode("utf-8"))
class PollMixinTests(unittest.TestCase):
def setUp(self):

View File

@ -121,6 +121,7 @@ PORTED_TEST_MODULES = [
"allmydata.test.test_monitor",
"allmydata.test.test_netstring",
"allmydata.test.test_no_network",
"allmydata.test.test_node",
"allmydata.test.test_observer",
"allmydata.test.test_pipeline",
"allmydata.test.test_python3",

View File

@ -271,11 +271,13 @@ def write_atomically(target, contents, mode="b"):
move_into_place(target+".tmp", target)
def write(path, data, mode="wb"):
if "b" in mode and isinstance(data, str):
data = data.encode("utf-8")
with open(path, mode) as f:
f.write(data)
def read(path):
with open(path, "rb") as rf:
def read(path, mode="rb"):
with open(path, mode) as rf:
return rf.read()
def put_file(path, inf):