def test_command_execution_cleanup_exception(self): '''Command execution through Docker, catch exception during cleanup''' response = Mock(status_code=500, reason='Because') self.mocks.configure_mock( 'docker_client', { 'exec_create.return_value': { 'Id': b'container-foobar' }, 'exec_start.return_value': b'This is the return', 'exec_inspect.return_value': { 'ExitCode': 0 }, 'kill.side_effect': DockerAPIError('Failure killing container', response, 'Failure killing container') }) build_env = DockerEnvironment(version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}) with build_env: build_env.run('echo', 'test', cwd='/tmp') self.mocks.docker_client.kill.assert_called_with( 'build-123-project-6-pip') self.assertTrue(build_env.successful) self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
def test_container_timeout(self): '''Docker container timeout and command failure''' response = Mock(status_code=404, reason='Container not found') self.mocks.configure_mock('docker_client', { 'inspect_container.side_effect': [ DockerAPIError( 'No container found', response, 'No container found', ), {'State': {'Running': False, 'ExitCode': 42}}, ], 'exec_create.return_value': {'Id': 'container-foobar'}, 'exec_start.return_value': 'This is the return', 'exec_inspect.return_value': {'ExitCode': 0}, }) build_env = DockerEnvironment(version=self.version, project=self.project, build={}) with build_env: build_env.run('echo', 'test', cwd='/tmp') self.assertEqual( str(build_env.failure), 'Build exited due to time out') self.assertEqual(self.mocks.docker_client.exec_create.call_count, 1) self.assertTrue(build_env.failed)
def test_api_failure_on_docker_memory_limit(self): """Docker exec_create raised memory issue on `exec`""" response = Mock(status_code=500, reason='Internal Server Error') self.mocks.configure_mock( 'docker_client', { 'exec_create.side_effect': DockerAPIError('Failure creating container', response, 'Failure creating container'), }) build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) with build_env: build_env.run('echo test', cwd='/tmp') self.assertEqual(build_env.commands[0].exit_code, -1) self.assertEqual(build_env.commands[0].error, None) self.assertTrue(build_env.failed) # api() is not called anymore, we use api_v2 instead self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called) self.mocks.mocks['api_v2.build']().put.assert_called_with({ 'id': DUMMY_BUILD_ID, 'version': self.version.pk, 'success': False, 'project': self.project.pk, 'setup_error': u'', 'exit_code': -1, 'length': mock.ANY, 'error': '', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_api_failure(self): """Failing API error response from docker should raise exception.""" response = Mock(status_code=500, reason='Because') self.mocks.configure_mock( 'docker_client', { 'create_container.side_effect': DockerAPIError('Failure creating container', response, 'Failure creating container') }) build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) def _inner(): with build_env: self.fail('Should not hit this') self.assertRaises(BuildEnvironmentError, _inner) # api() is not called anymore, we use api_v2 instead self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called) self.mocks.mocks['api_v2.build']().put.assert_called_with({ 'id': DUMMY_BUILD_ID, 'version': self.version.pk, 'success': False, 'project': self.project.pk, 'setup_error': u'', 'exit_code': 1, 'length': mock.ANY, 'error': 'Build environment creation failed', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_container_timeout(self): """Docker container timeout and command failure.""" response = Mock(status_code=404, reason='Container not found') self.mocks.configure_mock( 'docker_client', { 'inspect_container.side_effect': [ DockerAPIError( 'No container found', response, 'No container found', ), {'State': {'Running': False, 'ExitCode': 42}}, ], 'exec_create.return_value': {'Id': b'container-foobar'}, 'exec_start.return_value': b'This is the return', 'exec_inspect.return_value': {'ExitCode': 0}, }) build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) with build_env: build_env.run('echo', 'test', cwd='/tmp') self.assertEqual(str(build_env.failure), 'Build exited due to time out') self.assertEqual(self.mocks.docker_client.exec_create.call_count, 1) self.assertTrue(build_env.failed) # api() is not called anymore, we use api_v2 instead self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called) self.mocks.mocks['api_v2.build']().put.assert_called_with({ 'id': DUMMY_BUILD_ID, 'version': self.version.pk, 'success': False, 'project': self.project.pk, 'setup_error': u'', 'exit_code': 1, 'length': 0, 'error': 'Build exited due to time out', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_api_failure(self): '''Failing API error response from docker should raise exception''' response = Mock(status_code=500, reason='Because') self.mocks.configure_mock('docker_client', { 'create_container.side_effect': DockerAPIError( 'Failure creating container', response, 'Failure creating container' ) }) build_env = DockerEnvironment(version=self.version, project=self.project, build={}) def _inner(): with build_env: self.fail('Should not hit this') self.assertRaises(BuildEnvironmentError, _inner)
def test_command_execution_cleanup_exception(self): """Command execution through Docker, catch exception during cleanup.""" response = Mock(status_code=500, reason='Because') self.mocks.configure_mock( 'docker_client', { 'exec_create.return_value': {'Id': b'container-foobar'}, 'exec_start.return_value': b'This is the return', 'exec_inspect.return_value': {'ExitCode': 0}, 'kill.side_effect': DockerAPIError( 'Failure killing container', response, 'Failure killing container', ) }) build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) with build_env: build_env.run('echo', 'test', cwd='/tmp') self.mocks.docker_client.kill.assert_called_with( 'build-123-project-6-pip') self.assertTrue(build_env.successful) # api() is not called anymore, we use api_v2 instead self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called) self.mocks.mocks['api_v2.build']().put.assert_called_with({ 'id': DUMMY_BUILD_ID, 'version': self.version.pk, 'error': '', 'success': True, 'project': self.project.pk, 'setup_error': u'', 'exit_code': 0, 'length': 0, 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
run_req={}, run_timeout=0.1, retry_count=0) self.assertEqual(e.exception.errno, my_socket_error.errno) # This should be the last subTest, because this will the behavior of # change mock_remove_ctn with self.subTest( case="Docker ping timeout with InvalidPingResponse and " "remove container failed with APIError"): invalid_ping_resp_msg = "my custom invalid ping response exception" mock_ctn_request.side_effect = ( InvalidPingResponse(invalid_ping_resp_msg)) mock_remove_ctn.reset_mock() from django.http import HttpResponse fake_response_content = "this should not appear" mock_remove_ctn.side_effect = DockerAPIError( message="my custom docker api error", response=HttpResponse(content=fake_response_content)) # force timeout with mock.patch("course.page.code.DOCKER_TIMEOUT", 0.0001): res = request_python_run_with_retries( run_req={}, run_timeout=0.1, retry_count=0) self.assertEqual(res["result"], "uncaught_error") self.assertEqual(res['message'], "Timeout waiting for container.") self.assertEqual(res["exec_host"], fake_host_ip) self.assertIn(InvalidPingResponse.__name__, res["traceback"]) self.assertIn(invalid_ping_resp_msg, res["traceback"]) # No need to bother the students with this nonsense. self.assertNotIn(DockerAPIError.__name__, res["traceback"])