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,13 +26,14 @@ 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.
""" """
for container in client.containers(all=stopped): names = get_legacy_container_names(
name = get_container_name(container) client,
for service in services: project,
prefix = '%s_%s_%s' % (project, service, 'run_' if one_off else '') services,
if not name.startswith(prefix): stopped=stopped,
continue one_off=one_off)
for name in names:
log.warn( log.warn(
"Compose found a found a container named %s without any " "Compose found a found a container named %s without any "
"labels. As of compose 1.3.0 containers are identified with " "labels. As of compose 1.3.0 containers are identified with "
@ -41,6 +42,20 @@ def check_for_legacy_containers(
"`docker-compose migrate-to-labels`" % (name,)) "`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 name.startswith(prefix):
yield name
def add_labels(project, container, name): def add_labels(project, container, name):
project_name, service_name, one_off, number = NAME_RE.match(name).groups() project_name, service_name, one_off, number = NAME_RE.match(name).groups()
if project_name != project.name or service_name not in project.service_names: if project_name != project.name or service_name not in project.service_names:

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))