Merge remote-tracking branch 'origin/master' into clean-up-tor-and-i2p-providers

This commit is contained in:
Jean-Paul Calderone 2023-04-05 08:37:58 -04:00
commit 7cd0c9d724
9 changed files with 84 additions and 124 deletions

View File

@ -150,7 +150,8 @@ def flog_gatherer(reactor, temp_dir, flog_binary, request):
'--location', 'tcp:localhost:3117',
'--port', '3117',
gather_dir,
)
),
env=environ,
)
pytest_twisted.blockon(out_protocol.done)
@ -163,6 +164,7 @@ def flog_gatherer(reactor, temp_dir, flog_binary, request):
join(gather_dir, 'gatherer.tac'),
),
path=gather_dir,
env=environ,
)
pytest_twisted.blockon(twistd_protocol.magic_seen)
@ -181,6 +183,7 @@ def flog_gatherer(reactor, temp_dir, flog_binary, request):
(
'flogtool', 'dump', join(temp_dir, 'flog_gather', flogs[0])
),
env=environ,
)
print("Waiting for flogtool to complete")
try:

View File

@ -2,26 +2,11 @@
Integration tests for I2P support.
"""
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
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 sys
from os.path import join, exists
from os import mkdir
from os import mkdir, environ
from time import sleep
if PY2:
def which(path):
# This will result in skipping I2P tests on Python 2. Oh well.
return None
else:
from shutil import which
from shutil import which
from eliot import log_call
@ -62,6 +47,7 @@ def i2p_network(reactor, temp_dir, request):
"--log=stdout",
"--loglevel=info"
),
env=environ,
)
def cleanup():
@ -170,7 +156,8 @@ def test_i2p_service_storage(reactor, request, temp_dir, flog_gatherer, i2p_netw
sys.executable, '-b', '-m', 'allmydata.scripts.runner',
'-d', join(temp_dir, 'carol_i2p'),
'put', gold_path,
)
),
env=environ,
)
yield proto.done
cap = proto.output.getvalue().strip().split()[-1]
@ -184,7 +171,8 @@ def test_i2p_service_storage(reactor, request, temp_dir, flog_gatherer, i2p_netw
sys.executable, '-b', '-m', 'allmydata.scripts.runner',
'-d', join(temp_dir, 'dave_i2p'),
'get', cap,
)
),
env=environ,
)
yield proto.done
@ -211,7 +199,8 @@ def _create_anonymous_node(reactor, name, control_port, request, temp_dir, flog_
'--hide-ip',
'--listen', 'i2p',
node_dir.path,
)
),
env=environ,
)
yield proto.done

View File

@ -1,17 +1,10 @@
"""
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 sys
from os.path import join
from os import environ
from twisted.internet.error import ProcessTerminated
@ -45,7 +38,8 @@ def test_upload_immutable(reactor, temp_dir, introducer_furl, flog_gatherer, sto
sys.executable, '-b', '-m', 'allmydata.scripts.runner',
'-d', node_dir,
'put', __file__,
]
],
env=environ,
)
try:
yield proto.done

View File

@ -1,17 +1,10 @@
"""
Ported to Python 3.
"""
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
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 sys
from os.path import join
from os import environ
import pytest
import pytest_twisted
@ -35,9 +28,6 @@ from allmydata.test.common import (
if sys.platform.startswith('win'):
pytest.skip('Skipping Tor tests on Windows', allow_module_level=True)
if PY2:
pytest.skip('Skipping Tor tests on Python 2 because dependencies are hard to come by', allow_module_level=True)
@pytest_twisted.inlineCallbacks
def test_onion_service_storage(reactor, request, temp_dir, flog_gatherer, tor_network, tor_introducer_furl):
carol = yield _create_anonymous_node(reactor, 'carol', 8008, request, temp_dir, flog_gatherer, tor_network, tor_introducer_furl)
@ -65,7 +55,8 @@ def test_onion_service_storage(reactor, request, temp_dir, flog_gatherer, tor_ne
sys.executable, '-b', '-m', 'allmydata.scripts.runner',
'-d', join(temp_dir, 'carol'),
'put', gold_path,
)
),
env=environ,
)
yield proto.done
cap = proto.output.getvalue().strip().split()[-1]
@ -79,7 +70,8 @@ def test_onion_service_storage(reactor, request, temp_dir, flog_gatherer, tor_ne
sys.executable, '-b', '-m', 'allmydata.scripts.runner',
'-d', join(temp_dir, 'dave'),
'get', cap,
)
),
env=environ,
)
yield proto.done
@ -108,7 +100,8 @@ def _create_anonymous_node(reactor, name, control_port, request, temp_dir, flog_
'--tor-control-port', 'tcp:localhost:{}'.format(control_port),
'--listen', 'tor',
node_dir.path,
)
),
env=environ,
)
yield proto.done

View File

@ -9,7 +9,7 @@ no_implicit_optional = True
warn_redundant_casts = True
strict_equality = True
[mypy-allmydata.test.cli.wormholetesting,allmydata.listeners]
[mypy-allmydata.test.cli.wormholetesting,allmydata.listeners,allmydata.test.test_connection_status]
disallow_any_generics = True
disallow_subclassing_any = True
disallow_untyped_calls = True

0
newsfragments/4001.minor Normal file
View File

0
newsfragments/4003.minor Normal file
View File

View File

@ -1,25 +1,46 @@
"""
Tests for allmydata.util.connection_status.
Port 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 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 __future__ import annotations
import mock
from typing import Optional
from twisted.trial import unittest
from foolscap.reconnector import ReconnectionInfo, Reconnector
from foolscap.info import ConnectionInfo
from ..util import connection_status
from .common import SyncTestCase
class Status(unittest.TestCase):
def test_hint_statuses(self):
def reconnector(info: ReconnectionInfo) -> Reconnector:
rc = Reconnector(None, None, (), {}) # type: ignore[no-untyped-call]
rc._reconnectionInfo = info
return rc
def connection_info(
statuses: dict[str, str],
handlers: dict[str, str],
winningHint: Optional[str],
establishedAt: Optional[int],
) -> ConnectionInfo:
ci = ConnectionInfo() # type: ignore[no-untyped-call]
ci.connectorStatuses = statuses
ci.connectionHandlers = handlers
ci.winningHint = winningHint
ci.establishedAt = establishedAt
return ci
def reconnection_info(
state: str,
connection_info: ConnectionInfo,
) -> ReconnectionInfo:
ri = ReconnectionInfo() # type: ignore[no-untyped-call]
ri.state = state
ri.connectionInfo = connection_info
return ri
class Status(SyncTestCase):
def test_hint_statuses(self) -> None:
ncs = connection_status._hint_statuses(["h2","h1"],
{"h1": "hand1", "h4": "hand4"},
{"h1": "st1", "h2": "st2",
@ -27,17 +48,10 @@ class Status(unittest.TestCase):
self.assertEqual(ncs, {"h1 via hand1": "st1",
"h2": "st2"})
def test_reconnector_connected(self):
ci = mock.Mock()
ci.connectorStatuses = {"h1": "st1"}
ci.connectionHandlers = {"h1": "hand1"}
ci.winningHint = "h1"
ci.establishedAt = 120
ri = mock.Mock()
ri.state = "connected"
ri.connectionInfo = ci
rc = mock.Mock
rc.getReconnectionInfo = mock.Mock(return_value=ri)
def test_reconnector_connected(self) -> None:
ci = connection_info({"h1": "st1"}, {"h1": "hand1"}, "h1", 120)
ri = reconnection_info("connected", ci)
rc = reconnector(ri)
cs = connection_status.from_foolscap_reconnector(rc, 123)
self.assertEqual(cs.connected, True)
self.assertEqual(cs.summary, "Connected to h1 via hand1")
@ -45,17 +59,10 @@ class Status(unittest.TestCase):
self.assertEqual(cs.last_connection_time, 120)
self.assertEqual(cs.last_received_time, 123)
def test_reconnector_connected_others(self):
ci = mock.Mock()
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
ci.connectionHandlers = {"h1": "hand1"}
ci.winningHint = "h1"
ci.establishedAt = 120
ri = mock.Mock()
ri.state = "connected"
ri.connectionInfo = ci
rc = mock.Mock
rc.getReconnectionInfo = mock.Mock(return_value=ri)
def test_reconnector_connected_others(self) -> None:
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, "h1", 120)
ri = reconnection_info("connected", ci)
rc = reconnector(ri)
cs = connection_status.from_foolscap_reconnector(rc, 123)
self.assertEqual(cs.connected, True)
self.assertEqual(cs.summary, "Connected to h1 via hand1")
@ -63,18 +70,11 @@ class Status(unittest.TestCase):
self.assertEqual(cs.last_connection_time, 120)
self.assertEqual(cs.last_received_time, 123)
def test_reconnector_connected_listener(self):
ci = mock.Mock()
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
ci.connectionHandlers = {"h1": "hand1"}
def test_reconnector_connected_listener(self) -> None:
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, 120)
ci.listenerStatus = ("listener1", "successful")
ci.winningHint = None
ci.establishedAt = 120
ri = mock.Mock()
ri.state = "connected"
ri.connectionInfo = ci
rc = mock.Mock
rc.getReconnectionInfo = mock.Mock(return_value=ri)
ri = reconnection_info("connected", ci)
rc = reconnector(ri)
cs = connection_status.from_foolscap_reconnector(rc, 123)
self.assertEqual(cs.connected, True)
self.assertEqual(cs.summary, "Connected via listener (listener1)")
@ -83,15 +83,10 @@ class Status(unittest.TestCase):
self.assertEqual(cs.last_connection_time, 120)
self.assertEqual(cs.last_received_time, 123)
def test_reconnector_connecting(self):
ci = mock.Mock()
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
ci.connectionHandlers = {"h1": "hand1"}
ri = mock.Mock()
ri.state = "connecting"
ri.connectionInfo = ci
rc = mock.Mock
rc.getReconnectionInfo = mock.Mock(return_value=ri)
def test_reconnector_connecting(self) -> None:
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None)
ri = reconnection_info("connecting", ci)
rc = reconnector(ri)
cs = connection_status.from_foolscap_reconnector(rc, 123)
self.assertEqual(cs.connected, False)
self.assertEqual(cs.summary, "Trying to connect")
@ -100,19 +95,13 @@ class Status(unittest.TestCase):
self.assertEqual(cs.last_connection_time, None)
self.assertEqual(cs.last_received_time, 123)
def test_reconnector_waiting(self):
ci = mock.Mock()
ci.connectorStatuses = {"h1": "st1", "h2": "st2"}
ci.connectionHandlers = {"h1": "hand1"}
ri = mock.Mock()
ri.state = "waiting"
def test_reconnector_waiting(self) -> None:
ci = connection_info({"h1": "st1", "h2": "st2"}, {"h1": "hand1"}, None, None)
ri = reconnection_info("waiting", ci)
ri.lastAttempt = 10
ri.nextAttempt = 20
ri.connectionInfo = ci
rc = mock.Mock
rc.getReconnectionInfo = mock.Mock(return_value=ri)
with mock.patch("time.time", return_value=12):
cs = connection_status.from_foolscap_reconnector(rc, 5)
rc = reconnector(ri)
cs = connection_status.from_foolscap_reconnector(rc, 5, time=lambda: 12)
self.assertEqual(cs.connected, False)
self.assertEqual(cs.summary,
"Reconnecting in 8 seconds (last attempt 2s ago)")

View File

@ -1,21 +1,13 @@
"""
Parse connection status from Foolscap.
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 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 __future__ import annotations
import time
from zope.interface import implementer
from ..interfaces import IConnectionStatus
from foolscap.reconnector import Reconnector
@implementer(IConnectionStatus)
class ConnectionStatus(object):
@ -41,7 +33,7 @@ class ConnectionStatus(object):
last_received_time=None,
)
def _hint_statuses(which, handlers, statuses):
def _hint_statuses(which, handlers, statuses) -> dict[str, str]:
non_connected_statuses = {}
for hint in which:
handler = handlers.get(hint)
@ -50,7 +42,7 @@ def _hint_statuses(which, handlers, statuses):
non_connected_statuses["%s%s" % (hint, handler_dsc)] = dsc
return non_connected_statuses
def from_foolscap_reconnector(rc, last_received):
def from_foolscap_reconnector(rc: Reconnector, last_received: int, time=time.time) -> ConnectionStatus:
ri = rc.getReconnectionInfo()
# See foolscap/reconnector.py, ReconnectionInfo, for details about possible
# states. The returned result is a native string, it seems, so convert to
@ -80,7 +72,7 @@ def from_foolscap_reconnector(rc, last_received):
# ci describes the current in-progress attempt
summary = "Trying to connect"
elif state == "waiting":
now = time.time()
now = time()
elapsed = now - ri.lastAttempt
delay = ri.nextAttempt - now
summary = "Reconnecting in %d seconds (last attempt %ds ago)" % \