3 from __future__ import absolute_import
4 from builtins import object
9 from . import run_test_server
12 from . import arvados_testutil as tutil
13 from apiclient import http as apiclient_http
17 class ApiClientRetryTestMixin(object):
19 TEST_UUID = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
20 TEST_LOCATOR = 'd41d8cd98f00b204e9800998ecf8427e+0'
27 # Patch arvados.api() to return our mock API, so we can mock
29 self.api_client = arvados.api('v1', cache=False)
30 self.api_patch = mock.patch('arvados.api', return_value=self.api_client)
31 self.api_patch.start()
37 raise NotImplementedError("test subclasses must define run_method")
39 def test_immediate_success(self):
40 with tutil.mock_api_responses(self.api_client, '{}', [200]):
43 def test_immediate_failure(self):
44 with tutil.mock_api_responses(self.api_client, '{}', [400]), self.assertRaises(self.DEFAULT_EXCEPTION):
47 def test_retry_then_success(self):
48 with tutil.mock_api_responses(self.api_client, '{}', [500, 200]):
51 def test_error_after_default_retries_exhausted(self):
52 with tutil.mock_api_responses(self.api_client, '{}', [500, 500, 500, 500, 500, 500, 200]), self.assertRaises(self.DEFAULT_EXCEPTION):
55 def test_no_retry_after_immediate_success(self):
56 with tutil.mock_api_responses(self.api_client, '{}', [200, 400]):
60 class CurrentJobTestCase(ApiClientRetryTestMixin, unittest.TestCase):
62 DEFAULT_EXCEPTION = arvados.errors.ApiError
65 super(CurrentJobTestCase, self).setUp()
66 os.environ['JOB_UUID'] = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
67 os.environ['JOB_WORK'] = '.'
70 del os.environ['JOB_UUID']
71 del os.environ['JOB_WORK']
72 arvados._current_job = None
73 super(CurrentJobTestCase, self).tearDown()
79 class CurrentTaskTestCase(ApiClientRetryTestMixin, unittest.TestCase):
81 DEFAULT_EXCEPTION = arvados.errors.ApiError
84 super(CurrentTaskTestCase, self).setUp()
85 os.environ['TASK_UUID'] = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
86 os.environ['TASK_WORK'] = '.'
89 del os.environ['TASK_UUID']
90 del os.environ['TASK_WORK']
91 arvados._current_task = None
92 super(CurrentTaskTestCase, self).tearDown()
95 arvados.current_task()
98 class TaskSetOutputTestCase(CurrentTaskTestCase, unittest.TestCase):
100 DEFAULT_EXCEPTION = arvados.errors.ApiError
103 super(TaskSetOutputTestCase, self).tearDown()
104 run_test_server.reset()
106 def run_method(self, locator=ApiClientRetryTestMixin.TEST_LOCATOR):
107 arvados.task_set_output({'uuid':self.TEST_UUID},s=locator)