From f2f01e207b491866349db7168e3d48082d7abdda Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Wed, 17 Dec 2014 13:59:52 +0100 Subject: [PATCH] Allow overriding user on the command line Allow overriding a user on the command line from the one specified in the fig.yaml We are testing on the behavior of how the busybox image is made which is kind of tricky but hopefully that shouln't change. Signed-off-by: Chmouel Boudjnah --- fig/cli/main.py | 5 +++++ tests/fixtures/user-figfile/fig.yml | 3 +++ tests/integration/cli_test.py | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/fixtures/user-figfile/fig.yml diff --git a/fig/cli/main.py b/fig/cli/main.py index 2c6a04020..02cf81746 100644 --- a/fig/cli/main.py +++ b/fig/cli/main.py @@ -276,6 +276,7 @@ class TopLevelCommand(Command): new container name. --entrypoint CMD Override the entrypoint of the image. -e KEY=VAL Set an environment variable (can be used multiple times) + -u --user USER Run as specified user. --no-deps Don't start linked services. --rm Remove container after run. Ignored in detached mode. -T Disable pseudo-tty allocation. By default `fig run` @@ -320,6 +321,10 @@ class TopLevelCommand(Command): if options['--entrypoint']: container_options['entrypoint'] = options.get('--entrypoint') + + if options['--user']: + container_options['user'] = options.get('--user') + container = service.create_container( one_off=True, insecure_registry=insecure_registry, diff --git a/tests/fixtures/user-figfile/fig.yml b/tests/fixtures/user-figfile/fig.yml new file mode 100644 index 000000000..625131987 --- /dev/null +++ b/tests/fixtures/user-figfile/fig.yml @@ -0,0 +1,3 @@ +service: + image: busybox:latest + command: id diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index f03d72d2d..0675ade1c 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -1,6 +1,7 @@ from __future__ import absolute_import import sys +from docker.errors import APIError from six import StringIO from mock import patch @@ -8,6 +9,7 @@ from .testcases import DockerClientTestCase from fig.cli.main import TopLevelCommand + class CLITestCase(DockerClientTestCase): def setUp(self): super(CLITestCase, self).setUp() @@ -217,6 +219,21 @@ class CLITestCase(DockerClientTestCase): u'/bin/echo helloworld' ) + @patch('dockerpty.start') + def test_run_service_with_user_overridden(self, _): + name = 'service' + self.command.base_dir = 'tests/fixtures/user-figfile' + # NOTE(chmou): available in default busybox and has a shell + user = 'sshd' + args = ['run', '--user', user, name] + + self.command.dispatch(args, None) + + service = self.project.get_service(name) + container = service.containers(stopped=True, one_off=True)[0] + container.inspect() + self.assertEqual(user, container.dictionary['Config']['User']) + @patch('dockerpty.start') def test_run_service_with_environement_overridden(self, _): name = 'service'