Esempio n. 1
0
class ResilienceTest(DockerClientTestCase):
    def setUp(self):
        self.db = self.create_service(
            'db',
            volumes=[VolumeSpec.parse('/var/db')],
            command='top')
        self.project = Project('composetest', [self.db], self.client)

        container = self.db.create_container()
        container.start()
        self.host_path = container.get_mount('/var/db')['Source']

    def test_successful_recreate(self):
        self.project.up(strategy=ConvergenceStrategy.always)
        container = self.db.containers()[0]
        self.assertEqual(container.get_mount('/var/db')['Source'], self.host_path)

    def test_create_failure(self):
        with mock.patch('compose.service.Service.create_container', crash):
            with self.assertRaises(Crash):
                self.project.up(strategy=ConvergenceStrategy.always)

        self.project.up()
        container = self.db.containers()[0]
        self.assertEqual(container.get_mount('/var/db')['Source'], self.host_path)

    def test_start_failure(self):
        with mock.patch('compose.container.Container.start', crash):
            with self.assertRaises(Crash):
                self.project.up(strategy=ConvergenceStrategy.always)

        self.project.up()
        container = self.db.containers()[0]
        self.assertEqual(container.get_mount('/var/db')['Source'], self.host_path)
Esempio n. 2
0
    def test_no_warning_with_no_swarm_info(self):
        self.mock_client.info.return_value = {}
        project = Project('composetest', [], self.mock_client)

        with mock.patch('compose.project.log') as fake_log:
            project.up()
            assert fake_log.warn.call_count == 0
Esempio n. 3
0
    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'
Esempio n. 4
0
    def test_no_warning_in_normal_mode(self):
        self.mock_client.info.return_value = {'Swarm': {'LocalNodeState': 'inactive'}}
        project = Project('composetest', [], self.mock_client)

        with mock.patch('compose.project.log') as fake_log:
            project.up()
            assert fake_log.warn.call_count == 0
Esempio n. 5
0
class ResilienceTest(DockerClientTestCase):
    def setUp(self):
        self.db = self.create_service('db', volumes=['/var/db'], command='top')
        self.project = Project('composetest', [self.db], self.client)

        container = self.db.create_container()
        self.db.start_container(container)
        self.host_path = container.get('Volumes')['/var/db']

    def test_successful_recreate(self):
        self.project.up(force_recreate=True)
        container = self.db.containers()[0]
        self.assertEqual(container.get('Volumes')['/var/db'], self.host_path)

    def test_create_failure(self):
        with mock.patch('compose.service.Service.create_container', crash):
            with self.assertRaises(Crash):
                self.project.up(force_recreate=True)

        self.project.up()
        container = self.db.containers()[0]
        self.assertEqual(container.get('Volumes')['/var/db'], self.host_path)

    def test_start_failure(self):
        with mock.patch('compose.service.Service.start_container', crash):
            with self.assertRaises(Crash):
                self.project.up(force_recreate=True)

        self.project.up()
        container = self.db.containers()[0]
        self.assertEqual(container.get('Volumes')['/var/db'], self.host_path)
Esempio n. 6
0
 def test_get_services_with_include_links(self):
     db = Service(project="composetest", name="db", image="foo")
     web = Service(project="composetest", name="web", image="foo", links=[(db, "database")])
     cache = Service(project="composetest", name="cache", image="foo")
     console = Service(project="composetest", name="console", image="foo", links=[(web, "web")])
     project = Project("test", [web, db, cache, console], None)
     self.assertEqual(project.get_services(["console"], include_deps=True), [db, web, console])
Esempio n. 7
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)
Esempio n. 8
0
class ResilienceTest(DockerClientTestCase):
    def setUp(self):
        self.db = self.create_service("db", volumes=["/var/db"], command="top")
        self.project = Project("composetest", [self.db], self.client)

        container = self.db.create_container()
        self.db.start_container(container)
        self.host_path = container.get("Volumes")["/var/db"]

    def test_successful_recreate(self):
        self.project.up()
        container = self.db.containers()[0]
        self.assertEqual(container.get("Volumes")["/var/db"], self.host_path)

    def test_create_failure(self):
        with mock.patch("compose.service.Service.create_container", crash):
            with self.assertRaises(Crash):
                self.project.up()

        self.project.up()
        container = self.db.containers()[0]
        self.assertEqual(container.get("Volumes")["/var/db"], self.host_path)

    def test_start_failure(self):
        with mock.patch("compose.service.Service.start_container", crash):
            with self.assertRaises(Crash):
                self.project.up()

        self.project.up()
        container = self.db.containers()[0]
        self.assertEqual(container.get("Volumes")["/var/db"], self.host_path)
