From b12c29479ee7283f24af6025e2d3de0c15faf510 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 16 Jun 2015 10:45:49 -0700 Subject: [PATCH] Merge pull request #1521 from dano/validate-service-names Validate that service names passed to Project.containers aren't bogus. (cherry picked from commit bc14c473c97af14ed150160fe84d23fcb05fe4e2) Signed-off-by: Aanand Prasad --- compose/project.py | 12 ++++++++++++ tests/integration/cli_test.py | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/compose/project.py b/compose/project.py index 6446a6d33..907632276 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 @@ -274,6 +284,8 @@ class Project(object): service.remove_stopped(**options) def containers(self, service_names=None, stopped=False, one_off=False): + if service_names: + self.validate_service_names(service_names) containers = [ Container.from_ps(self.client, container) for container in self.client.containers( diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index 2d1f1f76e..dab9d4a2b 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -9,6 +9,7 @@ from mock import patch from .testcases import DockerClientTestCase from compose.cli.main import TopLevelCommand +from compose.project import NoSuchService class CLITestCase(DockerClientTestCase): @@ -351,6 +352,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')