Beispiel #1
0
 def setUp(self):
     test_map = ContainerMap('test_map',
                             initial=TEST_MAP_DATA,
                             check_integrity=False)
     dependency_items = list(test_map.dependency_items())
     self.f_res = ContainerDependencyResolver(dependency_items)
     self.r_res = ContainerDependentsResolver(dependency_items)
 def setUp(self):
     self.maxDiff = 2048
     self.map_name = 'main'
     self.sample_map1 = ContainerMap('main', MAP_DATA_1_NEW)
     self.sample_map2 = ContainerMap('main', MAP_DATA_1)
     self.client_version1 = CLIENT_DATA_1['version']
     self.client_version2 = CLIENT_DATA_2['version']
     self.sample_client_config1 = client_config1 = ClientConfiguration(**CLIENT_DATA_1)
     self.sample_client_config2 = client_config2 = ClientConfiguration(**CLIENT_DATA_2)
     self.policy = BasePolicy({'main': self.sample_map1}, {'__default__': client_config1,
                                                           'legacy': client_config2})
     self.runner = DockerClientRunner(self.policy, {})
 def setUp(self):
     self.maxDiff = 2048
     self.map_name = "main"
     self.sample_map = ContainerMap("main", MAP_DATA_1)
     self.client_version = CLIENT_DATA_1["version"]
     self.sample_client_config = ClientConfiguration(**CLIENT_DATA_1)
     self.policy = BasePolicy({"main": self.sample_map}, {"__default__": self.sample_client_config})
     self.runner = DockerClientRunner(self.policy, {})
 def setUp(self):
     test_map = ContainerMap('test_map', initial=TEST_MAP_DATA, check_integrity=False)
     dependency_items = list(test_map.dependency_items())
     self.f_res = ContainerDependencyResolver(dependency_items)
     self.r_res = ContainerDependentsResolver(dependency_items)
 def setUp(self):
     self.maxDiff = 2048
     self.map_name = 'main'
     self.sample_map = ContainerMap('main', MAP_DATA_1)
     self.sample_map_2 = ContainerMap('main2', MAP_DATA_4)
     self.sample_client_config = ClientConfiguration(**CLIENT_DATA_1)
