From d2d343c71067b7ffc92ea47c84f98cc5684cf413 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Fri, 8 Jun 2018 12:49:49 -0400 Subject: [PATCH 01/10] Remove usage from the local `TestCase` --- src/allmydata/test/test_node.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 93d38e5ca..4581a4549 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -36,12 +36,14 @@ class TestNode(Node): class TestCase(testutil.SignalMixin, unittest.TestCase): - @defer.inlineCallbacks def setUp(self): testutil.SignalMixin.setUp(self) self.parent = LoggingMultiService() self.parent.startService() - self._available_port = yield iputil.allocate_tcp_port() + # We can use a made-up port number because these tests never actually + # try to bind the port. We'll use a low-numbered one that's likely to + # conflict with another service to prove it. + self._available_port = 22 def tearDown(self): log.msg("%s.tearDown" % self.__class__.__name__) @@ -66,10 +68,9 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): lambda: local_addresses) n = TestNode(basedir) - n.setServiceParent(self.parent) furl = n.tub.registerReference(n) for address in expected_addresses: - self.failUnlessIn(address, furl) + self.assertIn(address, furl) def test_location1(self): return self._test_location(basedir="test_node/test_location1", From 97dfe54f4843c7d9be9439f4bb4997b86349032f Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Fri, 8 Jun 2018 13:05:20 -0400 Subject: [PATCH 02/10] Remove allocate_tcp_port from Listeners tests --- src/allmydata/test/test_node.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 4581a4549..08bbb6ff6 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -5,6 +5,14 @@ import sys import time import mock +from hypothesis import ( + given, + settings, +) +from hypothesis.strategies import ( + integers, + sets, +) from twisted.trial import unittest from twisted.internet import defer @@ -22,6 +30,9 @@ from allmydata.util.namespace import Namespace import allmydata.test.common_util as testutil +def port_numbers(): + return integers(min_value=1, max_value=2 ** 16 - 1) + class LoggingMultiService(service.MultiService): def log(self, msg, **kw): pass @@ -361,14 +372,27 @@ class FakeTub: def setServiceParent(self, parent): pass class Listeners(unittest.TestCase): - def test_multiple_ports(self): + # Randomly allocate a couple distinct port numbers to try out. The test + # never actually binds these port numbers so we don't care if they're "in + # use" on the system or not. We just want a couple distinct values we can + # check expected results against. + @given(ports=sets(elements=port_numbers(), min_size=2, max_size=2)) + # A few examples should satisfy us here. The logic for dealing with these + # numbers is hardly complex. On the flip side, all of the setup work we + # do to get a node we can test against is pretty expensive. + @settings(max_examples=10) + def test_multiple_ports(self, ports): + """ + When there are multiple listen addresses suggested by the ``tub.port`` and + ``tub.location`` configuration, the node's *main* port listens on all + of them. + """ n = EmptyNode() n.basedir = self.mktemp() n.config_fname = os.path.join(n.basedir, "tahoe.cfg") os.mkdir(n.basedir) os.mkdir(os.path.join(n.basedir, "private")) - port1 = iputil.allocate_tcp_port() - port2 = iputil.allocate_tcp_port() + port1, port2 = iter(ports) port = ("tcp:%d:interface=127.0.0.1,tcp:%d:interface=127.0.0.1" % (port1, port2)) location = "tcp:localhost:%d,tcp:localhost:%d" % (port1, port2) From 83dbcdb3bfc9b977e845d765dd8daff0ea0f132b Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 22 Oct 2020 14:31:33 -0400 Subject: [PATCH 03/10] Switch away from EmptyNode since it's gone from master --- src/allmydata/test/test_node.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 973f982c6..ac12b20b7 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -613,16 +613,15 @@ class Listeners(unittest.TestCase): ``tub.location`` configuration, the node's *main* port listens on all of them. """ - n = EmptyNode() - n.basedir = self.mktemp() - n.config_fname = os.path.join(n.basedir, "tahoe.cfg") - os.mkdir(n.basedir) - os.mkdir(os.path.join(n.basedir, "private")) + basedir = self.mktemp() + config_fname = os.path.join(basedir, "tahoe.cfg") + os.mkdir(basedir) + os.mkdir(os.path.join(basedir, "private")) port1, port2 = iter(ports) port = ("tcp:%d:interface=127.0.0.1,tcp:%d:interface=127.0.0.1" % (port1, port2)) location = "tcp:localhost:%d,tcp:localhost:%d" % (port1, port2) - with open(os.path.join(basedir, "tahoe.cfg"), "w") as f: + with open(config_fname, "w") as f: f.write(BASE_CONFIG) f.write("[node]\n") f.write("tub.port = %s\n" % port) From 8e41c2d3e1b8bdee486113fcb11710723638e45d Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 22 Oct 2020 14:36:45 -0400 Subject: [PATCH 04/10] whitespace --- src/allmydata/test/test_node.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index ac12b20b7..f04ec6213 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -117,7 +117,6 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): pass furl = tub.registerReference(Foo()) - for address in expected_addresses: self.assertIn(address, furl) @@ -571,6 +570,7 @@ class FakeTub(object): def setServiceParent(self, parent): pass class Listeners(unittest.TestCase): + def test_listen_on_zero(self): """ Trying to listen on port 0 should be an error From ea257681bb8d44decbabdb89442fb2aba06d1435 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 22 Oct 2020 14:36:49 -0400 Subject: [PATCH 05/10] Stop starting the services so that we stop binding the ports --- src/allmydata/test/test_node.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index f04ec6213..99a30605d 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -84,20 +84,11 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): def setUp(self): testutil.SignalMixin.setUp(self) self.parent = LoggingMultiService() - self.parent.startService() # We can use a made-up port number because these tests never actually # try to bind the port. We'll use a low-numbered one that's likely to # conflict with another service to prove it. self._available_port = 22 - def tearDown(self): - log.msg("%s.tearDown" % self.__class__.__name__) - testutil.SignalMixin.tearDown(self) - d = defer.succeed(None) - d.addCallback(lambda res: self.parent.stopService()) - d.addCallback(flushEventualQueue) - return d - def _test_location(self, basedir, expected_addresses, tub_port=None, tub_location=None, local_addresses=None): create_node_dir(basedir, "testing") config_data = "[node]\n" From 606617cbfd74d3bc149ed185bfb44dc531344697 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 22 Oct 2020 14:41:28 -0400 Subject: [PATCH 06/10] clean up the tub location tests a bit --- src/allmydata/test/test_node.py | 37 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 99a30605d..b26f9efd9 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -89,7 +89,31 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): # conflict with another service to prove it. self._available_port = 22 - def _test_location(self, basedir, expected_addresses, tub_port=None, tub_location=None, local_addresses=None): + def _test_location( + self, + expected_addresses, + tub_port=None, + tub_location=None, + local_addresses=None, + ): + """ + Verify that a Tub configured with the given *tub.port* and *tub.location* + values generates fURLs with the given addresses in its location hints. + + :param [str] expected_addresses: The addresses which must appear in + the generated fURL for the test to pass. All addresses must + appear. + + :param tub_port: If not ``None`` then a value for the *tub.port* + configuration item. + + :param tub_location: If not ``None`` then a value for the *tub.port* + configuration item. + + :param local_addresses: If not ``None`` then a list of addresses to + supply to the system under test as local addresses. + """ + basedir = self.mktemp() create_node_dir(basedir, "testing") config_data = "[node]\n" if tub_port: @@ -97,12 +121,11 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): if tub_location is not None: config_data += "tub.location = {}\n".format(tub_location) - if local_addresses: + if local_addresses is not None: self.patch(iputil, 'get_local_addresses_sync', lambda: local_addresses) tub = testing_tub(config_data) - tub.setServiceParent(self.parent) class Foo(object): pass @@ -112,19 +135,16 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): self.assertIn(address, furl) def test_location1(self): - return self._test_location(basedir="test_node/test_location1", - expected_addresses=["192.0.2.0:1234"], + return self._test_location(expected_addresses=["192.0.2.0:1234"], tub_location="192.0.2.0:1234") def test_location2(self): - return self._test_location(basedir="test_node/test_location2", - expected_addresses=["192.0.2.0:1234", "example.org:8091"], + return self._test_location(expected_addresses=["192.0.2.0:1234", "example.org:8091"], tub_location="192.0.2.0:1234,example.org:8091") def test_location_not_set(self): """Checks the autogenerated furl when tub.location is not set.""" return self._test_location( - basedir="test_node/test_location3", expected_addresses=[ "127.0.0.1:{}".format(self._available_port), "192.0.2.0:{}".format(self._available_port), @@ -136,7 +156,6 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): def test_location_auto_and_explicit(self): """Checks the autogenerated furl when tub.location contains 'AUTO'.""" return self._test_location( - basedir="test_node/test_location4", expected_addresses=[ "127.0.0.1:{}".format(self._available_port), "192.0.2.0:{}".format(self._available_port), From c2dd4dbeb951c2c260d86a191ac728712d8dece6 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 22 Oct 2020 14:42:21 -0400 Subject: [PATCH 07/10] news fragment --- newsfragments/2928.minor | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 newsfragments/2928.minor diff --git a/newsfragments/2928.minor b/newsfragments/2928.minor new file mode 100644 index 000000000..e69de29bb From a5e889f707d50ea2963c8ba634473715199c91c8 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 22 Oct 2020 15:31:39 -0400 Subject: [PATCH 08/10] flakes --- src/allmydata/test/test_node.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index b26f9efd9..81d7a1d36 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -19,9 +19,7 @@ from unittest import skipIf from twisted.trial import unittest from twisted.internet import defer -from twisted.python import log -from foolscap.api import flushEventualQueue import foolscap.logging.log from twisted.application import service From 4d469d7b643059c12e5367b810431359c3fa52c8 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Fri, 30 Oct 2020 21:05:46 -0400 Subject: [PATCH 09/10] The test is faster now, leave the max_examples setting alone --- src/allmydata/test/test_node.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 81d7a1d36..e994ac6ec 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -611,10 +611,6 @@ class Listeners(unittest.TestCase): # use" on the system or not. We just want a couple distinct values we can # check expected results against. @given(ports=sets(elements=port_numbers(), min_size=2, max_size=2)) - # A few examples should satisfy us here. The logic for dealing with these - # numbers is hardly complex. On the flip side, all of the setup work we - # do to get a node we can test against is pretty expensive. - @settings(max_examples=10) def test_multiple_ports(self, ports): """ When there are multiple listen addresses suggested by the ``tub.port`` and From 651c42ac0faf5635f75bddaabeb6f4aed6693c28 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Sat, 31 Oct 2020 11:41:20 -0400 Subject: [PATCH 10/10] remove unused import --- src/allmydata/test/test_node.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index e994ac6ec..c3a23a5b7 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -8,7 +8,6 @@ from textwrap import dedent from hypothesis import ( given, - settings, ) from hypothesis.strategies import ( integers,