Example #1
0
def generate(config, bootnode_config, wrkchain_id):
    wrkchain = config['wrkchain']

    services = []
    if bootnode_config['type'] == 'dedicated':
        services.append(bootnode(bootnode_config['nodes']))

    nodes = generate_nodes(wrkchain['nodes'], bootnode_config, wrkchain_id)
    services = services + nodes

    if config['wrkchain']['chaintest']['use']:
        services = services + [chaintest(config['wrkchain']['chaintest'])]

    networks = {
        'wrkchainnet': {
            'driver': 'bridge',
            'ipam': {
                'config': [{
                    'subnet': config['docker_network']['subnet']
                }]
            }
        }
    }

    config = Config(version=COMPOSE_VERSION,
                    services=services,
                    volumes=[],
                    networks=networks,
                    secrets=[],
                    configs=[])
    return serialize_config(config, None)
Example #2
0
 def test_container_without_name(self):
     self.mock_client.containers.return_value = [
         {'Image': BUSYBOX_IMAGE_WITH_TAG, 'Id': '1', 'Name': '1'},
         {'Image': BUSYBOX_IMAGE_WITH_TAG, 'Id': '2', 'Name': None},
         {'Image': BUSYBOX_IMAGE_WITH_TAG, 'Id': '3'},
     ]
     self.mock_client.inspect_container.return_value = {
         'Id': '1',
         'Config': {
             'Labels': {
                 LABEL_SERVICE: 'web',
             },
         },
     }
     project = Project.from_config(
         name='test',
         client=self.mock_client,
         config_data=Config(
             version=V2_0,
             services=[{
                 'name': 'web',
                 'image': BUSYBOX_IMAGE_WITH_TAG,
             }],
             networks=None,
             volumes=None,
             secrets=None,
             configs=None,
         ),
     )
     assert [c.id for c in project.containers()] == ['1']
Example #3
0
    def test_error_parallel_pull(self, mock_write):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=Config(
                project_name='',
                version=V2_0,
                services=[{
                    'name': 'web',
                    'image': BUSYBOX_IMAGE_WITH_TAG,
                }],
                networks=None,
                volumes=None,
                secrets=None,
                configs=None,
            ),
        )

        self.mock_client.pull.side_effect = OperationFailedError('pull error')
        with pytest.raises(ProjectError):
            project.pull(parallel_pull=True)

        self.mock_client.pull.side_effect = OperationFailedError(b'pull error')
        with pytest.raises(ProjectError):
            project.pull(parallel_pull=True)
Example #4
0
    def test_down_with_no_resources(self):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=Config(
                project_name='',
                version=V2_0,
                services=[{
                    'name': 'web',
                    'image': BUSYBOX_IMAGE_WITH_TAG,
                }],
                networks={'default': {}},
                volumes={'data': {}},
                secrets=None,
                configs=None,
            ),
        )
        self.mock_client.remove_network.side_effect = NotFound(
            None, None, 'oops')
        self.mock_client.remove_volume.side_effect = NotFound(
            None, None, 'oops')

        project.down(ImageType.all, True)
        self.mock_client.remove_image.assert_called_once_with(
            BUSYBOX_IMAGE_WITH_TAG)
Example #5
0
 def test_use_net_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(
             project_name='',
             version=V2_0,
             services=[
                 {
                     'name': 'test',
                     'image': BUSYBOX_IMAGE_WITH_TAG,
                     'network_mode': 'container:aaa'
                 },
             ],
             networks=None,
             volumes=None,
             secrets=None,
             configs=None,
         ),
     )
     service = project.get_service('test')
     assert service.network_mode.mode == 'container:' + container_id
