Esempio n. 1
0
    def test_get_addresses_failing_when_expired_in_cache(self, cache_logger):
        self.getaddrinfo.return_value = [(2, 1, 6, '', ('127.0.0.1', 0)),
                                         (2, 2, 17, '', ('127.0.0.1', 0)),
                                         (2, 3, 0, '', ('127.0.0.1', 0)),
                                         (2, 1, 6, '', ('10.237.214.247', 0)),
                                         (2, 2, 17, '', ('10.237.214.247', 0)),
                                         (2, 3, 0, '', ('10.237.214.247', 0))]

        # Given valid result is in chache but expired
        utils.get_addresses_by_name('some_host_name')
        self.clock.return_value = 1.0e6

        # Given addresses resolution is now failing
        given_error = RuntimeError("This is top secret.")

        def failing_getaddrinfo(name, service):
            raise given_error

        self.getaddrinfo.side_effect = failing_getaddrinfo

        self.assertRaises(RuntimeError, utils.get_addresses_by_name,
                          'some_host_name')

        # Then result is fetched more times
        self.getaddrinfo.assert_has_calls([
            mock.call('some_host_name', None),
            mock.call('some_host_name', None)
        ])
        cache_logger.warning.assert_called_once_with(
            'Error fetching values for keys: %r',
            "'some_host_name'",
            exc_info=(RuntimeError, given_error, mock.ANY))
Esempio n. 2
0
    def test_get_addresses_by_valid_name_when_cache_expires(self):
        self.getaddrinfo.return_value = [(2, 1, 6, '', ('127.0.0.1', 0)),
                                         (2, 2, 17, '', ('127.0.0.1', 0)),
                                         (2, 3, 0, '', ('127.0.0.1', 0)),
                                         (2, 1, 6, '', ('10.237.214.247', 0)),
                                         (2, 2, 17, '', ('10.237.214.247', 0)),
                                         (2, 3, 0, '', ('10.237.214.247', 0))]

        # When valid host name is requested
        result1 = utils.get_addresses_by_name('some_host_name')

        # and after a long time
        self.clock.return_value = 1.0e6

        # When valid host name is requested
        result2 = utils.get_addresses_by_name('some_host_name')

        # Then correct addresses are returned
        self.assertEqual(('127.0.0.1', '10.237.214.247'), result1)
        self.assertEqual(('127.0.0.1', '10.237.214.247'), result2)

        # Then addresses are fetched twice
        self.getaddrinfo.assert_has_calls([
            mock.call('some_host_name', None),
            mock.call('some_host_name', None)
        ])
Esempio n. 3
0
    def test_get_addresses_by_valid_name_when_cache_expires(self):
        self.getaddrinfo.return_value = [
            (2, 1, 6, '', ('127.0.0.1', 0)),
            (2, 2, 17, '', ('127.0.0.1', 0)),
            (2, 3, 0, '', ('127.0.0.1', 0)),
            (2, 1, 6, '', ('10.237.214.247', 0)),
            (2, 2, 17, '', ('10.237.214.247', 0)),
            (2, 3, 0, '', ('10.237.214.247', 0))]

        # When valid host name is requested
        result1 = utils.get_addresses_by_name('some_host_name')

        # and after a long time
        self.clock.return_value = 1.0e6

        # When valid host name is requested
        result2 = utils.get_addresses_by_name('some_host_name')

        # Then correct addresses are returned
        self.assertEqual(('127.0.0.1', '10.237.214.247'), result1)
        self.assertEqual(('127.0.0.1', '10.237.214.247'), result2)

        # Then addresses are fetched twice
        self.getaddrinfo.assert_has_calls(
            [mock.call('some_host_name', None),
             mock.call('some_host_name', None)])
Esempio n. 4
0
    def test_get_addresses_failing_when_expired_in_cache(self, cache_logger):
        self.getaddrinfo.return_value = [
            (2, 1, 6, '', ('127.0.0.1', 0)),
            (2, 2, 17, '', ('127.0.0.1', 0)),
            (2, 3, 0, '', ('127.0.0.1', 0)),
            (2, 1, 6, '', ('10.237.214.247', 0)),
            (2, 2, 17, '', ('10.237.214.247', 0)),
            (2, 3, 0, '', ('10.237.214.247', 0))]

        # Given valid result is in chache but expired
        utils.get_addresses_by_name('some_host_name')
        self.clock.return_value = 1.0e6

        # Given addresses resolution is now failing
        given_error = RuntimeError("This is top secret.")

        def failing_getaddrinfo(name, service):
            raise given_error

        self.getaddrinfo.side_effect = failing_getaddrinfo

        self.assertRaises(
            RuntimeError, utils.get_addresses_by_name, 'some_host_name')

        # Then result is fetched more times
        self.getaddrinfo.assert_has_calls(
            [mock.call('some_host_name', None),
             mock.call('some_host_name', None)])
        cache_logger.warning.assert_called_once_with(
            'Error fetching values for keys: %r', "'some_host_name'",
            exc_info=(RuntimeError, given_error, mock.ANY))
