Ejemplo n.º 1
0
 def test_order(self):
     record1 = r.DNSAddress("a", r._TYPE_SOA, r._CLASS_IN, 1, b"a")
     record2 = r.DNSAddress("a", r._TYPE_SOA, r._CLASS_IN, 1, b"b")
     cache = r.DNSCache()
     cache.add(record1)
     cache.add(record2)
     entry = r.DNSEntry("a", r._TYPE_SOA, r._CLASS_IN)
     cached_record = cache.get(entry)
     self.assertEqual(cached_record, record2)
Ejemplo n.º 2
0
 def test_order(self):
     record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a')
     record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
     cache = r.DNSCache()
     cache.add(record1)
     cache.add(record2)
     entry = r.DNSEntry('a', const._TYPE_SOA, const._CLASS_IN)
     cached_record = cache.get(entry)
     assert cached_record == record2
Ejemplo n.º 3
0
 def test_cache_empty_does_not_leak_memory_by_leaving_empty_list(self):
     record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a')
     record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
     cache = r.DNSCache()
     cache.add(record1)
     cache.add(record2)
     assert 'a' in cache.cache
     cache.remove(record1)
     cache.remove(record2)
     assert 'a' not in cache.cache
Ejemplo n.º 4
0
 def test_cache_empty_multiple_calls_does_not_throw(self):
     record1 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a')
     record2 = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
     cache = r.DNSCache()
     cache.add(record1)
     cache.add(record2)
     assert 'a' in cache.cache
     cache.remove(record1)
     cache.remove(record2)
     # Ensure multiple removes does not throw
     cache.remove(record1)
     cache.remove(record2)
     assert 'a' not in cache.cache
Ejemplo n.º 5
0
    def register_service(self, service_type, endpoint, params=None):
        """Register a service.

        This call announces the new services via multicast and instructs the
        built-in server to respond to queries about it.

        :param service_type: OpenStack service type, e.g. "baremetal".
        :param endpoint: full endpoint to reach the service.
        :param params: optional properties as a dictionary.
        :raises: :exc:`.ServiceRegistrationFailure` if the service cannot be
            registered, e.g. because of conflicts.
        """
        try:
            parsed = _parse_endpoint(endpoint)
        except socket.error as ex:
            msg = (_("Cannot resolve the host name of %(endpoint)s: "
                     "%(error)s. Hint: only IPv4 is supported for now.") % {
                         'endpoint': endpoint,
                         'error': ex
                     })
            raise exception.ServiceRegistrationFailure(service=service_type,
                                                       error=msg)

        all_params = CONF.mdns.params.copy()
        if params:
            all_params.update(params)
        all_params.update(parsed.params)

        # TODO(dtantsur): allow overriding TTL values via configuration when
        # https://github.com/jstasiak/python-zeroconf/commit/ecc021b7a3cec863eed5a3f71a1f28e3026c25b0
        # is released.
        info = zeroconf.ServiceInfo(_MDNS_DOMAIN,
                                    '%s.%s' % (service_type, _MDNS_DOMAIN),
                                    parsed.ip,
                                    parsed.port,
                                    properties=all_params,
                                    server=parsed.hostname)

        LOG.debug('Registering %s via mDNS', info)
        # Work around a potential race condition in the registration code:
        # https://github.com/jstasiak/python-zeroconf/issues/163
        delay = 0.1
        try:
            for attempt in range(CONF.mdns.registration_attempts):
                try:
                    self._zc.register_service(info)
                except zeroconf.NonUniqueNameException:
                    LOG.debug('Could not register %s - conflict', info)
                    if attempt == CONF.mdns.registration_attempts - 1:
                        raise
                    # reset the cache to purge learned records and retry
                    self._zc.cache = zeroconf.DNSCache()
                    time.sleep(delay)
                    delay *= 2
                else:
                    break
        except zeroconf.Error as exc:
            raise exception.ServiceRegistrationFailure(service=service_type,
                                                       error=exc)

        self._registered.append(info)