1
0

test_ddns.py 4.1 KB

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