[ARVADOS] updated: 22286e8b81fa7644500e197b95e6d6417ed25f7e

git at public.curoverse.com git at public.curoverse.com
Sat Jan 3 20:47:22 EST 2015


Summary of changes:
 sdk/python/tests/arvados_testutil.py | 19 +++++++++++----
 sdk/python/tests/test_arvfile.py     |  2 +-
 sdk/python/tests/test_collections.py | 28 +++++++++++-----------
 sdk/python/tests/test_keep_client.py | 45 ++++++++++++++++--------------------
 sdk/python/tests/test_stream.py      | 14 +++++------
 5 files changed, 56 insertions(+), 52 deletions(-)

       via  22286e8b81fa7644500e197b95e6d6417ed25f7e (commit)
      from  cdf4cf8189b0a56c25f63d39a1dc916082d08ecd (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 22286e8b81fa7644500e197b95e6d6417ed25f7e
Author: Peter Amstutz <peter.amstutz at curoverse.com>
Date:   Sat Jan 3 20:48:46 2015 -0500

    3198: Fixed up the rest of the tests to mock the session.

diff --git a/sdk/python/tests/arvados_testutil.py b/sdk/python/tests/arvados_testutil.py
index 351d430..474c068 100644
--- a/sdk/python/tests/arvados_testutil.py
+++ b/sdk/python/tests/arvados_testutil.py
@@ -41,7 +41,12 @@ def fake_requests_response(code, body, **headers):
 
 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)
+    if isinstance(body, tuple):
+        codes = list(codes)
+        codes.insert(0, body)
+        m.return_value.put.side_effect = (fake_requests_response(code, b, **headers) for b, code in codes)
+    else:
+        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):
@@ -49,9 +54,15 @@ def mock_get_responses(body, *codes, **headers):
     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))
+def mock_get(side_effect):
+    m = mock.MagicMock()
+    m.return_value.get.side_effect = side_effect
+    return mock.patch('requests.Session', m)
+
+def mock_put(side_effect):
+    m = mock.MagicMock()
+    m.return_value.put.side_effect = side_effect
+    return mock.patch('requests.Session', m)
 
 class MockStreamReader(object):
     def __init__(self, name='.', *data):
diff --git a/sdk/python/tests/test_arvfile.py b/sdk/python/tests/test_arvfile.py
index 7ccdf32..5bc54f5 100644
--- a/sdk/python/tests/test_arvfile.py
+++ b/sdk/python/tests/test_arvfile.py
@@ -40,7 +40,7 @@ class ArvadosFileWriterTestCase(unittest.TestCase):
                     self.r = r
                 def execute(self, num_retries=None):
                     return self.r
-            def create(self, body=None):
+            def create(self, ensure_unique_name=False, body=None):
                 if body != self.b:
                     raise Exception("Body %s does not match expectation %s" % (body, self.b))
                 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.r)
diff --git a/sdk/python/tests/test_collections.py b/sdk/python/tests/test_collections.py
index 4590115..d9850db 100644
--- a/sdk/python/tests/test_collections.py
+++ b/sdk/python/tests/test_collections.py
@@ -760,15 +760,14 @@ class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
 
     def test_open_flush(self):
         client = self.api_client_mock()
-        writer = arvados.CollectionWriter(client)
-        with writer.open('flush_test') as out_file:
-            out_file.write('flush1')
-            data_loc1 = hashlib.md5('flush1').hexdigest() + '+6'
-            with self.mock_keep(data_loc1, 200) as keep_mock:
+        data_loc1 = hashlib.md5('flush1').hexdigest() + '+6'
+        data_loc2 = hashlib.md5('flush2').hexdigest() + '+6'
+        with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
+            writer = arvados.CollectionWriter(client)
+            with writer.open('flush_test') as out_file:
+                out_file.write('flush1')
                 out_file.flush()
-            out_file.write('flush2')
-            data_loc2 = hashlib.md5('flush2').hexdigest() + '+6'
-        with self.mock_keep(data_loc2, 200) as keep_mock:
+                out_file.write('flush2')
             self.assertEqual(". {} {} 0:12:flush_test\n".format(data_loc1,
                                                                 data_loc2),
                              writer.manifest_text())
@@ -787,15 +786,14 @@ class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
 
     def test_two_opens_two_streams(self):
         client = self.api_client_mock()