Example #6
0
    def test_build_container_operation_with_timeout_func_does_not_mutate_options_with_timeout(self):
        config_data = Config(
            version=V3_7,
            services=[
                {'name': 'web', 'image': BUSYBOX_IMAGE_WITH_TAG},
                {'name': 'db', 'image': BUSYBOX_IMAGE_WITH_TAG, 'stop_grace_period': '1s'},
            ],
            networks={}, volumes={}, secrets=None, configs=None,
        )

        project = Project.from_config(name='test', client=self.mock_client, config_data=config_data)

        stop_op = project.build_container_operation_with_timeout_func('stop', options={})

        web_container = mock.create_autospec(Container, service='web')
        db_container = mock.create_autospec(Container, service='db')

        # `stop_grace_period` is not set to 'web' service,
        # then it is stopped with the default timeout.
        stop_op(web_container)
        web_container.stop.assert_called_once_with(timeout=DEFAULT_TIMEOUT)

        # `stop_grace_period` is set to 'db' service,
        # then it is stopped with the specified timeout and
        # the value is not overridden by the previous function call.
        stop_op(db_container)
        db_container.stop.assert_called_once_with(timeout=1)
Example #7
0
def test_to_bundle():
    image_digests = {'a': 'aaaa', 'b': 'bbbb'}
    services = [
        {'name': 'a', 'build': '.', },
        {'name': 'b', 'build': './b'},
    ]
    config = Config(
        version=2,
        services=services,
        volumes={'special': {}},
        networks={'extra': {}})

    with mock.patch('compose.bundle.log.warn', autospec=True) as mock_log:
        output = bundle.to_bundle(config, image_digests)

    assert mock_log.mock_calls == [
        mock.call("Unsupported top level key 'networks' - ignoring"),
        mock.call("Unsupported top level key 'volumes' - ignoring"),
    ]

    assert output == {
        'Version': '0.1',
        'Services': {
            'a': {'Image': 'aaaa', 'Networks': ['default']},
            'b': {'Image': 'bbbb', 'Networks': ['default']},
        }
    }
Example #8
0
    def test_use_net_from_service(self):
        container_name = 'test_aaa_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': 'aaa',
                        'image': 'busybox:latest'
                    },
                    {
                        'name': 'test',
                        'image': 'busybox:latest',
                        'network_mode': 'service:aaa'
                    },
                ],
                networks=None,
                volumes=None,
                secrets=None,
                configs=None,
            ),
        )

        service = project.get_service('test')
        self.assertEqual(service.network_mode.mode,
                         'container:' + container_name)
 def test_from_config_v1(self):
     config = Config(
         version=V1,
         services=[
             {
                 'name': 'web',
                 'image': 'busybox:latest',
             },
             {
                 'name': 'db',
                 'image': 'busybox:latest',
             },
         ],
         networks=None,
         volumes=None,
         secrets=None,
         configs=None,
     )
     project = Project.from_config(
         name='composetest',
         config_data=config,
         client=None,
     )
     assert len(project.services) == 2
     assert project.get_service('web').name == 'web'
     assert project.get_service('web').options['image'] == 'busybox:latest'
     assert project.get_service('db').name == 'db'
     assert project.get_service('db').options['image'] == 'busybox:latest'
     assert not project.networks.use_networking
Example #10
0
 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"
     ]
Example #11
0
 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"
     ]
Example #12
0
 def test_from_config_v1(self):
     config = Config(
         version=V1,
         services=[
             {
                 'name': 'web',
                 'image': 'busybox:latest',
             },
             {
                 'name': 'db',
                 'image': 'busybox:latest',
             },
         ],
         networks=None,
         volumes=None,
         secrets=None,
         configs=None,
     )
     project = Project.from_config(
         name='composetest',
         config_data=config,
         client=None,
     )
     self.assertEqual(len(project.services), 2)
     self.assertEqual(project.get_service('web').name, 'web')
     self.assertEqual(
         project.get_service('web').options['image'], 'busybox:latest')
     self.assertEqual(project.get_service('db').name, 'db')
     self.assertEqual(
         project.get_service('db').options['image'], 'busybox:latest')
     self.assertFalse(project.networks.use_networking)
Example #13
0
    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'
            ])
