[ARVADOS] updated: cbf80c08daa5f9099d0821603a128967254709ed

git at public.curoverse.com git at public.curoverse.com
Thu Feb 5 12:13:13 EST 2015


Summary of changes:
 sdk/python/tests/arvados_testutil.py | 35 +++++++++++++++++++
 sdk/python/tests/test_collections.py | 52 +++++------------------------
 sdk/python/tests/test_keep_client.py | 65 +++++++++++++-----------------------
 3 files changed, 67 insertions(+), 85 deletions(-)

       via  cbf80c08daa5f9099d0821603a128967254709ed (commit)
      from  56ad30382d7d4e11cb0160c5f2e30077e1f41c8b (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 cbf80c08daa5f9099d0821603a128967254709ed
Author: Tom Clegg <tom at curoverse.com>
Date:   Thu Feb 5 12:05:29 2015 -0500

    5011: Dry up three different solutions for mocking keep_services().accessible().

diff --git a/sdk/python/tests/arvados_testutil.py b/sdk/python/tests/arvados_testutil.py
index 04ca6b5..124fb35 100644
--- a/sdk/python/tests/arvados_testutil.py
+++ b/sdk/python/tests/arvados_testutil.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import arvados
 import errno
 import hashlib
 import httplib
@@ -66,6 +67,40 @@ class MockStreamReader(object):
         return self._data[start:start + size]
 
 
+class ApiClientMock(object):
+    def api_client_mock(self):
+        return mock.MagicMock(name='api_client_mock')
+
+    def mock_keep_services(self, api_mock=None, status=200, count=12,
+                           service_type='disk',
+                           service_host=None,
+                           service_port=None,
+                           service_ssl_flag=False):
+        if api_mock is None:
+            api_mock = self.api_client_mock()
+        body = {
+            'items_available': count,
+            'items': [{
+                'uuid': 'zzzzz-bi6l4-{:015x}'.format(i),
+                'owner_uuid': 'zzzzz-tpzed-000000000000000',
+                'service_host': service_host or 'keep0x{:x}'.format(i),
+                'service_port': service_port or 65535-i,
+                'service_ssl_flag': service_ssl_flag,
+                'service_type': service_type,
+            } for i in range(0, count)]
+        }
+        self._mock_api_call(api_mock.keep_services().accessible, status, body)
+        return api_mock
+
+    def _mock_api_call(self, mock_method, code, body):
+        mock_method = mock_method().execute
+        if code == 200:
+            mock_method.return_value = body
+        else:
+            mock_method.side_effect = arvados.errors.ApiError(
+                fake_httplib2_response(code), "{}")
+
+
 class ArvadosBaseTestCase(unittest.TestCase):
     # This class provides common utility functions for our tests.
 
diff --git a/sdk/python/tests/test_collections.py b/sdk/python/tests/test_collections.py
index d47d318..4ca8dfe 100644
--- a/sdk/python/tests/test_collections.py
+++ b/sdk/python/tests/test_collections.py
@@ -500,17 +500,7 @@ class ArvadosCollectionsTest(run_test_server.TestCaseWithServers,
                 ).manifest_text())
 
 
-class CollectionTestMixin(object):
-    PROXY_RESPONSE = {
-        'items_available': 1,
-        'items': [{
-                'uuid': 'zzzzz-bi6l4-mockproxy012345',
-                'owner_uuid': 'zzzzz-tpzed-mockowner012345',
-                'service_host': tutil.TEST_HOST,
-                'service_port': 65535,
-                'service_ssl_flag': True,
-                'service_type': 'proxy',
-                }]}
+class CollectionTestMixin(tutil.ApiClientMock):
     API_COLLECTIONS = run_test_server.fixture('collections')
     DEFAULT_COLLECTION = API_COLLECTIONS['foo_file']
     DEFAULT_DATA_HASH = DEFAULT_COLLECTION['portable_data_hash']
@@ -520,35 +510,11 @@ class CollectionTestMixin(object):
     ALT_DATA_HASH = ALT_COLLECTION['portable_data_hash']
     ALT_MANIFEST = ALT_COLLECTION['manifest_text']
 
-    def _mock_api_call(self, mock_method, code, body):
-        mock_method = mock_method().execute
-        if code == 200:
-            mock_method.return_value = body
-        else:
-            mock_method.side_effect = arvados.errors.ApiError(
-                tutil.fake_httplib2_response(code), "{}")
-
-    def mock_keep_services(self, api_mock, code, body):
-        self._mock_api_call(api_mock.keep_services().accessible, code, body)
-
-    def api_client_mock(self, code=200):
-        client = mock.MagicMock(name='api_client')
-        self.mock_keep_services(client, code, self.PROXY_RESPONSE)
+    def api_client_mock(self, status=200):
+        client = super(CollectionTestMixin, self).api_client_mock()
+        self.mock_keep_services(client, status=status, service_type='proxy', count=1)
         return client
 
