def test_command_execution(self): """Command execution through Docker.""" 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': 1 }, }) 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.exec_create.assert_called_with( container='build-123-project-6-pip', cmd="/bin/sh -c 'cd /tmp && echo\\ test'", stderr=True, stdout=True) self.assertEqual(build_env.commands[0].exit_code, 1) self.assertEqual(build_env.commands[0].output, u'This is the return') 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': 0, 'error': '', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
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_environment_failed_build_without_update_but_with_error(self): """A failed build exits cleanly and doesn't update build.""" build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, update_on_success=False, ) with build_env: raise BuildEnvironmentError('Test') self.assertFalse(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, 'success': False, 'project': self.project.pk, 'setup_error': u'', 'exit_code': 1, 'length': 0, 'error': 'Test', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_api_failure_on_error_in_exit(self): response = Mock(status_code=500, reason='Internal Server Error') self.mocks.configure_mock('docker_client', { 'kill.side_effect': BuildEnvironmentError('Failed') }) build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) with build_env: pass # 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': 'Failed', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_connection_failure(self): """Connection failure on to docker socket should raise exception.""" self.mocks.configure_mock('docker', {'side_effect': DockerException}) 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': 0, 'error': ( "There was a problem with Read the Docs while building your " "documentation. Please report this to us with your build id " "(123)." ), 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_environment_successful_build(self): """A successful build exits cleanly and reports the build output.""" build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) with build_env: pass 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, 'success': True, 'project': self.project.pk, 'setup_error': u'', 'length': 0, 'error': '', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_container_id(self): """Test docker build command.""" docker = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) self.assertEqual(docker.container_id, 'build-123-project-6-pip')
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_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_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, })
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, })
def test_command_execution(self): """Command execution through Docker.""" 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': 1}, }) 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.exec_create.assert_called_with( container='build-123-project-6-pip', cmd="/bin/sh -c 'cd /tmp && echo\\ test'", stderr=True, stdout=True) self.assertEqual(build_env.commands[0].exit_code, 1) self.assertEqual(build_env.commands[0].output, u'This is the return') 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': 0, 'error': '', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
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_api_failure_returns_previous_error_on_error_in_exit(self): """ Treat previously raised errors with more priority. Don't report a connection problem to Docker on cleanup if we have a more usable error to show the user. """ response = Mock(status_code=500, reason='Internal Server Error') self.mocks.configure_mock( 'docker_client', {'kill.side_effect': BuildEnvironmentError('Outer failed')}) build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, ) with build_env: raise BuildEnvironmentError('Inner 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': 'Inner failed', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def test_environment_successful_build_without_update(self): """A successful build exits cleanly and doesn't update build.""" build_env = DockerBuildEnvironment( version=self.version, project=self.project, build={'id': DUMMY_BUILD_ID}, update_on_success=False, ) with build_env: pass 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.assertFalse(self.mocks.mocks['api_v2.build']().put.called)
def test_container_already_exists(self): """Docker container already exists.""" self.mocks.configure_mock( 'docker_client', { 'inspect_container.return_value': {'State': {'Running': True}}, '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}, ) def _inner(): with build_env: build_env.run('echo', 'test', cwd='/tmp') self.assertRaises(BuildEnvironmentError, _inner) self.assertEqual( str(build_env.failure), 'A build environment is currently running for this version') self.assertEqual(self.mocks.docker_client.exec_create.call_count, 0) 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': 'A build environment is currently running for this version', 'setup': u'', 'output': u'', 'state': u'finished', 'builder': mock.ANY, })
def setup(self, requests_mock): # Save the reference to query it from inside the test self.requests_mock = requests_mock self.project = fixture.get( Project, slug='project', ) self.version = self.project.versions.get(slug='latest') self.build = fixture.get( Build, version=self.version, commit='a1b2c3', ) self.environment = DockerBuildEnvironment( project=self.project, version=self.version, build={'id': self.build.pk}, )