[ARVADOS] updated: cdf4cf8189b0a56c25f63d39a1dc916082d08ecd

git at public.curoverse.com git at public.curoverse.com
Fri Jan 2 23:32:47 EST 2015


Summary of changes:
 sdk/python/tests/arvados_testutil.py | 42 +++++++-------------
 sdk/python/tests/test_keep_client.py | 74 +++++++++++++++++-------------------
 2 files changed, 48 insertions(+), 68 deletions(-)

       via  cdf4cf8189b0a56c25f63d39a1dc916082d08ecd (commit)
      from  d251327b574e981cc02ff04c80d609df5c9ec30d (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit cdf4cf8189b0a56c25f63d39a1dc916082d08ecd
Author: Peter Amstutz <peter.amstutz at curoverse.com>
Date:   Fri Jan 2 23:34:15 2015 -0500

    3198: Figured out how to mock session object, reverted most tests updated in
    previous commit.

diff --git a/sdk/python/tests/arvados_testutil.py b/sdk/python/tests/arvados_testutil.py
index 5d58ea8..351d430 100644
--- a/sdk/python/tests/arvados_testutil.py
+++ b/sdk/python/tests/arvados_testutil.py
@@ -39,35 +39,19 @@ def fake_requests_response(code, body, **headers):
     r.raw = io.BytesIO(body)
     return r
 
-class MockSession(object):
-    def __init__(self, body, codes, headers):
-        if isinstance(body, list):
-            self.body = body
-        else:
-            self.body = [body for c in codes]
-        self.codes = codes
-        self.headers = headers
-        self.n = -1
-
-    def get(self, url, **kwargs):
-        self.n += 1
-        return fake_requests_response(self.codes[self.n], self.body[self.n], **self.headers)
-
-    def put(self, url, **kwargs):
-        self.n += 1
-        return fake_requests_response(self.codes[self.n], self.body[self.n], **self.headers)
-
-# def mock_get_responses(body, *codes, **headers):
-#     return mock.patch('requests.get', side_effect=(
-#         fake_requests_response(code, body, **headers) for code in codes))
-
-# def mock_put_responses(body, *codes, **headers):
-#     return mock.patch('requests.put', side_effect=(
-#         fake_requests_response(code, body, **headers) for code in codes))
-
-def mock_requestslib_responses(method, body, *codes, **headers):
-    return mock.patch(method, side_effect=(
-        fake_requests_response(code, body, **headers) for code in codes))
+def mock_put_responses(body, *codes, **headers):
+    m = mock.MagicMock()
+    m.return_value.put.side_effect = (fake_requests_response(code, body, **headers) for code in codes)
+    return mock.patch('requests.Session', m)
+
+def mock_get_responses(body, *codes, **headers):
+    m = mock.MagicMock()
+    m.return_value.get.side_effect = (fake_requests_response(code, body, **headers) for code in codes)
+    return mock.patch('requests.Session', m)
+
+#def mock_requestslib_responses(method, body, *codes, **headers):
+#    return mock.patch(method, side_effect=(
+#        fake_requests_response(code, body, **headers) for code in codes))
 
 class MockStreamReader(object):
     def __init__(self, name='.', *data):
diff --git a/sdk/python/tests/test_keep_client.py b/sdk/python/tests/test_keep_client.py
index 108b74c..803bb97 100644
--- a/sdk/python/tests/test_keep_client.py
+++ b/sdk/python/tests/test_keep_client.py
@@ -416,18 +416,11 @@ class KeepClientRetryTestMixin(object):
 
     def setUp(self):
         self.client_kwargs = {'proxy': self.PROXY_ADDR, 'local_store': ''}
-        self.mock_session = None
-
-    def mock_get_responses(self, body, *codes, **headers):
-        self.mock_session = tutil.MockSession(body, codes, headers)
-
-    def mock_put_responses(self, body, *codes, **headers):
-        self.mock_session = tutil.MockSession(body, codes, headers)
 
     def new_client(self, **caller_kwargs):
         kwargs = self.client_kwargs.copy()
         kwargs.update(caller_kwargs)
-        return arvados.KeepClient(session=self.mock_session, **kwargs)
+        return arvados.KeepClient(**kwargs)
 
     def run_method(self, *args, **kwargs):
         raise NotImplementedError("test subclasses must define run_method")
@@ -443,29 +436,29 @@ class KeepClientRetryTestMixin(object):
         self.assertRaises(error_class, self.run_method, *args, **kwargs)
 
     def test_immediate_success(self):
-        self.TEST_PATCHER(self.DEFAULT_EXPECT, 200)
-        self.check_success()
+        with self.TEST_PATCHER(self.DEFAULT_EXPECT, 200):
+            self.check_success()
 
     def test_retry_then_success(self):
-        self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 200)
-        self.check_success(num_retries=3)
+        with self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 200):
+            self.check_success(num_retries=3)
 
     def test_no_default_retry(self):
-        self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 200)
-        self.check_exception()
+        with self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 200):
+            self.check_exception()
 
     def test_no_retry_after_permanent_error(self):
-        self.TEST_PATCHER(self.DEFAULT_EXPECT, 403, 200)
-        self.check_exception(num_retries=3)
+        with self.TEST_PATCHER(self.DEFAULT_EXPECT, 403, 200):
+            self.check_exception(num_retries=3)
 
     def test_error_after_retries_exhausted(self):
