test_ddns.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import textwrap
  8. import salt.modules.ddns as ddns
  9. # Import Salt Libs
  10. import salt.utils.json
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, mock_open, patch
  14. from tests.support.unit import TestCase, skipIf
  15. try:
  16. import dns.query
  17. import dns.tsigkeyring
  18. HAS_DNS = True
  19. except ImportError:
  20. HAS_DNS = False
  21. @skipIf(HAS_DNS is False, "dnspython libs not installed")
  22. class DDNSTestCase(TestCase, LoaderModuleMockMixin):
  23. """
  24. TestCase for the salt.modules.ddns module
  25. """
  26. def setup_loader_modules(self):
  27. return {ddns: {}}
  28. def test_add_host(self):
  29. """
  30. Test cases for Add, replace, or update the A
  31. and PTR (reverse) records for a host.
  32. """
  33. with patch("salt.modules.ddns.update") as ddns_update:
  34. ddns_update.return_value = False
  35. self.assertFalse(ddns.add_host(zone="A", name="B", ttl=1, ip="172.27.0.0"))
  36. ddns_update.return_value = True
  37. self.assertTrue(ddns.add_host(zone="A", name="B", ttl=1, ip="172.27.0.0"))
  38. def test_delete_host(self):
  39. """
  40. Tests for delete the forward and reverse records for a host.
  41. """
  42. with patch("salt.modules.ddns.delete") as ddns_delete:
  43. ddns_delete.return_value = False
  44. with patch.object(dns.query, "udp") as mock:
  45. mock.answer = [{"address": "localhost"}]
  46. self.assertFalse(ddns.delete_host(zone="A", name="B"))
  47. def test_update(self):
  48. """
  49. Test to add, replace, or update a DNS record.
  50. """
  51. mock_request = textwrap.dedent(
  52. """\
  53. id 29380
  54. opcode QUERY
  55. rcode NOERROR
  56. flags RD
  57. ;QUESTION
  58. name.zone. IN AAAA
  59. ;ANSWER
  60. ;AUTHORITY
  61. ;ADDITIONAL"""
  62. )
  63. mock_rdtype = 28 # rdtype of AAAA record
  64. class MockRrset(object):
  65. def __init__(self):
  66. self.items = [{"address": "localhost"}]
  67. self.ttl = 2
  68. class MockAnswer(object):
  69. def __init__(self, *args, **kwargs):
  70. self.answer = [MockRrset()]
  71. def rcode(self):
  72. return 0
  73. def mock_udp_query(*args, **kwargs):
  74. return MockAnswer
  75. with patch.object(
  76. dns.message, "make_query", MagicMock(return_value=mock_request)
  77. ):
  78. with patch.object(dns.query, "udp", mock_udp_query()):
  79. with patch.object(
  80. dns.rdatatype, "from_text", MagicMock(return_value=mock_rdtype)
  81. ):
  82. with patch.object(ddns, "_get_keyring", return_value=None):
  83. with patch.object(ddns, "_config", return_value=None):
  84. self.assertTrue(
  85. ddns.update("zone", "name", 1, "AAAA", "::1")
  86. )
  87. def test_delete(self):
  88. """
  89. Test to delete a DNS record.
  90. """
  91. file_data = salt.utils.json.dumps({"A": "B"})
  92. class MockAnswer(object):
  93. def __init__(self, *args, **kwargs):
  94. self.answer = [{"address": "localhost"}]
  95. def rcode(self):
  96. return 0
  97. def mock_udp_query(*args, **kwargs):
  98. return MockAnswer
  99. with patch.object(dns.query, "udp", mock_udp_query()):
  100. with patch(
  101. "salt.utils.files.fopen", mock_open(read_data=file_data), create=True
  102. ):
  103. with patch.object(dns.tsigkeyring, "from_text", return_value=True):
  104. with patch.object(ddns, "_get_keyring", return_value=None):
  105. with patch.object(ddns, "_config", return_value=None):
  106. self.assertTrue(ddns.delete(zone="A", name="B"))