-        writer = arvados.CollectionWriter(client)
-        with writer.open('file') as out_file:
-            out_file.write('file')
-            data_loc1 = hashlib.md5('file').hexdigest() + '+4'
-        with self.mock_keep(data_loc1, 200) as keep_mock:
+        data_loc1 = hashlib.md5('file').hexdigest() + '+4'
+        data_loc2 = hashlib.md5('indir').hexdigest() + '+5'
+        with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
+            writer = arvados.CollectionWriter(client)
+            with writer.open('file') as out_file:
+                out_file.write('file')
             with writer.open('./dir', 'indir') as out_file:
                 out_file.write('indir')
-                data_loc2 = hashlib.md5('indir').hexdigest() + '+5'
-        with self.mock_keep(data_loc2, 200) as keep_mock:
             expected = ". {} 0:4:file\n./dir {} 0:5:indir\n".format(
                 data_loc1, data_loc2)
             self.assertEqual(expected, writer.manifest_text())
diff --git a/sdk/python/tests/test_keep_client.py b/sdk/python/tests/test_keep_client.py
index 803bb97..4f710fd 100644
--- a/sdk/python/tests/test_keep_client.py
+++ b/sdk/python/tests/test_keep_client.py
@@ -278,57 +278,57 @@ class KeepClientServiceTestCase(unittest.TestCase):
 
     def test_get_timeout(self):
         api_client = self.mock_keep_services(('keep', 10, False, 'disk'))
-        keep_client = arvados.KeepClient(api_client=api_client)
         force_timeout = [socket.timeout("timed out")]
-        with mock.patch('requests.get', side_effect=force_timeout) as mock_request:
+        with tutil.mock_get(force_timeout) as mock_session:
+            keep_client = arvados.KeepClient(api_client=api_client)
             with self.assertRaises(arvados.errors.KeepReadError):
                 keep_client.get('ffffffffffffffffffffffffffffffff')
-            self.assertTrue(mock_request.called)
+            self.assertTrue(mock_session.return_value.get.called)
             self.assertEqual(
                 arvados.KeepClient.DEFAULT_TIMEOUT,
-                mock_request.call_args[1]['timeout'])
+                mock_session.return_value.get.call_args[1]['timeout'])
 
     def test_put_timeout(self):
         api_client = self.mock_keep_services(('keep', 10, False, 'disk'))
-        keep_client = arvados.KeepClient(api_client=api_client)
         force_timeout = [socket.timeout("timed out")]
-        with mock.patch('requests.put', side_effect=force_timeout) as mock_request:
+        with tutil.mock_put(force_timeout) as mock_session:
+            keep_client = arvados.KeepClient(api_client=api_client)
             with self.assertRaises(arvados.errors.KeepWriteError):
                 keep_client.put('foo')
-            self.assertTrue(mock_request.called)
+            self.assertTrue(mock_session.return_value.put.called)
             self.assertEqual(
                 arvados.KeepClient.DEFAULT_TIMEOUT,
-                mock_request.call_args[1]['timeout'])
+                mock_session.return_value.put.call_args[1]['timeout'])
 
     def test_proxy_get_timeout(self):
         # Force a timeout, verifying that the requests.get or
         # requests.put method was called with the proxy_timeout
         # setting rather than the default timeout.
         api_client = self.mock_keep_services(('keep', 10, False, 'proxy'))
-        keep_client = arvados.KeepClient(api_client=api_client)
         force_timeout = [socket.timeout("timed out")]
-        with mock.patch('requests.get', side_effect=force_timeout) as mock_request:
+        with tutil.mock_get(force_timeout) as mock_session:
+            keep_client = arvados.KeepClient(api_client=api_client)
             with self.assertRaises(arvados.errors.KeepReadError):
                 keep_client.get('ffffffffffffffffffffffffffffffff')
-            self.assertTrue(mock_request.called)
+            self.assertTrue(mock_session.return_value.get.called)
             self.assertEqual(
                 arvados.KeepClient.DEFAULT_PROXY_TIMEOUT,
-                mock_request.call_args[1]['timeout'])
+                mock_session.return_value.get.call_args[1]['timeout'])
 
     def test_proxy_put_timeout(self):
         # Force a timeout, verifying that the requests.get or
         # requests.put method was called with the proxy_timeout
         # setting rather than the default timeout.
         api_client = self.mock_keep_services(('keep', 10, False, 'proxy'))
-        keep_client = arvados.KeepClient(api_client=api_client)
         force_timeout = [socket.timeout("timed out")]