class TestPolicyClientKwargs(unittest.TestCase):
    def setUp(self):
        self.maxDiff = 2048
        self.map_name = 'main'
        self.sample_map = ContainerMap('main', MAP_DATA_1)
        self.sample_map_2 = ContainerMap('main2', MAP_DATA_4)
        self.sample_client_config = ClientConfiguration(**CLIENT_DATA_1)

    def test_create_kwargs_without_host_config(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_create_kwargs(self.sample_map, cfg_name, cfg, '__default__', self.sample_client_config,
                                              c_name, None, include_host_config=False, kwargs=dict(ports=[22]))
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image='registry.example.com/nginx',
            volumes=['/etc/nginx'],
            environment=[],
            user=None,
            ports=[80, 443, 22],
            hostname='main.web_server',
            domainname=None,
        ))

    def test_host_config_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_host_config_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                                   self.sample_client_config, c_name, None,
                                                   kwargs=dict(binds={'/new_h': {'bind': '/new_c', 'ro': False}}))
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            links={
                'main.app_server.instance1': 'app_server.instance1',
                'main.app_server.instance2': 'app_server.instance2',
            },
            binds={
                '/var/lib/site/config/nginx': {'bind': '/etc/nginx', 'ro': True},
                '/new_h': {'bind': '/new_c', 'ro': False},
            },
            volumes_from=['main.app_server_socket', 'main.web_log'],
            port_bindings={80: 80, 443: 443},
        ))

    def test_create_kwargs_with_host_config(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_server'
        hc_kwargs = dict(binds={'/new_h': {'bind': '/new_c', 'ro': False}})
        kwargs = BasePolicy.get_create_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                              self.sample_client_config, c_name, 'instance1',
                                              include_host_config=True, kwargs=dict(host_config=hc_kwargs))
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image='registry.example.com/app',
            environment=[],
            volumes=[
                '/var/lib/app/config',
                '/var/lib/app/data'
            ],
            user='******',
            hostname='main.app_server',
            domainname=None,
            ports=[8880],
            host_config=create_host_config(
                links={},
                binds={
                    '/var/lib/site/config/app1': {'bind': '/var/lib/app/config', 'ro': True},
                    '/var/lib/site/data/app1': {'bind': '/var/lib/app/data', 'ro': False},
                    '/new_h': {'bind': '/new_c', 'ro': False},
                },
                volumes_from=['main.app_log', 'main.app_server_socket'],
                port_bindings={},
            ),
        ))

    def test_attached_create_kwargs_without_host_config(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_server'
        alias = 'app_server_socket'
        kwargs = BasePolicy.get_attached_create_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                                       self.sample_client_config, c_name, alias,
                                                       include_host_config=True)
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image=BasePolicy.base_image,
            volumes=['/var/lib/app/socket'],
            user='******',
            network_disabled=True,
        ))

    def test_attached_host_config_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_server'
        alias = 'app_server_socket'
        kwargs = BasePolicy.get_attached_host_config_kwargs(self.sample_map, cfg_name,  cfg, '__default__',
                                                            self.sample_client_config, c_name, alias)
        self.assertDictEqual(kwargs, dict(container=c_name))

    def test_attached_preparation_create_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'temp'
        alias = 'app_server_socket'
        v_name = 'main.app_server_socket'
        kwargs = BasePolicy.get_attached_preparation_create_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                                                   self.sample_client_config, c_name, alias,
                                                                   v_name, include_host_config=True)
        self.assertDictEqual(kwargs, dict(
            image=BasePolicy.core_image,
            command='chown -R 2000:2000 /var/lib/app/socket && chmod -R u=rwX,g=rX,o= /var/lib/app/socket',
            user='******',
            host_config=create_host_config(
                volumes_from=[v_name],
            ),
            network_disabled=True,
        ))

    def test_attached_preparation_host_config_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'temp'
        alias = 'app_server_socket'
        v_name = 'main.app_server_socket'
        kwargs = BasePolicy.get_attached_preparation_host_config_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                                                        self.sample_client_config, c_name, alias,
                                                                        v_name)
        self.assertDictEqual(kwargs, dict(
            container='temp',
            volumes_from=[v_name],
        ))

    def test_network_setting(self):
        cfg_name = 'app_extra'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_extra'
        kwargs = BasePolicy.get_host_config_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                                   self.sample_client_config, c_name, None)
        self.assertDictEqual(kwargs, dict(
            binds={},
            container=c_name,
            links={},
            network_mode='main.app_server.instance1',
            port_bindings={},
            volumes_from=[],
        ))

    def test_restart_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_restart_kwargs(self.sample_map, cfg_name, cfg, '__default__', self.sample_client_config,
                                               c_name, None)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            timeout=5,
        ))

    def test_stop_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_stop_kwargs(self.sample_map, cfg_name, cfg, '__default__', self.sample_client_config,
                                            c_name, None)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            timeout=5,
        ))

    def test_remove_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_remove_kwargs(self.sample_map, cfg_name, cfg, '__default__', self.sample_client_config,
                                              c_name, None)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
        ))

    def test_container_environment_as_list_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map_2.get_existing(cfg_name)
        c_name = 'main2.app_server'
        hc_kwargs = dict(binds={'/new_h': {'bind': '/new_c', 'ro': False}})
        kwargs = BasePolicy.get_create_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                              self.sample_client_config, c_name, 'instance1',
                                              include_host_config=True, kwargs=dict(host_config=hc_kwargs))
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image='registry.example.com/app',
            environment=[
                "DBDATA=/dbdata",
                "DBDATA1=/dbdata1"
            ],
            volumes=[
                u'/var/lib/app/config',
                u'/var/lib/app/data'
            ],
            user='******',
            hostname='main2.app_server',
            domainname=None,
            ports=[8880],
            host_config=create_host_config(
                links={},
                binds={
                    '/var/lib/site/config/app1': {'bind': '/var/lib/app/config', 'ro': True},
                    '/var/lib/site/data/app1': {'bind': '/var/lib/app/data', 'ro': False},
                    '/new_h': {'bind': '/new_c', 'ro': False},
                },
                volumes_from=['main.app_log', 'main.app_server_socket'],
                port_bindings={},
            ),
        ))

    def test_container_environment_as_dict_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map_2.get_existing(cfg_name)
        c_name = 'main2.web_server'
        hc_kwargs = dict(binds={'/new_h': {'bind': '/new_c', 'ro': False}})
        kwargs = BasePolicy.get_create_kwargs(self.sample_map, cfg_name, cfg, '__default__',
                                              self.sample_client_config, c_name, 'instance1',
                                              include_host_config=False, kwargs=dict(host_config=hc_kwargs))
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image='registry.example.com/nginx',
            environment=[
                "DBDATA=/dbdata",
                "DBDATA1=/dbdata1"
            ],
            volumes=[u'/etc/nginx'],
            user=None,
            hostname='main2.web_server',
            domainname=None,
            ports=[80,443],

        ))
