本文整理汇总了Python中readthedocs.rtd_tests.mocks.environment.EnvironmentMockGroup.api方法的典型用法代码示例。如果您正苦于以下问题:Python EnvironmentMockGroup.api方法的具体用法?Python EnvironmentMockGroup.api怎么用?Python EnvironmentMockGroup.api使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类readthedocs.rtd_tests.mocks.environment.EnvironmentMockGroup
的用法示例。
在下文中一共展示了EnvironmentMockGroup.api方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestLocalEnvironment
# 需要导入模块: from readthedocs.rtd_tests.mocks.environment import EnvironmentMockGroup [as 别名]
# 或者: from readthedocs.rtd_tests.mocks.environment.EnvironmentMockGroup import api [as 别名]
class TestLocalEnvironment(TestCase):
'''Test execution and exception handling in environment'''
fixtures = ['test_data']
def setUp(self):
self.project = Project.objects.get(slug='pip')
self.version = Version(slug='foo', verbose_name='foobar')
self.project.versions.add(self.version)
self.mocks = EnvironmentMockGroup()
self.mocks.start()
def tearDown(self):
self.mocks.stop()
def test_normal_execution(self):
'''Normal build in passing state'''
self.mocks.configure_mock('process', {
'communicate.return_value': ('This is okay', '')})
type(self.mocks.process).returncode = PropertyMock(return_value=0)
build_env = LocalEnvironment(version=self.version, project=self.project,
build={'id': DUMMY_BUILD_ID})
with build_env:
build_env.run('echo', 'test')
self.assertTrue(self.mocks.process.communicate.called)
self.assertTrue(build_env.done)
self.assertTrue(build_env.successful)
self.assertEqual(len(build_env.commands), 1)
self.assertEqual(build_env.commands[0].output, u'This is okay')
self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
def test_failing_execution(self):
'''Build in failing state'''
self.mocks.configure_mock('process', {
'communicate.return_value': ('This is not okay', '')})
type(self.mocks.process).returncode = PropertyMock(return_value=1)
build_env = LocalEnvironment(version=self.version, project=self.project,
build={'id': DUMMY_BUILD_ID})
with build_env:
build_env.run('echo', 'test')
self.fail('This should be unreachable')
self.assertTrue(self.mocks.process.communicate.called)
self.assertTrue(build_env.done)
self.assertTrue(build_env.failed)
self.assertEqual(len(build_env.commands), 1)
self.assertEqual(build_env.commands[0].output, u'This is not okay')
self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
def test_failing_execution_with_caught_exception(self):
'''Build in failing state with BuildEnvironmentError exception'''
build_env = LocalEnvironment(version=self.version, project=self.project,
build={'id': DUMMY_BUILD_ID})
with build_env:
raise BuildEnvironmentError('Foobar')
self.assertFalse(self.mocks.process.communicate.called)
self.assertEqual(len(build_env.commands), 0)
self.assertTrue(build_env.done)
self.assertTrue(build_env.failed)
self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
def test_failing_execution_with_unexpected_exception(self):
'''Build in failing state with exception from code'''
build_env = LocalEnvironment(version=self.version, project=self.project,
build={'id': DUMMY_BUILD_ID})
with build_env:
raise ValueError('uncaught')
self.assertFalse(self.mocks.process.communicate.called)
self.assertTrue(build_env.done)
self.assertTrue(build_env.failed)
self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
示例2: TestLocalEnvironment
# 需要导入模块: from readthedocs.rtd_tests.mocks.environment import EnvironmentMockGroup [as 别名]
# 或者: from readthedocs.rtd_tests.mocks.environment.EnvironmentMockGroup import api [as 别名]
class TestLocalEnvironment(TestCase):
"""Test execution and exception handling in environment."""
fixtures = ['test_data']
def setUp(self):
self.project = Project.objects.get(slug='pip')
self.version = Version(slug='foo', verbose_name='foobar')
self.project.versions.add(self.version, bulk=False)
self.mocks = EnvironmentMockGroup()
self.mocks.start()
def tearDown(self):
self.mocks.stop()
def test_normal_execution(self):
"""Normal build in passing state."""
self.mocks.configure_mock('process', {
'communicate.return_value': (b'This is okay', '')
})
type(self.mocks.process).returncode = PropertyMock(return_value=0)
build_env = LocalEnvironment(
version=self.version,
project=self.project,
build={'id': DUMMY_BUILD_ID},
)
with build_env:
build_env.run('echo', 'test')
self.assertTrue(self.mocks.process.communicate.called)
self.assertTrue(build_env.done)
self.assertTrue(build_env.successful)
self.assertEqual(len(build_env.commands), 1)
self.assertEqual(build_env.commands[0].output, u'This is okay')
# 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': mock.ANY,
'error': '',
'setup': u'',
'output': u'',
'state': u'finished',
'builder': mock.ANY,
'exit_code': 0,
})
def test_incremental_state_update_with_no_update(self):
"""Build updates to a non-finished state when update_on_success=True."""
build_envs = [
LocalEnvironment(
version=self.version,
project=self.project,
build={'id': DUMMY_BUILD_ID},
),
LocalEnvironment(
version=self.version,
project=self.project,
build={'id': DUMMY_BUILD_ID},
update_on_success=False,
),
]
for build_env in build_envs:
with build_env:
build_env.update_build(BUILD_STATE_CLONING)
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': mock.ANY,
'error': '',
'setup': u'',
'output': u'',
'state': BUILD_STATE_CLONING,
'builder': mock.ANY,
'exit_code': 0,
})
def test_failing_execution(self):
"""Build in failing state."""
self.mocks.configure_mock('process', {
'communicate.return_value': (b'This is not okay', '')
})
type(self.mocks.process).returncode = PropertyMock(return_value=1)
build_env = LocalEnvironment(
version=self.version,
project=self.project,
build={'id': DUMMY_BUILD_ID},
)
#.........这里部分代码省略.........
示例3: TestDockerEnvironment
# 需要导入模块: from readthedocs.rtd_tests.mocks.environment import EnvironmentMockGroup [as 别名]
# 或者: from readthedocs.rtd_tests.mocks.environment.EnvironmentMockGroup import api [as 别名]
class TestDockerEnvironment(TestCase):
'''Test docker build environment'''
fixtures = ['test_data']
def setUp(self):
self.project = Project.objects.get(slug='pip')
self.version = Version(slug='foo', verbose_name='foobar')
self.project.versions.add(self.version)
self.mocks = EnvironmentMockGroup()
self.mocks.start()
def tearDown(self):
self.mocks.stop()
def test_container_id(self):
'''Test docker build command'''
docker = DockerEnvironment(version=self.version, project=self.project,
build={'id': DUMMY_BUILD_ID})
self.assertEqual(docker.container_id,
'build-123-project-6-pip')
def test_connection_failure(self):
'''Connection failure on to docker socket should raise exception'''
self.mocks.configure_mock('docker', {
'side_effect': DockerException
})
build_env = DockerEnvironment(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)
self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
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={'id': DUMMY_BUILD_ID})
def _inner():
with build_env:
self.fail('Should not hit this')
self.assertRaises(BuildEnvironmentError, _inner)
self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
def test_command_execution(self):
'''Command execution through Docker'''
self.mocks.configure_mock('docker_client', {
'exec_create.return_value': {'Id': 'container-foobar'},
'exec_start.return_value': 'This is the return',
'exec_inspect.return_value': {'ExitCode': 1},
})
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.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, 'This is the return')
self.assertEqual(build_env.commands[0].error, None)
self.assertTrue(build_env.failed)
self.assertFalse(self.mocks.api()(DUMMY_BUILD_ID).put.called)
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': 'container-foobar'},
'exec_start.return_value': '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:
#.........这里部分代码省略.........
示例4: TestDockerEnvironment
# 需要导入模块: from readthedocs.rtd_tests.mocks.environment import EnvironmentMockGroup [as 别名]
# 或者: from readthedocs.rtd_tests.mocks.environment.EnvironmentMockGroup import api [as 别名]
class TestDockerEnvironment(TestCase):
"""Test docker build environment."""
fixtures = ['test_data']
def setUp(self):
self.project = Project.objects.get(slug='pip')
self.version = Version(slug='foo', verbose_name='foobar')
self.project.versions.add(self.version, bulk=False)
self.mocks = EnvironmentMockGroup()
self.mocks.start()
def tearDown(self):
self.mocks.stop()
def test_container_id(self):
"""Test docker build command."""
docker = DockerEnvironment(
version=self.version,
project=self.project,
build={'id': DUMMY_BUILD_ID},
)
self.assertEqual(docker.container_id, 'build-123-project-6-pip')
def test_environment_successful_build(self):
"""A successful build exits cleanly and reports the build output."""
build_env = DockerEnvironment(
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_environment_successful_build_without_update(self):
"""A successful build exits cleanly and doesn't update build."""
build_env = DockerEnvironment(
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_environment_failed_build_without_update_but_with_error(self):
"""A failed build exits cleanly and doesn't update build."""
build_env = DockerEnvironment(
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',
#.........这里部分代码省略.........