[tor-commits] [stem/master] Extra-info integraion tests

atagar at torproject.org atagar at torproject.org
Mon May 14 00:14:27 UTC 2012


commit 406205c2549e855140723bc42af874248a6b3026
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed May 9 08:42:48 2012 -0700

    Extra-info integraion tests
    
    ExtraInfo counterparts for parsing a metrics descriptor and tor's cached file.
    There's some common bits with the server descriptor tests that I plan to move
    out.
---
 run_tests.py                                  |    2 +
 test/integ/descriptor/__init__.py             |    2 +-
 test/integ/descriptor/extrainfo_descriptor.py |  108 +++++++++++++++++++++++++
 test/integ/descriptor/server_descriptor.py    |   19 ++---
 test/runner.py                                |    1 +
 5 files changed, 119 insertions(+), 13 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index bf2c2bc..85a4f65 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -36,6 +36,7 @@ import test.integ.socket.control_message
 import test.integ.socket.control_socket
 import test.integ.descriptor.reader
 import test.integ.descriptor.server_descriptor
+import test.integ.descriptor.extrainfo_descriptor
 import test.integ.util.conf
 import test.integ.util.system
 import test.integ.process
@@ -108,6 +109,7 @@ INTEG_TESTS = (
   test.integ.util.system.TestSystem,
   test.integ.descriptor.reader.TestDescriptorReader,
   test.integ.descriptor.server_descriptor.TestServerDescriptor,
+  test.integ.descriptor.extrainfo_descriptor.TestExtraInfoDescriptor,
   test.integ.version.TestVersion,
   test.integ.process.TestProcess,
   test.integ.socket.control_socket.TestControlSocket,
diff --git a/test/integ/descriptor/__init__.py b/test/integ/descriptor/__init__.py
index b143c2a..ec61ba7 100644
--- a/test/integ/descriptor/__init__.py
+++ b/test/integ/descriptor/__init__.py
@@ -2,5 +2,5 @@
 Integration tests for stem.descriptor.* contents.
 """
 
-__all__ = ["reader", "server_descriptor"]
+__all__ = ["reader", "extrainfo_descriptor", "server_descriptor"]
 
diff --git a/test/integ/descriptor/extrainfo_descriptor.py b/test/integ/descriptor/extrainfo_descriptor.py
new file mode 100644
index 0000000..74ac7fb
--- /dev/null
+++ b/test/integ/descriptor/extrainfo_descriptor.py
@@ -0,0 +1,108 @@
+"""
+Integration tests for stem.descriptor.extrainfo_descriptor.
+"""
+
+import os
+import datetime
+import unittest
+
+import stem.descriptor.extrainfo_descriptor
+import test.runner
+
+my_dir = os.path.dirname(__file__)
+DESCRIPTOR_TEST_DATA = os.path.join(my_dir, "data")
+
+# 'test_cached_descriptor' is a lengthy test and uneffected by testing targets,
+# so including a flag to prevent it from being ran multiple times
+
+RAN_CACHED_DESCRIPTOR_TEST = False
+
+class TestExtraInfoDescriptor(unittest.TestCase):
+  is_cached_descriptors_available = None
+  
+  def setUp(self):
+    if self.is_cached_descriptors_available is None:
+      test_dir = test.runner.get_runner().get_test_dir()
+      descriptor_path = os.path.join(test_dir, "cached-extrainfo")
+      self.is_cached_descriptors_available = os.path.exists(descriptor_path)
+  
+  def test_metrics_descriptor(self):
+    """
+    Parses and checks our results against an extrainfo descriptor from metrics.
+    """
+    
+    descriptor_path = os.path.join(DESCRIPTOR_TEST_DATA, "extrainfo_descriptor")
+    
+    descriptor_file = open(descriptor_path)
+    descriptor_contents = descriptor_file.read()
+    descriptor_file.close()
+    
+    expected_signature = """-----BEGIN SIGNATURE-----
+K5FSywk7qvw/boA4DQcqkls6Ize5vcBYfhQ8JnOeRQC9+uDxbnpm3qaYN9jZ8myj
+k0d2aofcVbHr4fPQOSST0LXDrhFl5Fqo5um296zpJGvRUeO6S44U/EfJAGShtqWw
+7LZqklu+gVvhMKREpchVqlAwXkWR44VENm24Hs+mT3M=
+-----END SIGNATURE-----"""
+    
+    desc = stem.descriptor.extrainfo_descriptor.ExtraInfoDescriptor(descriptor_contents)
+    self.assertEquals("NINJA", desc.nickname)
+    self.assertEquals("B2289C3EAB83ECD6EB916A2F481A02E6B76A0A48", desc.fingerprint)
+    self.assertEquals(datetime.datetime(2012, 5, 5, 17, 3, 50), desc.published)
+    self.assertEquals(datetime.datetime(2012, 5, 5, 17, 2, 45), desc.read_history_end)
+    self.assertEquals(900, desc.read_history_interval)
+    self.assertEquals(datetime.datetime(2012, 5, 5, 17, 2, 45), desc.write_history_end)
+    self.assertEquals(900, desc.write_history_interval)
+    self.assertEquals(expected_signature, desc.signature)
+    
+    # TODO: still missing dirreq-read-history and dirreq-write-history
+    #self.assertEquals([], desc.get_unrecognized_lines())
+    
+    # The read-history and write-history lines are pretty long so just checking
+    # the initial contents for the line and parsed values.
+    
+    read_start = "2012-05-05 17:02:45 (900 s) 3309568,9216,41984"
+    self.assertTrue(desc.read_history.startswith(read_start))
+    
+    read_values_start = [3309568, 9216, 41984, 27648, 123904]
+    self.assertEquals(read_values_start, desc.read_history_values[:5])
+    
+    write_start = "2012-05-05 17:02:45 (900 s) 1082368,19456,50176,272384"
+    self.assertTrue(desc.write_history.startswith(write_start))
+    
+    write_values_start = [1082368, 19456, 50176, 272384, 485376]
+    self.assertEquals(write_values_start, desc.write_history_values[:5])
+  
+  def test_cached_descriptor(self):
+    """
+    Parses the cached descriptor file in our data directory, checking that it
+    doesn't raise any validation issues and looking for unrecognized descriptor
+    additions.
+    """
+    
+    descriptor_path = os.path.join(test.runner.get_runner().get_test_dir(), "cached-extrainfo")
+    
+    if not self.is_cached_descriptors_available:
+      self.skipTest("(no cached descriptors)")
+    
+    global RAN_CACHED_DESCRIPTOR_TEST
+    
+    if RAN_CACHED_DESCRIPTOR_TEST:
+      self.skipTest("(already ran)")
+    else:
+      RAN_CACHED_DESCRIPTOR_TEST = True
+    
+    with open(descriptor_path) as descriptor_file:
+      for desc in stem.descriptor.extrainfo_descriptor.parse_file(descriptor_file):
+        # TODO: uncomment when we're done implementing the ExtraInfoDescriptor class
+        #unrecognized_lines = desc.get_unrecognized_lines()
+        unrecognized_lines = []
+        
+        if unrecognized_lines:
+          # TODO: This isn't actually a problem, and rather than failing we
+          # should alert the user about these entries at the end of the tests
+          # (along with new events, getinfo options, and such). For now though
+          # there doesn't seem to be anything in practice to trigger this so
+          # failing to get our attention if it does.
+          
+          print "Unrecognized descriptor content: %s" % unrecognized_lines
+          self.fail()
+
diff --git a/test/integ/descriptor/server_descriptor.py b/test/integ/descriptor/server_descriptor.py
index 5ad700f..a74e4eb 100644
--- a/test/integ/descriptor/server_descriptor.py
+++ b/test/integ/descriptor/server_descriptor.py
@@ -20,17 +20,17 @@ DESCRIPTOR_TEST_DATA = os.path.join(my_dir, "data")
 RAN_CACHED_DESCRIPTOR_TEST = False
 
 class TestServerDescriptor(unittest.TestCase):
-  is_descriptors_available = None
+  is_cached_descriptors_available = None
   
   def setUp(self):
     # If this is our first time running the integ tests and we didn't wait for
     # a full tor initialization then the cached descriptors won't exist yet.
     # Noting if they exist or not since some tests need them.
     
-    if self.is_descriptors_available is None:
+    if self.is_cached_descriptors_available is None:
       test_dir = test.runner.get_runner().get_test_dir()
       descriptor_path = os.path.join(test_dir, "cached-descriptors")
-      self.is_descriptors_available = os.path.exists(descriptor_path)
+      self.is_cached_descriptors_available = os.path.exists(descriptor_path)
   
   def test_metrics_descriptor(self):
     """
@@ -43,8 +43,6 @@ class TestServerDescriptor(unittest.TestCase):
     descriptor_contents = descriptor_file.read()
     descriptor_file.close()
     
-    expected_published = datetime.datetime(2012, 3, 1, 17, 15, 27)
-    
     expected_family = [
       "$0CE3CFB1E9CC47B63EA8869813BF6FAB7D4540C1",
       "$1FD187E8F69A9B74C9202DC16A25B9E7744AB9F6",
@@ -85,7 +83,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     self.assertEquals(stem.version.Version("0.2.1.30"), desc.tor_version)
     self.assertEquals("Linux x86_64", desc.operating_system)
     self.assertEquals(588217, desc.uptime)
-    self.assertEquals(expected_published, desc.published)
+    self.assertEquals(datetime.datetime(2012, 3, 1, 17, 15, 27), desc.published)
     self.assertEquals("www.atagar.com/contact", desc.contact)
     self.assertEquals(["1", "2"], desc.link_protocols)
     self.assertEquals(["1"], desc.circuit_protocols)
@@ -170,7 +168,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     
     descriptor_path = os.path.join(test.runner.get_runner().get_test_dir(), "cached-descriptors")
     
-    if not self.is_descriptors_available:
+    if not self.is_cached_descriptors_available:
       self.skipTest("(no cached descriptors)")
     
     global RAN_CACHED_DESCRIPTOR_TEST
@@ -211,7 +209,6 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     descriptor_contents = descriptor_file.read()
     descriptor_file.close()
     
-    expected_published = datetime.datetime(2012, 3, 21, 16, 28, 14)
     expected_contact = "2048R/F171EC1F Johan Bl\xc3\xa5b\xc3\xa4ck \xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf"
     
     desc = stem.descriptor.server_descriptor.RelayDescriptor(descriptor_contents)
@@ -225,7 +222,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     self.assertEquals(stem.version.Version("0.2.2.35"), desc.tor_version)
     self.assertEquals("Linux x86_64", desc.operating_system)
     self.assertEquals(3103848, desc.uptime)
-    self.assertEquals(expected_published, desc.published)
+    self.assertEquals(datetime.datetime(2012, 3, 21, 16, 28, 14), desc.published)
     self.assertEquals(expected_contact, desc.contact)
     self.assertEquals(["1", "2"], desc.link_protocols)
     self.assertEquals(["1"], desc.circuit_protocols)
@@ -303,8 +300,6 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     descriptor_contents = descriptor_file.read()
     descriptor_file.close()
     
-    expected_published = datetime.datetime(2012, 3, 22, 17, 34, 38)
-    
     expected_family = [
       "$CE396C72A3D0880F74C064FEA79D68C15BD380B9",
       "$AB8B00C00B1347BA80A88E548FAC9EDF701D7D0E",
@@ -322,7 +317,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     self.assertEquals(stem.version.Version("0.2.3.12-alpha"), desc.tor_version)
     self.assertEquals("Linux x86_64", desc.operating_system)
     self.assertEquals(186, desc.uptime)
-    self.assertEquals(expected_published, desc.published)
+    self.assertEquals(datetime.datetime(2012, 3, 22, 17, 34, 38), desc.published)
     self.assertEquals("somebody", desc.contact)
     self.assertEquals(["1", "2"], desc.link_protocols)
     self.assertEquals(["1"], desc.circuit_protocols)
diff --git a/test/runner.py b/test/runner.py
index 6acc68e..034f1e5 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -63,6 +63,7 @@ ERROR_ATTR = (term.Color.RED, term.Attr.BOLD)
 BASE_TORRC = """# configuration for stem integration tests
 DataDirectory %s
 SocksPort 0
+DownloadExtraInfo 1
 """
 
 # We make some paths relative to stem's base directory (the one above us)





More information about the tor-commits mailing list