test_dns.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from mock import Mock
  16. from twisted.internet import defer
  17. from twisted.names import dns, error
  18. from synapse.http.endpoint import resolve_service
  19. from tests.utils import MockClock
  20. from . import unittest
  21. @unittest.DEBUG
  22. class DnsTestCase(unittest.TestCase):
  23. @defer.inlineCallbacks
  24. def test_resolve(self):
  25. dns_client_mock = Mock()
  26. service_name = "test_service.example.com"
  27. host_name = "example.com"
  28. answer_srv = dns.RRHeader(
  29. type=dns.SRV, payload=dns.Record_SRV(target=host_name)
  30. )
  31. dns_client_mock.lookupService.return_value = defer.succeed(
  32. ([answer_srv], None, None)
  33. )
  34. cache = {}
  35. servers = yield resolve_service(
  36. service_name, dns_client=dns_client_mock, cache=cache
  37. )
  38. dns_client_mock.lookupService.assert_called_once_with(service_name)
  39. self.assertEquals(len(servers), 1)
  40. self.assertEquals(servers, cache[service_name])
  41. self.assertEquals(servers[0].host, host_name)
  42. @defer.inlineCallbacks
  43. def test_from_cache_expired_and_dns_fail(self):
  44. dns_client_mock = Mock()
  45. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  46. service_name = "test_service.example.com"
  47. entry = Mock(spec_set=["expires"])
  48. entry.expires = 0
  49. cache = {service_name: [entry]}
  50. servers = yield resolve_service(
  51. service_name, dns_client=dns_client_mock, cache=cache
  52. )
  53. dns_client_mock.lookupService.assert_called_once_with(service_name)
  54. self.assertEquals(len(servers), 1)
  55. self.assertEquals(servers, cache[service_name])
  56. @defer.inlineCallbacks
  57. def test_from_cache(self):
  58. clock = MockClock()
  59. dns_client_mock = Mock(spec_set=['lookupService'])
  60. dns_client_mock.lookupService = Mock(spec_set=[])
  61. service_name = "test_service.example.com"
  62. entry = Mock(spec_set=["expires"])
  63. entry.expires = 999999999
  64. cache = {service_name: [entry]}
  65. servers = yield resolve_service(
  66. service_name, dns_client=dns_client_mock, cache=cache, clock=clock
  67. )
  68. self.assertFalse(dns_client_mock.lookupService.called)
  69. self.assertEquals(len(servers), 1)
  70. self.assertEquals(servers, cache[service_name])
  71. @defer.inlineCallbacks
  72. def test_empty_cache(self):
  73. dns_client_mock = Mock()
  74. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  75. service_name = "test_service.example.com"
  76. cache = {}
  77. with self.assertRaises(error.DNSServerError):
  78. yield resolve_service(service_name, dns_client=dns_client_mock, cache=cache)
  79. @defer.inlineCallbacks
  80. def test_name_error(self):
  81. dns_client_mock = Mock()
  82. dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())
  83. service_name = "test_service.example.com"
  84. cache = {}
  85. servers = yield resolve_service(
  86. service_name, dns_client=dns_client_mock, cache=cache
  87. )
  88. self.assertEquals(len(servers), 0)
  89. self.assertEquals(len(cache), 0)