[tor-commits] [stem/master] Revising tutorial example tests
atagar at torproject.org
atagar at torproject.org
Sun Dec 21 23:15:53 UTC 2014
commit 8c44ef6f557055adc5aedb8a914466985803817d
Author: Damian Johnson <atagar at torproject.org>
Date: Sun Dec 21 15:12:30 2014 -0800
Revising tutorial example tests
Mostly just minor formatting revisions. The most substantial changes were for
test_votes_by_bandwidth_authorities to cut runtime from 5 to 0.05 seconds.
Also, think the Query objects were accidently still hitting the directory
authorities - they're now completely mocked.
---
test/unit/tutorial_examples.py | 216 +++++++++++++++++++++-------------------
1 file changed, 115 insertions(+), 101 deletions(-)
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index aeeb761..75a480d 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -21,9 +21,9 @@ from test.mocking import (
try:
# added in python 3.3
- from unittest.mock import patch
+ from unittest.mock import Mock, patch
except ImportError:
- from mock import patch
+ from mock import Mock, patch
CIRC_CONTENT = '650 CIRC %d %s \
%s \
@@ -106,19 +106,23 @@ def _get_circ_event(id, status, hop1, hop2, hop3, purpose):
def _get_router_status(address = None, port = None, nickname = None, fingerprint_base64 = None, s_line = None):
r_line = ROUTER_STATUS_ENTRY_V3_HEADER[0][1]
+
if address:
r_line = r_line.replace('71.35.150.29', address)
+
if port:
r_line = r_line.replace('9001', port)
+
if nickname:
r_line = r_line.replace('caerSidi', nickname)
+
if fingerprint_base64:
r_line = r_line.replace('p1aag7VwarGxqctS7/fS0y5FU+s', fingerprint_base64)
+
if s_line:
- content = get_router_status_entry_v3({'r': r_line, 's': s_line})
+ return get_router_status_entry_v3({'r': r_line, 's': s_line})
else:
- content = get_router_status_entry_v3({'r': r_line})
- return content
+ return get_router_status_entry_v3({'r': r_line})
class TestTutorialExamples(unittest.TestCase):
@@ -162,6 +166,7 @@ class TestTutorialExamples(unittest.TestCase):
controller = from_port_mock().__enter__()
controller.get_circuits.return_value = [circuit_4, circuit_6, circuit_10]
+
controller.get_network_status.side_effect = lambda fingerprint, *args: {
path_1[0]: _get_router_status("173.209.180.61"),
path_2[0]: _get_router_status("87.238.194.176"),
@@ -171,42 +176,46 @@ class TestTutorialExamples(unittest.TestCase):
path_6[0]: _get_router_status("86.59.119.83"),
path_7[0]: _get_router_status("176.67.169.171")
}[fingerprint]
+
tutorial_example()
self.assertEqual(LIST_CIRCUITS_OUTPUT, stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO.StringIO)
@patch('stem.control.Controller.from_port', spec = Controller)
def test_exit_used(self, from_port_mock, stdout_mock):
- import functools
+ def tutorial_example(mock_event):
+ import functools
- from stem import StreamStatus
- from stem.control import EventType, Controller
+ from stem import StreamStatus
+ from stem.control import EventType, Controller
- def main():
- print "Tracking requests for tor exits. Press 'enter' to end."
- print
+ def main():
+ print "Tracking requests for tor exits. Press 'enter' to end."
+ print
- with Controller.from_port() as controller:
- controller.authenticate()
+ with Controller.from_port() as controller:
+ controller.authenticate()
- stream_listener = functools.partial(stream_event, controller)
- controller.add_event_listener(stream_listener, EventType.STREAM)
+ stream_listener = functools.partial(stream_event, controller)
+ controller.add_event_listener(stream_listener, EventType.STREAM)
- raw_input()
+ stream_event(controller, mock_event) # simulate an event during the raw_input()
- def stream_event(controller, event):
- if event.status == StreamStatus.SUCCEEDED and event.circ_id:
- circ = controller.get_circuit(event.circ_id)
+ def stream_event(controller, event):
+ if event.status == StreamStatus.SUCCEEDED and event.circ_id:
+ circ = controller.get_circuit(event.circ_id)
- exit_fingerprint = circ.path[-1][0]
- exit_relay = controller.get_network_status(exit_fingerprint)
+ exit_fingerprint = circ.path[-1][0]
+ exit_relay = controller.get_network_status(exit_fingerprint)
- print "Exit relay for our connection to %s" % (event.target)
- print " address: %s:%i" % (exit_relay.address, exit_relay.or_port)
- print " fingerprint: %s" % exit_relay.fingerprint
- print " nickname: %s" % exit_relay.nickname
- print " locale: %s" % controller.get_info("ip-to-country/%s" % exit_relay.address, 'unknown')
- print
+ print "Exit relay for our connection to %s" % (event.target)
+ print " address: %s:%i" % (exit_relay.address, exit_relay.or_port)
+ print " fingerprint: %s" % exit_relay.fingerprint
+ print " nickname: %s" % exit_relay.nickname
+ print " locale: %s" % controller.get_info("ip-to-country/%s" % exit_relay.address, 'unknown')
+ print
+
+ main()
path_1 = ('9EA317EECA56BDF30CAEB208A253FB456EDAB1A0', 'bolobolo1')
path_2 = ('00C2C2A16AEDB51D5E5FB7D6168FC66B343D822F', 'ph3x')
@@ -216,15 +225,13 @@ class TestTutorialExamples(unittest.TestCase):
event_content = '650 STREAM 15 SUCCEEDED 3 64.15.112.44:80'
event = _get_event(event_content)
- controller = from_port_mock().__enter()
+ controller = from_port_mock().__enter__()
controller.get_circuit.return_value = circuit
- controller.get_network_status.return_value = _get_router_status("31.172.30.2", "443", path_3[1], "pZ4efH6u4IPXVu4f9uwxyj2GUdc=")
+ controller.get_network_status.return_value = _get_router_status('31.172.30.2', '443', path_3[1], 'pZ4efH6u4IPXVu4f9uwxyj2GUdc=')
controller.get_info.return_value = 'unknown'
- origin_raw_input = __builtins__['raw_input']
- __builtins__['raw_input'] = lambda: ""
- main()
- __builtins__['raw_input'] = origin_raw_input
- stream_event(controller, event)
+
+ tutorial_example(event)
+
self.assertEqual(EXIT_USED_OUTPUT, stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO.StringIO)
@@ -251,12 +258,15 @@ class TestTutorialExamples(unittest.TestCase):
print
print "%i outdated relays found, %i had contact information" % (count, with_contact)
- desc_1 = get_relay_server_descriptor({'platform': 'node-Tor 0.2.3.0 on Linux x86_64'})
- desc_2 = get_relay_server_descriptor({'platform': 'node-Tor 0.1.0 on Linux x86_64'})
- desc_3 = get_relay_server_descriptor({'opt': 'contact Random Person admin at gtr-10.de', 'platform': 'node-Tor 0.2.3.0 on Linux x86_64'})
- desc_4 = get_relay_server_descriptor({'opt': 'contact Sambuddha Basu', 'platform': 'node-Tor 0.1.0 on Linux x86_64'})
- downloader_mock().get_server_descriptors.return_value = [desc_1, desc_2, desc_3, desc_4]
+ downloader_mock().get_server_descriptors.return_value = [
+ get_relay_server_descriptor({'platform': 'node-Tor 0.2.3.0 on Linux x86_64'}),
+ get_relay_server_descriptor({'platform': 'node-Tor 0.1.0 on Linux x86_64'}),
+ get_relay_server_descriptor({'opt': 'contact Random Person admin at gtr-10.de', 'platform': 'node-Tor 0.2.3.0 on Linux x86_64'}),
+ get_relay_server_descriptor({'opt': 'contact Sambuddha Basu', 'platform': 'node-Tor 0.1.0 on Linux x86_64'}),
+ ]
+
tutorial_example()
+
self.assertEqual(OUTDATED_RELAYS_OUTPUT, stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO.StringIO)
@@ -307,36 +317,45 @@ class TestTutorialExamples(unittest.TestCase):
print "maatuska has the Running flag but moria1 doesn't: %s" % fingerprint
get_authorities_mock().items.return_value = [('moria1', DIRECTORY_AUTHORITIES['moria1']), ('maatuska', DIRECTORY_AUTHORITIES['maatuska'])]
- fingerprint = []
- fingerprint.append(('92FCB6748A40E6088E22FBAB943AB2DD743EA818', 'kvy2dIpA5giOIvurlDqy3XQ+qBg='))
- fingerprint.append(('6871F682350BA931838C0EC1E4A23044DAE06A73', 'aHH2gjULqTGDjA7B5KIwRNrganM='))
- fingerprint.append(('E2BB13AA2F6960CD93ABE5257A825687F3973C62', '4rsTqi9pYM2Tq+UleoJWh/OXPGI='))
- fingerprint.append(('546C54E2A89D88E0794D04AECBF1AC8AC9DA81DE', 'VGxU4qidiOB5TQSuy/Gsisnagd4='))
- fingerprint.append(('DCAEC3D069DC39AAE43D13C8AF31B5645E05ED61', '3K7D0GncOarkPRPIrzG1ZF4F7WE='))
- entry = []
- # Entries for moria1.
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[0][1], s_line = ' '))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[1][1], s_line = ' '))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[2][1], s_line = ' '))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[3][1]))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[4][1]))
- # Entries for maatuska.
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[0][1]))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[1][1]))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[2][1]))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[3][1], s_line = ' '))
- entry.append(_get_router_status(fingerprint_base64 = fingerprint[4][1], s_line = ' '))
- network_status = []
- network_status.append(get_network_status_document_v3(routers = (entry[0], entry[1], entry[2], entry[3], entry[4],)))
- network_status.append(get_network_status_document_v3(routers = (entry[5], entry[6], entry[7], entry[8], entry[9],)))
- query_mock().run.side_effect = [[network_status[0]], [network_status[1]]]
+
+ fingerprint = [
+ ('92FCB6748A40E6088E22FBAB943AB2DD743EA818', 'kvy2dIpA5giOIvurlDqy3XQ+qBg='),
+ ('6871F682350BA931838C0EC1E4A23044DAE06A73', 'aHH2gjULqTGDjA7B5KIwRNrganM='),
+ ('E2BB13AA2F6960CD93ABE5257A825687F3973C62', '4rsTqi9pYM2Tq+UleoJWh/OXPGI='),
+ ('546C54E2A89D88E0794D04AECBF1AC8AC9DA81DE', 'VGxU4qidiOB5TQSuy/Gsisnagd4='),
+ ('DCAEC3D069DC39AAE43D13C8AF31B5645E05ED61', '3K7D0GncOarkPRPIrzG1ZF4F7WE='),
+ ]
+
+ entry = [
+ # entries for moria1
+
+ _get_router_status(fingerprint_base64 = fingerprint[0][1], s_line = ' '),
+ _get_router_status(fingerprint_base64 = fingerprint[1][1], s_line = ' '),
+ _get_router_status(fingerprint_base64 = fingerprint[2][1], s_line = ' '),
+ _get_router_status(fingerprint_base64 = fingerprint[3][1]),
+ _get_router_status(fingerprint_base64 = fingerprint[4][1]),
+
+ # entries for maatuska
+
+ _get_router_status(fingerprint_base64 = fingerprint[0][1]),
+ _get_router_status(fingerprint_base64 = fingerprint[1][1]),
+ _get_router_status(fingerprint_base64 = fingerprint[2][1]),
+ _get_router_status(fingerprint_base64 = fingerprint[3][1], s_line = ' '),
+ _get_router_status(fingerprint_base64 = fingerprint[4][1], s_line = ' '),
+ ]
+
+ query_mock().run.side_effect = [
+ [get_network_status_document_v3(routers = (entry[0], entry[1], entry[2], entry[3], entry[4]))],
+ [get_network_status_document_v3(routers = (entry[5], entry[6], entry[7], entry[8], entry[9]))],
+ ]
+
tutorial_example()
self.assertEqual(COMPARE_FLAGS_OUTPUT, stdout_mock.getvalue())
@patch('sys.stdout', new_callable = StringIO.StringIO)
@patch('stem.descriptor.remote.get_authorities')
- @patch('stem.descriptor.remote.Query.run')
- def test_votes_by_bandwidth_authorities(self, query_run_mock, get_authorities_mock, stdout_mock):
+ @patch('stem.descriptor.remote.DescriptorDownloader.query')
+ def test_votes_by_bandwidth_authorities(self, query_mock, get_authorities_mock, stdout_mock):
def tutorial_example():
from stem.descriptor import remote
@@ -368,43 +387,37 @@ class TestTutorialExamples(unittest.TestCase):
except Exception as exc:
print " failed to get the vote (%s)" % exc
- directory_values = []
- directory_values.append(DIRECTORY_AUTHORITIES['gabelmoo'])
+ directory_values = [
+ DIRECTORY_AUTHORITIES['gabelmoo'],
+ DIRECTORY_AUTHORITIES['tor26'],
+ DIRECTORY_AUTHORITIES['moria1'],
+ DIRECTORY_AUTHORITIES['maatuska'],
+ ]
+
directory_values[0].address = '212.112.245.170'
- directory_values.append(DIRECTORY_AUTHORITIES['tor26'])
- directory_values.append(DIRECTORY_AUTHORITIES['moria1'])
- directory_values.append(DIRECTORY_AUTHORITIES['maatuska'])
get_authorities_mock().values.return_value = directory_values
- router_status = []
- # Count for gabelmoo.
- entry = []
- for count in range(5935):
- entry.append(get_router_status_entry_v3({'w': 'Bandwidth=1 Measured=1'}))
- for count in range(1332):
- entry.append(get_router_status_entry_v3())
- router_status.append(entry)
- # Count for tor26.
- entry = []
- for count in range(5735):
- entry.append(get_router_status_entry_v3({'w': 'Bandwidth=1 Measured=1'}))
- for count in range(1690):
- entry.append(get_router_status_entry_v3())
- router_status.append(entry)
- # Count for moria1.
- entry = []
- for count in range(6647):
- entry.append(get_router_status_entry_v3({'w': 'Bandwidth=1 Measured=1'}))
- for count in range(625):
- entry.append(get_router_status_entry_v3())
- router_status.append(entry)
- # Count for maatuska.
- entry = []
- for count in range(6313):
- entry.append(get_router_status_entry_v3({'w': 'Bandwidth=1 Measured=1'}))
- for count in range(1112):
- entry.append(get_router_status_entry_v3())
- router_status.append(entry)
- query_run_mock.side_effect = router_status
+
+ entry_with_measurement = get_router_status_entry_v3({'w': 'Bandwidth=1 Measured=1'})
+ entry_without_measurement = get_router_status_entry_v3()
+
+ query1 = Mock()
+ query1.download_url = 'http://212.112.245.170:80/tor/status-vote/current/authority'
+ query1.run.return_value = [entry_with_measurement] * 5935 + [entry_without_measurement] * 1332
+
+ query2 = Mock()
+ query2.download_url = 'http://86.59.21.38:80/tor/status-vote/current/authority'
+ query2.run.return_value = [entry_with_measurement] * 5735 + [entry_without_measurement] * 1690
+
+ query3 = Mock()
+ query3.download_url = 'http://128.31.0.39:9131/tor/status-vote/current/authority'
+ query3.run.return_value = [entry_with_measurement] * 6647 + [entry_without_measurement] * 625
+
+ query4 = Mock()
+ query4.download_url = 'http://171.25.193.9:443/tor/status-vote/current/authority'
+ query4.run.return_value = [entry_with_measurement] * 6313 + [entry_without_measurement] * 1112
+
+ query_mock.side_effect = [query1, query2, query3, query4]
+
tutorial_example()
self.assertEqual(VOTES_BY_BANDWIDTH_AUTHORITIES_OUTPUT, stdout_mock.getvalue())
@@ -435,10 +448,11 @@ class TestTutorialExamples(unittest.TestCase):
for fingerprint, relay in consensus.routers.items():
print "%s: %s" % (fingerprint, relay.nickname)
- entry = get_router_status_entry_v3()
- network_status = get_network_status_document_v3(routers = (entry,))
+ network_status = get_network_status_document_v3(routers = (get_router_status_entry_v3(),))
query_mock().run.return_value = [network_status]
parse_file_mock.return_value = itertools.cycle([network_status])
+
tutorial_example_1()
tutorial_example_2()
+
self.assertEqual(PERSISTING_A_CONSENSUS_OUTPUT, stdout_mock.getvalue())
More information about the tor-commits
mailing list