use port assigner and UseNode more in test_node.py

This commit is contained in:
Jean-Paul Calderone 2021-11-03 16:10:32 -04:00
parent 31649890ef
commit 5a71774bf8

View File

@ -69,6 +69,8 @@ import allmydata.test.common_util as testutil
from .common import ( from .common import (
ConstantAddresses, ConstantAddresses,
SameProcessStreamEndpointAssigner,
UseNode,
) )
def port_numbers(): def port_numbers():
@ -80,11 +82,10 @@ class LoggingMultiService(service.MultiService):
# see https://tahoe-lafs.org/trac/tahoe-lafs/ticket/2946 # see https://tahoe-lafs.org/trac/tahoe-lafs/ticket/2946
def testing_tub(config_data=''): def testing_tub(reactor, config_data=''):
""" """
Creates a 'main' Tub for testing purposes, from config data Creates a 'main' Tub for testing purposes, from config data
""" """
from twisted.internet import reactor
basedir = 'dummy_basedir' basedir = 'dummy_basedir'
config = config_from_string(basedir, 'DEFAULT_PORTNUMFILE_BLANK', config_data) config = config_from_string(basedir, 'DEFAULT_PORTNUMFILE_BLANK', config_data)
fileutil.make_dirs(os.path.join(basedir, 'private')) fileutil.make_dirs(os.path.join(basedir, 'private'))
@ -112,6 +113,9 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
# try to bind the port. We'll use a low-numbered one that's likely to # try to bind the port. We'll use a low-numbered one that's likely to
# conflict with another service to prove it. # conflict with another service to prove it.
self._available_port = 22 self._available_port = 22
self.port_assigner = SameProcessStreamEndpointAssigner()
self.port_assigner.setUp()
self.addCleanup(self.port_assigner.tearDown)
def _test_location( def _test_location(
self, self,
@ -137,11 +141,23 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
:param local_addresses: If not ``None`` then a list of addresses to :param local_addresses: If not ``None`` then a list of addresses to
supply to the system under test as local addresses. supply to the system under test as local addresses.
""" """
from twisted.internet import reactor
basedir = self.mktemp() basedir = self.mktemp()
create_node_dir(basedir, "testing") create_node_dir(basedir, "testing")
if tub_port is None:
# Always configure a usable tub.port address instead of relying on
# the automatic port assignment. The automatic port assignment is
# prone to collisions and spurious test failures.
_, tub_port = self.port_assigner.assign(reactor)
config_data = "[node]\n" config_data = "[node]\n"
if tub_port: config_data += "tub.port = {}\n".format(tub_port)
config_data += "tub.port = {}\n".format(tub_port)
# If they wanted a certain location, go for it. This probably won't
# agree with the tub.port value we set but that only matters if
# anything tries to use this to establish a connection ... which
# nothing in this test suite will.
if tub_location is not None: if tub_location is not None:
config_data += "tub.location = {}\n".format(tub_location) config_data += "tub.location = {}\n".format(tub_location)
@ -149,7 +165,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
self.patch(iputil, 'get_local_addresses_sync', self.patch(iputil, 'get_local_addresses_sync',
lambda: local_addresses) lambda: local_addresses)
tub = testing_tub(config_data) tub = testing_tub(reactor, config_data)
class Foo(object): class Foo(object):
pass pass
@ -431,7 +447,12 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def test_logdir_is_str(self): def test_logdir_is_str(self):
basedir = "test_node/test_logdir_is_str" from twisted.internet import reactor
basedir = FilePath(self.mktemp())
fixture = UseNode(None, None, basedir, "pb://introducer/furl", {}, reactor=reactor)
fixture.setUp()
self.addCleanup(fixture.cleanUp)
ns = Namespace() ns = Namespace()
ns.called = False ns.called = False
@ -440,8 +461,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
self.failUnless(isinstance(logdir, str), logdir) self.failUnless(isinstance(logdir, str), logdir)
self.patch(foolscap.logging.log, 'setLogDir', call_setLogDir) self.patch(foolscap.logging.log, 'setLogDir', call_setLogDir)
create_node_dir(basedir, "nothing to see here") yield fixture.create_node()
yield client.create_client(basedir)
self.failUnless(ns.called) self.failUnless(ns.called)
def test_set_config_unescaped_furl_hash(self): def test_set_config_unescaped_furl_hash(self):