1 from __future__ import absolute_import
2 from builtins import object
7 from . import run_test_server
10 from . import arvados_testutil as tutil
11 from apiclient import http as apiclient_http
15 class ApiClientRetryTestMixin(object):
17 TEST_UUID = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
18 TEST_LOCATOR = 'd41d8cd98f00b204e9800998ecf8427e+0'
25 # Patch arvados.api() to return our mock API, so we can mock
27 self.api_client = arvados.api('v1', cache=False)
28 self.api_patch = mock.patch('arvados.api', return_value=self.api_client)
29 self.api_patch.start()
35 raise NotImplementedError("test subclasses must define run_method")
37 def test_immediate_success(self):
38 with tutil.mock_api_responses(self.api_client, '{}', [200]):
41 def test_immediate_failure(self):
42 with tutil.mock_api_responses(self.api_client, '{}', [400]), self.assertRaises(self.DEFAULT_EXCEPTION):
45 def test_retry_then_success(self):
46 with tutil.mock_api_responses(self.api_client, '{}', [500, 200]):
49 def test_error_after_default_retries_exhausted(self):
50 with tutil.mock_api_responses(self.api_client, '{}', [500, 500, 500, 500, 500, 500, 200]), self.assertRaises(self.DEFAULT_EXCEPTION):
53 def test_no_retry_after_immediate_success(self):
54 with tutil.mock_api_responses(self.api_client, '{}', [200, 400]):
58 class CurrentJobTestCase(ApiClientRetryTestMixin, unittest.TestCase):
60 DEFAULT_EXCEPTION = arvados.errors.ApiError
63 super(CurrentJobTestCase, self).setUp()
64 os.environ['JOB_UUID'] = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
65 os.environ['JOB_WORK'] = '.'
68 del os.environ['JOB_UUID']
69 del os.environ['JOB_WORK']
70 arvados._current_job = None
71 super(CurrentJobTestCase, self).tearDown()
77 class CurrentTaskTestCase(ApiClientRetryTestMixin, unittest.TestCase):
79 DEFAULT_EXCEPTION = arvados.errors.ApiError
82 super(CurrentTaskTestCase, self).setUp()
83 os.environ['TASK_UUID'] = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
84 os.environ['TASK_WORK'] = '.'
87 del os.environ['TASK_UUID']
88 del os.environ['TASK_WORK']
89 arvados._current_task = None
90 super(CurrentTaskTestCase, self).tearDown()
93 arvados.current_task()
96 class TaskSetOutputTestCase(CurrentTaskTestCase, unittest.TestCase):
98 DEFAULT_EXCEPTION = arvados.errors.ApiError
101 super(TaskSetOutputTestCase, self).tearDown()
102 run_test_server.reset()
104 def run_method(self, locator=ApiClientRetryTestMixin.TEST_LOCATOR):
105 arvados.task_set_output({'uuid':self.TEST_UUID},s=locator)