Пример #1
0
    def test_project_up_with_network_label(self):
        self.require_api_version('1.23')

        network_name = 'network_with_label'

        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'networks': {network_name: None}
            }],
            volumes={},
            networks={
                network_name: {'labels': {'label_key': 'label_val'}}
            }
        )

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

        project.up()

        networks = [
            n for n in self.client.networks()
            if n['Name'].startswith('composetest_')
        ]

        assert [n['Name'] for n in networks] == ['composetest_{}'.format(network_name)]

        assert networks[0]['Labels'] == {'label_key': 'label_val'}
Пример #2
0
    def test_project_up_updated_driver(self):
        vol_name = '{0:x}'.format(random.getrandbits(32))
        full_vol_name = 'composetest_{0}'.format(vol_name)

        config_data = config.Config(
            version=2, services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top'
            }], volumes={vol_name: {'driver': 'local'}}
        )
        project = Project.from_config(
            name='composetest',
            config_data=config_data, client=self.client
        )
        project.initialize_volumes()

        volume_data = self.client.inspect_volume(full_vol_name)
        self.assertEqual(volume_data['Name'], full_vol_name)
        self.assertEqual(volume_data['Driver'], 'local')

        config_data = config_data._replace(
            volumes={vol_name: {'driver': 'smb'}}
        )
        project = Project.from_config(
            name='composetest',
            config_data=config_data, client=self.client
        )
        with self.assertRaises(config.ConfigurationError) as e:
            project.initialize_volumes()
        assert 'Configuration for volume {0} specifies driver smb'.format(
            vol_name
        ) in str(e.exception)
Пример #3
0
    def test_up_with_network_link_local_ips(self):
        config_data = config.Config(
            version=V2_1,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'networks': {
                    'linklocaltest': {
                        'link_local_ips': ['169.254.8.8']
                    }
                }
            }],
            volumes={},
            networks={'linklocaltest': {
                'driver': 'bridge'
            }})
        project = Project.from_config(client=self.client,
                                      name='composetest',
                                      config_data=config_data)
        project.up(detached=True)

        service_container = project.get_service('web').containers(
            stopped=True)[0]
        ipam_config = service_container.inspect().get(
            'NetworkSettings', {}).get('Networks',
                                       {}).get('composetest_linklocaltest',
                                               {}).get('IPAMConfig', {})
        assert 'LinkLocalIPs' in ipam_config
        assert ipam_config['LinkLocalIPs'] == ['169.254.8.8']
Пример #4
0
    def test_initialize_volumes_external_volumes(self):
        # Use composetest_ prefix so it gets garbage-collected in tearDown()
        vol_name = 'composetest_{0:x}'.format(random.getrandbits(32))
        full_vol_name = 'composetest_{0}'.format(vol_name)
        self.client.create_volume(vol_name)
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top'
            }],
            volumes={
                vol_name: {'external': True, 'external_name': vol_name}
            },
            networks=None,
        )
        project = Project.from_config(
            name='composetest',
            config_data=config_data, client=self.client
        )
        project.volumes.initialize()

        with self.assertRaises(NotFound):
            self.client.inspect_volume(full_vol_name)
Пример #5
0
    def test_up_with_network_static_addresses_missing_subnet(self):
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'networks': {
                    'static_test': {
                        'ipv4_address': '172.16.100.100',
                        'ipv6_address': 'fe80::1001:101'
                    }
                },
            }],
            volumes={},
            networks={
                'static_test': {
                    'driver': 'bridge',
                    'driver_opts': {
                        "com.docker.network.enable_ipv6": "true",
                    },
                    'ipam': {
                        'driver': 'default',
                    },
                },
            },
        )

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

        assert len(project.up()) == 0
Пример #6
0
    def test_project_up_with_network_internal(self):
        self.require_api_version('1.23')
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'networks': {
                    'internal': None
                },
            }],
            volumes={},
            networks={
                'internal': {
                    'driver': 'bridge',
                    'internal': True
                },
            },
        )

        project = Project.from_config(
            client=self.client,
            name='composetest',
            config_data=config_data,
        )
        project.up()

        network = self.client.networks(names=['composetest_internal'])[0]

        assert network['Internal'] is True
