mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-20 13:33:09 +00:00
replace PortLocations tests
The old tests were hard to read due to all the "if" statements; these might be slightly more verbose but also more explicit
This commit is contained in:
parent
4d7f8ec9dd
commit
ab947704f0
@ -265,86 +265,160 @@ EXPECTED = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
class PortLocation(unittest.TestCase):
|
|
||||||
def test_all(self):
|
|
||||||
for tp in EXPECTED.keys():
|
|
||||||
for tl in EXPECTED[tp].keys():
|
|
||||||
exp = EXPECTED[tp][tl]
|
|
||||||
self._try(tp, tl, exp)
|
|
||||||
|
|
||||||
def _try(self, tp, tl, exp):
|
class TestMissingPorts(unittest.TestCase):
|
||||||
log.msg("PortLocation._try:", tp, tl, exp)
|
"""
|
||||||
cfg_tubport = {"missing": None,
|
Test certain error-cases for ports setup
|
||||||
"empty": "",
|
"""
|
||||||
"disabled": "disabled",
|
|
||||||
"endpoint": "tcp:777",
|
|
||||||
}[tp]
|
|
||||||
cfg_location = {"missing": None,
|
|
||||||
"empty": "",
|
|
||||||
"disabled": "disabled",
|
|
||||||
"hintstring": "tcp:HOST:888,AUTO",
|
|
||||||
}[tl]
|
|
||||||
|
|
||||||
basedir = os.path.join("test_node/portlocation/%s/%s" % (tp, tl))
|
def setUp(self):
|
||||||
create_node_dir(basedir, "testing")
|
self.basedir = self.mktemp()
|
||||||
config = read_config(
|
create_node_dir(self.basedir, "testing")
|
||||||
basedir,
|
|
||||||
"node.port",
|
def test_0(self):
|
||||||
_valid_config_sections=client_valid_config_sections,
|
get_addr = mock.patch(
|
||||||
|
"allmydata.util.iputil.get_local_addresses_sync",
|
||||||
|
return_value=["LOCAL"],
|
||||||
)
|
)
|
||||||
n = EmptyNode(config)
|
alloc_port = mock.patch(
|
||||||
n._reveal_ip = True
|
"allmydata.util.iputil.allocate_tcp_port",
|
||||||
|
return_value=999,
|
||||||
|
)
|
||||||
|
config = read_config(self.basedir, "portnum")
|
||||||
|
|
||||||
if exp in ("ERR1", "ERR2", "ERR3", "ERR4"):
|
with get_addr, alloc_port:
|
||||||
e = self.assertRaises(ValueError, n.get_tub_portlocation,
|
n = Node(config)
|
||||||
cfg_tubport, cfg_location)
|
# could probably refactor this get_tub_portlocation into a
|
||||||
if exp == "ERR1":
|
# bare helper instead of method.
|
||||||
self.assertEqual("tub.port must not be empty", str(e))
|
cfg_tubport = "tcp:777"
|
||||||
elif exp == "ERR2":
|
cfg_location = "AUTO"
|
||||||
self.assertEqual("tub.location must not be empty", str(e))
|
tubport, tublocation = n.get_tub_portlocation(cfg_tubport, cfg_location)
|
||||||
elif exp == "ERR3":
|
self.assertEqual(tubport, "tcp:777")
|
||||||
self.assertEqual("tub.port is disabled, but not tub.location",
|
self.assertEqual(tublocation, "tcp:LOCAL:777")
|
||||||
str(e))
|
|
||||||
elif exp == "ERR4":
|
def test_1(self):
|
||||||
self.assertEqual("tub.location is disabled, but not tub.port",
|
get_addr = mock.patch(
|
||||||
str(e))
|
"allmydata.util.iputil.get_local_addresses_sync",
|
||||||
else:
|
return_value=["LOCAL"],
|
||||||
self.assert_(False)
|
)
|
||||||
elif exp == "no-listen":
|
alloc_port = mock.patch(
|
||||||
|
"allmydata.util.iputil.allocate_tcp_port",
|
||||||
|
return_value=999,
|
||||||
|
)
|
||||||
|
config = read_config(self.basedir, "portnum")
|
||||||
|
|
||||||
|
with get_addr, alloc_port:
|
||||||
|
n = Node(config)
|
||||||
|
# could probably refactor this get_tub_portlocation into a
|
||||||
|
# bare helper instead of method.
|
||||||
|
cfg_tubport = None
|
||||||
|
cfg_location = None
|
||||||
|
tubport, tublocation = n.get_tub_portlocation(cfg_tubport, cfg_location)
|
||||||
|
self.assertEqual(tubport, "tcp:999")
|
||||||
|
self.assertEqual(tublocation, "tcp:LOCAL:999")
|
||||||
|
|
||||||
|
def test_2(self):
|
||||||
|
get_addr = mock.patch(
|
||||||
|
"allmydata.util.iputil.get_local_addresses_sync",
|
||||||
|
return_value=["LOCAL"],
|
||||||
|
)
|
||||||
|
alloc_port = mock.patch(
|
||||||
|
"allmydata.util.iputil.allocate_tcp_port",
|
||||||
|
return_value=999,
|
||||||
|
)
|
||||||
|
config = read_config(self.basedir, "portnum")
|
||||||
|
|
||||||
|
with get_addr, alloc_port:
|
||||||
|
n = Node(config)
|
||||||
|
# could probably refactor this get_tub_portlocation into a
|
||||||
|
# bare helper instead of method.
|
||||||
|
cfg_tubport = None
|
||||||
|
cfg_location = "tcp:HOST:888,AUTO"
|
||||||
|
tubport, tublocation = n.get_tub_portlocation(cfg_tubport, cfg_location)
|
||||||
|
self.assertEqual(tubport, "tcp:999")
|
||||||
|
self.assertEqual(tublocation, "tcp:HOST:888,tcp:LOCAL:999")
|
||||||
|
|
||||||
|
def test_3(self):
|
||||||
|
get_addr = mock.patch(
|
||||||
|
"allmydata.util.iputil.get_local_addresses_sync",
|
||||||
|
return_value=["LOCAL"],
|
||||||
|
)
|
||||||
|
alloc_port = mock.patch(
|
||||||
|
"allmydata.util.iputil.allocate_tcp_port",
|
||||||
|
return_value=999,
|
||||||
|
)
|
||||||
|
config = read_config(self.basedir, "portnum")
|
||||||
|
|
||||||
|
with get_addr, alloc_port:
|
||||||
|
n = Node(config)
|
||||||
|
# could probably refactor this get_tub_portlocation into a
|
||||||
|
# bare helper instead of method.
|
||||||
|
cfg_tubport = "disabled"
|
||||||
|
cfg_location = "disabled"
|
||||||
res = n.get_tub_portlocation(cfg_tubport, cfg_location)
|
res = n.get_tub_portlocation(cfg_tubport, cfg_location)
|
||||||
self.assertEqual(res, None)
|
self.assertTrue(res is None)
|
||||||
elif exp in ("alloc/auto", "alloc/file", "auto", "manual"):
|
|
||||||
with mock.patch("allmydata.util.iputil.get_local_addresses_sync",
|
def test_empty_tub_port(self):
|
||||||
return_value=["LOCAL"]):
|
with open(os.path.join(self.basedir, "tahoe.cfg"), "w") as f:
|
||||||
with mock.patch("allmydata.util.iputil.allocate_tcp_port",
|
f.write(
|
||||||
return_value=999):
|
"[node]\n"
|
||||||
port, location = n.get_tub_portlocation(cfg_tubport,
|
"tub.port = \n"
|
||||||
cfg_location)
|
)
|
||||||
try:
|
config = read_config(self.basedir, "portnum")
|
||||||
with open(config.portnum_fname, "r") as f:
|
|
||||||
saved_port = f.read().strip()
|
with self.assertRaises(ValueError) as ctx:
|
||||||
except EnvironmentError:
|
Node(config)
|
||||||
saved_port = None
|
self.assertIn(
|
||||||
if exp == "alloc/auto":
|
"tub.port must not be empty",
|
||||||
self.assertEqual(port, "tcp:999")
|
str(ctx.exception)
|
||||||
self.assertEqual(location, "tcp:LOCAL:999")
|
)
|
||||||
self.assertEqual(saved_port, "tcp:999")
|
|
||||||
elif exp == "alloc/file":
|
def test_empty_tub_location(self):
|
||||||
self.assertEqual(port, "tcp:999")
|
with open(os.path.join(self.basedir, "tahoe.cfg"), "w") as f:
|
||||||
self.assertEqual(location, "tcp:HOST:888,tcp:LOCAL:999")
|
f.write(
|
||||||
self.assertEqual(saved_port, "tcp:999")
|
"[node]\n"
|
||||||
elif exp == "auto":
|
"tub.location = \n"
|
||||||
self.assertEqual(port, "tcp:777")
|
)
|
||||||
self.assertEqual(location, "tcp:LOCAL:777")
|
config = read_config(self.basedir, "portnum")
|
||||||
self.assertEqual(saved_port, None)
|
|
||||||
elif exp == "manual":
|
with self.assertRaises(ValueError) as ctx:
|
||||||
self.assertEqual(port, "tcp:777")
|
Node(config)
|
||||||
self.assertEqual(location, "tcp:HOST:888,tcp:LOCAL:777")
|
self.assertIn(
|
||||||
self.assertEqual(saved_port, None)
|
"tub.location must not be empty",
|
||||||
else:
|
str(ctx.exception)
|
||||||
self.assert_(False)
|
)
|
||||||
else:
|
|
||||||
self.assert_(False)
|
def test_disabled_port_not_tub(self):
|
||||||
|
with open(os.path.join(self.basedir, "tahoe.cfg"), "w") as f:
|
||||||
|
f.write(
|
||||||
|
"[node]\n"
|
||||||
|
"tub.port = disabled\n"
|
||||||
|
"tub.location = not_disabled\n"
|
||||||
|
)
|
||||||
|
config = read_config(self.basedir, "portnum")
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError) as ctx:
|
||||||
|
Node(config)
|
||||||
|
self.assertIn(
|
||||||
|
"tub.port is disabled, but not tub.location",
|
||||||
|
str(ctx.exception)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_disabled_tub_not_port(self):
|
||||||
|
with open(os.path.join(self.basedir, "tahoe.cfg"), "w") as f:
|
||||||
|
f.write(
|
||||||
|
"[node]\n"
|
||||||
|
"tub.port = not_disabled\n"
|
||||||
|
"tub.location = disabled\n"
|
||||||
|
)
|
||||||
|
config = read_config(self.basedir, "portnum")
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError) as ctx:
|
||||||
|
Node(config)
|
||||||
|
self.assertIn(
|
||||||
|
"tub.location is disabled, but not tub.port",
|
||||||
|
str(ctx.exception)
|
||||||
|
)
|
||||||
|
|
||||||
BASE_CONFIG = """
|
BASE_CONFIG = """
|
||||||
[client]
|
[client]
|
||||||
|
Loading…
Reference in New Issue
Block a user