def test_memcache_ctx(self):
        class MockCharmInstance(object):
            api_ports = {}
            name = 'hello'

            def __init__(self, release):
                self.release = release

        mch_result = False

        class MCHR(object):
            class_thing = None

            def __init__(self, thing):
                self.thing = thing
                self.__class__.class_thing = self

            def __gt__(self, other):
                return mch_result

        mpo = mock.patch.object
        with mpo(adapters.ch_host, 'lsb_release') as lsb_r, \
                mpo(adapters.ch_host, 'CompareHostReleases', new=MCHR), \
                mpo(adapters.ch_ip, 'is_ipv6_disabled') as is_ipv6_disabled:

            # first no memcache
            mci = MockCharmInstance('liberty')
            c = adapters.APIConfigurationAdapter(charm_instance=mci)
            self.assertEqual(c.memcache['use_memcache'], False)

            # next switch on memcache
            mci = MockCharmInstance('mitaka')
            # start with ipv6 disabled and ubuntu release is trusty
            lsb_r.return_value = {'DISTRIB_CODENAME': 'trusty'}
            is_ipv6_disabled.return_value = True
            c = adapters.APIConfigurationAdapter(charm_instance=mci)
            self.assertEqual(c.memcache['use_memcache'], True)
            self.assertEqual(MCHR.class_thing.thing, 'trusty')
            self.assertEqual(c.memcache['memcache_server'], 'localhost')
            self.assertEqual(c.memcache['memcache_server_formatted'],
                             '127.0.0.1')
            self.assertEqual(c.memcache['memcache_port'], '11211')
            self.assertEqual(c.memcache['memcache_url'], '127.0.0.1:11211')
            # make us later than trusty
            mch_result = True
            self.assertEqual(c.memcache['memcache_server'], '127.0.0.1')

            # now do ipv6 not disabled.
            mch_result = False
            is_ipv6_disabled.return_value = False
            c = adapters.APIConfigurationAdapter(charm_instance=mci)
            self.assertEqual(c.memcache['use_memcache'], True)
            self.assertEqual(MCHR.class_thing.thing, 'trusty')
            self.assertEqual(c.memcache['memcache_server'], 'ip6-localhost')
            self.assertEqual(c.memcache['memcache_server_formatted'], '[::1]')
            self.assertEqual(c.memcache['memcache_port'], '11211')
            self.assertEqual(c.memcache['memcache_url'], 'inet6:[::1]:11211')
            # make us later than trusty
            mch_result = True
            self.assertEqual(c.memcache['memcache_server'], '::1')
示例#2
0
 def test_memcache_server(self):
     with mock.patch.object(adapters.ch_host,
                            'lsb_release',
                            return_value={'DISTRIB_RELEASE': '14.04'}):
         c = adapters.APIConfigurationAdapter()
         self.assertEqual(c.memcache_server, 'ip6-localhost')
     with mock.patch.object(adapters.ch_host,
                            'lsb_release',
                            return_value={'DISTRIB_RELEASE': '16.04'}):
         c = adapters.APIConfigurationAdapter()
         self.assertEqual(c.memcache_server, '::1')
示例#3
0
 def test_determine_service_port(self):
     with mock.patch.object(adapters.APIConfigurationAdapter,
                            'apache_enabled',
                            new=True):
         c = adapters.APIConfigurationAdapter()
         self.assertEqual(c.determine_service_port(80), 60)
     with mock.patch.object(adapters.APIConfigurationAdapter,
                            'apache_enabled',
                            new=False):
         c = adapters.APIConfigurationAdapter()
         self.assertEqual(c.determine_service_port(80), 70)
示例#4
0
 def test_apache_enabled(self):
     with mock.patch.object(adapters.charms.reactive.bus,
                            'get_state',
                            return_value=True):
         c = adapters.APIConfigurationAdapter()
         self.assertTrue(c.apache_enabled)
     with mock.patch.object(adapters.charms.reactive.bus,
                            'get_state',
                            return_value=False):
         c = adapters.APIConfigurationAdapter()
         self.assertFalse(c.apache_enabled)
    def test_ipv6_enabled(self):
        with mock.patch.object(adapters.ch_ip,
                               'is_ipv6_disabled') as is_ipv6_disabled:

            # IPv6 disabled
            is_ipv6_disabled.return_value = True
            a = adapters.APIConfigurationAdapter()
            self.assertEqual(a.ipv6_enabled, False)

            # IPv6 enabled
            is_ipv6_disabled.return_value = False
            b = adapters.APIConfigurationAdapter()
            self.assertEqual(b.ipv6_enabled, True)
