Skip to content

Instantly share code, notes, and snippets.

@alistairncoles
Created March 21, 2016 19:48
Show Gist options
  • Save alistairncoles/0debf01f19c74eeaaf72 to your computer and use it in GitHub Desktop.
Save alistairncoles/0debf01f19c74eeaaf72 to your computer and use it in GitHub Desktop.
internal client test fixes
diff --git a/test/unit/common/test_internal_client.py b/test/unit/common/test_internal_client.py
index f0eea61..150f88c 100644
--- a/test/unit/common/test_internal_client.py
+++ b/test/unit/common/test_internal_client.py
@@ -14,6 +14,8 @@
# limitations under the License.
import json
+
+import itertools
import mock
import unittest
import zlib
@@ -34,6 +36,17 @@ from test.unit import with_tempdir, write_fake_ring, patch_policies
from test.unit.common.middleware.helpers import FakeSwift
+class FakeConn(object):
+ def __init__(self, body='irrelevant'):
+ self.body = body
+
+ def read(self):
+ return self.body
+
+ def info(self):
+ return {}
+
+
def not_sleep(seconds):
pass
@@ -339,17 +352,10 @@ class TestInternalClient(unittest.TestCase):
# verify that base_request passes timeout arg on to urlopen
body = {"some": "content"}
- class FakeConn(object):
- def read(self):
- return json.dumps(body)
-
- def info(self):
- return {}
-
for timeout in (0.0, 42.0, None):
mocked_func = 'swift.common.internal_client.urllib2.urlopen'
with mock.patch(mocked_func) as mock_urlopen:
- mock_urlopen.side_effect = [FakeConn()]
+ mock_urlopen.side_effect = [FakeConn(json.dumps(body))]
sc = internal_client.SimpleClient('http://0.0.0.0/')
_, resp_body = sc.base_request('GET', timeout=timeout)
mock_urlopen.assert_called_once_with(mock.ANY, timeout=timeout)
@@ -1184,18 +1190,17 @@ class TestGetAuth(unittest.TestCase):
'http://127.0.0.1', 'user', 'key', auth_version=2.0)
-mock_time_value = 1401224049.98
-
-
-def mock_time():
- global mock_time_value
- mock_time_value += 1
- return mock_time_value
+def make_time_iter():
+ return iter(1401224049.98 + t for t in itertools.count(1))
class TestSimpleClient(unittest.TestCase):
- def _test_get_head(self, request, urlopen, method, mocked_time_offset):
+ @mock.patch('eventlet.green.urllib2.urlopen')
+ @mock.patch('eventlet.green.urllib2.Request')
+ @mock.patch('swift.common.internal_client.time')
+ def _test_get_head(self, method, mocked_time, request, urlopen):
+ mocked_time.side_effect = make_time_iter()
# basic GET request, only url as kwarg
request.return_value.get_type.return_value = "http"
urlopen.return_value.read.return_value = ''
@@ -1212,11 +1217,9 @@ class TestSimpleClient(unittest.TestCase):
self.assertEqual([{'content-length': '345'}, None], retval)
self.assertEqual(method, request.return_value.get_method())
self.assertEqual(logger.log_dict['debug'], [(
- ('-> 2014-05-27T20:54:' + str(11 + mocked_time_offset) +
- ' ' + method + ' http://127.0.0.1%3Fformat%3Djson 200 '
- '123 345 14012240' + str(50 + mocked_time_offset) +
- '.98 14012240' + str(51 + mocked_time_offset) +
- '.98 1.0 -',), {})])
+ ('-> 2014-05-27T20:54:11 ' + method +
+ ' http://127.0.0.1%3Fformat%3Djson 200 '
+ '123 345 1401224050.98 1401224051.98 1.0 -',), {})])
# Check if JSON is decoded
urlopen.return_value.read.return_value = '{}'
@@ -1255,17 +1258,11 @@ class TestSimpleClient(unittest.TestCase):
data=None)
self.assertEqual([{'content-length': '345'}, {}], retval)
- @mock.patch('eventlet.green.urllib2.urlopen')
- @mock.patch('eventlet.green.urllib2.Request')
- @mock.patch('swift.common.internal_client.time', mock_time)
- def test_get(self, request, urlopen):
- self._test_get_head(request, urlopen, 'GET', 0)
+ def test_get(self):
+ self._test_get_head('GET')
- @mock.patch('eventlet.green.urllib2.urlopen')
- @mock.patch('eventlet.green.urllib2.Request')
- @mock.patch('swift.common.internal_client.time', mock_time)
- def test_head(self, request, urlopen):
- self._test_get_head(request, urlopen, 'HEAD', 12)
+ def test_head(self):
+ self._test_get_head('HEAD')
@mock.patch('eventlet.green.urllib2.urlopen')
@mock.patch('eventlet.green.urllib2.Request')
@@ -1396,14 +1393,6 @@ class TestSimpleClient(unittest.TestCase):
proxy = '%s://%s' % (scheme, proxy_host)
url = 'https://127.0.0.1:1/a'
- class FakeConn(object):
-
- def read(self):
- return 'irrelevant'
-
- def info(self):
- return {}
-
mocked = 'swift.common.internal_client.urllib2.urlopen'
# module level methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment