Split out fetching of legacy names so we can test it

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2015-05-21 12:12:02 +01:00
parent 0fdb8bf814
commit b5ce23885b
2 changed files with 51 additions and 19 deletions

View File

@ -26,19 +26,34 @@ def check_for_legacy_containers(
and warn the user that those containers may need to be migrated to and warn the user that those containers may need to be migrated to
using labels, so that compose can find them. 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): for container in client.containers(all=stopped):
name = get_container_name(container) name = get_container_name(container)
for service in services: for service in services:
prefix = '%s_%s_%s' % (project, service, 'run_' if one_off else '') prefix = '%s_%s_%s' % (project, service, 'run_' if one_off else '')
if not name.startswith(prefix): if name.startswith(prefix):
continue yield name
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 add_labels(project, container, name): def add_labels(project, container, name):

View File

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