From ef2fb77c1d08a65d9c2a13a94fc01315f04e19ad Mon Sep 17 00:00:00 2001 From: Chris Corbyn Date: Sat, 5 Jul 2014 08:19:54 +0000 Subject: [PATCH] Make fig run COMMAND parameter optional. This behaves more like the native docker client, where the absence of a command means docker runs the CMD in the Dockerfile. If a command is defined in fig.yml this is used instead. Signed-off-by: Chris Corbyn --- fig/cli/main.py | 9 +++++-- tests/fixtures/commands-figfile/fig.yml | 5 ++++ tests/integration/cli_test.py | 34 ++++++++++++++++++++----- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 tests/fixtures/commands-figfile/fig.yml diff --git a/fig/cli/main.py b/fig/cli/main.py index 44aab9828..11230331c 100644 --- a/fig/cli/main.py +++ b/fig/cli/main.py @@ -206,7 +206,7 @@ class TopLevelCommand(Command): running. If you do not want to start linked services, use `fig run --no-deps SERVICE COMMAND [ARGS...]`. - Usage: run [options] SERVICE COMMAND [ARGS...] + Usage: run [options] SERVICE [COMMAND] [ARGS...] Options: -d Detached mode: Run container in the background, print @@ -233,8 +233,13 @@ class TopLevelCommand(Command): if options['-d'] or options['-T'] or not sys.stdin.isatty(): tty = False + if options['COMMAND']: + command = [options['COMMAND']] + options['ARGS'] + else: + command = service.options.get('command') + container_options = { - 'command': [options['COMMAND']] + options['ARGS'], + 'command': command, 'tty': tty, 'stdin_open': not options['-d'], } diff --git a/tests/fixtures/commands-figfile/fig.yml b/tests/fixtures/commands-figfile/fig.yml new file mode 100644 index 000000000..707c18a75 --- /dev/null +++ b/tests/fixtures/commands-figfile/fig.yml @@ -0,0 +1,5 @@ +implicit: + image: figtest_test +explicit: + image: figtest_test + command: [ "/bin/true" ] diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index ecfee0f9c..00b1009be 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -109,7 +109,7 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(len(self.command.project.containers()), 0) @patch('dockerpty.start') - def test_run_service_with_links(self, mock_stdout): + def test_run_service_with_links(self, __): self.command.base_dir = 'tests/fixtures/links-figfile' self.command.dispatch(['run', 'web', '/bin/true'], None) db = self.command.project.get_service('db') @@ -118,18 +118,14 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(len(console.containers()), 0) @patch('dockerpty.start') - def test_run_with_no_deps(self, mock_stdout): - mock_stdout.fileno = lambda: 1 - + def test_run_with_no_deps(self, __): self.command.base_dir = 'tests/fixtures/links-figfile' self.command.dispatch(['run', '--no-deps', 'web', '/bin/true'], None) db = self.command.project.get_service('db') self.assertEqual(len(db.containers()), 0) @patch('dockerpty.start') - def test_run_does_not_recreate_linked_containers(self, mock_stdout): - mock_stdout.fileno = lambda: 1 - + def test_run_does_not_recreate_linked_containers(self, __): self.command.base_dir = 'tests/fixtures/links-figfile' self.command.dispatch(['up', '-d', 'db'], None) db = self.command.project.get_service('db') @@ -144,6 +140,30 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(old_ids, new_ids) + @patch('dockerpty.start') + def test_run_without_command(self, __): + self.command.base_dir = 'tests/fixtures/commands-figfile' + self.client.build('tests/fixtures/simple-dockerfile', tag='figtest_test') + + for c in self.command.project.containers(stopped=True, one_off=True): + c.remove() + + self.command.dispatch(['run', 'implicit'], None) + service = self.command.project.get_service('implicit') + containers = service.containers(stopped=True, one_off=True) + self.assertEqual( + [c.human_readable_command for c in containers], + [u'/bin/sh -c echo "success"'], + ) + + self.command.dispatch(['run', 'explicit'], None) + service = self.command.project.get_service('explicit') + containers = service.containers(stopped=True, one_off=True) + self.assertEqual( + [c.human_readable_command for c in containers], + [u'/bin/true'], + ) + def test_rm(self): service = self.command.project.get_service('simple') service.create_container()