def mock_api_error(job_id, run_id): if counter['failures'] < num_failures: counter['failures'] += 1 if failure_is_error: raise CivisAPIError(mock.MagicMock()) else: return response.Response({'id': run_id, 'container_id': job_id, 'state': 'failed'}) else: return response.Response({'id': run_id, 'container_id': job_id, 'state': 'succeeded'})
def _setup_client_mock(job_id=-10, run_id=100, n_failures=8, failure_is_error=False): """Return a Mock set up for use in testing container scripts Parameters ---------- job_id: int Mock-create containers with this ID when calling `post_containers` or `post_containers_runs`. run_id: int Mock-create runs with this ID when calling `post_containers_runs`. n_failures: int When calling `get_containers_runs`, fail this many times before succeeding. failure_is_error: bool If True, "failure" means raising a `CivisAPIError`. Returns ------- `unittest.mock.Mock` With `post_containers`, `post_containers_runs`, and `get_containers_runs` methods set up. """ c = mock.Mock() c.__class__ = APIClient mock_container = response.Response({'id': job_id}) c.scripts.post_containers.return_value = mock_container c.scripts.post_custom.return_value = mock_container mock_container_run = response.Response({ 'id': run_id, 'container_id': job_id, 'state': 'queued' }) c.scripts.post_containers_runs.return_value = mock_container_run c.jobs.post_runs.return_value = mock_container_run c.scripts.get_containers_runs.side_effect = _make_error_func( n_failures, failure_is_error) def change_state_to_cancelled(job_id): mock_container_run.state = "cancelled" return mock_container_run c.scripts.post_cancel.side_effect = change_state_to_cancelled del c.channels # Remove "channels" endpoint to fall back on polling return c
def test_get_service__not_found(mock_client, classes_mock): classes_mock.return_value = {} sc = ServiceClient(MOCK_SERVICE_ID) err_resp = response.Response({ 'status_code': 404, 'error': 'not_found', 'errorDescription': 'The requested resource could not be found.', 'content': True}) err_resp.json = lambda: err_resp.json_data mock_client.return_value.services.get.side_effect = CivisAPIError(err_resp) with pytest.raises(CivisAPIError) as excinfo: _get_service(sc) expected_error = ('(404) The requested resource could not be found.') assert str(excinfo.value) == expected_error
def test_cancel_finished_job(): # If we try to cancel a completed job, we get a 404 error. # That shouldn't be sent to the user. # Set up a mock client which will give an exception when # you try to cancel any job. c = _setup_client_mock() err_resp = response.Response({ 'status_code': 404, 'error': 'not_found', 'errorDescription': 'The requested resource could not be found.', 'content': True}) err_resp.json = lambda: err_resp.json_data c.scripts.post_cancel.side_effect = CivisAPIError(err_resp) c.scripts.post_containers_runs.return_value.state = 'running' fut = ContainerFuture(-10, 100, polling_interval=1, client=c, poll_on_creation=False) assert not fut.done() assert fut.cancel() is False