From bdec7e6b52500ce7ec3d0f0ee8acc789182e1e10 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 26 Aug 2015 13:32:21 -0400 Subject: [PATCH 1/2] Cleanup some test case, remove unused mock return values, and use standard single underscore for unused variable Signed-off-by: Daniel Nephin --- tests/integration/cli_test.py | 8 ++++---- tests/unit/service_test.py | 6 ------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index 9552bf6a6..78ff7604c 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -313,7 +313,7 @@ class CLITestCase(DockerClientTestCase): self.assertEqual('moto=bobo', container.environment['allo']) @mock.patch('dockerpty.start') - def test_run_service_without_map_ports(self, __): + def test_run_service_without_map_ports(self, _): # create one off container self.command.base_dir = 'tests/fixtures/ports-composefile' self.command.dispatch(['run', '-d', 'simple'], None) @@ -331,7 +331,7 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(port_assigned, None) @mock.patch('dockerpty.start') - def test_run_service_with_map_ports(self, __): + def test_run_service_with_map_ports(self, _): # create one off container self.command.base_dir = 'tests/fixtures/ports-composefile' @@ -354,7 +354,7 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(port_range[1], "0.0.0.0:49154") @mock.patch('dockerpty.start') - def test_run_service_with_explicitly_maped_ports(self, __): + def test_run_service_with_explicitly_maped_ports(self, _): # create one off container self.command.base_dir = 'tests/fixtures/ports-composefile' @@ -373,7 +373,7 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(port_full, "0.0.0.0:30001") @mock.patch('dockerpty.start') - def test_run_service_with_explicitly_maped_ip_ports(self, __): + def test_run_service_with_explicitly_maped_ip_ports(self, _): # create one off container self.command.base_dir = 'tests/fixtures/ports-composefile' diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 275bde1bd..5d37bfedf 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -99,14 +99,12 @@ class ServiceTest(unittest.TestCase): def test_split_domainname_none(self): service = Service('foo', image='foo', hostname='name', client=self.mock_client) - self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'image': 'foo'}, 1) self.assertEqual(opts['hostname'], 'name', 'hostname') self.assertFalse('domainname' in opts, 'domainname') def test_memory_swap_limit(self): service = Service(name='foo', image='foo', hostname='name', client=self.mock_client, mem_limit=1000000000, memswap_limit=2000000000) - self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'some': 'overrides'}, 1) self.assertEqual(opts['host_config']['MemorySwap'], 2000000000) self.assertEqual(opts['host_config']['Memory'], 1000000000) @@ -114,7 +112,6 @@ class ServiceTest(unittest.TestCase): def test_log_opt(self): log_opt = {'syslog-address': 'tcp://192.168.0.42:123'} service = Service(name='foo', image='foo', hostname='name', client=self.mock_client, log_driver='syslog', log_opt=log_opt) - self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'some': 'overrides'}, 1) self.assertIsInstance(opts['host_config']['LogConfig'], LogConfig) @@ -127,7 +124,6 @@ class ServiceTest(unittest.TestCase): hostname='name.domain.tld', image='foo', client=self.mock_client) - self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'image': 'foo'}, 1) self.assertEqual(opts['hostname'], 'name', 'hostname') self.assertEqual(opts['domainname'], 'domain.tld', 'domainname') @@ -139,7 +135,6 @@ class ServiceTest(unittest.TestCase): image='foo', domainname='domain.tld', client=self.mock_client) - self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'image': 'foo'}, 1) self.assertEqual(opts['hostname'], 'name', 'hostname') self.assertEqual(opts['domainname'], 'domain.tld', 'domainname') @@ -151,7 +146,6 @@ class ServiceTest(unittest.TestCase): domainname='domain.tld', image='foo', client=self.mock_client) - self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'image': 'foo'}, 1) self.assertEqual(opts['hostname'], 'name.sub', 'hostname') self.assertEqual(opts['domainname'], 'domain.tld', 'domainname') From d2718bed9938ad3d72500fb722c02970de4d1ac8 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 26 Aug 2015 13:33:03 -0400 Subject: [PATCH 2/2] Allow setting a one-off container name Signed-off-by: Daniel Nephin --- compose/cli/main.py | 4 ++++ compose/service.py | 2 +- tests/integration/cli_test.py | 10 ++++++++++ tests/unit/cli_test.py | 4 ++++ tests/unit/service_test.py | 13 +++++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index 890a3c371..58e542851 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -308,6 +308,7 @@ class TopLevelCommand(Command): --allow-insecure-ssl Deprecated - no effect. -d Detached mode: Run container in the background, print new container name. + --name NAME Assign a name to the container --entrypoint CMD Override the entrypoint of the image. -e KEY=VAL Set an environment variable (can be used multiple times) -u, --user="" Run as specified username or uid @@ -374,6 +375,9 @@ class TopLevelCommand(Command): 'can not be used togather' ) + if options['--name']: + container_options['name'] = options['--name'] + try: container = service.create_container( quiet=True, diff --git a/compose/service.py b/compose/service.py index a15ee1b9a..a0423ff44 100644 --- a/compose/service.py +++ b/compose/service.py @@ -586,7 +586,7 @@ class Service(object): if self.custom_container_name() and not one_off: container_options['name'] = self.custom_container_name() - else: + elif not container_options.get('name'): container_options['name'] = self.get_container_name(number, one_off) if add_config_hash: diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index 78ff7604c..b94d7d1ee 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -391,6 +391,16 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(port_short, "127.0.0.1:30000") self.assertEqual(port_full, "127.0.0.1:30001") + @mock.patch('dockerpty.start') + def test_run_with_custom_name(self, _): + self.command.base_dir = 'tests/fixtures/environment-composefile' + name = 'the-container-name' + self.command.dispatch(['run', '--name', name, 'service'], None) + + service = self.project.get_service('service') + container, = service.containers(stopped=True, one_off=True) + self.assertEqual(container.name, name) + def test_rm(self): service = self.project.get_service('simple') service.create_container() diff --git a/tests/unit/cli_test.py b/tests/unit/cli_test.py index 7d22ad02f..1fd9f529e 100644 --- a/tests/unit/cli_test.py +++ b/tests/unit/cli_test.py @@ -112,6 +112,7 @@ class CLITestCase(unittest.TestCase): '--service-ports': None, '--publish': [], '--rm': None, + '--name': None, }) _, _, call_kwargs = mock_client.create_container.mock_calls[0] @@ -141,6 +142,7 @@ class CLITestCase(unittest.TestCase): '--service-ports': None, '--publish': [], '--rm': None, + '--name': None, }) _, _, call_kwargs = mock_client.create_container.mock_calls[0] self.assertEquals(call_kwargs['host_config']['RestartPolicy']['Name'], 'always') @@ -166,6 +168,7 @@ class CLITestCase(unittest.TestCase): '--service-ports': None, '--publish': [], '--rm': True, + '--name': None, }) _, _, call_kwargs = mock_client.create_container.mock_calls[0] self.assertFalse('RestartPolicy' in call_kwargs['host_config']) @@ -195,4 +198,5 @@ class CLITestCase(unittest.TestCase): '--service-ports': True, '--publish': ['80:80'], '--rm': None, + '--name': None, }) diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 5d37bfedf..a24e524dd 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -150,6 +150,19 @@ class ServiceTest(unittest.TestCase): self.assertEqual(opts['hostname'], 'name.sub', 'hostname') self.assertEqual(opts['domainname'], 'domain.tld', 'domainname') + def test_get_container_create_options_with_name_option(self): + service = Service( + 'foo', + image='foo', + client=self.mock_client, + container_name='foo1') + name = 'the_new_name' + opts = service._get_container_create_options( + {'name': name}, + 1, + one_off=True) + self.assertEqual(opts['name'], name) + def test_get_container_not_found(self): self.mock_client.containers.return_value = [] service = Service('foo', client=self.mock_client, image='foo')