2015-01-12 15:59:05 +01:00
|
|
|
from compose.project import sort_service_dicts, DependencyError
|
2014-04-25 23:58:21 +02:00
|
|
|
from .. import unittest
|
2014-01-23 23:27:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SortServiceTest(unittest.TestCase):
|
|
|
|
def test_sort_service_dicts_1(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['redis'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'grunt'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'redis'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
|
|
|
self.assertEqual(len(sorted_services), 3)
|
|
|
|
self.assertEqual(sorted_services[0]['name'], 'grunt')
|
|
|
|
self.assertEqual(sorted_services[1]['name'], 'redis')
|
|
|
|
self.assertEqual(sorted_services[2]['name'], 'web')
|
|
|
|
|
|
|
|
def test_sort_service_dicts_2(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['redis', 'postgres'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'postgres',
|
|
|
|
'links': ['redis']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'redis'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
|
|
|
self.assertEqual(len(sorted_services), 3)
|
|
|
|
self.assertEqual(sorted_services[0]['name'], 'redis')
|
|
|
|
self.assertEqual(sorted_services[1]['name'], 'postgres')
|
|
|
|
self.assertEqual(sorted_services[2]['name'], 'web')
|
|
|
|
|
2014-02-12 10:09:55 +01:00
|
|
|
def test_sort_service_dicts_3(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'name': 'child'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'parent',
|
|
|
|
'links': ['child']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'links': ['parent'],
|
|
|
|
'name': 'grandparent'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
|
|
|
self.assertEqual(len(sorted_services), 3)
|
|
|
|
self.assertEqual(sorted_services[0]['name'], 'child')
|
|
|
|
self.assertEqual(sorted_services[1]['name'], 'parent')
|
|
|
|
self.assertEqual(sorted_services[2]['name'], 'grandparent')
|
|
|
|
|
2014-01-23 23:27:54 +01:00
|
|
|
def test_sort_service_dicts_circular_imports(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['redis'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'redis',
|
|
|
|
'links': ['web']
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
|
|
|
sort_service_dicts(services)
|
|
|
|
except DependencyError as e:
|
|
|
|
self.assertIn('redis', e.msg)
|
|
|
|
self.assertIn('web', e.msg)
|
|
|
|
else:
|
|
|
|
self.fail('Should have thrown an DependencyError')
|
|
|
|
|
|
|
|
def test_sort_service_dicts_circular_imports_2(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['postgres', 'redis'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'redis',
|
|
|
|
'links': ['web']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'postgres'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
|
|
|
sort_service_dicts(services)
|
|
|
|
except DependencyError as e:
|
|
|
|
self.assertIn('redis', e.msg)
|
|
|
|
self.assertIn('web', e.msg)
|
|
|
|
else:
|
|
|
|
self.fail('Should have thrown an DependencyError')
|
|
|
|
|
2014-02-12 10:09:55 +01:00
|
|
|
def test_sort_service_dicts_circular_imports_3(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['b'],
|
|
|
|
'name': 'a'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'b',
|
|
|
|
'links': ['c']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'c',
|
|
|
|
'links': ['a']
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
|
|
|
sort_service_dicts(services)
|
|
|
|
except DependencyError as e:
|
|
|
|
self.assertIn('a', e.msg)
|
|
|
|
self.assertIn('b', e.msg)
|
|
|
|
else:
|
|
|
|
self.fail('Should have thrown an DependencyError')
|
|
|
|
|
2014-01-23 23:27:54 +01:00
|
|
|
def test_sort_service_dicts_self_imports(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['web'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
|
|
|
sort_service_dicts(services)
|
|
|
|
except DependencyError as e:
|
|
|
|
self.assertIn('web', e.msg)
|
|
|
|
else:
|
|
|
|
self.fail('Should have thrown an DependencyError')
|