Example #14
0
 def test_container_without_name(self):
     self.mock_client.containers.return_value = [
         {'Image': 'busybox:latest', 'Id': '1', 'Name': '1'},
         {'Image': 'busybox:latest', 'Id': '2', 'Name': None},
         {'Image': 'busybox:latest', 'Id': '3'},
     ]
     self.mock_client.inspect_container.return_value = {
         'Id': '1',
         'Config': {
             'Labels': {
                 LABEL_SERVICE: 'web',
             },
         },
     }
     project = Project.from_config(
         name='test',
         client=self.mock_client,
         config_data=Config(
             version=None,
             services=[{
                 'name': 'web',
                 'image': 'busybox:latest',
             }],
             networks=None,
             volumes=None,
         ),
     )
     self.assertEqual([c.id for c in project.containers()], ['1'])
Example #15
0
 def test_use_net_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',
                     'network_mode': 'container:aaa'
                 },
             ],
             networks=None,
             volumes=None,
             secrets=None,
             configs=None,
         ),
     )
     service = project.get_service('test')
     self.assertEqual(service.network_mode.mode,
                      'container:' + container_id)
Example #16
0
 def test_container_without_name(self):
     self.mock_client.containers.return_value = [
         {
             'Image': 'busybox:latest',
             'Id': '1',
             'Name': '1'
         },
         {
             'Image': 'busybox:latest',
             'Id': '2',
             'Name': None
         },
         {
             'Image': 'busybox:latest',
             'Id': '3'
         },
     ]
     self.mock_client.inspect_container.return_value = {
         'Id': '1',
         'Config': {
             'Labels': {
                 LABEL_SERVICE: 'web',
             },
         },
     }
     project = Project.from_config(
         'test',
         Config(None, [{
             'name': 'web',
             'image': 'busybox:latest',
         }], None),
         self.mock_client,
     )
     self.assertEqual([c.id for c in project.containers()], ['1'])
    def test_project_platform_value(self):
        service_config = {
            'name': 'web',
            'image': 'busybox:latest',
        }
        config_data = Config(version=V2_4,
                             services=[service_config],
                             networks={},
                             volumes={},
                             secrets=None,
                             configs=None)

        project = Project.from_config(name='test',
                                      client=self.mock_client,
                                      config_data=config_data)
        assert project.get_service('web').platform is None

        project = Project.from_config(name='test',
                                      client=self.mock_client,
                                      config_data=config_data,
                                      default_platform='windows')
        assert project.get_service('web').platform == 'windows'

        service_config['platform'] = 'linux/s390x'
        project = Project.from_config(name='test',
                                      client=self.mock_client,
                                      config_data=config_data)
        assert project.get_service('web').platform == 'linux/s390x'

        project = Project.from_config(name='test',
                                      client=self.mock_client,
                                      config_data=config_data,
                                      default_platform='windows')
        assert project.get_service('web').platform == 'linux/s390x'
Example #18
0
def build_config(**kwargs):
    return Config(
        version=kwargs.get('version', VERSION),
        services=kwargs.get('services'),
        volumes=kwargs.get('volumes'),
        networks=kwargs.get('networks'),
        secrets=kwargs.get('secrets'),
        configs=kwargs.get('configs'),
    )
Example #19
0
 def test_net_unset(self):
     project = Project.from_config(
         'test',
         Config(None, [{
             'name': 'test',
             'image': 'busybox:latest',
         }], None), self.mock_client)
     service = project.get_service('test')
     self.assertEqual(service.net.id, None)
     self.assertNotIn('NetworkMode', service._get_container_host_config({}))
Example #20
0
 def test_use_net_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(
         'test',
         Config(None, [{
             'name': 'test',
             'image': 'busybox:latest',
             'net': 'container:aaa'
         }], None), self.mock_client)
     service = project.get_service('test')
     self.assertEqual(service.net.mode, 'container:' + container_id)
Example #21
0
 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(
         'test',
         Config(None, [{
             'name': 'test',
             'image': 'busybox:latest',
             'volumes_from': [VolumeFromSpec('aaa', 'rw')]
         }], None), self.mock_client)
     self.assertEqual(
         project.get_service('test')._get_volumes_from(),
         [container_id + ":rw"])
