def test_disabled_service(self): """ test the behaviour when there is a single record which is ".". """ service_name = b"test_service.example.com" lookup_deferred = Deferred() dns_client_mock = Mock() dns_client_mock.lookupService.return_value = lookup_deferred cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) # Old versions of Twisted don't have an ensureDeferred in failureResultOf. resolve_d = defer.ensureDeferred(resolver.resolve_service(service_name)) # returning a single "." should make the lookup fail with a ConenctError lookup_deferred.callback( ( [dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"."))], None, None, ) ) self.failureResultOf(resolve_d, ConnectError)
def test_non_srv_answer(self): """ test the behaviour when the dns server gives us a spurious non-SRV response """ service_name = b"test_service.example.com" lookup_deferred = Deferred() dns_client_mock = Mock() dns_client_mock.lookupService.return_value = lookup_deferred cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) resolve_d = resolver.resolve_service(service_name) self.assertNoResult(resolve_d) lookup_deferred.callback(( [ dns.RRHeader(type=dns.A, payload=dns.Record_A()), dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"host")), ], None, None, )) servers = self.successResultOf(resolve_d) self.assertEquals(len(servers), 1) self.assertEquals(servers, cache[service_name]) self.assertEquals(servers[0].host, b"host")
def test_from_cache(self): clock = MockClock() dns_client_mock = Mock(spec_set=["lookupService"]) dns_client_mock.lookupService = Mock(spec_set=[]) service_name = b"test_service.example.com" entry = Mock(spec_set=["expires", "priority", "weight"]) entry.expires = 999999999 entry.priority = 0 entry.weight = 0 cache = {service_name: [entry]} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache, get_time=clock.time) servers = yield defer.ensureDeferred( resolver.resolve_service(service_name)) self.assertFalse(dns_client_mock.lookupService.called) self.assertEquals(len(servers), 1) self.assertEquals(servers, cache[service_name])
def test_non_srv_answer(self): """ test the behaviour when the dns server gives us a spurious non-SRV response """ service_name = b"test_service.example.com" lookup_deferred = Deferred() dns_client_mock = Mock() dns_client_mock.lookupService.return_value = lookup_deferred cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) resolve_d = resolver.resolve_service(service_name) self.assertNoResult(resolve_d) lookup_deferred.callback( ( [ dns.RRHeader(type=dns.A, payload=dns.Record_A()), dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"host")), ], None, None, ) ) servers = self.successResultOf(resolve_d) self.assertEquals(len(servers), 1) self.assertEquals(servers, cache[service_name]) self.assertEquals(servers[0].host, b"host")
def test_disabled_service(self): """ test the behaviour when there is a single record which is ".". """ service_name = b"test_service.example.com" lookup_deferred = Deferred() dns_client_mock = Mock() dns_client_mock.lookupService.return_value = lookup_deferred cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) resolve_d = resolver.resolve_service(service_name) self.assertNoResult(resolve_d) # returning a single "." should make the lookup fail with a ConenctError lookup_deferred.callback( ( [dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"."))], None, None, ) ) self.failureResultOf(resolve_d, ConnectError)
def test_empty_cache(self): dns_client_mock = Mock() dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError()) service_name = b"test_service.example.com" cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) with self.assertRaises(error.DNSServerError): yield defer.ensureDeferred(resolver.resolve_service(service_name))
def test_empty_cache(self): dns_client_mock = Mock() dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError()) service_name = b"test_service.example.com" cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) with self.assertRaises(error.DNSServerError): yield resolver.resolve_service(service_name)
def test_name_error(self): dns_client_mock = Mock() dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError()) service_name = b"test_service.example.com" cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) servers = yield defer.ensureDeferred(resolver.resolve_service(service_name)) self.assertEqual(len(servers), 0) self.assertEqual(len(cache), 0)
def test_name_error(self): dns_client_mock = Mock() dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError()) service_name = b"test_service.example.com" cache = {} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) servers = yield resolver.resolve_service(service_name) self.assertEquals(len(servers), 0) self.assertEquals(len(cache), 0)
def test_from_cache_expired_and_dns_fail(self): dns_client_mock = Mock() dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError()) service_name = b"test_service.example.com" entry = Mock(spec_set=["expires"]) entry.expires = 0 cache = {service_name: [entry]} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache) servers = yield resolver.resolve_service(service_name) dns_client_mock.lookupService.assert_called_once_with(service_name) self.assertEquals(len(servers), 1) self.assertEquals(servers, cache[service_name])
def test_from_cache(self): clock = MockClock() dns_client_mock = Mock(spec_set=['lookupService']) dns_client_mock.lookupService = Mock(spec_set=[]) service_name = b"test_service.example.com" entry = Mock(spec_set=["expires"]) entry.expires = 999999999 cache = {service_name: [entry]} resolver = SrvResolver(dns_client=dns_client_mock, cache=cache, get_time=clock.time) servers = yield resolver.resolve_service(service_name) self.assertFalse(dns_client_mock.lookupService.called) self.assertEquals(len(servers), 1) self.assertEquals(servers, cache[service_name])
def test_from_cache(self): clock = MockClock() dns_client_mock = Mock(spec_set=['lookupService']) dns_client_mock.lookupService = Mock(spec_set=[]) service_name = b"test_service.example.com" entry = Mock(spec_set=["expires"]) entry.expires = 999999999 cache = {service_name: [entry]} resolver = SrvResolver( dns_client=dns_client_mock, cache=cache, get_time=clock.time ) servers = yield resolver.resolve_service(service_name) self.assertFalse(dns_client_mock.lookupService.called) self.assertEquals(len(servers), 1) self.assertEquals(servers, cache[service_name])