Esempio n. 5
0
    def test_get_addresses_by_valid_name(self):
        self.getaddrinfo.return_value = [(2, 1, 6, '', ('127.0.0.1', 0)),
                                         (2, 2, 17, '', ('127.0.0.1', 0)),
                                         (2, 3, 0, '', ('127.0.0.1', 0)),
                                         (2, 1, 6, '', ('10.237.214.247', 0)),
                                         (2, 2, 17, '', ('10.237.214.247', 0)),
                                         (2, 3, 0, '', ('10.237.214.247', 0))]

        # When valid host name is requested
        result = utils.get_addresses_by_name('some_host_name')

        # Then correct addresses are returned
        self.assertEqual(('127.0.0.1', '10.237.214.247'), result)

        # Then fetched addresses are cached
        self.assertEqual(result, utils.get_addresses_by_name('some_host_name'))

        # Then addresses are fetched only once
        self.getaddrinfo.assert_called_once_with('some_host_name', None)
Esempio n. 6
0
    def test_get_addresses_by_valid_name(self):
        self.getaddrinfo.return_value = [
            (2, 1, 6, '', ('127.0.0.1', 0)),
            (2, 2, 17, '', ('127.0.0.1', 0)),
            (2, 3, 0, '', ('127.0.0.1', 0)),
            (2, 1, 6, '', ('10.237.214.247', 0)),
            (2, 2, 17, '', ('10.237.214.247', 0)),
            (2, 3, 0, '', ('10.237.214.247', 0))]

        # When valid host name is requested
        result = utils.get_addresses_by_name('some_host_name')

        # Then correct addresses are returned
        self.assertEqual(('127.0.0.1', '10.237.214.247'), result)

        # Then fetched addresses are cached
        self.assertEqual(result, utils.get_addresses_by_name('some_host_name'))

        # Then addresses are fetched only once
        self.getaddrinfo.assert_called_once_with('some_host_name', None)
Esempio n. 7
0
    def _fetch_elements_by_host(self, host_name, cache_timeout=60.0):
        '''Yields all network topology elements referring to given host name

        '''

        host_addresses = [host_name]
        try:
            # It uses both compute host name and known IP addresses to
            # recognize topology elements valid for given computed host
            ip_addresses = utils.get_addresses_by_name(host_name)
        except Exception:
            ip_addresses = []
            LOG.exception(
                _LE('Unable to resolve IP addresses for host %(host_name)r'),
                {'host_name': host_name})
        else:
            host_addresses.extend(ip_addresses)

        yield_elements = set()
        try:
            for __, element in self._elements_by_ip.fetch_all(
                    host_addresses, cache_timeout):
                # yields every element only once
                if element not in yield_elements:
                    yield_elements.add(element)
                    yield element

        except cache.CacheFetchError as error:
            # This error is expected on most of the cases because typically not
            # all host_addresses maps to a network topology element.
            if yield_elements:
                # As we need only one element for every host we ignore the
                # case in which others host addresseses didn't map to any host
                LOG.debug(
                    'Host addresses not found in networking topology: %s',
                    ', '.join(error.missing_keys))
            else:
                LOG.exception(
                    _LE('No such network topology elements for given host '
                        '%(host_name)r and given IPs: %(ip_addresses)s.'), {
                            'host_name': host_name,
                            'ip_addresses': ", ".join(ip_addresses)
                        })
                error.reraise_cause()
    def _fetch_elements_by_host(self, host_name, cache_timeout=60.0):
        '''Yields all network topology elements referring to given host name

        '''

        host_addresses = [host_name]
        try:
            # It uses both compute host name and known IP addresses to
            # recognize topology elements valid for given computed host
            ip_addresses = utils.get_addresses_by_name(host_name)
        except Exception:
            ip_addresses = []
            LOG.exception(
                _LE('Unable to resolve IP addresses for host %(host_name)r'),
                {'host_name': host_name})
        else:
            host_addresses.extend(ip_addresses)

        yield_elements = set()
        try:
            for _, element in self._elements_by_ip.fetch_all(
                    host_addresses, cache_timeout):
                # yields every element only once
                if element not in yield_elements:
                    yield_elements.add(element)
                    yield element

        except cache.CacheFetchError as error:
            # This error is expected on most of the cases because typically not
            # all host_addresses maps to a network topology element.
            if yield_elements:
                # As we need only one element for every host we ignore the
                # case in which others host addresseses didn't map to any host
                LOG.debug(
                    'Host addresses not found in networking topology: %s',
                    ', '.join(error.missing_keys))
            else:
                LOG.exception(
                    _LE('No such network topology elements for given host '
                        '%(host_name)r and given IPs: %(ip_addresses)s.'),
                    {'host_name': host_name,
                     'ip_addresses': ", ".join(ip_addresses)})
                error.reraise_cause()