-    def disk_services_available(self, number_of_disks):
-        return {
-            'items_available': number_of_disks,
-            'items': [{
-                'uuid': 'zzzzz-bi6l4-mockdisk%07d' % i,
-                'owner_uuid': 'zzzzz-tpzed-000000000000000',
-                'service_host': tutil.TEST_HOST,
-                'service_port': 65535-i,
-                'service_ssl_flag': True,
-                'service_type': 'disk',
-            } for i in range(0, number_of_disks)]
-        }
-
 
 @tutil.skip_sleep
 class CollectionReaderTestCase(unittest.TestCase, CollectionTestMixin):
@@ -556,9 +522,9 @@ class CollectionReaderTestCase(unittest.TestCase, CollectionTestMixin):
         body = self.API_COLLECTIONS.get(body)
         self._mock_api_call(api_mock.collections().get, code, body)
 
-    def api_client_mock(self, code=200):
-        client = super(CollectionReaderTestCase, self).api_client_mock(code)
-        self.mock_get_collection(client, code, 'foo_file')
+    def api_client_mock(self, status=200):
+        client = super(CollectionReaderTestCase, self).api_client_mock()
+        self.mock_get_collection(client, status, 'foo_file')
         return client
 
     def test_init_no_default_retries(self):
@@ -741,7 +707,7 @@ class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
 
     def test_write_insufficient_replicas_via_disks(self):
         client = mock.MagicMock(name='api_client')
