[ARVADOS] updated: 670bb19401f5495cf553a725616a4ead5ea3046b
git at public.curoverse.com
git at public.curoverse.com
Thu Oct 22 14:45:23 EDT 2015
Summary of changes:
sdk/python/arvados/__init__.py | 23 +++-------
sdk/python/tests/arvados_testutil.py | 4 ++
sdk/python/tests/test_retry_job_helpers.py | 69 +++++++++++++++---------------
3 files changed, 44 insertions(+), 52 deletions(-)
via 670bb19401f5495cf553a725616a4ead5ea3046b (commit)
from 4b585dc257f9a02492fd66a5322144864d06a1ec (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 670bb19401f5495cf553a725616a4ead5ea3046b
Author: Tom Clegg <tom at curoverse.com>
Date: Thu Oct 22 14:44:14 2015 -0400
6600: Mock arvados.api()
diff --git a/sdk/python/arvados/__init__.py b/sdk/python/arvados/__init__.py
index 3c4c93c..b74f828 100644
--- a/sdk/python/arvados/__init__.py
+++ b/sdk/python/arvados/__init__.py
@@ -38,14 +38,10 @@ logger.addHandler(log_handler)
logger.setLevel(logging.DEBUG if config.get('ARVADOS_DEBUG')
else logging.WARNING)
-def task_set_output(self,s,api_client=None,num_retries=5):
-
- if not api_client:
- api_client = api('v1')
-
+def task_set_output(self, s, num_retries=5):
for tries_left in RetryLoop(num_retries=num_retries, backoff_start=0):
try:
- return api_client.job_tasks().update(
+ return api('v1').job_tasks().update(
uuid=self['uuid'],
body={
'output':s,
@@ -59,18 +55,14 @@ def task_set_output(self,s,api_client=None,num_retries=5):
raise
_current_task = None
-def current_task(api_client=None, num_retries=5):
-
+def current_task(num_retries=5):
global _current_task
if _current_task:
return _current_task
- if not api_client:
- api_client = api('v1')
-
for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
try:
- task = api_client.job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
+ task = api('v1').job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
task = UserDict.UserDict(task)
task.set_output = types.MethodType(task_set_output, task)
task.tmpdir = os.environ['TASK_WORK']
@@ -83,17 +75,14 @@ def current_task(api_client=None, num_retries=5):
raise
_current_job = None
-def current_job(api_client=None, num_retries=5):
+def current_job(num_retries=5):
global _current_job
if _current_job:
return _current_job
- if not api_client:
- api_client = api('v1')
-
for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
try:
- job = api_client.jobs().get(uuid=os.environ['JOB_UUID']).execute()
+ job = api('v1').jobs().get(uuid=os.environ['JOB_UUID']).execute()
job = UserDict.UserDict(job)
job.tmpdir = os.environ['JOB_WORK']
_current_job = job
diff --git a/sdk/python/tests/arvados_testutil.py b/sdk/python/tests/arvados_testutil.py
index 6e2a078..ea318c6 100644
--- a/sdk/python/tests/arvados_testutil.py
+++ b/sdk/python/tests/arvados_testutil.py
@@ -43,6 +43,10 @@ def mock_responses(body, *codes, **headers):
return mock.patch('httplib2.Http.request', side_effect=queue_with((
(fake_httplib2_response(code, **headers), body) for code in codes)))
+def mock_api_responses(api_client, body, codes, headers={}):
+ return mock.patch.object(api_client._http, 'request', side_effect=queue_with((
+ (fake_httplib2_response(code, **headers), body) for code in codes)))
+
class FakeCurl:
@classmethod
diff --git a/sdk/python/tests/test_retry_job_helpers.py b/sdk/python/tests/test_retry_job_helpers.py
index 9d4d9ba..6e562a0 100644
--- a/sdk/python/tests/test_retry_job_helpers.py
+++ b/sdk/python/tests/test_retry_job_helpers.py
@@ -21,66 +21,54 @@ class ApiClientRetryTestMixin(object):
def setUpClass(cls):
run_test_server.run()
+ def setUp(self):
+ # Patch arvados.api() to return our mock API, so we can mock
+ # its http requests.
+ self.api_client = arvados.api('v1', cache=False)
+ self.api_patch = mock.patch('arvados.api', return_value=self.api_client)
+ self.api_patch.start()
+
def tearDown(self):
- run_test_server.reset()
+ self.api_patch.stop()
def run_method(self):
raise NotImplementedError("test subclasses must define run_method")
-# def check_success(self, expected=None, *args, **kwargs):
-# try:
-# self.run_method()
-# except: # This seems really iffy, because random exceptions can occur.
-# self.assertTrue(False)
-
-# def check_exception(self, error_class=None, *args, **kwargs):
-# if error_class is None:
-# error_class = self.DEFAULT_EXCEPTION
-# self.assertRaises(error_class, self.run_method)
-
def test_immediate_success(self):
- with tutil.mock_responses('{}', 200):
+ with tutil.mock_api_responses(self.api_client, '{}', [200]):
self.run_method()
def test_immediate_failure(self):
- with tutil.mock_responses('{}', 400), self.assertRaises(self.DEFAULT_EXCEPTION):
+ with tutil.mock_api_responses(self.api_client, '{}', [400]), self.assertRaises(self.DEFAULT_EXCEPTION):
self.run_method()
def test_retry_then_success(self):
- with tutil.mock_responses('{}', 500, 200):
+ with tutil.mock_api_responses(self.api_client, '{}', [500, 200]):
self.run_method()
def test_error_after_default_retries_exhausted(self):
- with tutil.mock_responses('{}', 500, 500, 500, 500, 500, 500, 200), self.assertRaises(self.DEFAULT_EXCEPTION):
+ with tutil.mock_api_responses(self.api_client, '{}', [500, 500, 500, 500, 500, 500, 200]), self.assertRaises(self.DEFAULT_EXCEPTION):
self.run_method()
def test_no_retry_after_immediate_success(self):
- with tutil.mock_responses('{}', 200, 400):
+ with tutil.mock_api_responses(self.api_client, '{}', [200, 400]):
self.run_method()
-class TaskSetOutputTestCase(ApiClientRetryTestMixin, unittest.TestCase):
-
- DEFAULT_EXCEPTION = arvados.errors.ApiError
-
- def run_method(self, locator=ApiClientRetryTestMixin.TEST_LOCATOR):
- arvados.task_set_output({'uuid':self.TEST_UUID},s=locator)
-
-
class CurrentJobTestCase(ApiClientRetryTestMixin, unittest.TestCase):
DEFAULT_EXCEPTION = arvados.errors.ApiError
def setUp(self):
+ super(CurrentJobTestCase, self).setUp()
os.environ['JOB_UUID'] = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
os.environ['JOB_WORK'] = '.'
def tearDown(self):
- super(CurrentJobTestCase,self).tearDown()
- try:
- arvados._current_job = None
- except:
- raise
+ del os.environ['JOB_UUID']
+ del os.environ['JOB_WORK']
+ arvados._current_job = None
+ super(CurrentJobTestCase, self).tearDown()
def run_method(self):
arvados.current_job()
@@ -91,16 +79,27 @@ class CurrentTaskTestCase(ApiClientRetryTestMixin, unittest.TestCase):
DEFAULT_EXCEPTION = arvados.errors.ApiError
def setUp(self):
+ super(CurrentTaskTestCase, self).setUp()
os.environ['TASK_UUID'] = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
os.environ['TASK_WORK'] = '.'
def tearDown(self):
- super(CurrentTaskTestCase,self).tearDown()
- try:
- arvados._current_task = None
- except:
- raise
+ del os.environ['TASK_UUID']
+ del os.environ['TASK_WORK']
+ arvados._current_task = None
+ super(CurrentTaskTestCase, self).tearDown()
def run_method(self):
arvados.current_task()
+
+class TaskSetOutputTestCase(CurrentTaskTestCase, unittest.TestCase):
+
+ DEFAULT_EXCEPTION = arvados.errors.ApiError
+
+ def tearDown(self):
+ super(TaskSetOutputTestCase, self).tearDown()
+ run_test_server.reset()
+
+ def run_method(self, locator=ApiClientRetryTestMixin.TEST_LOCATOR):
+ arvados.task_set_output({'uuid':self.TEST_UUID},s=locator)
-----------------------------------------------------------------------
hooks/post-receive
--
More information about the arvados-commits
mailing list