Bläddra i källkod

Fixes #1434, Project.containers with service_names.

Signed-off-by: Daniel Nephin <[email protected]>
Daniel Nephin 10 år sedan
förälder
incheckning
022f81711e
2 ändrade filer med 30 tillägg och 2 borttagningar
  1. 7 2
      compose/project.py
  2. 23 0
      tests/integration/project_test.py

+ 7 - 2
compose/project.py

@@ -6,7 +6,7 @@ from functools import reduce
 from docker.errors import APIError
 from docker.errors import APIError
 
 
 from .config import get_service_name_from_net, ConfigurationError
 from .config import get_service_name_from_net, ConfigurationError
-from .const import LABEL_PROJECT, LABEL_ONE_OFF
+from .const import LABEL_PROJECT, LABEL_SERVICE, LABEL_ONE_OFF
 from .service import Service, check_for_legacy_containers
 from .service import Service, check_for_legacy_containers
 from .container import Container
 from .container import Container
 
 
@@ -276,6 +276,11 @@ class Project(object):
                 all=stopped,
                 all=stopped,
                 filters={'label': self.labels(one_off=one_off)})]
                 filters={'label': self.labels(one_off=one_off)})]
 
 
+        def matches_service_names(container):
+            if not service_names:
+                return True
+            return container.labels.get(LABEL_SERVICE) in service_names
+
         if not containers:
         if not containers:
             check_for_legacy_containers(
             check_for_legacy_containers(
                 self.client,
                 self.client,
@@ -284,7 +289,7 @@ class Project(object):
                 stopped=stopped,
                 stopped=stopped,
                 one_off=one_off)
                 one_off=one_off)
 
 
-        return containers
+        return filter(matches_service_names, containers)
 
 
     def _inject_deps(self, acc, service):
     def _inject_deps(self, acc, service):
         dep_names = service.get_dependency_names()
         dep_names = service.get_dependency_names()

+ 23 - 0
tests/integration/project_test.py

@@ -6,6 +6,29 @@ from .testcases import DockerClientTestCase
 
 
 
 
 class ProjectTest(DockerClientTestCase):
 class ProjectTest(DockerClientTestCase):
+
+    def test_containers(self):
+        web = self.create_service('web')
+        db = self.create_service('db')
+        project = Project('composetest', [web, db], self.client)
+
+        project.up()
+
+        containers = project.containers()
+        self.assertEqual(len(containers), 2)
+
+    def test_containers_with_service_names(self):
+        web = self.create_service('web')
+        db = self.create_service('db')
+        project = Project('composetest', [web, db], self.client)
+
+        project.up()
+
+        containers = project.containers(['web'])
+        self.assertEqual(
+            [c.name for c in containers],
+            ['composetest_web_1'])
+
     def test_volumes_from_service(self):
     def test_volumes_from_service(self):
         service_dicts = config.from_dictionary({
         service_dicts = config.from_dictionary({
             'data': {
             'data': {