class TestPolicyClientKwargs(unittest.TestCase):
    def setUp(self):
        self.maxDiff = 2048
        self.map_name = 'main'
        self.sample_map1 = ContainerMap('main', MAP_DATA_1_NEW)
        self.sample_map2 = ContainerMap('main', MAP_DATA_1)
        self.client_version1 = CLIENT_DATA_1['version']
        self.client_version2 = CLIENT_DATA_2['version']
        self.sample_client_config1 = client_config1 = ClientConfiguration(**CLIENT_DATA_1)
        self.sample_client_config2 = client_config2 = ClientConfiguration(**CLIENT_DATA_2)
        self.policy = BasePolicy({'main': self.sample_map1}, {'__default__': client_config1,
                                                              'legacy': client_config2})
        self.runner = DockerClientRunner(self.policy, {})

    def test_create_kwargs_without_host_config(self):
        cfg_name = 'web_server'
        cfg = self.sample_map1.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name)
        c_name = 'main.web_server'
        self.sample_client_config2.features['host_config'] = False
        config = ActionConfig('legacy', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        kwargs = self.runner.get_container_create_kwargs(config, c_name, kwargs=dict(ports=[22]))
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image='registry.example.com/nginx:latest',
            volumes=['/etc/nginx'],
            user=None,
            ports=[80, 443, 22],
            hostname='main-web-server-legacy',
            domainname=None,
        ))

    def test_host_config_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map2.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name)
        c_name = 'main.web_server'
        config = ActionConfig('legacy', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        kwargs = self.runner.get_container_host_config_kwargs(config, c_name,
                                                              kwargs=dict(binds=['/new_h:/new_c:rw']))
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            links=[
                ('main.app_server.instance1', 'app-server-instance1'),
                ('main.app_server.instance2', 'app-server-instance2'),
            ],
            binds=[
                '/var/lib/site/config/nginx:/etc/nginx:ro',
                '/new_h:/new_c:rw',
            ],
            volumes_from=['main.app_server_socket', 'main.web_log'],
            port_bindings={80: [80], 443: [443]},
        ))

    def test_create_kwargs_with_host_config(self):
        cfg_name = 'app_server'
        cfg = self.sample_map2.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name, 'instance1')
        c_name = 'main.app_server'
        config = ActionConfig('legacy', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        hc_kwargs = dict(binds=['/new_h:/new_c:rw'])
        kwargs = self.runner.get_container_create_kwargs(config, c_name, kwargs=dict(host_config=hc_kwargs))
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image='registry.example.com/app:custom',
            volumes=[
                '/var/lib/app/config',
                '/var/lib/app/data'
            ],
            user='******',
            hostname='main-app-server-legacy',
            domainname=None,
            ports=[8880],
            host_config=HostConfig(
                links={},
                binds=[
                    '/var/lib/site/config/app1:/var/lib/app/config:ro',
                    '/var/lib/site/data/app1:/var/lib/app/data:rw',
                    '/new_h:/new_c:rw',
                ],
                volumes_from=['main.app_log', 'main.app_server_socket'],
                port_bindings={},
                version=self.client_version2,
            ),
        ))

    def test_create_kwargs_with_host_config_and_volumes_networks(self):
        cfg_name = 'app_server'
        cfg = self.sample_map1.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name, 'instance1')
        c_name = 'main.app_server'
        config = ActionConfig('__default__', cfg_id, self.sample_client_config1, None, self.sample_map1, cfg)
        hc_kwargs = dict(binds=['/new_h:/new_c:rw'])
        kwargs = self.runner.get_container_create_kwargs(config, c_name, kwargs=dict(host_config=hc_kwargs))
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image='registry.example.com/app:custom',
            volumes=[
                '/var/lib/app/config',
                '/var/lib/app/data',
                '/var/lib/app/log',
                '/var/lib/app/socket',
            ],
            user='******',
            hostname='main-app-server',
            domainname=None,
            ports=[8880],
            networking_config={'EndpointsConfig': {'main.app': {}}},
            stop_timeout=10,
            healthcheck={
                'test': ['CMD', 'curl', 'http://localhost/'],
                'interval': 60000000000,
                'timeout': 1000000000,
                'retries': 3,
                'start_period': 5000000000,
            },
            host_config=HostConfig(
                links={},
                binds=[
                    '/var/lib/site/config/app1:/var/lib/app/config:ro',
                    '/var/lib/site/data/app1:/var/lib/app/data:rw',
                    'main.app_log:/var/lib/app/log:rw',
                    'main.app_server_socket:/var/lib/app/socket:rw',
                    '/new_h:/new_c:rw',
                ],
                volumes_from=[],
                port_bindings={},
                version=self.client_version1,
            ),
            stop_signal='SIGTERM',
        ))

    def test_attached_create_kwargs_without_host_config(self):
        cfg_name = 'app_server'
        cfg = self.sample_map2.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.VOLUME, 'main', cfg_name, 'app_server_socket')
        c_name = 'main.app_server'
        config = ActionConfig('legacy', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        kwargs = self.runner.get_attached_container_create_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(
            name=c_name,
            image=BasePolicy.base_image,
            volumes=['/var/lib/app/socket'],
            user='******',
            network_disabled=True,
        ))

    def test_attached_host_config_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map2.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.VOLUME, 'main', cfg_name, 'app_server_socket')
        c_name = 'main.app_server'
        config = ActionConfig('legacy', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        kwargs = self.runner.get_attached_container_host_config_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(container=c_name))

    def test_attached_preparation_create_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map2.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.VOLUME, 'main', cfg_name, 'app_server_socket')
        v_name = 'main.app_server_socket'
        config = ActionConfig('legacy', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        kwargs = self.runner.get_attached_preparation_create_kwargs(config, v_name, 'app_server_socket')
        self.assertDictEqual(kwargs, dict(
            image=BasePolicy.core_image,
            command='chown -R 2000:2000 /var/lib/app/socket && chmod -R u=rwX,g=rX,o= /var/lib/app/socket',
            user='******',
            host_config=HostConfig(
                volumes_from=[v_name],
                version=self.client_version2,
            ),
            network_disabled=True,
        ))

    def test_attached_preparation_create_kwargs_with_volumes(self):
        cfg_name = 'app_server'
        cfg = self.sample_map1.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.VOLUME, 'main', cfg_name, 'app_server_socket')
        v_name = 'main.app_server_socket'
        config = ActionConfig('__default__', cfg_id, self.sample_client_config1, None, self.sample_map1, cfg)
        kwargs = self.runner.get_attached_preparation_create_kwargs(config, v_name, 'app_server_socket')
        self.assertDictEqual(kwargs, dict(
            image=BasePolicy.core_image,
            command='chown -R 2000:2000 /volume-tmp && chmod -R u=rwX,g=rX,o= /volume-tmp',
            user='******',
            volumes=['/volume-tmp'],
            host_config=HostConfig(
                binds=['main.app_server_socket:/volume-tmp'],
                version=self.client_version1,
            ),
            network_disabled=True,
        ))

    def test_attached_preparation_host_config_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map2.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.VOLUME, 'main', cfg_name, 'app_server_socket')
        c_name = 'temp'
        v_name = 'main.app_server_socket'
        config = ActionConfig('legacy', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        kwargs = self.runner.get_attached_preparation_host_config_kwargs(config, c_name, v_name)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            volumes_from=[v_name],
        ))

    def test_network_setting(self):
        cfg_name = 'app_extra'
        cfg = self.sample_map2.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name)
        c_name = 'main.app_extra'
        config = ActionConfig('__default__', cfg_id, self.sample_client_config2, None, self.sample_map2, cfg)
        kwargs = self.runner.get_container_host_config_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(
            binds=[],
            container=c_name,
            links=[],
            network_mode='container:main.app_server.instance1',
            port_bindings={},
            volumes_from=[],
        ))

    def test_restart_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map1.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name)
        c_name = 'main.web_server'
        config = ActionConfig('__default__', cfg_id, self.sample_client_config1, None, self.sample_map1, cfg)
        kwargs = self.runner.get_container_restart_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            timeout=5,
        ))

    def test_stop_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map1.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name)
        c_name = 'main.web_server'
        config = ActionConfig('__default__', cfg_id, self.sample_client_config1, None, self.sample_map1, cfg)
        kwargs = self.runner.get_container_stop_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            timeout=5,
        ))

    def test_remove_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map1.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name)
        c_name = 'main.web_server'
        config = ActionConfig('__default__', cfg_id, self.sample_client_config1, None, self.sample_map1, cfg)
        kwargs = self.runner.get_container_remove_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
        ))

    def test_update_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map1.get_existing(cfg_name)
        cfg_id = MapConfigId(ItemType.CONTAINER, 'main', cfg_name)
        c_name = 'main.web_server'
        config = ActionConfig('__default__', cfg_id, self.sample_client_config1, None, self.sample_map1, cfg)
        kwargs = self.runner.get_container_update_kwargs(config, c_name, {
            'mem_limit': '2g',
            'cpu_shares': 512,
        })
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            mem_limit='2g',
            cpu_shares=512,
        ))
 def setUp(self):
     test_map = ContainerMap('test_map', initial=TEST_MAP_DATA, check_integrity=False)
     self.f_res = ContainerDependencyResolver()
     self.f_res.update(test_map.dependency_items())
     self.r_res = ContainerDependencyResolver()
     self.r_res.update_backward(test_map.dependency_items(reverse=True))