Пример #7
0
    def test_project_up_networks(self):
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top',
                'networks': {'foo': None, 'bar': None, 'baz': None},
            }],
            volumes={},
            networks={
                'foo': {'driver': 'bridge'},
                'bar': {'driver': None},
                'baz': {},
            },
        )

        project = Project.from_config(
            client=self.client,
            name='composetest',
            config_data=config_data,
        )
        project.up()
        self.assertEqual(len(project.containers()), 1)

        for net_name in ['foo', 'bar', 'baz']:
            full_net_name = 'composetest_{}'.format(net_name)
            network_data = self.client.inspect_network(full_net_name)
            self.assertEqual(network_data['Name'], full_net_name)

        foo_data = self.client.inspect_network('composetest_foo')
        self.assertEqual(foo_data['Driver'], 'bridge')
Пример #8
0
    def test_project_up_volumes(self):
        vol_name = '{0:x}'.format(random.getrandbits(32))
        full_vol_name = 'composetest_{0}'.format(vol_name)
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top'
            }],
            volumes={vol_name: {
                'driver': 'local'
            }},
            networks={},
        )

        project = Project.from_config(name='composetest',
                                      config_data=config_data,
                                      client=self.client)
        project.up()
        self.assertEqual(len(project.containers()), 1)

        volume_data = self.client.inspect_volume(full_vol_name)
        self.assertEqual(volume_data['Name'], full_vol_name)
        self.assertEqual(volume_data['Driver'], 'local')
Пример #9
0
    def test_initialize_volumes_updated_blank_driver(self):
        vol_name = '{0:x}'.format(random.getrandbits(32))
        full_vol_name = 'composetest_{0}'.format(vol_name)

        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top'
            }],
            volumes={vol_name: {
                'driver': 'local'
            }},
            networks={},
        )
        project = Project.from_config(name='composetest',
                                      config_data=config_data,
                                      client=self.client)
        project.volumes.initialize()

        volume_data = self.client.inspect_volume(full_vol_name)
        self.assertEqual(volume_data['Name'], full_vol_name)
        self.assertEqual(volume_data['Driver'], 'local')

        config_data = config_data._replace(volumes={vol_name: {}})
        project = Project.from_config(name='composetest',
                                      config_data=config_data,
                                      client=self.client)
        project.volumes.initialize()
        volume_data = self.client.inspect_volume(full_vol_name)
        self.assertEqual(volume_data['Name'], full_vol_name)
        self.assertEqual(volume_data['Driver'], 'local')
Пример #10
0
    def test_up_with_ipam_config(self):
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'networks': {'front': None},
            }],
            volumes={},
            networks={
                'front': {
                    'driver': 'bridge',
                    'driver_opts': {
                        "com.docker.network.bridge.enable_icc": "false",
                    },
                    'ipam': {
                        'driver': 'default',
                        'config': [{
                            "subnet": "172.28.0.0/16",
                            "ip_range": "172.28.5.0/24",
                            "gateway": "172.28.5.254",
                            "aux_addresses": {
                                "a": "172.28.1.5",
                                "b": "172.28.1.6",
                                "c": "172.28.1.7",
                            },
                        }],
                    },
                },
            },
        )

        project = Project.from_config(
            client=self.client,
            name='composetest',
            config_data=config_data,
        )
        project.up()

        network = self.client.networks(names=['composetest_front'])[0]

        assert network['Options'] == {
            "com.docker.network.bridge.enable_icc": "false"
        }

        assert network['IPAM'] == {
            'Driver': 'default',
            'Options': None,
            'Config': [{
                'Subnet': "172.28.0.0/16",
                'IPRange': "172.28.5.0/24",
                'Gateway': "172.28.5.254",
                'AuxiliaryAddresses': {
                    'a': '172.28.1.5',
                    'b': '172.28.1.6',
                    'c': '172.28.1.7',
                },
            }],
        }