-        self.mock_keep_services(client, 200, self.disk_services_available(2))
+        self.mock_keep_services(client, status=200, service_type='disk', count=2)
         writer = self.foo_writer(api_client=client, replication=3)
         with self.mock_keep(
                 None, 200, 200,
@@ -751,7 +717,7 @@ class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
 
     def test_write_three_replicas(self):
         client = mock.MagicMock(name='api_client')
-        self.mock_keep_services(client, 200, self.disk_services_available(6))
+        self.mock_keep_services(client, status=200, service_type='disk', count=6)
         writer = self.foo_writer(api_client=client, replication=3)
         with self.mock_keep(
                 None, 200, 500, 200, 500, 200, 200,
diff --git a/sdk/python/tests/test_keep_client.py b/sdk/python/tests/test_keep_client.py
index 37f274a..3a2aaf2 100644
--- a/sdk/python/tests/test_keep_client.py
+++ b/sdk/python/tests/test_keep_client.py
@@ -1,6 +1,7 @@
 import hashlib
 import mock
 import os
+import random
 import re
 import socket
 import unittest
@@ -231,44 +232,22 @@ class KeepProxyTestCase(run_test_server.TestCaseWithServers):
         self.assertTrue(keep_client.using_proxy)
 
 
-class KeepClientServiceTestCase(unittest.TestCase):
-    def mock_keep_services(self, *services):
-        api_client = mock.MagicMock(name='api_client')
-        api_client.keep_services().accessible().execute.return_value = {
-            'items_available': len(services),
-            'items': [{
-                    'uuid': 'zzzzz-bi6l4-{:015x}'.format(index),
-                    'owner_uuid': 'zzzzz-tpzed-000000000000000',
-                    'service_host': host,
-                    'service_port': port,
-                    'service_ssl_flag': ssl,
-                    'service_type': servtype,
-                    } for index, (host, port, ssl, servtype)
-                      in enumerate(services)],
-            }
-        return api_client
-
-    def mock_n_keep_disks(self, service_count):
-        return self.mock_keep_services(
-            *[("keep0x{:x}".format(index), 80, False, 'disk')
-              for index in range(service_count)])
-
-    def get_service_roots(self, *services):
-        api_client = self.mock_keep_services(*services)
+class KeepClientServiceTestCase(unittest.TestCase, tutil.ApiClientMock):
+    def get_service_roots(self, api_client):
         keep_client = arvados.KeepClient(api_client=api_client)
         services = keep_client.weighted_service_roots('000000')
         return [urlparse.urlparse(url) for url in sorted(services)]
 
     def test_ssl_flag_respected_in_roots(self):
-        services = self.get_service_roots(('keep', 10, False, 'disk'),
-                                          ('keep', 20, True, 'disk'))
-        self.assertEqual(10, services[0].port)
-        self.assertEqual('http', services[0].scheme)
-        self.assertEqual(20, services[1].port)
-        self.assertEqual('https', services[1].scheme)
+        for ssl_flag in [False, True]:
+            services = self.get_service_roots(self.mock_keep_services(
+                service_ssl_flag=ssl_flag))
+            self.assertEqual(
+                ('https' if ssl_flag else 'http'), services[0].scheme)
 
     def test_correct_ports_with_ipv6_addresses(self):
-        service = self.get_service_roots(('100::1', 10, True, 'proxy'))[0]
+        service = self.get_service_roots(self.mock_keep_services(
+            service_type='proxy', service_host='100::1', service_port=10, count=1))[0]
         self.assertEqual('100::1', service.hostname)
         self.assertEqual(10, service.port)
 
@@ -277,7 +256,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
     # when connected directly to a Keep server (i.e. non-proxy timeout)
 
     def test_get_timeout(self):
-        api_client = self.mock_keep_services(('keep', 10, False, 'disk'))
+        api_client = self.mock_keep_services(count=1)
         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:
@@ -289,7 +268,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
                 mock_request.call_args[1]['timeout'])
 
     def test_put_timeout(self):
-        api_client = self.mock_keep_services(('keep', 10, False, 'disk'))
+        api_client = self.mock_keep_services(count=1)
         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:
@@ -304,7 +283,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
         # 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'))
+        api_client = self.mock_keep_services(service_type='proxy', count=1)
         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:
@@ -319,7 +298,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
         # 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'))
+        api_client = self.mock_keep_services(service_type='proxy', count=1)
         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:
@@ -346,7 +325,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
         hashes = [
             hashlib.md5("{:064x}".format(x)).hexdigest()
             for x in range(len(expected_order))]
-        api_client = self.mock_n_keep_disks(16)
+        api_client = self.mock_keep_services(count=16)
         keep_client = arvados.KeepClient(api_client=api_client)
         for i, hash in enumerate(hashes):
             roots = keep_client.weighted_service_roots(hash)
@@ -359,12 +338,12 @@ class KeepClientServiceTestCase(unittest.TestCase):
         hashes = [
             hashlib.md5("{:064x}".format(x)).hexdigest() for x in range(100)]
         initial_services = 12
-        api_client = self.mock_n_keep_disks(initial_services)
+        api_client = self.mock_keep_services(count=initial_services)
         keep_client = arvados.KeepClient(api_client=api_client)
         probes_before = [
             keep_client.weighted_service_roots(hash) for hash in hashes]
         for added_services in range(1, 12):
-            api_client = self.mock_n_keep_disks(initial_services+added_services)
+            api_client = self.mock_keep_services(count=initial_services+added_services)
             keep_client = arvados.KeepClient(api_client=api_client)
             total_penalty = 0
             for hash_index in range(len(hashes)):
@@ -398,7 +377,9 @@ class KeepClientServiceTestCase(unittest.TestCase):
         data = '0' * 64
         if verb == 'get':
             data = hashlib.md5(data).hexdigest() + '+1234'
-        api_client = self.mock_n_keep_disks(16)
+        # Arbitrary port number:
+        aport = random.randint(1024,65535)
+        api_client = self.mock_keep_services(service_port=aport, count=16)
         keep_client = arvados.KeepClient(api_client=api_client)
         with mock.patch('requests.' + verb,
                         side_effect=socket.timeout) as req_mock, \
@@ -406,7 +387,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
             getattr(keep_client, verb)(data)
         urls = [urlparse.urlparse(url)
                 for url in err_check.exception.service_errors()]
-        self.assertEqual([('keep0x' + c, 80) for c in '3eab2d5fc9681074'],
+        self.assertEqual([('keep0x' + c, aport) for c in '3eab2d5fc9681074'],
                          [(url.hostname, url.port) for url in urls])
 
     def test_get_error_shows_probe_order(self):
@@ -431,7 +412,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
         self.check_no_services_error('put', arvados.errors.KeepWriteError)
 
     def check_errors_from_last_retry(self, verb, exc_class):
-        api_client = self.mock_n_keep_disks(2)
+        api_client = self.mock_keep_services(count=2)
         keep_client = arvados.KeepClient(api_client=api_client)
         req_mock = getattr(tutil, 'mock_{}_responses'.format(verb))(
             "retry error reporting test", 500, 500, 403, 403)
@@ -452,7 +433,7 @@ class KeepClientServiceTestCase(unittest.TestCase):
     def test_put_error_does_not_include_successful_puts(self):
         data = 'partial failure test'
         data_loc = '{}+{}'.format(hashlib.md5(data).hexdigest(), len(data))
-        api_client = self.mock_n_keep_disks(3)
+        api_client = self.mock_keep_services(count=3)
         keep_client = arvados.KeepClient(api_client=api_client)
         with tutil.mock_put_responses(data_loc, 200, 500, 500) as req_mock, \
                 self.assertRaises(arvados.errors.KeepWriteError) as exc_check:

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


hooks/post-receive
-- 




More information about the arvados-commits mailing list