Esempio n. 9
0
 def test_get_services_with_include_links(self):
     db = Service(
         project='composetest',
         name='db',
         image='foo',
     )
     web = Service(
         project='composetest',
         name='web',
         image='foo',
         links=[(db, 'database')]
     )
     cache = Service(
         project='composetest',
         name='cache',
         image='foo'
     )
     console = Service(
         project='composetest',
         name='console',
         image='foo',
         links=[(web, 'web')]
     )
     project = Project('test', [web, db, cache, console], None)
     self.assertEqual(
         project.get_services(['console'], include_deps=True),
         [db, web, console]
     )
Esempio n. 10
0
    def test_get_network(self):
        self.require_api_version('1.21')
        client = docker_client(version='1.21')

        network_name = 'network_does_exist'
        project = Project(network_name, [], client)
        client.create_network(network_name)
        assert project.get_network()['name'] == network_name
Esempio n. 11
0
    def test_containers(self):
        web = self.create_service('web')
        db = self.create_service('db')
        project = Project('composetest', [web, db], self.client)

        project.up()

        containers = project.containers()
        self.assertEqual(len(containers), 2)
Esempio n. 12
0
 def test_get_service(self):
     web = Service(
         project='composetest',
         name='web',
         client=None,
         image="busybox:latest",
     )
     project = Project('test', [web], None)
     self.assertEqual(project.get_service('web'), web)
Esempio n. 13
0
    def test_containers(self):
        web = self.create_service("web")
        db = self.create_service("db")
        project = Project("composetest", [web, db], self.client)

        project.up()

        containers = project.containers()
        self.assertEqual(len(containers), 2)
Esempio n. 14
0
 def test_uses_default_network_container(self):
     container = mock.Mock(id='test')
     web = Service(
         'web',
         project='test',
         image="alpine",
         net=ContainerNet(container))
     project = Project('test', [web], None)
     assert not project.uses_default_network()
Esempio n. 15
0
    def test_containers_with_service_names(self):
        web = self.create_service("web")
        db = self.create_service("db")
        project = Project("composetest", [web, db], self.client)

        project.up()

        containers = project.containers(["web"])
        self.assertEqual([c.name for c in containers], ["composetest_web_1"])
Esempio n. 16
0
    def test_containers_with_extra_service(self):
        web = self.create_service("web")
        web_1 = web.create_container()

        db = self.create_service("db")
        db_1 = db.create_container()

        self.create_service("extra").create_container()

        project = Project("composetest", [web, db], self.client)
        self.assertEqual(set(project.containers(stopped=True)), set([web_1, db_1]))
Esempio n. 17
0
    def test_containers_with_service_names(self):
        web = self.create_service('web')
        db = self.create_service('db')
        project = Project('composetest', [web, db], self.client)

        project.up()

        containers = project.containers(['web'])
        self.assertEqual(
            [c.name for c in containers],
            ['composetest_web_1'])
Esempio n. 18
0
    def test_create(self):
        web = self.create_service('web')
        db = self.create_service('db', volumes=[VolumeSpec.parse('/var/db')])
        project = Project('composetest', [web, db], self.client)

        project.create(['db'])
        self.assertEqual(len(project.containers()), 0)
        self.assertEqual(len(project.containers(stopped=True)), 1)
        self.assertEqual(len(db.containers()), 0)
        self.assertEqual(len(db.containers(stopped=True)), 1)
        self.assertEqual(len(web.containers(stopped=True)), 0)
Esempio n. 19
0
 def test_get_services_returns_listed_services_with_args(self):
     web = Service(
         project='composetest',
         name='web',
     )
     console = Service(
         project='composetest',
         name='console',
     )
     project = Project('test', [web, console], None)
     self.assertEqual(project.get_services(['console']), [console])
Esempio n. 20
0
    def test_create_with_links(self):
        db = self.create_service('db')
        web = self.create_service('web', links=[(db, 'db')])
        project = Project('composetest', [db, web], self.client)

        project.create(['web'])
        self.assertEqual(len(project.containers()), 0)
        self.assertEqual(len(project.containers(stopped=True)), 2)
        self.assertEqual(len(db.containers()), 0)
        self.assertEqual(len(db.containers(stopped=True)), 1)
        self.assertEqual(len(web.containers()), 0)
        self.assertEqual(len(web.containers(stopped=True)), 1)
