test_dns.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 . import unittest
  16. from twisted.internet import defer
  17. from twisted.names import dns, error
  18. from mock import Mock
  19. from synapse.http.endpoint import resolve_service
  20. from tests.utils import MockClock
  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. ip_address = "127.0.0.1"
  29. ip6_address = "::1"
  30. answer_srv = dns.RRHeader(
  31. type=dns.SRV,
  32. payload=dns.Record_SRV(
  33. target=host_name,
  34. )
  35. )
  36. answer_a = dns.RRHeader(
  37. type=dns.A,
  38. payload=dns.Record_A(
  39. address=ip_address,
  40. )
  41. )
  42. answer_aaaa = dns.RRHeader(
  43. type=dns.AAAA,
  44. payload=dns.Record_AAAA(
  45. address=ip6_address,
  46. )
  47. )
  48. dns_client_mock.lookupService.return_value = defer.succeed(
  49. ([answer_srv], None, None),
  50. )
  51. dns_client_mock.lookupAddress.return_value = defer.succeed(
  52. ([answer_a], None, None),
  53. )
  54. dns_client_mock.lookupIPV6Address.return_value = defer.succeed(
  55. ([answer_aaaa], None, None),
  56. )
  57. cache = {}
  58. servers = yield resolve_service(
  59. service_name, dns_client=dns_client_mock, cache=cache
  60. )
  61. dns_client_mock.lookupService.assert_called_once_with(service_name)
  62. dns_client_mock.lookupAddress.assert_called_once_with(host_name)
  63. dns_client_mock.lookupIPV6Address.assert_called_once_with(host_name)
  64. self.assertEquals(len(servers), 2)
  65. self.assertEquals(servers, cache[service_name])
  66. self.assertEquals(servers[0].host, ip_address)
  67. self.assertEquals(servers[1].host, ip6_address)
  68. @defer.inlineCallbacks
  69. def test_from_cache_expired_and_dns_fail(self):
  70. dns_client_mock = Mock()
  71. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  72. service_name = "test_service.examle.com"
  73. entry = Mock(spec_set=["expires"])
  74. entry.expires = 0
  75. cache = {
  76. service_name: [entry]
  77. }
  78. servers = yield resolve_service(
  79. service_name, dns_client=dns_client_mock, cache=cache
  80. )
  81. dns_client_mock.lookupService.assert_called_once_with(service_name)
  82. self.assertEquals(len(servers), 1)
  83. self.assertEquals(servers, cache[service_name])
  84. @defer.inlineCallbacks
  85. def test_from_cache(self):
  86. clock = MockClock()
  87. dns_client_mock = Mock(spec_set=['lookupService'])
  88. dns_client_mock.lookupService = Mock(spec_set=[])
  89. service_name = "test_service.examle.com"
  90. entry = Mock(spec_set=["expires"])
  91. entry.expires = 999999999
  92. cache = {
  93. service_name: [entry]
  94. }
  95. servers = yield resolve_service(
  96. service_name, dns_client=dns_client_mock, cache=cache, clock=clock,
  97. )
  98. self.assertFalse(dns_client_mock.lookupService.called)
  99. self.assertEquals(len(servers), 1)
  100. self.assertEquals(servers, cache[service_name])
  101. @defer.inlineCallbacks
  102. def test_empty_cache(self):
  103. dns_client_mock = Mock()
  104. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  105. service_name = "test_service.examle.com"
  106. cache = {}
  107. with self.assertRaises(error.DNSServerError):
  108. yield resolve_service(
  109. service_name, dns_client=dns_client_mock, cache=cache
  110. )
  111. @defer.inlineCallbacks
  112. def test_name_error(self):
  113. dns_client_mock = Mock()
  114. dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())
  115. service_name = "test_service.examle.com"
  116. cache = {}
  117. servers = yield resolve_service(
  118. service_name, dns_client=dns_client_mock, cache=cache
  119. )
  120. self.assertEquals(len(servers), 0)
  121. self.assertEquals(len(cache), 0)