Пример #1
0
    def test_should_create_varnish_api_clients_for_all_servers(self):
        expected_construct_args = [
            call(['127.0.0.1', '6082', 1.0], 'secret-1'),
            call(['127.0.0.2', '6083', 1.0], 'secret-2'),
            call(['127.0.0.3', '6084', 1.0], 'secret-3')
        ]
        sample_extractor = Mock(servers=servers)

        with patch('vaas.cluster.cluster.ServerExtractor',
                   Mock(return_value=sample_extractor)):
            with patch.object(VarnishApi, '__init__',
                              return_value=None) as construct_mock:
                varnish_cluster = VarnishApiProvider()
                api_objects = []
                for api in varnish_cluster.get_varnish_api():
                    """
                        Workaround - we cannot mock __del__ method:
                        https://docs.python.org/3/library/unittest.mock.html

                        We inject sock field to eliminate warning raised by cleaning actions in __del__ method
                        """
                    api.sock = None
                    api_objects.append(api)

                assert_equals(3, len(api_objects))
                assert_list_equal(expected_construct_args,
                                  construct_mock.call_args_list)
Пример #2
0
class BackendStatusManager(object):
    def __init__(self):
        self.varnish_api_provider = VarnishApiProvider()

    def load_from_varnish(self):
        pattern = re.compile(
            "\((\d+\.\d+\.\d+\.\d+),[^,]*,(\d+)\)\s+\w+\s+\w+\s+(\w+)")
        backend_to_status_map = {}

        for varnish_api in self.varnish_api_provider.get_varnish_api():
            backend_statuses = varnish_api.fetch('backend.list')[1][0:].split(
                '\n')

            for backend in backend_statuses:
                ips = re.search(pattern, backend)
                if ips is not None:
                    backend_address = str(ips.group(1)) + ':' + str(
                        ips.group(2))

                    if backend_address not in backend_to_status_map or ips.group(
                            3) == 'Sick':
                        backend_to_status_map[backend_address] = ips.group(3)

        return backend_to_status_map

    def store_backend_statuses(self, backend_to_status_map):
        now = datetime.datetime.utcnow().replace(tzinfo=utc)
        for key, status in backend_to_status_map.items():
            address, port = key.split(":")
            try:
                backend_status = BackendStatus.objects.get(address=address,
                                                           port=port)
                backend_status.status = status
                backend_status.timestamp = now
                backend_status.save()
            except BackendStatus.DoesNotExist:
                BackendStatus.objects.create(address=address,
                                             port=port,
                                             status=status,
                                             timestamp=now)

        BackendStatus.objects.filter(timestamp__lt=now).delete()

    def refresh_statuses(self):
        self.store_backend_statuses(self.load_from_varnish())
Пример #3
0
    def test_should_create_varnish_api_clients_for_all_servers(self):
        expected_construct_args = [
            call(['127.0.0.1', '6082', 1.0], 'secret-1'),
            call(['127.0.0.2', '6083', 1.0], 'secret-2'),
            call(['127.0.0.3', '6084', 1.0], 'secret-3')]
        sample_extractor = Mock(servers=servers)

        with patch('vaas.cluster.cluster.ServerExtractor', Mock(return_value=sample_extractor)):
                with patch.object(VarnishApi, '__init__', return_value=None) as construct_mock:
                    varnish_cluster = VarnishApiProvider()
                    api_objects = []
                    for api in varnish_cluster.get_varnish_api():
                        """
                        Workaround - we cannot mock __del__ method:
                        https://docs.python.org/3/library/unittest.mock.html

                        We inject sock field to eliminate warning raised by cleaning actions in __del__ method
                        """
                        api.sock = None
                        api_objects.append(api)

                    assert_equals(3, len(api_objects))
                    assert_list_equal(expected_construct_args, construct_mock.call_args_list)
Пример #4
0
class BackendStatusManager(object):
    def __init__(self):
        self.varnish_api_provider = VarnishApiProvider()

    def load_from_varnish(self):
        pattern = re.compile("\((\d+\.\d+\.\d+\.\d+),[^,]*,(\d+)\)\s+\w+\s+\w+\s+(\w+)")
        backend_to_status_map = {}

        for varnish_api in self.varnish_api_provider.get_varnish_api():
            backend_statuses = varnish_api.fetch('backend.list')[1][0:].split('\n')

            for backend in backend_statuses:
                ips = re.search(pattern, backend)
                if ips is not None:
                    backend_address = str(ips.group(1)) + ':' + str(ips.group(2))

                    if backend_address not in backend_to_status_map or ips.group(3) == 'Sick':
                        backend_to_status_map[backend_address] = ips.group(3)

        return backend_to_status_map

    def store_backend_statuses(self, backend_to_status_map):
        now = datetime.datetime.utcnow().replace(tzinfo=utc)
        for key, status in backend_to_status_map.items():
            address, port = key.split(":")
            try:
                backend_status = BackendStatus.objects.get(address=address, port=port)
                backend_status.status = status
                backend_status.timestamp = now
                backend_status.save()
            except BackendStatus.DoesNotExist:
                BackendStatus.objects.create(address=address, port=port, status=status, timestamp=now)

        BackendStatus.objects.filter(timestamp__lt=now).delete()

    def refresh_statuses(self):
        self.store_backend_statuses(self.load_from_varnish())