test_dns.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class DnsTestCase(unittest.TestCase):
  21. @defer.inlineCallbacks
  22. def test_resolve(self):
  23. dns_client_mock = Mock()
  24. service_name = "test_service.examle.com"
  25. host_name = "example.com"
  26. ip_address = "127.0.0.1"
  27. answer_srv = dns.RRHeader(
  28. type=dns.SRV,
  29. payload=dns.Record_SRV(
  30. target=host_name,
  31. )
  32. )
  33. answer_a = dns.RRHeader(
  34. type=dns.A,
  35. payload=dns.Record_A(
  36. address=ip_address,
  37. )
  38. )
  39. dns_client_mock.lookupService.return_value = ([answer_srv], None, None)
  40. dns_client_mock.lookupAddress.return_value = ([answer_a], None, None)
  41. cache = {}
  42. servers = yield resolve_service(
  43. service_name, dns_client=dns_client_mock, cache=cache
  44. )
  45. dns_client_mock.lookupService.assert_called_once_with(service_name)
  46. dns_client_mock.lookupAddress.assert_called_once_with(host_name)
  47. self.assertEquals(len(servers), 1)
  48. self.assertEquals(servers, cache[service_name])
  49. self.assertEquals(servers[0].host, ip_address)
  50. @defer.inlineCallbacks
  51. def test_from_cache(self):
  52. dns_client_mock = Mock()
  53. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  54. service_name = "test_service.examle.com"
  55. cache = {
  56. service_name: [object()]
  57. }
  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. self.assertEquals(len(servers), 1)
  63. self.assertEquals(servers, cache[service_name])
  64. @defer.inlineCallbacks
  65. def test_empty_cache(self):
  66. dns_client_mock = Mock()
  67. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  68. service_name = "test_service.examle.com"
  69. cache = {}
  70. with self.assertRaises(error.DNSServerError):
  71. yield resolve_service(
  72. service_name, dns_client=dns_client_mock, cache=cache
  73. )
  74. @defer.inlineCallbacks
  75. def test_name_error(self):
  76. dns_client_mock = Mock()
  77. dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())
  78. service_name = "test_service.examle.com"
  79. cache = {}
  80. servers = yield resolve_service(
  81. service_name, dns_client=dns_client_mock, cache=cache
  82. )
  83. self.assertEquals(len(servers), 0)
  84. self.assertEquals(len(cache), 0)