Esempio n. 21
0
class ProjectTest(DockerClientTestCase):

    def setUp(self):
        super(ProjectTest, self).setUp()

        db = self.create_service('db')
        web = self.create_service('web', links=[(db, 'db')])
        nginx = self.create_service('nginx', links=[(web, 'web')])

        self.services = [db, web, nginx]
        self.project = Project('composetest', self.services, self.client)

        # Create a legacy container for each service
        for service in self.services:
            service.ensure_image_exists()
            container = self.client.create_container(
                name='{}_{}_1'.format(self.project.name, service.name),
                **service.options
            )
            self.client.start(container)

        # Create a single one-off legacy container
        self.client.create_container(
            name='{}_{}_run_1'.format(self.project.name, self.services[0].name),
            **self.services[0].options
        )

    def get_legacy_containers(self, **kwargs):
        return list(legacy.get_legacy_containers(
            self.client,
            self.project.name,
            [s.name for s in self.services],
            **kwargs
        ))

    def test_get_legacy_container_names(self):
        self.assertEqual(len(self.get_legacy_containers()), len(self.services))

    def test_get_legacy_container_names_one_off(self):
        self.assertEqual(len(self.get_legacy_containers(stopped=True, one_off=True)), 1)

    def test_migration_to_labels(self):
        with self.assertRaises(legacy.LegacyContainersError) as cm:
            self.assertEqual(self.project.containers(stopped=True), [])

        self.assertEqual(
            set(cm.exception.names),
            set(['composetest_db_1', 'composetest_web_1', 'composetest_nginx_1']),
        )

        legacy.migrate_project_to_labels(self.project)
        self.assertEqual(len(self.project.containers(stopped=True)), len(self.services))
Esempio n. 22
0
    def test_project_up_with_custom_network(self):
        self.require_api_version('1.21')
        client = docker_client(version='1.21')
        network_name = 'composetest-custom'

        client.create_network(network_name)
        self.addCleanup(client.remove_network, network_name)

        web = self.create_service('web', net=Net(network_name))
        project = Project('composetest', [web], client, use_networking=True)
        project.up()

        assert project.get_network() is None
Esempio n. 23
0
 def test_get_services_returns_all_services_without_args(self):
     web = Service(
         project='composetest',
         name='web',
         image='foo',
     )
     console = Service(
         project='composetest',
         name='console',
         image='foo',
     )
     project = Project('test', [web, console], None)
     self.assertEqual(project.get_services(), [web, console])
Esempio n. 24
0
 def test_get_services_returns_listed_services_with_args(self):
     web = Service(
         project='composetest',
         name='web',
         image='foo',
     )
     console = Service(
         project='composetest',
         name='console',
         image='foo',
     )
     project = Project('test', [web, console], None)
     assert project.get_services(['console']) == [console]
Esempio n. 25
0
    def test_migration_to_labels(self):
        web = self.create_service('web')
        db = self.create_service('db')
        project = Project('composetest', [web, db], self.client)

        self.client.create_container(name='composetest_web_1', **web.options)
        self.client.create_container(name='composetest_db_1', **db.options)

        with mock.patch.object(service, 'log', autospec=True) as mock_log:
            self.assertEqual(project.containers(stopped=True), [])
            self.assertEqual(mock_log.warn.call_count, 2)

        migration.migrate_project_to_labels(project)
        self.assertEqual(len(project.containers(stopped=True)), 2)
Esempio n. 26
0
class ProjectTest(DockerClientTestCase):

    def setUp(self):
        super(ProjectTest, self).setUp()

        self.services = [
            self.create_service('web'),
            self.create_service('db'),
        ]

        self.project = Project('composetest', self.services, self.client)

        # Create a legacy container for each service
        for service in self.services:
            service.ensure_image_exists()
            self.client.create_container(
                name='{}_{}_1'.format(self.project.name, service.name),
                **service.options
            )

        # Create a single one-off legacy container
        self.client.create_container(
            name='{}_{}_run_1'.format(self.project.name, self.services[0].name),
            **self.services[0].options
        )

    def get_names(self, **kwargs):
        if 'stopped' not in kwargs:
            kwargs['stopped'] = True

        return list(legacy.get_legacy_container_names(
            self.client,
            self.project.name,
            [s.name for s in self.services],
            **kwargs
        ))

    def test_get_legacy_container_names(self):
        self.assertEqual(len(self.get_names()), len(self.services))

    def test_get_legacy_container_names_one_off(self):
        self.assertEqual(len(self.get_names(one_off=True)), 1)

    def test_migration_to_labels(self):
        with mock.patch.object(legacy, 'log', autospec=True) as mock_log:
            self.assertEqual(self.project.containers(stopped=True), [])
            self.assertEqual(mock_log.warn.call_count, len(self.services))

        legacy.migrate_project_to_labels(self.project)
        self.assertEqual(len(self.project.containers(stopped=True)), len(self.services))