示例#6
0
 def test_use_memcache(self):
     test_config = {'openstack-origin': 'distro'}
     with mock.patch.object(adapters.hookenv,
                            'config',
                            new=lambda: test_config):
         with mock.patch.object(adapters.ch_utils,
                                'get_os_codename_install_source',
                                return_value='liberty'):
             c = adapters.APIConfigurationAdapter()
             self.assertFalse(c.use_memcache)
         with mock.patch.object(adapters.ch_utils,
                                'get_os_codename_install_source',
                                return_value='newton'):
             c = adapters.APIConfigurationAdapter()
             self.assertTrue(c.use_memcache)
示例#7
0
 def test_external_endpoints(self):
     test_config = {
         'prefer-ipv6': False,
         'vip': None,
     }
     with mock.patch.object(adapters.hookenv, 'config',
                            new=lambda: test_config), \
             mock.patch.object(adapters.ch_utils, 'get_host_ip',
                               return_value='10.0.0.10'), \
             mock.patch.object(adapters.APIConfigurationAdapter,
                               'get_network_addresses'), \
             mock.patch.object(adapters.hookenv, 'local_unit',
                               return_value='my-unit/0'):
         c = adapters.APIConfigurationAdapter(port_map=self.api_ports)
         self.assertEqual(
             c.external_endpoints, {
                 'svc1': {
                     'proto': 'https',
                     'ip': '10.0.0.10',
                     'port': 9001,
                     'url': 'https://10.0.0.10:9001'
                 },
                 'svc2': {
                     'proto': 'https',
                     'ip': '10.0.0.10',
                     'port': 9002,
                     'url': 'https://10.0.0.10:9002'
                 }
             })
 def test_memcache_url(self):
     with mock.patch.object(adapters.APIConfigurationAdapter,
                            'memcache',
                            new_callable=mock.PropertyMock) as memcache:
         memcache.return_value = {}
         c = adapters.APIConfigurationAdapter()
         self.assertEqual(c.memcache_url, '')
         memcache.return_value = {'memcache_url': 'hello'}
         self.assertEqual(c.memcache_url, 'hello')
    def test_workers(self):
        class FakeWorkerConfigContext(object):
            def __call__(self):
                return {"workers": 8}

        with mock.patch.object(adapters.ch_context,
                               'WorkerConfigContext',
                               new=FakeWorkerConfigContext):
            c = adapters.APIConfigurationAdapter()
            self.assertEqual(c.workers, 8)
 def test_use_memcache(self):
     with mock.patch.object(adapters.APIConfigurationAdapter,
                            'memcache',
                            new_callable=mock.PropertyMock) as memcache:
         memcache.return_value = {}
         c = adapters.APIConfigurationAdapter()
         self.assertEqual(c.use_memcache, False)
         memcache.return_value = {'use_memcache': False}
         self.assertEqual(c.use_memcache, False)
         memcache.return_value = {'use_memcache': True}
         self.assertEqual(c.use_memcache, True)
示例#11
0
    def test_class_init_using_charm_instance(self):
        class TestCharm(object):

            api_ports = TestAPIConfigurationAdapter.api_ports
            name = 'test-charm'

        with mock.patch.object(adapters.hookenv, 'config', new=lambda: {}), \
                mock.patch.object(adapters.APIConfigurationAdapter,
                                  'get_network_addresses'):
            c = adapters.APIConfigurationAdapter(charm_instance=TestCharm())
            self.assertEqual(c.port_map, TestCharm.api_ports)
            self.assertEqual(c.service_name, 'test-charm')
示例#12
0
 def test_class(self):
     test_config = {
         'prefer-ipv6': False,
         'vip': '',
     }
     with mock.patch.object(adapters.hookenv, 'config',
                            new=lambda: test_config), \
             mock.patch.object(adapters.APIConfigurationAdapter,
                               'get_network_addresses'), \
             mock.patch.object(adapters.hookenv, 'local_unit',
                               return_value='my-unit/0'):
         c = adapters.APIConfigurationAdapter()
         self.assertEqual(c.local_unit_name, 'my-unit-0')
         self.assertEqual(c.haproxy_stat_port, '8888')
         self.assertEqual(c.service_ports, {})
         self.assertEqual(c.service_listen_info, {})
         self.assertEqual(c.external_endpoints, {})
示例#13
0
 def test_ipv6_mode(self):
     test_config = {
         'prefer-ipv6': True,
         'vip': '',
     }
     with mock.patch.object(adapters.hookenv, 'config',
                            new=lambda: test_config), \
             mock.patch.object(
                 adapters.ch_ip,
                 'get_ipv6_addr',
                 return_value=['fe80::f2de:f1ff:fedd:8dc7']), \
             mock.patch.object(adapters.APIConfigurationAdapter,
                               'get_network_addresses'):
         c = adapters.APIConfigurationAdapter()
         self.assertTrue(c.ipv6_mode)
         self.assertEqual(c.local_address, 'fe80::f2de:f1ff:fedd:8dc7')
         self.assertEqual(c.local_host, 'ip6-localhost')
         self.assertEqual(c.haproxy_host, '::')
示例#14
0
 def test_ipv4_mode(self):
     test_config = {
         'prefer-ipv6': False,
         'vip': '',
     }
     with mock.patch.object(adapters.ch_utils, 'get_host_ip',
                            return_value='10.0.0.10'), \
             mock.patch.object(adapters.hookenv, 'config',
                               new=lambda: test_config), \
             mock.patch.object(adapters.hookenv, 'unit_get',
                               return_value='10.0.0.20'), \
             mock.patch.object(adapters.APIConfigurationAdapter,
                               'get_network_addresses'):
         c = adapters.APIConfigurationAdapter(service_name='svc1')
         self.assertFalse(c.ipv6_mode)
         self.assertEqual(c.local_address, '10.0.0.10')
         self.assertEqual(c.local_host, '127.0.0.1')
         self.assertEqual(c.haproxy_host, '0.0.0.0')
         self.assertEqual(c.service_name, 'svc1')
 def test_external_endpoints(self):
     with mock.patch.object(adapters.os_ip,
                            'resolve_address',
                            return_value="10.0.0.10"):
         c = adapters.APIConfigurationAdapter(port_map=self.api_ports)
         self.assertEqual(
             c.external_endpoints, {
                 'svc1': {
                     'proto': 'https',
                     'ip': '10.0.0.10',
                     'port': 9001,
                     'url': 'https://10.0.0.10:9001'
                 },
                 'svc2': {
                     'proto': 'https',
                     'ip': '10.0.0.10',
                     'port': 9002,
                     'url': 'https://10.0.0.10:9002'
                 }
             })
    def test_get_network_addresses(self):
        test_config = {
            'prefer-ipv6': False,
            'os-admin-network': 'admin_net',
            'os-public-network': 'public_net',
            'os-internal-network': 'internal_net',
        }
        test_networks = {
            'admin_net': 'admin_addr',
            'public_net': 'public_addr',
            'internal_net': 'internal_addr',
        }
        resolved_addresses = {
            'admin': 'admin_addr',
            'public': 'public_addr',
            'int': 'int_vip',
        }

        def _is_address_in_network(cidr, vip):
            return cidr == vip.replace('vip_', '')

        def _resolve_address(endpoint_type=None):
            return resolved_addresses[endpoint_type]

        with mock.patch.object(adapters.hookenv, 'config',
                               return_value=test_config), \
                mock.patch.object(adapters.hookenv, 'unit_get',
                                  return_value='thisunit'), \
                mock.patch.object(adapters.ch_ip, 'get_address_in_network',
                                  new=lambda x, y: test_networks[x]), \
                mock.patch.object(adapters.ch_ip, 'get_relation_ip',
                                  new=lambda x, y: test_networks[x]), \
                mock.patch.object(adapters.os_ip, 'resolve_address',
                                  new=_resolve_address):
            c = adapters.APIConfigurationAdapter()
            self.assertEqual(c.get_network_addresses(),
                             [('admin_addr', 'admin_addr'),
                              ('internal_addr', 'int_vip'),
                              ('public_addr', 'public_addr')])