Пример #11
0
    def test_project_up_networks(self):
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top',
                'networks': {
                    'foo': None,
                    'bar': None,
                    'baz': {
                        'aliases': ['extra']
                    },
                },
            }],
            volumes={},
            networks={
                'foo': {
                    'driver': 'bridge'
                },
                'bar': {
                    'driver': None
                },
                'baz': {},
            },
        )

        project = Project.from_config(
            client=self.client,
            name='composetest',
            config_data=config_data,
        )
        project.up()

        containers = project.containers()
        assert len(containers) == 1
        container, = containers

        for net_name in ['foo', 'bar', 'baz']:
            full_net_name = 'composetest_{}'.format(net_name)
            network_data = self.client.inspect_network(full_net_name)
            assert network_data['Name'] == full_net_name

        aliases_key = 'NetworkSettings.Networks.{net}.Aliases'
        assert 'web' in container.get(
            aliases_key.format(net='composetest_foo'))
        assert 'web' in container.get(
            aliases_key.format(net='composetest_baz'))
        assert 'extra' in container.get(
            aliases_key.format(net='composetest_baz'))

        foo_data = self.client.inspect_network('composetest_foo')
        assert foo_data['Driver'] == 'bridge'
Пример #12
0
    def test_up_with_network_static_addresses(self):
        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top',
                'networks': {
                    'static_test': {
                        'ipv4_address': '172.16.100.100',
                        'ipv6_address': 'fe80::1001:102'
                    }
                },
            }],
            volumes={},
            networks={
                'static_test': {
                    'driver': 'bridge',
                    'driver_opts': {
                        "com.docker.network.enable_ipv6": "true",
                    },
                    'ipam': {
                        'driver': 'default',
                        'config': [
                            {"subnet": "172.16.100.0/24",
                             "gateway": "172.16.100.1"},
                            {"subnet": "fe80::/64",
                             "gateway": "fe80::1001:1"}
                        ]
                    }
                }
            }
        )
        project = Project.from_config(
            client=self.client,
            name='composetest',
            config_data=config_data,
        )
        project.up(detached=True)

        network = self.client.networks(names=['static_test'])[0]
        service_container = project.get_service('web').containers()[0]

        assert network['Options'] == {
            "com.docker.network.enable_ipv6": "true"
        }

        IPAMConfig = (service_container.inspect().get('NetworkSettings', {}).
                      get('Networks', {}).get('composetest_static_test', {}).
                      get('IPAMConfig', {}))
        assert IPAMConfig.get('IPv4Address') == '172.16.100.100'
        assert IPAMConfig.get('IPv6Address') == 'fe80::1001:102'
Пример #13
0
 def test_up_with_invalid_isolation(self):
     self.require_api_version('1.24')
     config_data = config.Config(version=V2_1,
                                 services=[{
                                     'name': 'web',
                                     'image': 'busybox:latest',
                                     'isolation': 'foobar'
                                 }],
                                 volumes={},
                                 networks={})
     project = Project.from_config(client=self.client,
                                   name='composetest',
                                   config_data=config_data)
     with self.assertRaises(ProjectError):
         project.up()
Пример #14
0
    def test_project_up_invalid_volume_driver(self):
        vol_name = '{0:x}'.format(random.getrandbits(32))

        config_data = config.Config(
            version=2, services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top'
            }], volumes={vol_name: {'driver': 'foobar'}}
        )

        project = Project.from_config(
            name='composetest',
            config_data=config_data, client=self.client
        )
        with self.assertRaises(config.ConfigurationError):
            project.initialize_volumes()
