Browse Source

Split out fetching of legacy names so we can test it

Signed-off-by: Aanand Prasad <[email protected]>
Aanand Prasad 10 năm trước cách đây
mục cha
commit
b5ce23885b
2 tập tin đã thay đổi với 50 bổ sung18 xóa
  1. 24 9
      compose/legacy.py
  2. 26 9
      tests/integration/legacy_test.py

+ 24 - 9
compose/legacy.py

@@ -26,19 +26,34 @@ def check_for_legacy_containers(
     and warn the user that those containers may need to be migrated to
     using labels, so that compose can find them.
     """
+    names = get_legacy_container_names(
+        client,
+        project,
+        services,
+        stopped=stopped,
+        one_off=one_off)
+
+    for name in names:
+        log.warn(
+            "Compose found a found a container named %s without any "
+            "labels. As of compose 1.3.0 containers are identified with "
+            "labels instead of naming convention. If you'd like compose "
+            "to use this container, please run "
+            "`docker-compose migrate-to-labels`" % (name,))
+
+
+def get_legacy_container_names(
+        client,
+        project,
+        services,
+        stopped=False,
+        one_off=False):
     for container in client.containers(all=stopped):
         name = get_container_name(container)
         for service in services:
             prefix = '%s_%s_%s' % (project, service, 'run_' if one_off else '')
-            if not name.startswith(prefix):
-                continue
-
-            log.warn(
-                "Compose found a found a container named %s without any "
-                "labels. As of compose 1.3.0 containers are identified with "
-                "labels instead of naming convention. If you'd like compose "
-                "to use this container, please run "
-                "`docker-compose migrate-to-labels`" % (name,))
+            if name.startswith(prefix):
+                yield name
 
 
 def add_labels(project, container, name):

+ 26 - 9
tests/integration/legacy_test.py

@@ -7,24 +7,41 @@ from .testcases import DockerClientTestCase
 
 class ProjectTest(DockerClientTestCase):
 
-    def test_migration_to_labels(self):
-        services = [
+    def setUp(self):
+        super(ProjectTest, self).setUp()
+
+        self.services = [
             self.create_service('web'),
             self.create_service('db'),
         ]
 
-        project = Project('composetest', services, self.client)
+        self.project = Project('composetest', self.services, self.client)
 
-        for service in services:
+        for service in self.services:
             service.ensure_image_exists()
             self.client.create_container(
-                name='{}_{}_1'.format(project.name, service.name),
+                name='{}_{}_1'.format(self.project.name, service.name),
                 **service.options
             )
 
+    def get_names(self, **kwargs):
+        if 'stopped' not in kwargs:
+            kwargs['stopped'] = True
+
+        return list(legacy.get_legacy_container_names(
+            self.client,
+            self.project.name,
+            [s.name for s in self.services],
+            **kwargs
+        ))
+
+    def test_get_legacy_container_names(self):
+        self.assertEqual(len(self.get_names()), len(self.services))
+
+    def test_migration_to_labels(self):
         with mock.patch.object(legacy, 'log', autospec=True) as mock_log:
-            self.assertEqual(project.containers(stopped=True), [])
-            self.assertEqual(mock_log.warn.call_count, 2)
+            self.assertEqual(self.project.containers(stopped=True), [])
+            self.assertEqual(mock_log.warn.call_count, len(self.services))
 
-        legacy.migrate_project_to_labels(project)
-        self.assertEqual(len(project.containers(stopped=True)), 2)
+        legacy.migrate_project_to_labels(self.project)
+        self.assertEqual(len(self.project.containers(stopped=True)), len(self.services))