2015-10-30 21:22:51 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-01-19 21:41:45 +01:00
|
|
|
import pytest
|
|
|
|
|
2015-11-17 19:35:28 +01:00
|
|
|
from compose.config.errors import DependencyError
|
|
|
|
from compose.config.sort_services import sort_service_dicts
|
2015-11-14 00:20:09 +01:00
|
|
|
from compose.config.types import VolumeFromSpec
|
2014-01-23 23:27:54 +01:00
|
|
|
|
|
|
|
|
2016-01-19 21:52:17 +01:00
|
|
|
class TestSortService(object):
|
2014-01-23 23:27:54 +01:00
|
|
|
def test_sort_service_dicts_1(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['redis'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'grunt'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'redis'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert len(sorted_services) == 3
|
|
|
|
assert sorted_services[0]['name'] == 'grunt'
|
|
|
|
assert sorted_services[1]['name'] == 'redis'
|
|
|
|
assert sorted_services[2]['name'] == 'web'
|
2014-01-23 23:27:54 +01:00
|
|
|
|
|
|
|
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)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert len(sorted_services) == 3
|
|
|
|
assert sorted_services[0]['name'] == 'redis'
|
|
|
|
assert sorted_services[1]['name'] == 'postgres'
|
|
|
|
assert sorted_services[2]['name'] == 'web'
|
2014-01-23 23:27:54 +01:00
|
|
|
|
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)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert len(sorted_services) == 3
|
|
|
|
assert sorted_services[0]['name'] == 'child'
|
|
|
|
assert sorted_services[1]['name'] == 'parent'
|
|
|
|
assert sorted_services[2]['name'] == 'grandparent'
|
2014-02-12 10:09:55 +01:00
|
|
|
|
2015-03-06 22:30:56 +01:00
|
|
|
def test_sort_service_dicts_4(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'name': 'child'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'parent',
|
2016-01-13 20:41:34 +01:00
|
|
|
'volumes_from': [VolumeFromSpec('child', 'rw', 'service')]
|
2015-03-06 22:30:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'links': ['parent'],
|
|
|
|
'name': 'grandparent'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert len(sorted_services) == 3
|
|
|
|
assert sorted_services[0]['name'] == 'child'
|
|
|
|
assert sorted_services[1]['name'] == 'parent'
|
|
|
|
assert sorted_services[2]['name'] == 'grandparent'
|
2015-03-06 22:30:56 +01:00
|
|
|
|
|
|
|
def test_sort_service_dicts_5(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['parent'],
|
|
|
|
'name': 'grandparent'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'parent',
|
2016-01-25 13:45:30 +01:00
|
|
|
'network_mode': 'service:child'
|
2015-03-06 22:30:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'child'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert len(sorted_services) == 3
|
|
|
|
assert sorted_services[0]['name'] == 'child'
|
|
|
|
assert sorted_services[1]['name'] == 'parent'
|
|
|
|
assert sorted_services[2]['name'] == 'grandparent'
|
2015-03-06 22:30:56 +01:00
|
|
|
|
|
|
|
def test_sort_service_dicts_6(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['parent'],
|
|
|
|
'name': 'grandparent'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'parent',
|
2016-01-13 20:41:34 +01:00
|
|
|
'volumes_from': [VolumeFromSpec('child', 'ro', 'service')]
|
2015-03-06 22:30:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'child'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert len(sorted_services) == 3
|
|
|
|
assert sorted_services[0]['name'] == 'child'
|
|
|
|
assert sorted_services[1]['name'] == 'parent'
|
|
|
|
assert sorted_services[2]['name'] == 'grandparent'
|
2015-03-06 22:30:56 +01:00
|
|
|
|
|
|
|
def test_sort_service_dicts_7(self):
|
|
|
|
services = [
|
|
|
|
{
|
2016-01-25 13:45:30 +01:00
|
|
|
'network_mode': 'service:three',
|
2015-03-06 22:30:56 +01:00
|
|
|
'name': 'four'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'links': ['two'],
|
|
|
|
'name': 'three'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'two',
|
2016-01-13 20:41:34 +01:00
|
|
|
'volumes_from': [VolumeFromSpec('one', 'rw', 'service')]
|
2015-03-06 22:30:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'one'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
sorted_services = sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert len(sorted_services) == 4
|
|
|
|
assert sorted_services[0]['name'] == 'one'
|
|
|
|
assert sorted_services[1]['name'] == 'two'
|
|
|
|
assert sorted_services[2]['name'] == 'three'
|
|
|
|
assert sorted_services[3]['name'] == 'four'
|
2015-03-06 22:30:56 +01:00
|
|
|
|
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']
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2016-01-19 21:52:17 +01:00
|
|
|
with pytest.raises(DependencyError) as exc:
|
2014-01-23 23:27:54 +01:00
|
|
|
sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert 'redis' in exc.exconly()
|
|
|
|
assert 'web' in exc.exconly()
|
2014-01-23 23:27:54 +01:00
|
|
|
|
|
|
|
def test_sort_service_dicts_circular_imports_2(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['postgres', 'redis'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'redis',
|
|
|
|
'links': ['web']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'postgres'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2016-01-19 21:52:17 +01:00
|
|
|
with pytest.raises(DependencyError) as exc:
|
2014-01-23 23:27:54 +01:00
|
|
|
sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert 'redis' in exc.exconly()
|
|
|
|
assert 'web' in exc.exconly()
|
2014-01-23 23:27:54 +01:00
|
|
|
|
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']
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2016-01-19 21:52:17 +01:00
|
|
|
with pytest.raises(DependencyError) as exc:
|
2014-02-12 10:09:55 +01:00
|
|
|
sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert 'a' in exc.exconly()
|
|
|
|
assert 'b' in exc.exconly()
|
2014-02-12 10:09:55 +01:00
|
|
|
|
2014-01-23 23:27:54 +01:00
|
|
|
def test_sort_service_dicts_self_imports(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'links': ['web'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2016-01-19 21:52:17 +01:00
|
|
|
with pytest.raises(DependencyError) as exc:
|
2014-01-23 23:27:54 +01:00
|
|
|
sort_service_dicts(services)
|
2016-01-19 21:52:17 +01:00
|
|
|
assert 'web' in exc.exconly()
|
2016-01-19 21:41:45 +01:00
|
|
|
|
|
|
|
def test_sort_service_dicts_depends_on_self(self):
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
'depends_on': ['web'],
|
|
|
|
'name': 'web'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
with pytest.raises(DependencyError) as exc:
|
|
|
|
sort_service_dicts(services)
|
|
|
|
assert 'A service can not depend on itself: web' in exc.exconly()
|