示例#17
0
    def test_endpoints_and_ext_ports(self):
        _net_addrs = [('admin_addr', 'vip_admin_net'),
                      ('internal_addr', 'vip_internal_net')]
        with mock.patch.object(adapters.APIConfigurationAdapter,
                               'get_network_addresses',
                               return_value=_net_addrs), \
                mock.patch.object(adapters.ch_cluster, 'determine_apache_port',
                                  new=lambda x, singlenode_mode: x - 10), \
                mock.patch.object(adapters.ch_cluster, 'determine_api_port',
                                  new=lambda x, singlenode_mode: x - 20):
            c = adapters.APIConfigurationAdapter(port_map=self.api_ports)
            expect = [
                ('admin_addr', 'vip_admin_net', 8991, 8981),
                ('admin_addr', 'vip_admin_net', 8992, 8982),
                ('admin_addr', 'vip_admin_net', 8993, 8983),
                ('internal_addr', 'vip_internal_net', 8991, 8981),
                ('internal_addr', 'vip_internal_net', 8992, 8982),
                ('internal_addr', 'vip_internal_net', 8993, 8983),
            ]

            self.assertEqual(c.endpoints, expect)
            self.assertEqual(c.ext_ports, [8991, 8992, 8993])
示例#18
0
 def test_memcache_host(self):
     self.assertEqual(adapters.APIConfigurationAdapter().memcache_host,
                      '[::1]')
    def test_wsgi_worker_context(self):
        class ChInstance1(object):
            name = 'test-name'
            wsgi_script = 'test-script'
            api_ports = {}

        class ChInstance2(object):
            name = 'test-name'
            wsgi_script = 'test-script'
            wsgi_admin_script = 'test-admin-script'
            wsgi_public_script = 'test-public-script'
            wsgi_process_weight = 0.5
            wsgi_admin_process_weight = 0.1
            wsgi_public_process_weight = 0.4
            api_ports = {}

        class ChInstance3(object):
            name = 'test-name'
            wsgi_script = None
            wsgi_admin_script = 'test-admin-script'
            wsgi_public_script = 'test-public-script'
            wsgi_process_weight = None
            wsgi_admin_process_weight = 0.1
            wsgi_public_process_weight = 0.4
            api_ports = {}

        class FakeWSGIWorkerConfigContext():
            copy_kwargs = None

            def __init__(self, **kwargs):
                self.__class__.copy_kwargs = kwargs.copy()

            def __call__(self):
                return "T"

        with mock.patch.object(adapters.ch_context,
                               'WSGIWorkerConfigContext',
                               new=FakeWSGIWorkerConfigContext):
            # start with no charm instance to get default values
            c = adapters.APIConfigurationAdapter()
            self.assertEqual(c.wsgi_worker_context, "T")
            self.assertEqual(FakeWSGIWorkerConfigContext.copy_kwargs, {})
            # start with a minimal charm_instance
            instance = ChInstance1()
            c = adapters.APIConfigurationAdapter(charm_instance=instance)
            self.assertEqual(c.wsgi_worker_context, "T")
            self.assertEqual(FakeWSGIWorkerConfigContext.copy_kwargs, {
                'name': 'test-name',
                'script': 'test-script'
            })
            # And then, all the options set:
            instance = ChInstance2()
            c = adapters.APIConfigurationAdapter(charm_instance=instance)
            self.assertEqual(c.wsgi_worker_context, "T")
            self.assertEqual(
                FakeWSGIWorkerConfigContext.copy_kwargs, {
                    'name': 'test-name',
                    'script': 'test-script',
                    'admin_script': 'test-admin-script',
                    'public_script': 'test-public-script',
                    'process_weight': 0.5,
                    'admin_process_weight': 0.1,
                    'public_process_weight': 0.4
                })
            # and finally, with some of the options set to None, to test
            # filtering
            instance = ChInstance3()
            c = adapters.APIConfigurationAdapter(charm_instance=instance)
            self.assertEqual(c.wsgi_worker_context, "T")
            self.assertEqual(
                FakeWSGIWorkerConfigContext.copy_kwargs, {
                    'name': 'test-name',
                    'admin_script': 'test-admin-script',
                    'public_script': 'test-public-script',
                    'admin_process_weight': 0.1,
                    'public_process_weight': 0.4
                })
示例#20
0
 def test_external_ports(self):
     c = adapters.APIConfigurationAdapter(port_map=self.api_ports)
     self.assertEqual(c.external_ports, {9001, 9002, 9003})
示例#21
0
 def test_memcache_port(self):
     self.assertEqual(adapters.APIConfigurationAdapter().memcache_port,
                      '11211')
示例#22
0
 def test_memcache_url(self):
     self.assertEqual(adapters.APIConfigurationAdapter().memcache_url,
                      'inet6:[::1]:11211')