Esempio n. 27
0
    def test_containers_with_extra_service(self):
        web = self.create_service('web')
        web_1 = web.create_container()

        db = self.create_service('db')
        db_1 = db.create_container()

        self.create_service('extra').create_container()

        project = Project('composetest', [web, db], self.client)
        self.assertEqual(
            set(project.containers(stopped=True)),
            set([web_1, db_1]),
        )
Esempio n. 28
0
 def test_get_services_removes_duplicates_following_links(self):
     db = Service(
         project='composetest',
         name='db',
         image='foo',
     )
     web = Service(
         project='composetest',
         name='web',
         image='foo',
         links=[(db, 'database')]
     )
     project = Project('test', [web, db], None)
     assert project.get_services(['web', 'db'], include_deps=True) == [db, web]
Esempio n. 29
0
    def test_create_strategy_never(self):
        db = self.create_service('db')
        project = Project('composetest', [db], self.client)
        project.create(['db'])
        old_id = project.containers(stopped=True)[0].id

        project.create(['db'], strategy=ConvergenceStrategy.never)
        self.assertEqual(len(project.containers()), 0)
        self.assertEqual(len(project.containers(stopped=True)), 1)

        db_container = project.containers(stopped=True)[0]
        self.assertEqual(db_container.id, old_id)
Esempio n. 30
0
    def setUp(self):
        self.db = self.create_service('db', volumes=['/var/db'], command='top')
        self.project = Project('composetest', [self.db], self.client)

        container = self.db.create_container()
        self.db.start_container(container)
        self.host_path = container.get('Volumes')['/var/db']
Esempio n. 31
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 not project.uses_default_network()
Esempio n. 32
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=None,
             services=[{
                 'name': 'test',
                 'image': 'busybox:latest',
                 'volumes_from': [VolumeFromSpec('aaa', 'rw', 'container')]
             }],
             networks=None,
             volumes=None,
         ),
     )
     assert project.get_service('test')._get_volumes_from() == [container_id + ":rw"]
Esempio n. 33
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_dicts('test', [{
            'name': 'aaa',
            'image': 'busybox:latest'
        }, {
            'name': 'test',
            'image': 'busybox:latest',
            'net': 'container:aaa'
        }], self.mock_client)

        service = project.get_service('test')
        self.assertEqual(service.net.mode, 'container:' + container_name)
Esempio n. 34
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)
Esempio n. 35
0
    def test_uses_default_network_true(self):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=build_config(
                services=[
                    {
                        'name': 'foo',
                        'image': BUSYBOX_IMAGE_WITH_TAG
                    },
                ],
                networks=None,
                volumes=None,
                secrets=None,
                configs=None,
            ),
        )

        assert 'default' in project.networks.networks
Esempio n. 36
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")
Esempio n. 37
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({}))
Esempio n. 38
0
 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({})
Esempio n. 39
0
 def test_volumes_from_service(self):
     service_dicts = build_service_dicts({
         'data': {
             'image': 'busybox:latest',
             'volumes': ['/var/data'],
         },
         'db': {
             'image': 'busybox:latest',
             'volumes_from': ['data'],
         },
     })
     project = Project.from_dicts(
         name='composetest',
         service_dicts=service_dicts,
         client=self.client,
     )
     db = project.get_service('db')
     data = project.get_service('data')
     self.assertEqual(db.volumes_from, [VolumeFromSpec(data, 'rw')])
Esempio n. 40
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')
Esempio n. 41
0
 def test_volumes_from_service(self):
     service_dicts = config.from_dictionary({
         'data': {
             'image': 'busybox:latest',
             'volumes': ['/var/data'],
         },
         'db': {
             'image': 'busybox:latest',
             'volumes_from': ['data'],
         },
     }, working_dir='.')
     project = Project.from_dicts(
         name='composetest',
         service_dicts=service_dicts,
         client=self.client,
     )
     db = project.get_service('db')
     data = project.get_service('data')
     self.assertEqual(db.volumes_from, [data])