Example #22
0
 def test_net_unset(self):
     project = Project.from_config(
         name='test',
         client=self.mock_client,
         config_data=Config(
             version=None,
             services=[{
                 'name': 'test',
                 'image': 'busybox:latest',
             }],
             networks=None,
             volumes=None,
         ),
     )
     service = project.get_service('test')
     self.assertEqual(service.network_mode.id, None)
     self.assertNotIn('NetworkMode', service._get_container_host_config({}))
Example #23
0
    def test_uses_default_network_true(self):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=Config(
                version=2,
                services=[
                    {
                        'name': 'foo',
                        'image': 'busybox:latest'
                    },
                ],
                networks=None,
                volumes=None,
            ),
        )

        assert 'default' in project.networks.networks
Example #24
0
    def test_down_with_no_resources(self):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=Config(
                version='2',
                services=[{
                    'name': 'web',
                    'image': 'busybox:latest',
                }],
                networks={'default': {}},
                volumes={'data': {}},
            ),
        )
        self.mock_client.remove_network.side_effect = NotFound(None, None, 'oops')
        self.mock_client.remove_volume.side_effect = NotFound(None, None, 'oops')

        project.down(ImageType.all, True)
        self.mock_client.remove_image.assert_called_once_with("busybox:latest")
Example #25
0
 def test_from_config(self):
     dicts = Config(None, [
         {
             'name': 'web',
             'image': 'busybox:latest',
         },
         {
             'name': 'db',
             'image': 'busybox:latest',
         },
     ], None)
     project = Project.from_config('composetest', dicts, None)
     self.assertEqual(len(project.services), 2)
     self.assertEqual(project.get_service('web').name, 'web')
     self.assertEqual(
         project.get_service('web').options['image'], 'busybox:latest')
     self.assertEqual(project.get_service('db').name, 'db')
     self.assertEqual(
         project.get_service('db').options['image'], 'busybox:latest')
Example #26
0
    def test_uses_default_network_false(self):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=Config(
                version=2,
                services=[
                    {
                        'name': 'foo',
                        'image': 'busybox:latest',
                        'networks': ['custom']
                    },
                ],
                networks={'custom': {}},
                volumes=None,
            ),
        )

        assert 'default' not in project.networks.networks
Example #27
0
 def test_from_config_v2(self):
     config = Config(
         version=2,
         services=[
             {
                 'name': 'web',
                 'image': 'busybox:latest',
             },
             {
                 'name': 'db',
                 'image': 'busybox:latest',
             },
         ],
         networks=None,
         volumes=None,
     )
     project = Project.from_config('composetest', config, None)
     self.assertEqual(len(project.services), 2)
     self.assertTrue(project.networks.use_networking)
 def test_net_unset(self):
     project = Project.from_config(
         name='test',
         client=self.mock_client,
         config_data=Config(
             version=V1,
             services=[{
                 'name': 'test',
                 'image': 'busybox:latest',
             }],
             networks=None,
             volumes=None,
             secrets=None,
             configs=None,
         ),
     )
     service = project.get_service('test')
     assert service.network_mode.id is None
     assert 'NetworkMode' not in service._get_container_host_config({})
Example #29
0
    def test_uses_default_network_true(self):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=Config(
                version=V2_0,
                services=[
                    {
                        'name': 'foo',
                        'image': BUSYBOX_IMAGE_WITH_TAG
                    },
                ],
                networks=None,
                volumes=None,
                secrets=None,
                configs=None,
            ),
        )

        assert 'default' in project.networks.networks
 def test_from_config_v2(self):
     config = Config(
         version=V2_0,
         services=[
             {
                 'name': 'web',
                 'image': 'busybox:latest',
             },
             {
                 'name': 'db',
                 'image': 'busybox:latest',
             },
         ],
         networks=None,
         volumes=None,
         secrets=None,
         configs=None,
     )
     project = Project.from_config('composetest', config, None)
     assert len(project.services) == 2
     assert project.networks.use_networking