From b5ce23885b22377aab9a1eff8f27f58474c29c76 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Thu, 21 May 2015 12:12:02 +0100 Subject: [PATCH] Split out fetching of legacy names so we can test it Signed-off-by: Aanand Prasad --- compose/legacy.py | 33 ++++++++++++++++++++-------- tests/integration/legacy_test.py | 37 +++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/compose/legacy.py b/compose/legacy.py index dc90079d..7f97c3e9 100644 --- a/compose/legacy.py +++ b/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): diff --git a/tests/integration/legacy_test.py b/tests/integration/legacy_test.py index d39635b7..85cc4032 100644 --- a/tests/integration/legacy_test.py +++ b/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 ) - 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) + def get_names(self, **kwargs): + if 'stopped' not in kwargs: + kwargs['stopped'] = True - legacy.migrate_project_to_labels(project) - self.assertEqual(len(project.containers(stopped=True)), 2) + 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(self.project.containers(stopped=True), []) + self.assertEqual(mock_log.warn.call_count, len(self.services)) + + legacy.migrate_project_to_labels(self.project) + self.assertEqual(len(self.project.containers(stopped=True)), len(self.services))