[tor-commits] [stem/master] Using remote module for last descriptor tutorial

atagar at torproject.org atagar at torproject.org
Tue Aug 27 04:31:28 UTC 2013


commit 847d2377846bf1a64881934e74c10193ec3fb130
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Aug 26 21:27:50 2013 -0700

    Using remote module for last descriptor tutorial
    
    Using the remote descriptor fetching module for the last descriptor tutorial
    rather than the controller. Also updating our unit tests for the tutorials.
---
 docs/tutorials/mirror_mirror_on_the_wall.rst |   18 +++++------
 test/unit/tutorial.py                        |   42 +++++++++++++++++++-------
 2 files changed, 40 insertions(+), 20 deletions(-)

diff --git a/docs/tutorials/mirror_mirror_on_the_wall.rst b/docs/tutorials/mirror_mirror_on_the_wall.rst
index 5b86ba0..2a5a28c 100644
--- a/docs/tutorials/mirror_mirror_on_the_wall.rst
+++ b/docs/tutorials/mirror_mirror_on_the_wall.rst
@@ -153,28 +153,28 @@ As discussed above there are three methods for reading descriptors...
 * Reading with the `DescriptorReader <../api/descriptor/reader.html>`_. This is best if you have you want to read everything from a directory or archive.
 
 Now lets say you want to figure out who the *biggest* exit relays are. You
-could use any of the methods above, but for this example we'll use the
-:class:`~stem.control.Controller`. This uses server descriptors, so keep in
-mind that you'll likely need to set "UseMicrodescriptors 0" in your torrc for
-this to work.
+could use any of the methods above, but for this example we'll use
+`stem.descriptor.remote <../api/descriptor/remote.html>`_...
 
 ::
 
-  import sys
+  import sys 
 
-  from stem.control import Controller
+  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 = {}
 
-    with Controller.from_port(port = 9051) as controller:
-      controller.authenticate()
+    downloader = DescriptorDownloader()
 
-      for desc in controller.get_server_descriptors():
+    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
 
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 0f66a83..7a582be 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -45,8 +45,27 @@ class TestTutorial(unittest.TestCase):
     self.assertEqual("My Tor relay has read 33406 bytes and written 29649.\n", stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO.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()
+    self.assertEqual("found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n", stdout_mock.getvalue())
+
+  @patch('sys.stdout', new_callable = StringIO.StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
-  def test_mirror_mirror_on_the_wall_1(self, from_port_mock, stdout_mock):
+  def test_mirror_mirror_on_the_wall_2(self, from_port_mock, stdout_mock):
     def tutorial_example():
       from stem.control import Controller
 
@@ -64,7 +83,7 @@ class TestTutorial(unittest.TestCase):
 
   @patch('sys.stdout', new_callable = StringIO.StringIO)
   @patch('%s.open' % __name__, create = True)
-  def test_mirror_mirror_on_the_wall_2(self, open_mock, stdout_mock):
+  def test_mirror_mirror_on_the_wall_3(self, open_mock, stdout_mock):
     def tutorial_example():
       from stem.descriptor import parse_file
 
@@ -85,7 +104,7 @@ class TestTutorial(unittest.TestCase):
   @patch('sys.stdout', new_callable = StringIO.StringIO)
   @patch('stem.descriptor.reader.DescriptorReader', spec = DescriptorReader)
   @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock())
-  def test_mirror_mirror_on_the_wall_3(self, reader_mock, stdout_mock):
+  def test_mirror_mirror_on_the_wall_4(self, reader_mock, stdout_mock):
     def tutorial_example():
       from stem.descriptor.reader import DescriptorReader
 
@@ -100,23 +119,25 @@ class TestTutorial(unittest.TestCase):
     self.assertEqual("found relay caerSidi (None)\n", stdout_mock.getvalue())
 
   @patch('sys.stdout', new_callable = StringIO.StringIO)
-  @patch('stem.control.Controller.from_port', spec = Controller)
+  @patch('stem.descriptor.remote.DescriptorDownloader')
   @patch('stem.descriptor.server_descriptor.RelayDescriptor._verify_digest', Mock())
-  def test_mirror_mirror_on_the_wall_4(self, from_port_mock, stdout_mock):
+  def test_mirror_mirror_on_the_wall_5(self, downloader_mock, stdout_mock):
     def tutorial_example():
-      from stem.control import Controller
+      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 = {}
 
-        with Controller.from_port(control_port = 9051) as controller:
-          controller.authenticate()
+        downloader = DescriptorDownloader()
 
-          for desc in controller.get_server_descriptors():
+        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
 
@@ -140,8 +161,7 @@ class TestTutorial(unittest.TestCase):
     exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
     exit_descriptor = RelayDescriptor(exit_descriptor)
 
-    controller = from_port_mock().__enter__()
-    controller.get_server_descriptors.return_value = [
+    downloader_mock().get_server_descriptors().run.return_value = [
       exit_descriptor,
       mocking.get_relay_server_descriptor(),  # non-exit
       exit_descriptor,



More information about the tor-commits mailing list