[tor-commits] [stem/master] Fix test_router_status_entry for python3

atagar at torproject.org atagar at torproject.org
Mon Sep 4 20:09:24 UTC 2017


commit 982c36e23931b4f54f6a5f2a2c41244c4269de95
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Sep 4 13:05:18 2017 -0700

    Fix test_router_status_entry for python3
    
    Ick, that took me a while to track down. On python3 unit tests sometimes pass,
    and sometimes failed with...
    
      ======================================================================
      FAIL: test_router_status_entry
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/descriptor/server_descriptor.py", line 296, in test_router_status_entry
          self.assertEqual('4F0069BF91C04581B7C3CA9272E2D3228D4EA571', desc.digest)
      AssertionError: '4F0069BF91C04581B7C3CA9272E2D3228D4EA571' != 'A863EFE8395C41C880782B89B850D20EDD242BDA'
      - 4F0069BF91C04581B7C3CA9272E2D3228D4EA571
      + A863EFE8395C41C880782B89B850D20EDD242BDA
    
    This non-deterministic behavior was because the server descriptor's attributes
    were made from a dictionary, and hence had no defined ordering. With python 2.x
    they happened to always keep the same order, but with python3 it varied from
    run to run. Using OrderedDicts in this call path to ensure the order is
    consistent.
---
 stem/descriptor/__init__.py               |  2 +-
 test/unit/descriptor/server_descriptor.py | 24 +++++++++++++++---------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 66dc573b..5c6e5f3d 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -392,7 +392,7 @@ def _descriptor_content(attr = None, exclude = (), header_template = (), footer_
   """
 
   header_content, footer_content = [], []
-  attr = {} if attr is None else dict(attr)  # shallow copy since we're destructive
+  attr = {} if attr is None else OrderedDict(attr)  # shallow copy since we're destructive
 
   for content, template in ((header_content, header_template),
                             (footer_content, footer_template)):
diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py
index b31ad688..7bf0b2cf 100644
--- a/test/unit/descriptor/server_descriptor.py
+++ b/test/unit/descriptor/server_descriptor.py
@@ -30,6 +30,12 @@ from test.unit.descriptor import (
 )
 
 try:
+  # Added in 2.7
+  from collections import OrderedDict
+except ImportError:
+  from stem.util.ordereddict import OrderedDict
+
+try:
   # added in python 3.3
   from unittest.mock import Mock, patch
 except ImportError:
@@ -270,14 +276,14 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     exc_msg = 'Server descriptor lacks a fingerprint. This is an optional field, but required to make a router status entry.'
     self.assertRaisesRegexp(ValueError, exc_msg, desc_without_fingerprint.make_router_status_entry)
 
-    desc = RelayDescriptor.create({
-      'router': 'caerSidi 71.35.133.197 9001 0 0',
-      'published': '2012-02-29 04:03:19',
-      'fingerprint': '4F0C 867D F0EF 6816 0568 C826 838F 482C EA7C FE44',
-      'or-address': ['71.35.133.197:9001', '[12ab:2e19:3bcf::02:9970]:9001'],
-      'onion-key': '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % stem.descriptor.CRYPTO_BLOB,
-      'signing-key': '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % stem.descriptor.CRYPTO_BLOB,
-    }).make_router_status_entry()
+    desc = RelayDescriptor.create(OrderedDict((
+      ('router', 'caerSidi 71.35.133.197 9001 0 0'),
+      ('published', '2012-02-29 04:03:19'),
+      ('fingerprint', '4F0C 867D F0EF 6816 0568 C826 838F 482C EA7C FE44'),
+      ('or-address', ['71.35.133.197:9001', '[12ab:2e19:3bcf::02:9970]:9001']),
+      ('onion-key', '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % stem.descriptor.CRYPTO_BLOB),
+      ('signing-key', '\n-----BEGIN RSA PUBLIC KEY-----%s-----END RSA PUBLIC KEY-----' % stem.descriptor.CRYPTO_BLOB),
+    ))).make_router_status_entry()
 
     self.assertEqual(stem.descriptor.router_status_entry.RouterStatusEntryV3, type(desc))
     self.assertEqual('caerSidi', desc.nickname)
@@ -293,7 +299,7 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     self.assertEqual([('71.35.133.197', 9001, False), ('12ab:2e19:3bcf::02:9970', 9001, True)], desc.or_addresses)
     self.assertEqual(None, desc.identifier_type)
     self.assertEqual(None, desc.identifier)
-    self.assertEqual('4F0069BF91C04581B7C3CA9272E2D3228D4EA571', desc.digest)
+    self.assertEqual('A863EFE8395C41C880782B89B850D20EDD242BDA', desc.digest)
     self.assertEqual(153600, desc.bandwidth)
     self.assertEqual(None, desc.measured)
     self.assertEqual(False, desc.is_unmeasured)



More information about the tor-commits mailing list