2015-05-21 12:25:06 +02:00
|
|
|
from compose import legacy
|
|
|
|
from compose.project import Project
|
|
|
|
from .testcases import DockerClientTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectTest(DockerClientTestCase):
|
|
|
|
|
2015-05-21 13:12:02 +02:00
|
|
|
def setUp(self):
|
|
|
|
super(ProjectTest, self).setUp()
|
|
|
|
|
|
|
|
self.services = [
|
2015-05-21 12:25:06 +02:00
|
|
|
self.create_service('web'),
|
|
|
|
self.create_service('db'),
|
|
|
|
]
|
|
|
|
|
2015-05-21 13:12:02 +02:00
|
|
|
self.project = Project('composetest', self.services, self.client)
|
2015-05-21 12:25:06 +02:00
|
|
|
|
2015-05-21 13:12:30 +02:00
|
|
|
# Create a legacy container for each service
|
2015-05-21 13:12:02 +02:00
|
|
|
for service in self.services:
|
2015-05-21 12:25:06 +02:00
|
|
|
service.ensure_image_exists()
|
|
|
|
self.client.create_container(
|
2015-05-21 13:12:02 +02:00
|
|
|
name='{}_{}_1'.format(self.project.name, service.name),
|
2015-05-21 12:25:06 +02:00
|
|
|
**service.options
|
|
|
|
)
|
|
|
|
|
2015-05-21 13:12:30 +02:00
|
|
|
# Create a single one-off legacy container
|
|
|
|
self.client.create_container(
|
|
|
|
name='{}_{}_run_1'.format(self.project.name, self.services[0].name),
|
|
|
|
**self.services[0].options
|
|
|
|
)
|
|
|
|
|
2015-05-21 13:12:02 +02:00
|
|
|
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))
|
|
|
|
|
2015-05-21 13:12:30 +02:00
|
|
|
def test_get_legacy_container_names_one_off(self):
|
|
|
|
self.assertEqual(len(self.get_names(one_off=True)), 1)
|
|
|
|
|
2015-05-21 13:12:02 +02:00
|
|
|
def test_migration_to_labels(self):
|
2015-05-21 13:41:36 +02:00
|
|
|
with self.assertRaises(legacy.LegacyContainersError) as cm:
|
2015-05-21 13:12:02 +02:00
|
|
|
self.assertEqual(self.project.containers(stopped=True), [])
|
2015-05-21 13:41:36 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(cm.exception.names),
|
|
|
|
set(['composetest_web_1', 'composetest_db_1']),
|
|
|
|
)
|
2015-05-21 12:25:06 +02:00
|
|
|
|
2015-05-21 13:12:02 +02:00
|
|
|
legacy.migrate_project_to_labels(self.project)
|
|
|
|
self.assertEqual(len(self.project.containers(stopped=True)), len(self.services))
|