commit b8ab2cc30cad6850d5a011ecca129b35af47802a Author: Damian Johnson atagar@torproject.org Date: Mon Sep 28 00:07:20 2020 -0700
Move tor_descriptors test --- test/settings.cfg | 1 - test/unit/examples.py | 29 ++++++++++++++++++-- test/unit/tutorial.py | 74 --------------------------------------------------- 3 files changed, 27 insertions(+), 77 deletions(-)
diff --git a/test/settings.cfg b/test/settings.cfg index 91f287cf..853c4483 100644 --- a/test/settings.cfg +++ b/test/settings.cfg @@ -289,7 +289,6 @@ test.unit_tests |test.unit.manual.TestManual |test.unit.directory.authority.TestAuthority |test.unit.directory.fallback.TestFallback -|test.unit.tutorial.TestTutorial |test.unit.response.add_onion.TestAddOnionResponse |test.unit.response.control_message.TestControlMessage |test.unit.response.control_line.TestControlLine diff --git a/test/unit/examples.py b/test/unit/examples.py index c972c7e9..dc9fc242 100644 --- a/test/unit/examples.py +++ b/test/unit/examples.py @@ -163,6 +163,12 @@ EXPECTED_RUNNING_HIDDEN_SERVICE = """\ * Shutting down our hidden service """
+EXPECTED_TOR_DESCRIPTORS = """\ +1. speedyexit (102.13 KB/s) +2. speedyexit (102.13 KB/s) +3. speedyexit (102.13 KB/s) +""" + EXPECTED_VOTES_BY_BANDWIDTH_AUTHORITIES = """\ Getting gabelmoo's vote from http://131.188.40.189:80/tor/status-vote/current/authority: 5935 measured entries and 1332 unmeasured @@ -193,6 +199,11 @@ class TestExamples(unittest.TestCase): def tearDown(self): sys.path = self.original_path
+ # Ensure we don't cache a Mock object as our downloader. Otherwise future + # tests will understandably be very sad. :P + + stem.descriptor.remote.SINGLETON_DOWNLOADER = None + def test_runs_everything(self): """ Ensure we have tests for all our examples. @@ -620,8 +631,22 @@ class TestExamples(unittest.TestCase): def test_slow_listener(self): pass
- def test_tor_descriptors(self): - pass + @patch('stem.descriptor.remote.DescriptorDownloader') + @patch('sys.stdout', new_callable = io.StringIO) + def test_tor_descriptors(self, stdout_mock, downloader_mock): + exit_descriptor = RelayDescriptor.content({'router': 'speedyexit 149.255.97.109 9001 0 0'}).replace(b'reject *:*', b'accept *:*') + exit_descriptor = RelayDescriptor(exit_descriptor) + + downloader_mock().get_server_descriptors().run.return_value = [ + exit_descriptor, + RelayDescriptor.create(), # non-exit + exit_descriptor, + exit_descriptor, + ] + + import tor_descriptors + + self.assertEqual(EXPECTED_TOR_DESCRIPTORS, stdout_mock.getvalue())
def test_utilities(self): pass diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py deleted file mode 100644 index e2b621e8..00000000 --- a/test/unit/tutorial.py +++ /dev/null @@ -1,74 +0,0 @@ -""" -Tests for the examples given in stem's tutorial. -""" - -import io -import unittest - -import stem.descriptor.remote - -from unittest.mock import patch - -from stem.descriptor.server_descriptor import RelayDescriptor - -MIRROR_MIRROR_OUTPUT = """\ -1. speedyexit (102.13 KB/s) -2. speedyexit (102.13 KB/s) -3. speedyexit (102.13 KB/s) -""" - - -class TestTutorial(unittest.TestCase): - def tearDown(self): - # Ensure we don't cache a Mock object as our downloader. Otherwise future - # tests will understandably be very sad. :P - - stem.descriptor.remote.SINGLETON_DOWNLOADER = None - - @patch('sys.stdout', new_callable = io.StringIO) - @patch('stem.descriptor.remote.DescriptorDownloader') - def test_mirror_mirror_on_the_wall_5(self, downloader_mock, stdout_mock): - def tutorial_example(): - from stem.descriptor.remote import DescriptorDownloader - from stem.util import str_tools - - # provides a mapping of observed bandwidth to the relay nicknames - def get_bw_to_relay(): - bw_to_relay = {} - - downloader = DescriptorDownloader() - - try: - for desc in downloader.get_server_descriptors().run(): - if desc.exit_policy.is_exiting_allowed(): - bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname) - except Exception as exc: - print('Unable to retrieve the server descriptors: %s' % exc) - - return bw_to_relay - - # prints the top fifteen relays - - bw_to_relay = get_bw_to_relay() - count = 1 - - for bw_value in sorted(bw_to_relay.keys(), reverse = True): - for nickname in bw_to_relay[bw_value]: - print('%i. %s (%s/s)' % (count, nickname, str_tools.size_label(bw_value, 2))) - count += 1 - - if count > 15: - return - - exit_descriptor = RelayDescriptor.content({'router': 'speedyexit 149.255.97.109 9001 0 0'}).replace(b'reject *:*', b'accept *:*') - exit_descriptor = RelayDescriptor(exit_descriptor) - - downloader_mock().get_server_descriptors().run.return_value = [ - exit_descriptor, - RelayDescriptor.create(), # non-exit - exit_descriptor, - exit_descriptor, - ] - - tutorial_example() - self.assertEqual(MIRROR_MIRROR_OUTPUT, stdout_mock.getvalue())