test_dns.py 3.8 KB

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