-        with mock.patch('requests.put', side_effect=force_timeout) as mock_request:
+        with tutil.mock_put(force_timeout) as mock_session:
+            keep_client = arvados.KeepClient(api_client=api_client)
             with self.assertRaises(arvados.errors.KeepWriteError):
                 keep_client.put('foo')
-            self.assertTrue(mock_request.called)
+            self.assertTrue(mock_session.return_value.put.called)
             self.assertEqual(
                 arvados.KeepClient.DEFAULT_PROXY_TIMEOUT,
-                mock_request.call_args[1]['timeout'])
+                mock_session.return_value.put.call_args[1]['timeout'])
 
     def test_probe_order_reference_set(self):
         # expected_order[i] is the probe order for
@@ -494,18 +494,13 @@ class KeepClientRetryGetTestCase(KeepClientRetryTestMixin, unittest.TestCase):
             self.check_success(locator=self.HINTED_LOCATOR)
 
     def test_try_next_server_after_timeout(self):
-        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.Session', m):
+        with tutil.mock_get([
+                socket.timeout("timed out"),
+                tutil.fake_requests_response(200, self.DEFAULT_EXPECT)]):
             self.check_success(locator=self.HINTED_LOCATOR)
 
     def test_retry_data_with_wrong_checksum(self):
-        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):
+        with tutil.mock_get((tutil.fake_requests_response(200, s) for s in ['baddata', self.TEST_DATA])):
             self.check_success(locator=self.HINTED_LOCATOR)
 
 
diff --git a/sdk/python/tests/test_stream.py b/sdk/python/tests/test_stream.py
index e246d91..11ee694 100644
--- a/sdk/python/tests/test_stream.py
+++ b/sdk/python/tests/test_stream.py
@@ -184,48 +184,48 @@ class StreamRetryTestMixin(object):
 
     @tutil.skip_sleep
     def test_success_without_retries(self):
-        reader = self.reader_for('bar_file')
         with tutil.mock_get_responses('bar', 200):
+            reader = self.reader_for('bar_file')
             self.assertEqual('bar', self.read_for_test(reader, 3))
 
     @tutil.skip_sleep
     def test_read_no_default_retry(self):
-        reader = self.reader_for('user_agreement')
         with tutil.mock_get_responses('', 500):
+            reader = self.reader_for('user_agreement')
             with self.assertRaises(arvados.errors.KeepReadError):
                 self.read_for_test(reader, 10)
 
     @tutil.skip_sleep
     def test_read_with_instance_retries(self):
-        reader = self.reader_for('foo_file', num_retries=3)
         with tutil.mock_get_responses('foo', 500, 200):
+            reader = self.reader_for('foo_file', num_retries=3)
             self.assertEqual('foo', self.read_for_test(reader, 3))
 
     @tutil.skip_sleep
     def test_read_with_method_retries(self):
-        reader = self.reader_for('foo_file')
         with tutil.mock_get_responses('foo', 500, 200):
+            reader = self.reader_for('foo_file')
             self.assertEqual('foo',
                              self.read_for_test(reader, 3, num_retries=3))
 
     @tutil.skip_sleep
     def test_read_instance_retries_exhausted(self):
-        reader = self.reader_for('bar_file', num_retries=3)
         with tutil.mock_get_responses('bar', 500, 500, 500, 500, 200):
+            reader = self.reader_for('bar_file', num_retries=3)
             with self.assertRaises(arvados.errors.KeepReadError):
                 self.read_for_test(reader, 3)
 
     @tutil.skip_sleep
     def test_read_method_retries_exhausted(self):
-        reader = self.reader_for('bar_file')
         with tutil.mock_get_responses('bar', 500, 500, 500, 500, 200):
+            reader = self.reader_for('bar_file')
             with self.assertRaises(arvados.errors.KeepReadError):
                 self.read_for_test(reader, 3, num_retries=3)
 
     @tutil.skip_sleep
     def test_method_retries_take_precedence(self):
-        reader = self.reader_for('user_agreement', num_retries=10)
         with tutil.mock_get_responses('', 500, 500, 500, 200):
+            reader = self.reader_for('user_agreement', num_retries=10)
             with self.assertRaises(arvados.errors.KeepReadError):
                 self.read_for_test(reader, 10, num_retries=1)
 

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


hooks/post-receive
-- 




More information about the arvados-commits mailing list