Esempio n. 42
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')
Esempio n. 43
0
    def test_build_container_operation_with_timeout_func_does_not_mutate_options_with_timeout(
            self):
        config_data = Config(
            project_name='',
            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)
Esempio n. 44
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(
             project_name='',
             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']
Esempio n. 45
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:latest'
                    },
                ],
                networks=None,
                volumes=None,
                secrets=None,
                configs=None,
            ),
        )

        assert 'default' in project.networks.networks
Esempio n. 46
0
    def test_use_net_from_service(self):
        container_name = 'test_aaa_1'
        mock_client = mock.create_autospec(docker.Client)
        mock_client.containers.return_value = [{
            "Name": container_name,
            "Names": [container_name],
            "Id": container_name,
            "Image": 'busybox:latest'
        }]
        project = Project.from_dicts('test', [{
            'name': 'aaa',
            'image': 'busybox:latest'
        }, {
            'name': 'test',
            'image': 'busybox:latest',
            'net': 'container:aaa'
        }], mock_client)

        service = project.get_service('test')
        self.assertEqual(service._get_net(), 'container:' + container_name)
Esempio n. 47
0
 def test_from_config_v2(self):
     config = build_config(
         services=[
             {
                 'name': 'web',
                 'image': BUSYBOX_IMAGE_WITH_TAG,
             },
             {
                 'name': 'db',
                 'image': BUSYBOX_IMAGE_WITH_TAG,
             },
         ],
         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
Esempio n. 48
0
    def test_initialize_volumes_invalid_volume_driver(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: {'driver': 'foobar'}},
            networks={},
        )

        project = Project.from_config(
            name='composetest',
            config_data=config_data, client=self.client
        )
        with self.assertRaises(config.ConfigurationError):
            project.volumes.initialize()
Esempio n. 49
0
    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'])
Esempio n. 50
0
 def test_volumes_from_container(self):
     data_container = Container.create(
         self.client,
         image='busybox:latest',
         volumes=['/var/data'],
         name='composetest_data_container',
         labels={LABEL_PROJECT: 'composetest'},
     )
     project = Project.from_dicts(
         name='composetest',
         service_dicts=config.from_dictionary({
             'db': {
                 'image': 'busybox:latest',
                 'volumes_from': ['composetest_data_container'],
             },
         }),
         client=self.client,
     )
     db = project.get_service('db')
     self.assertEqual(db.volumes_from, [data_container])
Esempio n. 51
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_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"])
Esempio n. 52
0
    def test_down_with_no_resources(self):
        project = Project.from_config(
            name='test',
            client=self.mock_client,
            config_data=build_config(
                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)
Esempio n. 53
0
 def test_volumes_from_container(self):
     data_container = Container.create(
         self.client,
         image='busybox:latest',
         volumes=['/var/data'],
         name='composetest_data_container',
         labels={LABEL_PROJECT: 'composetest'},
     )
     project = Project.from_config(
         name='composetest',
         config_data=build_config({
             'db': {
                 'image': 'busybox:latest',
                 'volumes_from': ['composetest_data_container'],
             },
         }),
         client=self.client,
     )
     db = project.get_service('db')
     self.assertEqual(db._get_volumes_from(), [data_container.id + ':rw'])
Esempio n. 54
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(detached=True)
     service_container = project.get_service('web').containers(stopped=True)[0]
     assert service_container.inspect()['HostConfig']['Isolation'] == 'default'
Esempio n. 55
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,
             secrets=None,
             configs=None,
         ),
     )
     self.assertEqual([c.id for c in project.containers()], ['1'])
Esempio n. 56
0
 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
Esempio n. 57
0
    def test_uses_default_network_false(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': {'custom': None}
                    },
                ],
                networks={'custom': {}},
                volumes=None,
                secrets=None,
                configs=None,
            ),
        )

        assert 'default' not in project.networks.networks
Esempio n. 58
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'
Esempio n. 59
0
    def test_initialize_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: {}},
            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')
Esempio n. 60
0
    def test_net_from_service_v1(self):
        project = Project.from_config(
            name='composetest',
            config_data=build_config({
                'net': {
                    'image': 'busybox:latest',
                    'command': ["top"]
                },
                'web': {
                    'image': 'busybox:latest',
                    'net': 'container:net',
                    'command': ["top"]
                },
            }),
            client=self.client,
        )

        project.up()

        web = project.get_service('web')
        net = project.get_service('net')
        self.assertEqual(web.network_mode.mode, 'container:' + net.containers()[0].id)