← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~daniel-thewatkins/cloud-init/fix-py34-test-hang into lp:cloud-init

 

Dan Watkins has proposed merging lp:~daniel-thewatkins/cloud-init/fix-py34-test-hang into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~daniel-thewatkins/cloud-init/fix-py34-test-hang/+merge/251725

Fixes test hangs on Python 3.4.2 (i.e. python3 in utopic).
-- 
Your team cloud init development team is requested to review the proposed merge of lp:~daniel-thewatkins/cloud-init/fix-py34-test-hang into lp:cloud-init.
=== modified file 'tests/unittests/helpers.py'
--- tests/unittests/helpers.py	2015-02-26 00:40:33 +0000
+++ tests/unittests/helpers.py	2015-03-04 12:31:58 +0000
@@ -1,5 +1,6 @@
 from __future__ import print_function
 
+import functools
 import os
 import sys
 import shutil
@@ -25,9 +26,10 @@
 PY26 = False
 PY27 = False
 PY3 = False
+FIX_HTTPRETTY = False
 
 _PY_VER = sys.version_info
-_PY_MAJOR, _PY_MINOR = _PY_VER[0:2]
+_PY_MAJOR, _PY_MINOR, _PY_MICRO = _PY_VER[0:3]
 if (_PY_MAJOR, _PY_MINOR) <= (2, 6):
     if (_PY_MAJOR, _PY_MINOR) == (2, 6):
         PY26 = True
@@ -39,6 +41,8 @@
         PY2 = True
     if (_PY_MAJOR, _PY_MINOR) >= (3, 0):
         PY3 = True
+        if _PY_MINOR == 4 and _PY_MICRO < 3:
+            FIX_HTTPRETTY = True
 
 if PY26:
     # For now add these on, taken from python 2.7 + slightly adjusted.  Drop
@@ -268,6 +272,37 @@
                 mock.patch.object(sys, 'stderr', stderr))
 
 
+def import_httpretty():
+    """Import HTTPretty and monkey patch Python 3.4 issue.
+    See https://github.com/gabrielfalcao/HTTPretty/pull/193 and
+    as well as https://github.com/gabrielfalcao/HTTPretty/issues/221.
+
+    Lifted from
+    https://github.com/inveniosoftware/datacite/blob/master/tests/helpers.py
+    """
+    if not FIX_HTTPRETTY:
+        import httpretty
+    else:
+        import socket
+        old_SocketType = socket.SocketType
+
+        import httpretty
+        from httpretty import core
+
+        def sockettype_patch(f):
+            @functools.wraps(f)
+            def inner(*args, **kwargs):
+                f(*args, **kwargs)
+                socket.SocketType = old_SocketType
+                socket.__dict__['SocketType'] = old_SocketType
+            return inner
+
+        core.httpretty.disable = sockettype_patch(
+            httpretty.httpretty.disable
+        )
+    return httpretty
+
+
 class HttprettyTestCase(TestCase):
     # necessary as http_proxy gets in the way of httpretty
     # https://github.com/gabrielfalcao/HTTPretty/issues/122

=== modified file 'tests/unittests/test_datasource/test_digitalocean.py'
--- tests/unittests/test_datasource/test_digitalocean.py	2015-01-23 02:21:04 +0000
+++ tests/unittests/test_datasource/test_digitalocean.py	2015-03-04 12:31:58 +0000
@@ -15,7 +15,6 @@
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import httpretty
 import re
 
 from six.moves.urllib_parse import urlparse
@@ -26,6 +25,8 @@
 
 from .. import helpers as test_helpers
 
+httpretty = test_helpers.import_httpretty()
+
 # Abbreviated for the test
 DO_INDEX = """id
            hostname

=== modified file 'tests/unittests/test_datasource/test_gce.py'
--- tests/unittests/test_datasource/test_gce.py	2015-02-26 00:40:33 +0000
+++ tests/unittests/test_datasource/test_gce.py	2015-03-04 12:31:58 +0000
@@ -15,7 +15,6 @@
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import httpretty
 import re
 
 from base64 import b64encode, b64decode
@@ -27,6 +26,8 @@
 
 from .. import helpers as test_helpers
 
+httpretty = test_helpers.import_httpretty()
+
 GCE_META = {
     'instance/id': '123',
     'instance/zone': 'foo/bar',

=== modified file 'tests/unittests/test_datasource/test_openstack.py'
--- tests/unittests/test_datasource/test_openstack.py	2015-02-26 00:40:33 +0000
+++ tests/unittests/test_datasource/test_openstack.py	2015-03-04 12:31:58 +0000
@@ -31,7 +31,7 @@
 from cloudinit.sources.helpers import openstack
 from cloudinit import util
 
-import httpretty as hp
+hp = test_helpers.import_httpretty()
 
 BASE_URL = "http://169.254.169.254";
 PUBKEY = u'ssh-rsa AAAAB3NzaC1....sIkJhq8wdX+4I3A4cYbYP ubuntu@server-460\n'

=== modified file 'tests/unittests/test_ec2_util.py'
--- tests/unittests/test_ec2_util.py	2015-02-26 00:40:33 +0000
+++ tests/unittests/test_ec2_util.py	2015-03-04 12:31:58 +0000
@@ -3,7 +3,7 @@
 from cloudinit import ec2_utils as eu
 from cloudinit import url_helper as uh
 
-import httpretty as hp
+hp = helpers.import_httpretty()
 
 
 class TestEc2Util(helpers.HttprettyTestCase):


Follow ups