Beispiel #9
0
 def setUp(self):
     self.maxDiff = 2048
     self.map_name = 'main'
     self.sample_map = ContainerMap('main', MAP_DATA_1)
     self.client_version = CLIENT_DATA_1['version']
     self.sample_client_config = ClientConfiguration(**CLIENT_DATA_1)
Beispiel #10
0
class TestPolicyClientKwargs(unittest.TestCase):
    def setUp(self):
        self.maxDiff = 2048
        self.map_name = 'main'
        self.sample_map = ContainerMap('main', MAP_DATA_1)
        self.client_version = CLIENT_DATA_1['version']
        self.sample_client_config = ClientConfiguration(**CLIENT_DATA_1)

    def test_create_kwargs_without_host_config(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_create_kwargs(self.sample_map,
                                              cfg_name,
                                              cfg,
                                              '__default__',
                                              self.sample_client_config,
                                              c_name,
                                              None,
                                              include_host_config=False,
                                              kwargs=dict(ports=[22]))
        self.assertDictEqual(
            kwargs,
            dict(
                name=c_name,
                image='registry.example.com/nginx:latest',
                volumes=['/etc/nginx'],
                user=None,
                ports=[80, 443, 22],
                hostname='main.web_server',
                domainname=None,
            ))

    def test_host_config_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_host_config_kwargs(
            self.sample_map,
            cfg_name,
            cfg,
            '__default__',
            self.sample_client_config,
            c_name,
            None,
            kwargs=dict(binds={'/new_h': {
                'bind': '/new_c',
                'ro': False
            }}))
        self.assertDictEqual(
            kwargs,
            dict(
                container=c_name,
                links=[
                    ('main.app_server.instance1', 'app_server.instance1'),
                    ('main.app_server.instance2', 'app_server.instance2'),
                ],
                binds={
                    '/var/lib/site/config/nginx': {
                        'bind': '/etc/nginx',
                        'ro': True
                    },
                    '/new_h': {
                        'bind': '/new_c',
                        'ro': False
                    },
                },
                volumes_from=['main.app_server_socket', 'main.web_log'],
                port_bindings={
                    80: [80],
                    443: [443]
                },
                version=self.client_version,
            ))

    def test_create_kwargs_with_host_config(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_server'
        hc_kwargs = dict(binds={'/new_h': {'bind': '/new_c', 'ro': False}})
        kwargs = BasePolicy.get_create_kwargs(
            self.sample_map,
            cfg_name,
            cfg,
            '__default__',
            self.sample_client_config,
            c_name,
            'instance1',
            include_host_config=True,
            kwargs=dict(host_config=hc_kwargs))
        self.assertDictEqual(
            kwargs,
            dict(
                name=c_name,
                image='registry.example.com/app:custom',
                volumes=['/var/lib/app/config', '/var/lib/app/data'],
                user='******',
                hostname='main.app_server',
                domainname=None,
                ports=[8880],
                host_config=create_host_config(
                    links={},
                    binds={
                        '/var/lib/site/config/app1': {
                            'bind': '/var/lib/app/config',
                            'ro': True
                        },
                        '/var/lib/site/data/app1': {
                            'bind': '/var/lib/app/data',
                            'ro': False
                        },
                        '/new_h': {
                            'bind': '/new_c',
                            'ro': False
                        },
                    },
                    volumes_from=['main.app_log', 'main.app_server_socket'],
                    port_bindings={},
                    version=self.client_version,
                ),
            ))

    def test_attached_create_kwargs_without_host_config(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_server'
        alias = 'app_server_socket'
        kwargs = BasePolicy.get_attached_create_kwargs(
            self.sample_map,
            cfg_name,
            cfg,
            '__default__',
            self.sample_client_config,
            c_name,
            alias,
            include_host_config=False)
        self.assertDictEqual(
            kwargs,
            dict(
                name=c_name,
                image=BasePolicy.base_image,
                volumes=['/var/lib/app/socket'],
                user='******',
                network_disabled=True,
            ))

    def test_attached_host_config_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_server'
        alias = 'app_server_socket'
        kwargs = BasePolicy.get_attached_host_config_kwargs(
            self.sample_map, cfg_name, cfg, '__default__',
            self.sample_client_config, c_name, alias)
        self.assertDictEqual(
            kwargs, dict(container=c_name, version=self.client_version))

    def test_attached_preparation_create_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'temp'
        alias = 'app_server_socket'
        v_name = 'main.app_server_socket'
        kwargs = BasePolicy.get_attached_preparation_create_kwargs(
            self.sample_map,
            cfg_name,
            cfg,
            '__default__',
            self.sample_client_config,
            c_name,
            alias,
            v_name,
            include_host_config=True)
        self.assertDictEqual(
            kwargs,
            dict(
                image=BasePolicy.core_image,
                command=
                'chown -R 2000:2000 /var/lib/app/socket && chmod -R u=rwX,g=rX,o= /var/lib/app/socket',
                user='******',
                host_config=create_host_config(
                    volumes_from=[v_name],
                    version=self.client_version,
                ),
                network_disabled=True,
            ))

    def test_attached_preparation_host_config_kwargs(self):
        cfg_name = 'app_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'temp'
        alias = 'app_server_socket'
        v_name = 'main.app_server_socket'
        kwargs = BasePolicy.get_attached_preparation_host_config_kwargs(
            self.sample_map, cfg_name, cfg, '__default__',
            self.sample_client_config, c_name, alias, v_name)
        self.assertDictEqual(
            kwargs,
            dict(
                container='temp',
                volumes_from=[v_name],
                version=self.client_version,
            ))

    def test_network_setting(self):
        cfg_name = 'app_extra'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.app_extra'
        kwargs = BasePolicy.get_host_config_kwargs(self.sample_map, cfg_name,
                                                   cfg, '__default__',
                                                   self.sample_client_config,
                                                   c_name, None)
        self.assertDictEqual(
            kwargs,
            dict(
                binds={},
                container=c_name,
                links=[],
                network_mode='container:main.app_server.instance1',
                port_bindings={},
                volumes_from=[],
                version=self.client_version,
            ))

    def test_restart_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_restart_kwargs(self.sample_map, cfg_name, cfg,
                                               '__default__',
                                               self.sample_client_config,
                                               c_name, None)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            timeout=5,
        ))

    def test_stop_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_stop_kwargs(self.sample_map, cfg_name, cfg,
                                            '__default__',
                                            self.sample_client_config, c_name,
                                            None)
        self.assertDictEqual(kwargs, dict(
            container=c_name,
            timeout=5,
        ))

    def test_remove_kwargs(self):
        cfg_name = 'web_server'
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = 'main.web_server'
        kwargs = BasePolicy.get_remove_kwargs(self.sample_map, cfg_name, cfg,
                                              '__default__',
                                              self.sample_client_config,
                                              c_name, None)
        self.assertDictEqual(kwargs, dict(container=c_name, ))
class TestPolicyClientKwargs(unittest.TestCase):
    def setUp(self):
        self.maxDiff = 2048
        self.map_name = "main"
        self.sample_map = ContainerMap("main", MAP_DATA_1)
        self.client_version = CLIENT_DATA_1["version"]
        self.sample_client_config = ClientConfiguration(**CLIENT_DATA_1)
        self.policy = BasePolicy({"main": self.sample_map}, {"__default__": self.sample_client_config})
        self.runner = DockerClientRunner(self.policy, {})

    def test_create_kwargs_without_host_config(self):
        cfg_name = "web_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.web_server"
        self.sample_client_config.use_host_config = False
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, None
        )
        kwargs = self.runner.get_create_kwargs(config, c_name, kwargs=dict(ports=[22]))
        self.assertDictEqual(
            kwargs,
            dict(
                name=c_name,
                image="registry.example.com/nginx:latest",
                volumes=["/etc/nginx"],
                user=None,
                ports=[80, 443, 22],
                hostname="main.web_server",
                domainname=None,
            ),
        )

    def test_host_config_kwargs(self):
        cfg_name = "web_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.web_server"
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, None
        )
        kwargs = self.runner.get_host_config_kwargs(config, c_name, kwargs=dict(binds=["/new_h:/new_c:rw"]))
        self.assertDictEqual(
            kwargs,
            dict(
                container=c_name,
                links=[
                    ("main.app_server.instance1", "app_server.instance1"),
                    ("main.app_server.instance2", "app_server.instance2"),
                ],
                binds=["/var/lib/site/config/nginx:/etc/nginx:ro", "/new_h:/new_c:rw"],
                volumes_from=["main.app_server_socket", "main.web_log"],
                port_bindings={80: [80], 443: [443]},
            ),
        )

    def test_create_kwargs_with_host_config(self):
        cfg_name = "app_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.app_server"
        self.sample_client_config.use_host_config = True
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, "instance1"
        )
        hc_kwargs = dict(binds=["/new_h:/new_c:rw"])
        kwargs = self.runner.get_create_kwargs(config, c_name, kwargs=dict(host_config=hc_kwargs))
        self.assertDictEqual(
            kwargs,
            dict(
                name=c_name,
                image="registry.example.com/app:custom",
                volumes=["/var/lib/app/config", "/var/lib/app/data"],
                user="******",
                hostname="main.app_server",
                domainname=None,
                ports=[8880],
                host_config=create_host_config(
                    links={},
                    binds=[
                        "/var/lib/site/config/app1:/var/lib/app/config:ro",
                        "/var/lib/site/data/app1:/var/lib/app/data:rw",
                        "/new_h:/new_c:rw",
                    ],
                    volumes_from=["main.app_log", "main.app_server_socket"],
                    port_bindings={},
                    version=self.client_version,
                ),
            ),
        )

    def test_attached_create_kwargs_without_host_config(self):
        cfg_name = "app_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.app_server"
        alias = "app_server_socket"
        self.sample_client_config.use_host_config = False
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, alias
        )
        kwargs = self.runner.get_attached_create_kwargs(config, c_name)
        self.assertDictEqual(
            kwargs,
            dict(
                name=c_name,
                image=BasePolicy.base_image,
                volumes=["/var/lib/app/socket"],
                user="******",
                network_disabled=True,
            ),
        )

    def test_attached_host_config_kwargs(self):
        cfg_name = "app_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.app_server"
        alias = "app_server_socket"
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, alias
        )
        kwargs = self.runner.get_attached_host_config_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(container=c_name))

    def test_attached_preparation_create_kwargs(self):
        cfg_name = "app_server"
        cfg = self.sample_map.get_existing(cfg_name)
        alias = "app_server_socket"
        v_name = "main.app_server_socket"
        self.sample_client_config.use_host_config = True
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, alias
        )
        kwargs = self.runner.get_attached_preparation_create_kwargs(config, v_name)
        self.assertDictEqual(
            kwargs,
            dict(
                image=BasePolicy.core_image,
                command="chown -R 2000:2000 /var/lib/app/socket && chmod -R u=rwX,g=rX,o= /var/lib/app/socket",
                user="******",
                host_config=create_host_config(volumes_from=[v_name], version=self.client_version),
                network_disabled=True,
            ),
        )

    def test_attached_preparation_host_config_kwargs(self):
        cfg_name = "app_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "temp"
        alias = "app_server_socket"
        v_name = "main.app_server_socket"
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, alias
        )
        kwargs = self.runner.get_attached_preparation_host_config_kwargs(config, c_name, v_name)
        self.assertDictEqual(kwargs, dict(container=c_name, volumes_from=[v_name]))

    def test_network_setting(self):
        cfg_name = "app_extra"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.app_extra"
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, None
        )
        kwargs = self.runner.get_host_config_kwargs(config, c_name)
        self.assertDictEqual(
            kwargs,
            dict(
                binds=[],
                container=c_name,
                links=[],
                network_mode="container:main.app_server.instance1",
                port_bindings={},
                volumes_from=[],
            ),
        )

    def test_restart_kwargs(self):
        cfg_name = "web_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.web_server"
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, None
        )
        kwargs = self.runner.get_restart_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(container=c_name, timeout=5))

    def test_stop_kwargs(self):
        cfg_name = "web_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.web_server"
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, None
        )
        kwargs = self.runner.get_stop_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(container=c_name, timeout=5))

    def test_remove_kwargs(self):
        cfg_name = "web_server"
        cfg = self.sample_map.get_existing(cfg_name)
        c_name = "main.web_server"
        config = ActionConfig(
            "main", self.sample_map, cfg_name, cfg, "__default__", self.sample_client_config, None, None
        )
        kwargs = self.runner.get_remove_kwargs(config, c_name)
        self.assertDictEqual(kwargs, dict(container=c_name))