Пример #15
0
    def test_up_with_enable_ipv6(self):
        self.require_api_version('1.23')
        config_data = config.Config(version=V2_0,
                                    services=[{
                                        'name': 'web',
                                        'image': 'busybox:latest',
                                        'command': 'top',
                                        'networks': {
                                            'static_test': {
                                                'ipv6_address':
                                                'fe80::1001:102'
                                            }
                                        },
                                    }],
                                    volumes={},
                                    networks={
                                        'static_test': {
                                            'driver': 'bridge',
                                            'enable_ipv6': True,
                                            'ipam': {
                                                'driver':
                                                'default',
                                                'config': [{
                                                    "subnet":
                                                    "fe80::/64",
                                                    "gateway":
                                                    "fe80::1001:1"
                                                }]
                                            }
                                        }
                                    })
        project = Project.from_config(
            client=self.client,
            name='composetest',
            config_data=config_data,
        )
        project.up(detached=True)
        network = self.client.networks(names=['static_test'])[0]
        service_container = project.get_service('web').containers()[0]

        assert network['EnableIPv6'] is True
        ipam_config = (service_container.inspect().get(
            'NetworkSettings', {}).get('Networks',
                                       {}).get('composetest_static_test',
                                               {}).get('IPAMConfig', {}))
        assert ipam_config.get('IPv6Address') == 'fe80::1001:102'
Пример #16
0
 def test_up_with_isolation(self):
     self.require_api_version('1.24')
     config_data = config.Config(version=V2_1,
                                 services=[{
                                     'name': 'web',
                                     'image': 'busybox:latest',
                                     'isolation': 'default'
                                 }],
                                 volumes={},
                                 networks={})
     project = Project.from_config(client=self.client,
                                   name='composetest',
                                   config_data=config_data)
     project.up()
     service_container = project.get_service('web').containers()[0]
     assert service_container.inspect(
     )['HostConfig']['Isolation'] == 'default'
Пример #17
0
    def test_project_up_implicit_volume_driver(self):
        vol_name = '{0:x}'.format(random.getrandbits(32))
        full_vol_name = 'composetest_{0}'.format(vol_name)
        config_data = config.Config(version=2,
                                    services=[{
                                        'name': 'web',
                                        'image': 'busybox:latest',
                                        'command': 'top'
                                    }],
                                    volumes={vol_name: {}})

        project = Project.from_config(name='composetest',
                                      config_data=config_data,
                                      client=self.client)
        project.up()

        volume_data = self.client.inspect_volume(full_vol_name)
        self.assertEqual(volume_data['Name'], full_vol_name)
        self.assertEqual(volume_data['Driver'], 'local')
Пример #18
0
    def test_project_up_with_volume_labels(self):
        self.require_api_version('1.23')

        volume_name = 'volume_with_label'

        config_data = config.Config(
            version=V2_0,
            services=[{
                'name':
                'web',
                'image':
                'busybox:latest',
                'volumes': [VolumeSpec.parse('{}:/data'.format(volume_name))]
            }],
            volumes={volume_name: {
                'labels': {
                    'label_key': 'label_val'
                }
            }},
            networks={},
        )

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

        project.up()

        volumes = [
            v for v in self.client.volumes().get('Volumes', [])
            if v['Name'].startswith('composetest_')
        ]

        assert [v['Name']
                for v in volumes] == ['composetest_{}'.format(volume_name)]

        assert 'label_key' in volumes[0]['Labels']
        assert volumes[0]['Labels']['label_key'] == 'label_val'
Пример #19
0
    def test_initialize_volumes_inexistent_external_volume(self):
        vol_name = '{0:x}'.format(random.getrandbits(32))

        config_data = config.Config(
            version=V2_0,
            services=[{
                'name': 'web',
                'image': 'busybox:latest',
                'command': 'top'
            }],
            volumes={vol_name: {
                'external': True,
                'external_name': vol_name
            }},
            networks=None,
        )
        project = Project.from_config(name='composetest',
                                      config_data=config_data,
                                      client=self.client)
        with self.assertRaises(config.ConfigurationError) as e:
            project.volumes.initialize()
        assert 'Volume {0} declared as external'.format(vol_name) in str(
            e.exception)