[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Move persisting_a_consensus tests
commit 1a8cff380c76b8a90a868472f92951cd08e35919
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sun Sep 27 15:58:27 2020 -0700
Move persisting_a_consensus tests
---
test/settings.cfg | 1 -
test/unit/__init__.py | 11 -----
test/unit/examples.py | 54 +++++++++++++++++++------
test/unit/tutorial.py | 12 +++++-
test/unit/tutorial_examples.py | 92 ------------------------------------------
5 files changed, 51 insertions(+), 119 deletions(-)
diff --git a/test/settings.cfg b/test/settings.cfg
index 26561327..91f287cf 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -290,7 +290,6 @@ test.unit_tests
|test.unit.directory.authority.TestAuthority
|test.unit.directory.fallback.TestFallback
|test.unit.tutorial.TestTutorial
-|test.unit.tutorial_examples.TestTutorialExamples
|test.unit.response.add_onion.TestAddOnionResponse
|test.unit.response.control_message.TestControlMessage
|test.unit.response.control_line.TestControlLine
diff --git a/test/unit/__init__.py b/test/unit/__init__.py
index 1b16bc99..7fb58a67 100644
--- a/test/unit/__init__.py
+++ b/test/unit/__init__.py
@@ -2,9 +2,6 @@
Unit tests for the stem library.
"""
-import os
-import test
-
__all__ = [
'client',
'connection',
@@ -16,11 +13,3 @@ __all__ = [
'util',
'version',
]
-
-
-def exec_documentation_example(filename):
- path = os.path.join(test.STEM_BASE, 'docs', '_static', 'example', filename)
-
- with open(path) as f:
- code = compile(f.read(), path, 'exec')
- exec(code)
diff --git a/test/unit/examples.py b/test/unit/examples.py
index 1b055628..3f528b80 100644
--- a/test/unit/examples.py
+++ b/test/unit/examples.py
@@ -152,6 +152,9 @@ Checking for outdated relays...
2 outdated relays found, 1 had contact information
"""
+EXPECTED_PERSISTING_A_CONSENSUS = """\
+A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB: caerSidi
+"""
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
@@ -168,6 +171,12 @@ def _make_circ_event(circ_id, hop1, hop2, hop3):
return ControlMessage.from_str(content, 'EVENT', normalize = True)
+def _download_of(desc):
+ query = Mock()
+ query.run.return_value = [desc]
+ return Mock(return_value = query)
+
+
class TestExamples(unittest.TestCase):
def setUp(self):
self.original_path = list(sys.path)
@@ -268,11 +277,6 @@ class TestExamples(unittest.TestCase):
@test.require.cryptography
def test_check_digests(self):
- def download_of(desc):
- query = Mock()
- query.run.return_value = [desc]
- return Mock(return_value = query)
-
import check_digests as module
fingerprint = 'A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB'
@@ -289,18 +293,18 @@ class TestExamples(unittest.TestCase):
'r': 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0',
})
- with patch('stem.descriptor.remote.get_server_descriptors', download_of(server_desc)):
- with patch('stem.descriptor.remote.get_extrainfo_descriptors', download_of(extrainfo_desc)):
+ with patch('stem.descriptor.remote.get_server_descriptors', _download_of(server_desc)):
+ with patch('stem.descriptor.remote.get_extrainfo_descriptors', _download_of(extrainfo_desc)):
# correctly signed descriptors
- with patch('stem.descriptor.remote.get_consensus', download_of(consensus_desc)):
+ with patch('stem.descriptor.remote.get_consensus', _download_of(consensus_desc)):
with patch('sys.stdout', new_callable = io.StringIO) as stdout_mock:
module.validate_relay(fingerprint)
self.assertEqual(EXPECTED_CHECK_DIGESTS_OK, stdout_mock.getvalue())
# incorrect server descriptor digest
- with patch('stem.descriptor.remote.get_consensus', download_of(bad_consensus_desc)):
+ with patch('stem.descriptor.remote.get_consensus', _download_of(bad_consensus_desc)):
with patch('sys.stdout', new_callable = io.StringIO) as stdout_mock:
module.validate_relay(fingerprint)
self.assertEqual(EXPECTED_CHECK_DIGESTS_BAD % server_desc.digest(), stdout_mock.getvalue())
@@ -494,11 +498,35 @@ class TestExamples(unittest.TestCase):
self.assertEqual(EXPECTED_OUTDATED_RELAYS, stdout_mock.getvalue())
- def test_persisting_a_consensus(self):
- pass
+ @patch('stem.descriptor.remote.DescriptorDownloader')
+ def test_persisting_a_consensus(self, downloader_mock):
+ consensus = NetworkStatusDocumentV3.create(routers = (RouterStatusEntryV3.create({
+ 'r': 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0',
+ }),))
- def test_persisting_a_consensus_with_parse_file(self):
- pass
+ downloader_mock().get_consensus = _download_of(consensus)
+
+ try:
+ import persisting_a_consensus
+
+ with open('/tmp/descriptor_dump') as output_file:
+ self.assertEqual(str(consensus), output_file.read())
+ finally:
+ if os.path.exists('/tmp/descriptor_dump'):
+ os.remove('/tmp/descriptor_dump')
+
+ @patch('stem.descriptor.parse_file')
+ @patch('sys.stdout', new_callable = io.StringIO)
+ def test_persisting_a_consensus_with_parse_file(self, stdout_mock, parse_file_mock):
+ consensus = NetworkStatusDocumentV3.create(routers = (RouterStatusEntryV3.create({
+ 'r': 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0',
+ }),))
+
+ parse_file_mock.return_value = iter([consensus])
+
+ import persisting_a_consensus_with_parse_file
+
+ self.assertEqual(EXPECTED_PERSISTING_A_CONSENSUS, stdout_mock.getvalue())
def test_queue_listener(self):
pass
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 35b2fe28..140bbba5 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -3,9 +3,11 @@ Tests for the examples given in stem's tutorial.
"""
import io
+import os
import unittest
import stem.descriptor.remote
+import test
from unittest.mock import Mock, patch
@@ -14,8 +16,6 @@ from stem.descriptor.router_status_entry import RouterStatusEntryV2, RouterStatu
from stem.descriptor.networkstatus import NetworkStatusDocumentV3
from stem.descriptor.server_descriptor import RelayDescriptor
from stem.exit_policy import ExitPolicy
-from test.unit import exec_documentation_example
-
OVER_THE_RIVER_OUTPUT = """\
* Connecting to tor
@@ -31,6 +31,14 @@ MIRROR_MIRROR_OUTPUT = """\
"""
+def exec_documentation_example(filename):
+ path = os.path.join(test.STEM_BASE, 'docs', '_static', 'example', filename)
+
+ with open(path) as f:
+ code = compile(f.read(), path, 'exec')
+ exec(code)
+
+
class TestTutorial(unittest.TestCase):
def tearDown(self):
# Ensure we don't cache a Mock object as our downloader. Otherwise future
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
deleted file mode 100644
index 451bb2aa..00000000
--- a/test/unit/tutorial_examples.py
+++ /dev/null
@@ -1,92 +0,0 @@
-"""
-Tests for the examples given in stem's tutorial.
-"""
-
-import io
-import itertools
-import os
-import unittest
-
-from unittest.mock import patch
-
-from stem.descriptor.networkstatus import NetworkStatusDocumentV3
-from stem.descriptor.router_status_entry import RouterStatusEntryV3
-from stem.response import ControlMessage
-
-from test.unit import exec_documentation_example
-
-OPEN_FUNCTION = open # make a reference so mocking open() won't mess with us
-
-CIRC_CONTENT = '650 CIRC %d %s \
-%s \
-PURPOSE=%s'
-
-PATH_CONTENT = '$%s=%s,$%s=%s,$%s=%s'
-
-PERSISTING_A_CONSENSUS_OUTPUT = """\
-A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB: caerSidi
-"""
-
-
-def _get_event(content):
- return ControlMessage.from_str(content, 'EVENT', normalize = True)
-
-
-def _get_circ_event(id, status, hop1, hop2, hop3, purpose):
- path = PATH_CONTENT % (hop1[0], hop1[1], hop2[0], hop2[1], hop3[0], hop3[1])
- content = CIRC_CONTENT % (id, status, path, purpose)
- return _get_event(content)
-
-
-def _get_router_status(address = None, port = None, nickname = None, fingerprint_base64 = None, s_line = None):
- r_line = 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0'
-
- 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:
- return RouterStatusEntryV3.create({'r': r_line, 's': s_line})
- else:
- return RouterStatusEntryV3.create({'r': r_line})
-
-
-class TestTutorialExamples(unittest.TestCase):
- @patch('sys.stdout', new_callable = io.StringIO)
- @patch('stem.descriptor.parse_file')
- @patch('stem.descriptor.remote.Query')
- def test_persisting_a_consensus(self, query_mock, parse_file_mock, stdout_mock):
- def tutorial_example_2():
- from stem.descriptor import DocumentHandler, parse_file
-
- consensus = next(parse_file(
- '/tmp/descriptor_dump',
- descriptor_type = 'network-status-consensus-3 1.0',
- document_handler = DocumentHandler.DOCUMENT,
- ))
-
- for fingerprint, relay in consensus.routers.items():
- print('%s: %s' % (fingerprint, relay.nickname))
-
- network_status = NetworkStatusDocumentV3.create(routers = (RouterStatusEntryV3.create({
- 'r': 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0',
- }),))
-
- query_mock().run.return_value = [network_status]
- parse_file_mock.return_value = itertools.cycle([network_status])
-
- exec_documentation_example('persisting_a_consensus.py')
- exec_documentation_example('persisting_a_consensus_with_parse_file.py')
-
- self.assertEqual(PERSISTING_A_CONSENSUS_OUTPUT, stdout_mock.getvalue())
-
- if os.path.exists('/tmp/descriptor_dump'):
- os.remove('/tmp/descriptor_dump')
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits