Merge lp:~daniel-thewatkins/cloud-init/fix-py34-test-hang into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Dan Watkins on 2015-03-04
Status: Merged
Merged at revision: 1076
Proposed branch: lp:~daniel-thewatkins/cloud-init/fix-py34-test-hang
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 137 lines (+42/-5)
5 files modified
tests/unittests/helpers.py (+36/-1)
tests/unittests/test_datasource/test_digitalocean.py (+2/-1)
tests/unittests/test_datasource/test_gce.py (+2/-1)
tests/unittests/test_datasource/test_openstack.py (+1/-1)
tests/unittests/test_ec2_util.py (+1/-1)
To merge this branch: bzr merge lp:~daniel-thewatkins/cloud-init/fix-py34-test-hang
Reviewer Review Type Date Requested Status
cloud-init commiters 2015-03-04 Pending
Review via email: mp+251725@code.launchpad.net

Description of the Change

Fixes test hangs on Python 3.4.2 (i.e. python3 in utopic).

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/unittests/helpers.py'
2--- tests/unittests/helpers.py 2015-02-26 00:40:33 +0000
3+++ tests/unittests/helpers.py 2015-03-04 12:37:24 +0000
4@@ -1,5 +1,6 @@
5 from __future__ import print_function
6
7+import functools
8 import os
9 import sys
10 import shutil
11@@ -25,9 +26,10 @@
12 PY26 = False
13 PY27 = False
14 PY3 = False
15+FIX_HTTPRETTY = False
16
17 _PY_VER = sys.version_info
18-_PY_MAJOR, _PY_MINOR = _PY_VER[0:2]
19+_PY_MAJOR, _PY_MINOR, _PY_MICRO = _PY_VER[0:3]
20 if (_PY_MAJOR, _PY_MINOR) <= (2, 6):
21 if (_PY_MAJOR, _PY_MINOR) == (2, 6):
22 PY26 = True
23@@ -39,6 +41,8 @@
24 PY2 = True
25 if (_PY_MAJOR, _PY_MINOR) >= (3, 0):
26 PY3 = True
27+ if _PY_MINOR == 4 and _PY_MICRO < 3:
28+ FIX_HTTPRETTY = True
29
30 if PY26:
31 # For now add these on, taken from python 2.7 + slightly adjusted. Drop
32@@ -268,6 +272,37 @@
33 mock.patch.object(sys, 'stderr', stderr))
34
35
36+def import_httpretty():
37+ """Import HTTPretty and monkey patch Python 3.4 issue.
38+ See https://github.com/gabrielfalcao/HTTPretty/pull/193 and
39+ as well as https://github.com/gabrielfalcao/HTTPretty/issues/221.
40+
41+ Lifted from
42+ https://github.com/inveniosoftware/datacite/blob/master/tests/helpers.py
43+ """
44+ if not FIX_HTTPRETTY:
45+ import httpretty
46+ else:
47+ import socket
48+ old_SocketType = socket.SocketType
49+
50+ import httpretty
51+ from httpretty import core
52+
53+ def sockettype_patch(f):
54+ @functools.wraps(f)
55+ def inner(*args, **kwargs):
56+ f(*args, **kwargs)
57+ socket.SocketType = old_SocketType
58+ socket.__dict__['SocketType'] = old_SocketType
59+ return inner
60+
61+ core.httpretty.disable = sockettype_patch(
62+ httpretty.httpretty.disable
63+ )
64+ return httpretty
65+
66+
67 class HttprettyTestCase(TestCase):
68 # necessary as http_proxy gets in the way of httpretty
69 # https://github.com/gabrielfalcao/HTTPretty/issues/122
70
71=== modified file 'tests/unittests/test_datasource/test_digitalocean.py'
72--- tests/unittests/test_datasource/test_digitalocean.py 2015-01-23 02:21:04 +0000
73+++ tests/unittests/test_datasource/test_digitalocean.py 2015-03-04 12:37:24 +0000
74@@ -15,7 +15,6 @@
75 # You should have received a copy of the GNU General Public License
76 # along with this program. If not, see <http://www.gnu.org/licenses/>.
77
78-import httpretty
79 import re
80
81 from six.moves.urllib_parse import urlparse
82@@ -26,6 +25,8 @@
83
84 from .. import helpers as test_helpers
85
86+httpretty = test_helpers.import_httpretty()
87+
88 # Abbreviated for the test
89 DO_INDEX = """id
90 hostname
91
92=== modified file 'tests/unittests/test_datasource/test_gce.py'
93--- tests/unittests/test_datasource/test_gce.py 2015-02-26 00:40:33 +0000
94+++ tests/unittests/test_datasource/test_gce.py 2015-03-04 12:37:24 +0000
95@@ -15,7 +15,6 @@
96 # You should have received a copy of the GNU General Public License
97 # along with this program. If not, see <http://www.gnu.org/licenses/>.
98
99-import httpretty
100 import re
101
102 from base64 import b64encode, b64decode
103@@ -27,6 +26,8 @@
104
105 from .. import helpers as test_helpers
106
107+httpretty = test_helpers.import_httpretty()
108+
109 GCE_META = {
110 'instance/id': '123',
111 'instance/zone': 'foo/bar',
112
113=== modified file 'tests/unittests/test_datasource/test_openstack.py'
114--- tests/unittests/test_datasource/test_openstack.py 2015-02-26 00:40:33 +0000
115+++ tests/unittests/test_datasource/test_openstack.py 2015-03-04 12:37:24 +0000
116@@ -31,7 +31,7 @@
117 from cloudinit.sources.helpers import openstack
118 from cloudinit import util
119
120-import httpretty as hp
121+hp = test_helpers.import_httpretty()
122
123 BASE_URL = "http://169.254.169.254"
124 PUBKEY = u'ssh-rsa AAAAB3NzaC1....sIkJhq8wdX+4I3A4cYbYP ubuntu@server-460\n'
125
126=== modified file 'tests/unittests/test_ec2_util.py'
127--- tests/unittests/test_ec2_util.py 2015-02-26 00:40:33 +0000
128+++ tests/unittests/test_ec2_util.py 2015-03-04 12:37:24 +0000
129@@ -3,7 +3,7 @@
130 from cloudinit import ec2_utils as eu
131 from cloudinit import url_helper as uh
132
133-import httpretty as hp
134+hp = helpers.import_httpretty()
135
136
137 class TestEc2Util(helpers.HttprettyTestCase):