From 2527ef8055410ee5100631da807ed650ec64c36f Mon Sep 17 00:00:00 2001 From: dano Date: Sat, 6 Jun 2015 15:12:13 -0400 Subject: [PATCH 1/3] Validate that service names passed to Project.containers aren't bogus. Signed-off-by: Dan O'Reilly --- compose/project.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compose/project.py b/compose/project.py index d3deeeaf..e6ec6d67 100644 --- a/compose/project.py +++ b/compose/project.py @@ -274,6 +274,9 @@ class Project(object): service.remove_stopped(**options) def containers(self, service_names=None, stopped=False, one_off=False): + if service_names: + # Will raise NoSuchService if one of the names is invalid + self.get_services(service_names) containers = [ Container.from_ps(self.client, container) for container in self.client.containers( From c59c9dd95159036a82a9a6ce8d50a4cadc2f9e07 Mon Sep 17 00:00:00 2001 From: Dan O'Reilly Date: Mon, 8 Jun 2015 17:04:42 -0400 Subject: [PATCH 2/3] Add integration test for service name verification Add a test to make sure NoSuchService is raised if a bogus service name is given to 'docker-compose logs'. Signed-off-by: Dan O'Reilly --- tests/integration/cli_test.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index ae57e919..a5289ef8 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -7,6 +7,7 @@ from mock import patch from .testcases import DockerClientTestCase from compose.cli.main import TopLevelCommand +from compose.project import NoSuchService class CLITestCase(DockerClientTestCase): @@ -349,6 +350,10 @@ class CLITestCase(DockerClientTestCase): self.assertEqual(len(service.containers(stopped=True)), 1) self.assertFalse(service.containers(stopped=True)[0].is_running) + def test_logs_invalid_service_name(self): + with self.assertRaises(NoSuchService): + self.command.dispatch(['logs', 'madeupname'], None) + def test_kill(self): self.command.dispatch(['up', '-d'], None) service = self.project.get_service('simple') From 464ab3d7273317507fe42fe1ef03ac3e08d0bd86 Mon Sep 17 00:00:00 2001 From: Dan O'Reilly Date: Mon, 15 Jun 2015 23:06:06 -0400 Subject: [PATCH 3/3] Add a method specifically for service name validation. Signed-off-by: Dan O'Reilly --- compose/project.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compose/project.py b/compose/project.py index e6ec6d67..d7d35061 100644 --- a/compose/project.py +++ b/compose/project.py @@ -99,6 +99,16 @@ class Project(object): raise NoSuchService(name) + def validate_service_names(self, service_names): + """ + Validate that the given list of service names only contains valid + services. Raises NoSuchService if one of the names is invalid. + """ + valid_names = self.service_names + for name in service_names: + if name not in valid_names: + raise NoSuchService(name) + def get_services(self, service_names=None, include_deps=False): """ Returns a list of this project's services filtered @@ -275,8 +285,7 @@ class Project(object): def containers(self, service_names=None, stopped=False, one_off=False): if service_names: - # Will raise NoSuchService if one of the names is invalid - self.get_services(service_names) + self.validate_service_names(service_names) containers = [ Container.from_ps(self.client, container) for container in self.client.containers(