def test_follow_container_logs(self, fixtures): docker_client = MagicMock(spec=DockerClient) container = MagicMock(spec=Container) logs = MagicMock() messages = [m.encode('utf-8') for m in ('Running', 'the', 'command')] logs.__iter__.return_value = iter(messages) container.logs.return_value = logs container.attrs = {'State': {'ExitCode': 0}} docker_client.containers.create.return_value = container with patch('cdflow.print') as print_: docker_run( docker_client, fixtures['image_id'], fixtures['command'], fixtures['project_root'], fixtures['environment_variables'], fixtures['platform_config_paths'], ) container.logs.assert_called_once_with(stream=True, follow=True, stdout=True, stderr=True) assert print_.call_args_list[0][1]['end'] == '' assert print_.call_args_list[0][0][0] == messages[0]\ .decode('utf-8') assert print_.call_args_list[1][0][0] == messages[1]\ .decode('utf-8') assert print_.call_args_list[2][0][0] == messages[2]\ .decode('utf-8')
def test_run_args_without_platform_config(self, fixtures): docker_client = MagicMock(spec=DockerClient) image_id = fixtures['image_id'] command = fixtures['command'] project_root = fixtures['project_root'] environment_variables = fixtures['environment_variables'] container = MagicMock(spec=Container) container.attrs = {'State': {'ExitCode': 0}} docker_client.containers.create.return_value = container exit_status, output = docker_run(docker_client, image_id, command, project_root, environment_variables) assert exit_status == 0 assert output == '' docker_client.containers.create.assert_called_once_with( image_id, command=command, environment=environment_variables, detach=True, volumes={ project_root: { 'bind': project_root, 'mode': 'rw', }, '/var/run/docker.sock': { 'bind': '/var/run/docker.sock', 'mode': 'ro', }, }, working_dir=project_root, ) container.start.assert_called_once()
def test_container_can_be_removed_at_script_exit(self, fixtures): docker_client = MagicMock(spec=DockerClient) container = MagicMock(spec=Container) container.attrs = {'State': {'ExitCode': 0}} docker_client.containers.create.return_value = container with patch('cdflow.atexit') as atexit: docker_run( docker_client, fixtures['image_id'], fixtures['command'], fixtures['project_root'], fixtures['environment_variables'], fixtures['platform_config_paths'], ) atexit.register.assert_called_once_with(_remove_container, container)
def test_run_shell_command(self, fixtures): docker_client = MagicMock(spec=docker.from_env()) image_id = fixtures['image_id'] command = ['shell'] + fixtures['command'] project_root = fixtures['project_root'] environment_variables = fixtures['environment_variables'] container = MagicMock(spec=Container) container.attrs = {'State': {'ExitCode': 0}} docker_client.containers.create.return_value = container with patch('cdflow.dockerpty') as dockerpty, patch( 'cdflow.check_output', ) as check_output: check_output.return_value = '42\n' exit_status, output = docker_run(docker_client, image_id, command, project_root, environment_variables) dockerpty.start.assert_called_once_with( docker_client.api, container.id, ) assert exit_status == 0 assert output == 'Shell end' expected_environment_variable = deepcopy(environment_variables) expected_environment_variable['COLUMNS'] = 42 expected_environment_variable['LINES'] = 42 docker_client.containers.create.assert_called_once_with( image_id, command=command, environment=expected_environment_variable, volumes={ project_root: { 'bind': project_root, 'mode': 'rw', }, '/var/run/docker.sock': { 'bind': '/var/run/docker.sock', 'mode': 'ro', }, }, working_dir=project_root, tty=True, stdin_open=True, )
def test_error_from_docker(self, fixtures): image_id = fixtures['image_id'] command = fixtures['command'] project_root = fixtures['project_root'] platform_config_paths = fixtures['platform_config_paths'] environment_variables = fixtures['environment_variables'] docker_client = MagicMock(spec=DockerClient) docker_client.containers.create.side_effect = DockerException exit_status, output = docker_run( docker_client, image_id, command, project_root, environment_variables, platform_config_paths, ) assert exit_status == 1 assert output == str(DockerException())
def test_exit_code_from_container_is_returned(self, fixtures): assume(fixtures['exit_code'] != 0) docker_client = MagicMock(spec=DockerClient) container = MagicMock(spec=Container) container.attrs = { 'State': { 'ExitCode': fixtures['exit_code'], } } docker_client.containers.create.return_value = container exit_status, output = docker_run( docker_client, fixtures['image_id'], fixtures['command'], fixtures['project_root'], fixtures['environment_variables'], fixtures['platform_config_paths'], ) assert exit_status == fixtures['exit_code'] assert output == ''