test_dns.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. class DnsTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def test_resolve(self):
  24. dns_client_mock = Mock()
  25. service_name = "test_service.examle.com"
  26. host_name = "example.com"
  27. ip_address = "127.0.0.1"
  28. answer_srv = dns.RRHeader(
  29. type=dns.SRV,
  30. payload=dns.Record_SRV(
  31. target=host_name,
  32. )
  33. )
  34. answer_a = dns.RRHeader(
  35. type=dns.A,
  36. payload=dns.Record_A(
  37. address=ip_address,
  38. )
  39. )
  40. dns_client_mock.lookupService.return_value = ([answer_srv], None, None)
  41. dns_client_mock.lookupAddress.return_value = ([answer_a], None, None)
  42. cache = {}
  43. servers = yield resolve_service(
  44. service_name, dns_client=dns_client_mock, cache=cache
  45. )
  46. dns_client_mock.lookupService.assert_called_once_with(service_name)
  47. dns_client_mock.lookupAddress.assert_called_once_with(host_name)
  48. self.assertEquals(len(servers), 1)
  49. self.assertEquals(servers, cache[service_name])
  50. self.assertEquals(servers[0].host, ip_address)
  51. @defer.inlineCallbacks
  52. def test_from_cache_expired_and_dns_fail(self):
  53. dns_client_mock = Mock()
  54. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  55. service_name = "test_service.examle.com"
  56. entry = Mock(spec_set=["expires"])
  57. entry.expires = 0
  58. cache = {
  59. service_name: [entry]
  60. }
  61. servers = yield resolve_service(
  62. service_name, dns_client=dns_client_mock, cache=cache
  63. )
  64. dns_client_mock.lookupService.assert_called_once_with(service_name)
  65. self.assertEquals(len(servers), 1)
  66. self.assertEquals(servers, cache[service_name])
  67. @defer.inlineCallbacks
  68. def test_from_cache(self):
  69. clock = MockClock()
  70. dns_client_mock = Mock(spec_set=['lookupService'])
  71. dns_client_mock.lookupService = Mock(spec_set=[])
  72. service_name = "test_service.examle.com"
  73. entry = Mock(spec_set=["expires"])
  74. entry.expires = 999999999
  75. cache = {
  76. service_name: [entry]
  77. }
  78. servers = yield resolve_service(
  79. service_name, dns_client=dns_client_mock, cache=cache, clock=clock,
  80. )
  81. self.assertFalse(dns_client_mock.lookupService.called)
  82. self.assertEquals(len(servers), 1)
  83. self.assertEquals(servers, cache[service_name])
  84. @defer.inlineCallbacks
  85. def test_empty_cache(self):
  86. dns_client_mock = Mock()
  87. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  88. service_name = "test_service.examle.com"
  89. cache = {}
  90. with self.assertRaises(error.DNSServerError):
  91. yield resolve_service(
  92. service_name, dns_client=dns_client_mock, cache=cache
  93. )
  94. @defer.inlineCallbacks
  95. def test_name_error(self):
  96. dns_client_mock = Mock()
  97. dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())
  98. service_name = "test_service.examle.com"
  99. cache = {}
  100. servers = yield resolve_service(
  101. service_name, dns_client=dns_client_mock, cache=cache
  102. )
  103. self.assertEquals(len(servers), 0)
  104. self.assertEquals(len(cache), 0)