def test_config_dict(self): self.mock_client.inspect_image.return_value = {'Id': 'abcd'} service = Service( 'foo', image='example.com/foo', client=self.mock_client, network_mode=ServiceNetworkMode(Service('other')), networks={'default': None}, links=[(Service('one'), 'one')], volumes_from=[VolumeFromSpec(Service('two'), 'rw', 'service')]) config_dict = service.config_dict() expected = { 'image_id': 'abcd', 'options': { 'image': 'example.com/foo' }, 'links': [('one', 'one')], 'net': 'other', 'networks': { 'default': None }, 'volumes_from': [('two', 'rw')], } assert config_dict == expected
def test_use_volumes_from_container(self): container_id = 'aabbccddee' container_dict = dict(Name='aaa', Id=container_id) self.mock_client.inspect_container.return_value = container_dict project = Project.from_config( name='test', client=self.mock_client, config_data=Config( version=V2_0, services=[{ 'name': 'test', 'image': 'busybox:latest', 'volumes_from': [VolumeFromSpec('aaa', 'rw', 'container')] }], networks=None, volumes=None, secrets=None, configs=None, ), ) assert project.get_service('test')._get_volumes_from() == [ container_id + ":rw" ]
def test_use_volumes_from_service_no_container(self): container_name = 'test_vol_1' self.mock_client.containers.return_value = [{ "Name": container_name, "Names": [container_name], "Id": container_name, "Image": 'busybox:latest' }] project = Project.from_config( name='test', client=self.mock_client, config_data=Config( version=V2_0, services=[{ 'name': 'vol', 'image': 'busybox:latest' }, { 'name': 'test', 'image': 'busybox:latest', 'volumes_from': [VolumeFromSpec('vol', 'rw', 'service')] }], networks=None, volumes=None, secrets=None, configs=None, ), ) assert project.get_service('test')._get_volumes_from() == [ container_name + ":rw" ]
def test_use_volumes_from_service_container(self): container_ids = ['aabbccddee', '12345'] project = Project.from_config( name='test', client=None, config_data=Config( version=V2_0, services=[{ 'name': 'vol', 'image': 'busybox:latest' }, { 'name': 'test', 'image': 'busybox:latest', 'volumes_from': [VolumeFromSpec('vol', 'rw', 'service')] }], networks=None, volumes=None, secrets=None, configs=None, ), ) with mock.patch.object(Service, 'containers') as mock_return: mock_return.return_value = [ mock.Mock(id=container_id, spec=Container) for container_id in container_ids ] assert (project.get_service('test')._get_volumes_from() == [ container_ids[0] + ':rw' ])
def test_use_volumes_from_service_no_container(self): container_name = 'test_vol_1' self.mock_client.containers.return_value = [{ "Name": container_name, "Names": [container_name], "Id": container_name, "Image": BUSYBOX_IMAGE_WITH_TAG }] project = Project.from_config( name='test', client=self.mock_client, config_data=build_config( services=[{ 'name': 'vol', 'image': BUSYBOX_IMAGE_WITH_TAG }, { 'name': 'test', 'image': BUSYBOX_IMAGE_WITH_TAG, 'volumes_from': [VolumeFromSpec('vol', 'rw', 'service')] }], networks=None, volumes=None, secrets=None, configs=None, imports=None, ), ) assert project.get_service('test')._get_volumes_from() == [ container_name + ":rw" ]
def test_sort_service_dicts_7(self): services = [ { 'net': 'container:three', 'name': 'four' }, { 'links': ['two'], 'name': 'three' }, { 'name': 'two', 'volumes_from': [VolumeFromSpec('one', 'rw')] }, { 'name': 'one' } ] sorted_services = sort_service_dicts(services) self.assertEqual(len(sorted_services), 4) self.assertEqual(sorted_services[0]['name'], 'one') self.assertEqual(sorted_services[1]['name'], 'two') self.assertEqual(sorted_services[2]['name'], 'three') self.assertEqual(sorted_services[3]['name'], 'four')
def test_get_volumes_from_container_read_only(self): container_id = 'aabbccddee' service = Service( 'test', image='foo', volumes_from=[VolumeFromSpec(mock.Mock(id=container_id, spec=Container), 'ro')]) self.assertEqual(service._get_volumes_from(), [container_id + ':ro'])
def test_get_volumes_from_service_container_exists(self): container_ids = ['aabbccddee', '12345'] from_service = mock.create_autospec(Service) from_service.containers.return_value = [ mock.Mock(id=container_id, spec=Container) for container_id in container_ids ] service = Service('test', volumes_from=[VolumeFromSpec(from_service, 'rw')], image='foo') self.assertEqual(service._get_volumes_from(), [container_ids[0] + ":rw"])
def test_get_volumes_from_service_container_exists_with_flags(self): for mode in ['ro', 'rw', 'z', 'rw,z', 'z,rw']: container_ids = ['aabbccddee:' + mode, '12345:' + mode] from_service = mock.create_autospec(Service) from_service.containers.return_value = [ mock.Mock(id=container_id.split(':')[0], spec=Container) for container_id in container_ids ] service = Service('test', volumes_from=[VolumeFromSpec(from_service, mode)], image='foo') self.assertEqual(service._get_volumes_from(), [container_ids[0]])
def test_get_volumes_from_container(self): container_id = 'aabbccddee' service = Service('test', image='foo', volumes_from=[ VolumeFromSpec( mock.Mock(id=container_id, spec=Container), 'rw', 'container') ]) assert service._get_volumes_from() == [container_id + ':rw']
def test_get_volumes_from_service_no_container(self): container_id = 'abababab' from_service = mock.create_autospec(Service) from_service.containers.return_value = [] from_service.create_container.return_value = mock.Mock( id=container_id, spec=Container) service = Service('test', image='foo', volumes_from=[VolumeFromSpec(from_service, 'rw')]) self.assertEqual(service._get_volumes_from(), [container_id + ':rw']) from_service.create_container.assert_called_once_with()
def test_create_container_with_volumes_from(self): volume_service = self.create_service('data') volume_container_1 = volume_service.create_container() volume_container_2 = Container.create( self.client, image='busybox:latest', command=["top"], labels={LABEL_PROJECT: 'composetest'}, ) host_service = self.create_service( 'host', volumes_from=[ VolumeFromSpec(volume_service, 'rw', 'service'), VolumeFromSpec(volume_container_2, 'rw', 'container') ]) host_container = host_service.create_container() host_service.start_container(host_container) self.assertIn(volume_container_1.id + ':rw', host_container.get('HostConfig.VolumesFrom')) self.assertIn(volume_container_2.id + ':rw', host_container.get('HostConfig.VolumesFrom'))
def test_use_volumes_from_container(self): container_id = 'aabbccddee' container_dict = dict(Name='aaa', Id=container_id) self.mock_client.inspect_container.return_value = container_dict project = Project.from_dicts( 'test', [{ 'name': 'test', 'image': 'busybox:latest', 'volumes_from': [VolumeFromSpec('aaa', 'rw')] }], self.mock_client) self.assertEqual( project.get_service('test')._get_volumes_from(), [container_id + ":rw"])
def test_sort_service_dicts_6(self): services = [{ 'links': ['parent'], 'name': 'grandparent' }, { 'name': 'parent', 'volumes_from': [VolumeFromSpec('child', 'ro', 'service')] }, { 'name': 'child' }] sorted_services = sort_service_dicts(services) assert len(sorted_services) == 3 assert sorted_services[0]['name'] == 'child' assert sorted_services[1]['name'] == 'parent' assert sorted_services[2]['name'] == 'grandparent'
def test_sort_service_dicts_6(self): services = [{ 'links': ['parent'], 'name': 'grandparent' }, { 'name': 'parent', 'volumes_from': [VolumeFromSpec('child', 'ro')] }, { 'name': 'child' }] 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')
def test_volumes_from_service(self): project = Project.from_config( name='composetest', config_data=build_config({ 'data': { 'image': 'busybox:latest', 'volumes': ['/var/data'], }, 'db': { 'image': 'busybox:latest', 'volumes_from': ['data'], }, }), client=self.client, ) db = project.get_service('db') data = project.get_service('data') self.assertEqual(db.volumes_from, [VolumeFromSpec(data, 'rw', 'service')])
def test_config_hash_matches_label(self): self.mock_client.inspect_image.return_value = {'Id': 'abcd'} service = Service( 'foo', image='example.com/foo', client=self.mock_client, network_mode=NetworkMode('bridge'), networks={'bridge': {}}, links=[(Service('one', client=self.mock_client), 'one')], volumes_from=[VolumeFromSpec(Service('two', client=self.mock_client), 'rw', 'service')] ) config_hash = service.config_hash for api_version in set(API_VERSIONS.values()): self.mock_client.api_version = api_version assert service._get_container_create_options({}, 1)['labels'][LABEL_CONFIG_HASH] == ( config_hash )
def test_use_volumes_from_service_no_container(self): container_name = 'test_vol_1' self.mock_client.containers.return_value = [{ "Name": container_name, "Names": [container_name], "Id": container_name, "Image": 'busybox:latest' }] project = Project.from_dicts( 'test', [{ 'name': 'vol', 'image': 'busybox:latest' }, { 'name': 'test', 'image': 'busybox:latest', 'volumes_from': [VolumeFromSpec('vol', 'rw')] }], self.mock_client) self.assertEqual( project.get_service('test')._get_volumes_from(), [container_name + ":rw"])
def test_use_volumes_from_service_container(self): container_ids = ['aabbccddee', '12345'] project = Project.from_dicts( 'test', [{ 'name': 'vol', 'image': 'busybox:latest' }, { 'name': 'test', 'image': 'busybox:latest', 'volumes_from': [VolumeFromSpec('vol', 'rw')] }], None) with mock.patch.object(Service, 'containers') as mock_return: mock_return.return_value = [ mock.Mock(id=container_id, spec=Container) for container_id in container_ids ] self.assertEqual( project.get_service('test')._get_volumes_from(), [container_ids[0] + ':rw'])
def test_sort_service_dicts_7(self): services = [{ 'net': 'container:three', 'name': 'four' }, { 'links': ['two'], 'name': 'three' }, { 'name': 'two', 'volumes_from': [VolumeFromSpec('one', 'rw', 'service')] }, { 'name': 'one' }] sorted_services = sort_service_dicts(services) 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'
def test_parse_v2_from_service(self): volume_from = VolumeFromSpec.parse('servicea', self.services, V2_0) assert volume_from == VolumeFromSpec('servicea', 'rw', 'service')
def test_parse_v2_from_container_with_mode(self): volume_from = VolumeFromSpec.parse('container:foo:ro', self.services, V2_0) assert volume_from == VolumeFromSpec('foo', 'ro', 'container')
def test_parse_v2_from_service_with_mode(self): volume_from = VolumeFromSpec.parse('servicea:ro', self.services, V2) assert volume_from == VolumeFromSpec('servicea', 'ro', 'service')
def test_parse_v1_from_container(self): volume_from = VolumeFromSpec.parse('foo:ro', self.services, V1) assert volume_from == VolumeFromSpec('foo', 'ro', 'container')
def test_parse_v2_from_container(self): volume_from = VolumeFromSpec.parse('container:foo', self.services, V2) assert volume_from == VolumeFromSpec('foo', 'rw', 'container')