[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Read python scripts from docs/_static/example directory for tutorial tests



commit b94f9aa28fa453ad2be69fcfc9d9882e8849aa2c
Author: Sambuddha Basu <sambuddhabasu1@xxxxxxxxx>
Date:   Tue May 26 04:45:31 2015 +0400

    Read python scripts from docs/_static/example directory for tutorial tests
    
    Outdated Relays and Compare Flags now reads from reads python script from docs/_static/example/
    
    All the tutorial_examples read python script from docs/_static/example/
    
    test/unit/tutorial.py now read python scripts from docs/_static/example
---
 test/unit/tutorial.py          |   46 ++-----------
 test/unit/tutorial_examples.py |  143 ++--------------------------------------
 2 files changed, 10 insertions(+), 179 deletions(-)

diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 4ae82c7..39e4f03 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -40,24 +40,13 @@ class TestTutorial(unittest.TestCase):
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_the_little_relay_that_could(self, from_port_mock, stdout_mock):
-    def tutorial_example():
-      from stem.control import Controller
-
-      with Controller.from_port(control_port = 9051) as controller:
-        controller.authenticate()  # provide the password here if you set one
-
-        bytes_read = controller.get_info('traffic/read')
-        bytes_written = controller.get_info('traffic/written')
-
-        print('My Tor relay has read %s bytes and written %s.' % (bytes_read, bytes_written))
-
     controller = from_port_mock().__enter__()
     controller.get_info.side_effect = lambda arg: {
       'traffic/read': '33406',
       'traffic/written': '29649',
     }[arg]
 
-    tutorial_example()
+    execfile('docs/_static/example/hello_world.py')
     self.assertEqual('My Tor relay has read 33406 bytes and written 29649.\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -129,38 +118,18 @@ class TestTutorial(unittest.TestCase):
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.descriptor.remote.DescriptorDownloader')
   def test_mirror_mirror_on_the_wall_1(self, downloader_mock, stdout_mock):
-    def tutorial_example():
-      from stem.descriptor.remote import DescriptorDownloader
-
-      downloader = DescriptorDownloader()
-
-      try:
-        for desc in downloader.get_consensus().run():
-          print('found relay %s (%s)' % (desc.nickname, desc.fingerprint))
-      except Exception as exc:
-        print('Unable to retrieve the consensus: %s' % exc)
-
     downloader_mock().get_consensus().run.return_value = [mocking.get_router_status_entry_v2()]
 
-    tutorial_example()
+    execfile('docs/_static/example/current_descriptors.py')
     self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_mirror_mirror_on_the_wall_2(self, from_port_mock, stdout_mock):
-    def tutorial_example():
-      from stem.control import Controller
-
-      with Controller.from_port(control_port = 9051) as controller:
-        controller.authenticate()
-
-        for desc in controller.get_network_statuses():
-          print('found relay %s (%s)' % (desc.nickname, desc.fingerprint))
-
     controller = from_port_mock().__enter__()
     controller.get_network_statuses.return_value = [mocking.get_router_status_entry_v2()]
 
-    tutorial_example()
+    execfile('docs/_static/example/descriptor_from_tor_control_socket.py')
     self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -186,17 +155,10 @@ class TestTutorial(unittest.TestCase):
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.descriptor.reader.DescriptorReader', spec = DescriptorReader)
   def test_mirror_mirror_on_the_wall_4(self, reader_mock, stdout_mock):
-    def tutorial_example():
-      from stem.descriptor.reader import DescriptorReader
-
-      with DescriptorReader(['/home/atagar/server-descriptors-2013-03.tar']) as reader:
-        for desc in reader:
-          print('found relay %s (%s)' % (desc.nickname, desc.fingerprint))
-
     reader = reader_mock().__enter__()
     reader.__iter__.return_value = iter([mocking.get_relay_server_descriptor()])
 
-    tutorial_example()
+    execfile('docs/_static/example/past_descriptors.py')
     self.assertEqual('found relay caerSidi (None)\n', stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index 3b470f5..ae73239 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -6,12 +6,6 @@ import itertools
 import unittest
 
 try:
-  # added in python 2.7
-  from collections import OrderedDict
-except ImportError:
-  from stem.util.ordereddict import OrderedDict
-
-try:
   from StringIO import StringIO
 except ImportError:
   from io import StringIO
@@ -148,28 +142,6 @@ class TestTutorialExamples(unittest.TestCase):
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_list_circuits(self, from_port_mock, stdout_mock):
-    def tutorial_example():
-      from stem import CircStatus
-      from stem.control import Controller
-
-      with Controller.from_port(port = 9051) as controller:
-        controller.authenticate()
-
-        for circ in sorted(controller.get_circuits()):
-          if circ.status != CircStatus.BUILT:
-            continue
-
-          print('\nCircuit %s (%s)' % (circ.id, circ.purpose))
-
-          for i, entry in enumerate(circ.path):
-            div = '+' if (i == len(circ.path) - 1) else '|'
-            fingerprint, nickname = entry
-
-            desc = controller.get_network_status(fingerprint, None)
-            address = desc.address if desc else 'unknown'
-
-            print(' %s- %s (%s, %s)' % (div, fingerprint, nickname, address))
-
     path_1 = ('B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C', 'ByTORAndTheSnowDog')
     path_2 = ('0DD9935C5E939CFA1E07B8DDA6D91C1A2A9D9338', 'afo02')
     path_3 = ('DB3B1CFBD3E4D97B84B548ADD5B9A31451EEC4CC', 'edwardsnowden3')
@@ -195,7 +167,7 @@ class TestTutorialExamples(unittest.TestCase):
       path_7[0]: _get_router_status('176.67.169.171')
     }[fingerprint]
 
-    tutorial_example()
+    execfile('docs/_static/example/list_circuits.py')
     self.assert_equal_unordered(LIST_CIRCUITS_OUTPUT, stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -252,25 +224,6 @@ class TestTutorialExamples(unittest.TestCase):
   @patch('sys.stdout', new_callable = StringIO)
   @patch('stem.descriptor.remote.DescriptorDownloader')
   def test_outdated_relays(self, downloader_mock, stdout_mock):
-    def tutorial_example():
-      from stem.descriptor.remote import DescriptorDownloader
-      from stem.version import Version
-
-      downloader = DescriptorDownloader()
-      count, with_contact = 0, 0
-
-      print('Checking for outdated relays...\n')
-
-      for desc in downloader.get_server_descriptors():
-        if desc.tor_version < Version('0.2.3.0'):
-          count += 1
-
-          if desc.contact:
-            print('  %-15s %s' % (desc.tor_version, desc.contact.decode('utf-8', 'replace')))
-            with_contact += 1
-
-      print('\n%i outdated relays found, %i had contact information' % (count, with_contact))
-
     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'}),
@@ -278,7 +231,7 @@ class TestTutorialExamples(unittest.TestCase):
       get_relay_server_descriptor({'opt': 'contact Sambuddha Basu', 'platform': 'node-Tor 0.1.0 on Linux x86_64'}),
     ]
 
-    tutorial_example()
+    execfile('docs/_static/example/outdated_relays.py')
 
     self.assert_equal_unordered(OUTDATED_RELAYS_OUTPUT, stdout_mock.getvalue())
 
@@ -286,49 +239,6 @@ class TestTutorialExamples(unittest.TestCase):
   @patch('stem.descriptor.remote.Query')
   @patch('stem.descriptor.remote.get_authorities')
   def test_compare_flags(self, get_authorities_mock, query_mock, stdout_mock):
-    def tutorial_example():
-      from stem.descriptor import DocumentHandler, remote
-
-      # Query all authority votes asynchronously.
-
-      downloader = remote.DescriptorDownloader(document_handler = DocumentHandler.DOCUMENT)
-      queries = OrderedDict()
-
-      for name, authority in remote.get_authorities().items():
-        if authority.v3ident is None:
-          continue  # authority doens't vote if it lacks a v3ident
-
-        queries[name] = downloader.get_vote(authority)
-
-      # Wait for the votes to finish being downloaded, this produces a dictionary of
-      # authority nicknames to their vote.
-
-      votes = dict((name, query.run()[0]) for (name, query) in queries.items())
-
-      # Get a superset of all the fingerprints in all the votes.
-
-      all_fingerprints = set()
-
-      for vote in votes.values():
-        all_fingerprints.update(vote.routers.keys())
-
-      # Finally, compare moria1's votes to maatuska.
-
-      for fingerprint in all_fingerprints:
-        moria1_vote = votes['moria1'].routers.get(fingerprint)
-        maatuska_vote = votes['maatuska'].routers.get(fingerprint)
-
-        if not moria1_vote and not maatuska_vote:
-          print("both moria1 and maatuska haven't voted about %s" % fingerprint)
-        elif not moria1_vote:
-          print("moria1 hasn't voted about %s" % fingerprint)
-        elif not maatuska_vote:
-          print("maatuska hasn't voted about %s" % fingerprint)
-        elif 'Running' in moria1_vote.flags and 'Running' not in maatuska_vote.flags:
-          print("moria1 has the Running flag but maatuska doesn't: %s" % fingerprint)
-        elif 'Running' in maatuska_vote.flags and 'Running' not in moria1_vote.flags:
-          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 = [
@@ -362,7 +272,7 @@ class TestTutorialExamples(unittest.TestCase):
       [get_network_status_document_v3(routers = (entry[5], entry[6], entry[7], entry[8], entry[9]))],
     ]
 
-    tutorial_example()
+    execfile('docs/_static/example/compare_flags.py')
 
     self.assert_equal_unordered(COMPARE_FLAGS_OUTPUT, stdout_mock.getvalue())
 
@@ -370,37 +280,6 @@ class TestTutorialExamples(unittest.TestCase):
   @patch('stem.descriptor.remote.get_authorities')
   @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
-
-      # request votes from all the bandwidth authorities
-
-      queries = {}
-      downloader = remote.DescriptorDownloader()
-
-      for authority in remote.get_authorities().values():
-        if authority.is_bandwidth_authority:
-          queries[authority.nickname] = downloader.query(
-            '/tor/status-vote/current/authority',
-            endpoints = [(authority.address, authority.dir_port)],
-          )
-
-      for authority_name, query in queries.items():
-        try:
-          print("Getting %s's vote from %s:" % (authority_name, query.download_url))
-
-          measured, unmeasured = 0, 0
-
-          for desc in query.run():
-            if desc.measured:
-              measured += 1
-            else:
-              unmeasured += 1
-
-          print('  %i measured entries and %i unmeasured' % (measured, unmeasured))
-        except Exception as exc:
-          print('  failed to get the vote (%s)' % exc)
-
     directory_values = [
       DIRECTORY_AUTHORITIES['gabelmoo'],
       DIRECTORY_AUTHORITIES['tor26'],
@@ -432,7 +311,7 @@ class TestTutorialExamples(unittest.TestCase):
 
     query_mock.side_effect = [query1, query2, query3, query4]
 
-    tutorial_example()
+    execfile('docs/_static/example/votes_by_bandwidth_authorities.py')
     self.assert_equal_unordered(VOTES_BY_BANDWIDTH_AUTHORITIES_OUTPUT, stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO)
@@ -440,16 +319,6 @@ class TestTutorialExamples(unittest.TestCase):
   @patch('%s.open' % __name__, create = True)
   @patch('stem.descriptor.remote.Query')
   def test_persisting_a_consensus(self, query_mock, open_mock, parse_file_mock, stdout_mock):
-    def tutorial_example_1():
-      from stem.descriptor import DocumentHandler
-      from stem.descriptor.remote import DescriptorDownloader
-
-      downloader = DescriptorDownloader()
-      consensus = downloader.get_consensus(document_handler = DocumentHandler.DOCUMENT).run()[0]
-
-      with open('/tmp/descriptor_dump', 'w') as descriptor_file:
-        descriptor_file.write(str(consensus))
-
     def tutorial_example_2():
       from stem.descriptor import DocumentHandler, parse_file
 
@@ -466,7 +335,7 @@ class TestTutorialExamples(unittest.TestCase):
     query_mock().run.return_value = [network_status]
     parse_file_mock.return_value = itertools.cycle([network_status])
 
-    tutorial_example_1()
-    tutorial_example_2()
+    execfile('docs/_static/example/persisting_a_consensus.py')
+    execfile('docs/_static/example/persisting_a_consensus_with_parse_file.py')
 
     self.assertEqual(PERSISTING_A_CONSENSUS_OUTPUT, stdout_mock.getvalue())



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits