Get rid of the remaining mocks

This commit is contained in:
Jean-Paul Calderone 2020-12-11 10:38:15 -05:00
parent 0cdf66a991
commit 9259264d27
2 changed files with 51 additions and 65 deletions

View File

@ -62,10 +62,16 @@ from twisted.internet.endpoints import AdoptedStreamServerEndpoint
from twisted.trial.unittest import TestCase as _TrialTestCase from twisted.trial.unittest import TestCase as _TrialTestCase
from allmydata import uri from allmydata import uri
from allmydata.interfaces import IMutableFileNode, IImmutableFileNode,\ from allmydata.interfaces import (
NotEnoughSharesError, ICheckable, \ IMutableFileNode,
IMutableUploadable, SDMF_VERSION, \ IImmutableFileNode,
MDMF_VERSION NotEnoughSharesError,
ICheckable,
IMutableUploadable,
SDMF_VERSION,
MDMF_VERSION,
IAddressFamily,
)
from allmydata.check_results import CheckResults, CheckAndRepairResults, \ from allmydata.check_results import CheckResults, CheckAndRepairResults, \
DeepCheckResults, DeepCheckAndRepairResults DeepCheckResults, DeepCheckAndRepairResults
from allmydata.storage_client import StubServer from allmydata.storage_client import StubServer
@ -1145,6 +1151,27 @@ def _corrupt_uri_extension(data, debug=False):
return corrupt_field(data, 0x0c+uriextoffset, uriextlen) return corrupt_field(data, 0x0c+uriextoffset, uriextlen)
@attr.s
@implementer(IAddressFamily)
class FakeProvider(object):
"""
Pretend to provide support for some address family but just hand out
canned responses.
"""
_listener = attr.ib(default=None)
_handler = attr.ib(default=None)
def get_listener(self):
if self._listener is None:
raise Exception("{!r} has no listener.")
return self._listener
def get_client_endpoint(self):
if self._handler is None:
raise Exception("{!r} has no client endpoint.")
class _TestCaseMixin(object): class _TestCaseMixin(object):
""" """
A mixin for ``TestCase`` which collects helpful behaviors for subclasses. A mixin for ``TestCase`` which collects helpful behaviors for subclasses.

View File

@ -15,7 +15,6 @@ import os
import stat import stat
import sys import sys
import time import time
import mock
from textwrap import dedent from textwrap import dedent
import configparser import configparser
@ -36,7 +35,6 @@ from twisted.trial import unittest
from twisted.internet import defer from twisted.internet import defer
import foolscap.logging.log import foolscap.logging.log
from foolscap.connections.tcp import default as make_tcp_handler
from twisted.application import service from twisted.application import service
from allmydata.node import ( from allmydata.node import (
@ -69,6 +67,9 @@ from allmydata.util.i2p_provider import create as create_i2p_provider
from allmydata.util.tor_provider import create as create_tor_provider from allmydata.util.tor_provider import create as create_tor_provider
import allmydata.test.common_util as testutil import allmydata.test.common_util as testutil
from .common import (
FakeProvider,
)
def port_numbers(): def port_numbers():
return integers(min_value=1, max_value=2 ** 16 - 1) return integers(min_value=1, max_value=2 ** 16 - 1)
@ -796,70 +797,38 @@ class Listeners(unittest.TestCase):
``tub.location`` configuration, the node's *main* port listens on all ``tub.location`` configuration, the node's *main* port listens on all
of them. of them.
""" """
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) port1, port2 = iter(ports)
port = ("tcp:%d:interface=127.0.0.1,tcp:%d:interface=127.0.0.1" % port = ("tcp:%d:interface=127.0.0.1,tcp:%d:interface=127.0.0.1" %
(port1, port2)) (port1, port2))
location = "tcp:localhost:%d,tcp:localhost:%d" % (port1, port2) location = "tcp:localhost:%d,tcp:localhost:%d" % (port1, port2)
with open(config_fname, "w") as f:
f.write(BASE_CONFIG)
f.write("[node]\n")
f.write("tub.port = %s\n" % port)
f.write("tub.location = %s\n" % location)
config = client.read_config(basedir, "client.port")
fch = {"tcp": make_tcp_handler()}
dfh = create_default_connection_handlers(
None,
config,
fch,
)
tub_options = create_tub_options(config)
t = FakeTub() t = FakeTub()
set_tub_locations(None, None, t, (port, location))
with mock.patch("allmydata.node.Tub", return_value=t):
create_main_tub(config, tub_options, dfh, fch, None, None)
self.assertEqual(t.listening_ports, self.assertEqual(t.listening_ports,
["tcp:%d:interface=127.0.0.1" % port1, ["tcp:%d:interface=127.0.0.1" % port1,
"tcp:%d:interface=127.0.0.1" % port2]) "tcp:%d:interface=127.0.0.1" % port2])
def test_tor_i2p_listeners(self): def test_tor_i2p_listeners(self):
basedir = self.mktemp() """
config_fname = os.path.join(basedir, "tahoe.cfg") When configured to listen on an "i2p" or "tor" address,
os.mkdir(basedir) ``set_tub_locations`` tells the Tub to listen on endpoints supplied by
os.mkdir(os.path.join(basedir, "private")) the given Tor and I2P providers.
with open(config_fname, "w") as f: """
f.write(BASE_CONFIG)
f.write("[node]\n")
f.write("tub.port = listen:i2p,listen:tor\n")
f.write("tub.location = tcp:example.org:1234\n")
config = client.read_config(basedir, "client.port")
tub_options = create_tub_options(config)
t = FakeTub() t = FakeTub()
fch = {"tcp": make_tcp_handler()} i2p_listener = object()
dfh = create_default_connection_handlers( i2p_provider = FakeProvider(i2p_listener)
None, tor_listener = object()
config, tor_provider = FakeProvider(tor_listener)
fch,
set_tub_locations(
i2p_provider,
tor_provider,
t,
("listen:i2p,listen:tor", "tcp:example.org:1234"),
) )
with mock.patch("allmydata.node.Tub", return_value=t):
i2p_provider = mock.Mock()
tor_provider = mock.Mock()
create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider)
self.assertEqual(i2p_provider.get_listener.mock_calls, [mock.call()])
self.assertEqual(tor_provider.get_listener.mock_calls, [mock.call()])
self.assertEqual( self.assertEqual(
t.listening_ports, t.listening_ports,
[ [i2p_listener, tor_listener],
i2p_provider.get_listener(),
tor_provider.get_listener(),
]
) )
@ -967,16 +936,6 @@ class Configuration(unittest.TestCase):
class FakeProvider(object):
"""Emulate Tor and I2P providers."""
def get_tor_handler(self):
return "TORHANDLER!"
def get_i2p_handler(self):
return "I2PHANDLER!"
class CreateConnectionHandlers(unittest.TestCase): class CreateConnectionHandlers(unittest.TestCase):
""" """
Tests for create_connection_handlers(). Tests for create_connection_handlers().