Browse Source

Merge pull request #6813 from ulyssessouza/fix_stdin_open

Fix stdin_open when running docker-compose run
Silvin Lubecki 6 years ago
parent
commit
cd8e2f870f
2 changed files with 74 additions and 2 deletions
  1. 14 2
      compose/cli/main.py
  2. 60 0
      tests/unit/cli/main_test.py

+ 14 - 2
compose/cli/main.py

@@ -885,6 +885,8 @@ class TopLevelCommand(object):
         else:
             command = service.options.get('command')
 
+        options['stdin_open'] = service.options.get('stdin_open', True)
+
         container_options = build_one_off_container_options(options, detach, command)
         run_one_off_container(
             container_options, self.project, service, options,
@@ -1286,7 +1288,7 @@ def build_one_off_container_options(options, detach, command):
     container_options = {
         'command': command,
         'tty': not (detach or options['-T'] or not sys.stdin.isatty()),
-        'stdin_open': not detach,
+        'stdin_open': options.get('stdin_open'),
         'detach': detach,
     }
 
@@ -1368,7 +1370,7 @@ def run_one_off_container(container_options, project, service, options, toplevel
             if IS_WINDOWS_PLATFORM or use_cli:
                 service.connect_container_to_networks(container, use_network_aliases)
                 exit_code = call_docker(
-                    ["start", "--attach", "--interactive", container.id],
+                    get_docker_start_call(container_options, container.id),
                     toplevel_options, environment
                 )
             else:
@@ -1395,6 +1397,16 @@ def run_one_off_container(container_options, project, service, options, toplevel
     sys.exit(exit_code)
 
 
+def get_docker_start_call(container_options, container_id):
+    docker_call = ["start"]
+    if not container_options.get('detach'):
+        docker_call.append("--attach")
+    if container_options.get('stdin_open'):
+        docker_call.append("--interactive")
+    docker_call.append(container_id)
+    return docker_call
+
+
 def log_printer_from_project(
     project,
     containers,

+ 60 - 0
tests/unit/cli/main_test.py

@@ -9,9 +9,11 @@ import pytest
 from compose import container
 from compose.cli.errors import UserError
 from compose.cli.formatter import ConsoleWarningFormatter
+from compose.cli.main import build_one_off_container_options
 from compose.cli.main import call_docker
 from compose.cli.main import convergence_strategy_from_opts
 from compose.cli.main import filter_containers_to_service_names
+from compose.cli.main import get_docker_start_call
 from compose.cli.main import setup_console_handler
 from compose.cli.main import warn_for_swarm_mode
 from compose.service import ConvergenceStrategy
@@ -65,6 +67,64 @@ class TestCLIMainTestCase(object):
             warn_for_swarm_mode(mock_client)
             assert fake_log.warning.call_count == 1
 
+    def test_build_one_off_container_options(self):
+        command = 'build myservice'
+        detach = False
+        options = {
+            '-e': ['MYVAR=MYVALUE'],
+            '-T': True,
+            '--label': ['MYLABEL'],
+            '--entrypoint': 'bash',
+            '--user': 'MYUSER',
+            '--service-ports': [],
+            '--publish': '',
+            '--name': 'MYNAME',
+            '--workdir': '.',
+            '--volume': [],
+            'stdin_open': False,
+        }
+
+        expected_container_options = {
+            'command': command,
+            'tty': False,
+            'stdin_open': False,
+            'detach': detach,
+            'entrypoint': 'bash',
+            'environment': {'MYVAR': 'MYVALUE'},
+            'labels': {'MYLABEL': ''},
+            'name': 'MYNAME',
+            'ports': [],
+            'restart': None,
+            'user': 'MYUSER',
+            'working_dir': '.',
+        }
+
+        container_options = build_one_off_container_options(options, detach, command)
+        assert container_options == expected_container_options
+
+    def test_get_docker_start_call(self):
+        container_id = 'my_container_id'
+
+        mock_container_options = {'detach': False, 'stdin_open': True}
+        expected_docker_start_call = ['start', '--attach', '--interactive', container_id]
+        docker_start_call = get_docker_start_call(mock_container_options, container_id)
+        assert expected_docker_start_call == docker_start_call
+
+        mock_container_options = {'detach': False, 'stdin_open': False}
+        expected_docker_start_call = ['start', '--attach', container_id]
+        docker_start_call = get_docker_start_call(mock_container_options, container_id)
+        assert expected_docker_start_call == docker_start_call
+
+        mock_container_options = {'detach': True, 'stdin_open': True}
+        expected_docker_start_call = ['start', '--interactive', container_id]
+        docker_start_call = get_docker_start_call(mock_container_options, container_id)
+        assert expected_docker_start_call == docker_start_call
+
+        mock_container_options = {'detach': True, 'stdin_open': False}
+        expected_docker_start_call = ['start', container_id]
+        docker_start_call = get_docker_start_call(mock_container_options, container_id)
+        assert expected_docker_start_call == docker_start_call
+
 
 class TestSetupConsoleHandlerTestCase(object):