-        self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 500, 200)
-        self.check_exception(num_retries=1)
+        with self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 500, 200):
+            self.check_exception(num_retries=1)
 
     def test_num_retries_instance_fallback(self):
         self.client_kwargs['num_retries'] = 3
-        self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 200)
-        self.check_success()
+        with self.TEST_PATCHER(self.DEFAULT_EXPECT, 500, 200):
+            self.check_success()
 
 
 @tutil.skip_sleep
@@ -473,15 +466,15 @@ class KeepClientRetryGetTestCase(KeepClientRetryTestMixin, unittest.TestCase):
     DEFAULT_EXPECT = KeepClientRetryTestMixin.TEST_DATA
     DEFAULT_EXCEPTION = arvados.errors.KeepReadError
     HINTED_LOCATOR = KeepClientRetryTestMixin.TEST_LOCATOR + '+K at xyzzy'
-    TEST_PATCHER = KeepClientRetryTestMixin.mock_get_responses
+    TEST_PATCHER = staticmethod(tutil.mock_get_responses)
 
     def run_method(self, locator=KeepClientRetryTestMixin.TEST_LOCATOR,
                    *args, **kwargs):
         return self.new_client().get(locator, *args, **kwargs)
 
     def test_specific_exception_when_not_found(self):
-        self.mock_get_responses(self.DEFAULT_EXPECT, 404, 200)
-        self.check_exception(arvados.errors.NotFoundError, num_retries=3)
+        with tutil.mock_get_responses(self.DEFAULT_EXPECT, 404, 200):
+            self.check_exception(arvados.errors.NotFoundError, num_retries=3)
 
     def test_general_exception_with_mixed_errors(self):
         # get should raise a NotFoundError if no server returns the block,
@@ -489,40 +482,43 @@ class KeepClientRetryGetTestCase(KeepClientRetryTestMixin, unittest.TestCase):
         # This test rigs up 50/50 disagreement between two servers, and
         # checks that it does not become a NotFoundError.
         client = self.new_client()
-        self.mock_get_responses(self.DEFAULT_EXPECT, 404, 500)
-        with self.assertRaises(arvados.errors.KeepReadError) as exc_check:
-            client.get(self.HINTED_LOCATOR)
-        self.assertNotIsInstance(
-            exc_check.exception, arvados.errors.NotFoundError,
-            "mixed errors raised NotFoundError")
+        with tutil.mock_get_responses(self.DEFAULT_EXPECT, 404, 500):
+            with self.assertRaises(arvados.errors.KeepReadError) as exc_check:
+                client.get(self.HINTED_LOCATOR)
+            self.assertNotIsInstance(
+                exc_check.exception, arvados.errors.NotFoundError,
+                "mixed errors raised NotFoundError")
 
     def test_hint_server_can_succeed_without_retries(self):
-        self.mock_get_responses(self.DEFAULT_EXPECT, 404, 200, 500)
-        self.check_success(locator=self.HINTED_LOCATOR)
+        with tutil.mock_get_responses(self.DEFAULT_EXPECT, 404, 200, 500):
+            self.check_success(locator=self.HINTED_LOCATOR)
 
     def test_try_next_server_after_timeout(self):
-        side_effects = [
+        m = mock.MagicMock()
+        m.return_value.get.side_effect = [
             socket.timeout("timed out"),
             tutil.fake_requests_response(200, self.DEFAULT_EXPECT)]
-        with mock.patch('requests.get',
-                        side_effect=iter(side_effects)):
+        with mock.patch('requests.Session', m):
             self.check_success(locator=self.HINTED_LOCATOR)
 
     def test_retry_data_with_wrong_checksum(self):
-        self.mock_get_responses(['baddata', self.TEST_DATA], 200, 200)
-        self.check_success(locator=self.HINTED_LOCATOR)
+        m = mock.MagicMock()
+        m.return_value.get.side_effect = (tutil.fake_requests_response(200, s)
+                        for s in ['baddata', self.TEST_DATA])
+        with mock.patch('requests.Session', m):
+            self.check_success(locator=self.HINTED_LOCATOR)
 
 
 @tutil.skip_sleep
 class KeepClientRetryPutTestCase(KeepClientRetryTestMixin, unittest.TestCase):
     DEFAULT_EXPECT = KeepClientRetryTestMixin.TEST_LOCATOR
     DEFAULT_EXCEPTION = arvados.errors.KeepWriteError
-    TEST_PATCHER = KeepClientRetryTestMixin.mock_put_responses
+    TEST_PATCHER = staticmethod(tutil.mock_put_responses)
 
     def run_method(self, data=KeepClientRetryTestMixin.TEST_DATA,
                    copies=1, *args, **kwargs):
         return self.new_client().put(data, copies, *args, **kwargs)
 
     def test_do_not_send_multiple_copies_to_same_server(self):
-        self.mock_put_responses(self.DEFAULT_EXPECT, 200)
-        self.check_exception(copies=2, num_retries=3)
+        with tutil.mock_put_responses(self.DEFAULT_EXPECT, 200):
+            self.check_exception(copies=2, num_retries=3)

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list