test_dnsutil.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. """
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. # Import Salt Libs
  9. import salt.modules.dnsutil as dnsutil
  10. import salt.utils.stringutils
  11. from tests.support.mock import MagicMock, mock_open, patch
  12. # Import Salt Testing Libs
  13. from tests.support.unit import TestCase, skipIf
  14. log = logging.getLogger(__name__)
  15. mock_hosts_file = salt.utils.stringutils.to_str(
  16. "##\n"
  17. "# Host Database\n"
  18. "#\n"
  19. "# localhost is used to configure the loopback interface\n"
  20. "# when the system is booting. Do not change this entry.\n"
  21. "##\n"
  22. "127.0.0.1 localhost\n"
  23. "255.255.255.255 broadcasthost\n"
  24. "::1 localhost\n"
  25. "fe80::1%lo0 localhost"
  26. )
  27. mock_hosts_file_rtn = {
  28. "::1": ["localhost"],
  29. "255.255.255.255": ["broadcasthost"],
  30. "127.0.0.1": ["localhost"],
  31. "fe80::1%lo0": ["localhost"],
  32. }
  33. mock_soa_zone = salt.utils.stringutils.to_str(
  34. "$TTL 3D\n"
  35. "@ IN SOA land-5.com. root.land-5.com. (\n"
  36. "199609203 ; Serial\n"
  37. "28800 ; Refresh\n"
  38. "7200 ; Retry\n"
  39. "604800 ; Expire\n"
  40. "86400) ; Minimum TTL\n"
  41. "NS land-5.com.\n\n"
  42. "1 PTR localhost."
  43. )
  44. mock_writes_list = salt.utils.data.decode(
  45. [
  46. "##\n",
  47. "# Host Database\n",
  48. "#\n",
  49. "# localhost is used to configure the loopback interface\n",
  50. "# when the system is booting. Do not change this entry.\n",
  51. "##\n",
  52. "127.0.0.1 localhost",
  53. "\n",
  54. "255.255.255.255 broadcasthost",
  55. "\n",
  56. "::1 localhost",
  57. "\n",
  58. "fe80::1%lo0 localhost",
  59. "\n",
  60. ],
  61. to_str=True,
  62. )
  63. class DNSUtilTestCase(TestCase):
  64. def test_parse_hosts(self):
  65. with patch("salt.utils.files.fopen", mock_open(read_data=mock_hosts_file)):
  66. self.assertEqual(
  67. dnsutil.parse_hosts(),
  68. {
  69. "::1": ["localhost"],
  70. "255.255.255.255": ["broadcasthost"],
  71. "127.0.0.1": ["localhost"],
  72. "fe80::1%lo0": ["localhost"],
  73. },
  74. )
  75. def test_hosts_append(self):
  76. with patch(
  77. "salt.utils.files.fopen", mock_open(read_data=mock_hosts_file)
  78. ) as m_open, patch(
  79. "salt.modules.dnsutil.parse_hosts",
  80. MagicMock(return_value=mock_hosts_file_rtn),
  81. ):
  82. dnsutil.hosts_append("/etc/hosts", "127.0.0.1", "ad1.yuk.co,ad2.yuk.co")
  83. writes = m_open.write_calls()
  84. # We should have called .write() only once, with the expected
  85. # content
  86. num_writes = len(writes)
  87. assert num_writes == 1, num_writes
  88. expected = salt.utils.stringutils.to_str(
  89. "\n127.0.0.1 ad1.yuk.co ad2.yuk.co"
  90. )
  91. assert writes[0] == expected, writes[0]
  92. def test_hosts_remove(self):
  93. to_remove = "ad1.yuk.co"
  94. new_mock_file = mock_hosts_file + "\n127.0.0.1 " + to_remove + "\n"
  95. with patch(
  96. "salt.utils.files.fopen", mock_open(read_data=new_mock_file)
  97. ) as m_open:
  98. dnsutil.hosts_remove("/etc/hosts", to_remove)
  99. writes = m_open.write_calls()
  100. assert writes == mock_writes_list, writes
  101. @skipIf(True, "Waiting on bug report fixes")
  102. def test_parse_zone(self):
  103. with patch("salt.utils.files.fopen", mock_open(read_data=mock_soa_zone)):
  104. log.debug(mock_soa_zone)
  105. log.debug(dnsutil.parse_zone("/var/lib/named/example.com.zone"))
  106. def test_to_seconds_hour(self):
  107. self.assertEqual(
  108. dnsutil._to_seconds("4H"),
  109. 14400,
  110. msg="Did not detect valid hours as invalid",
  111. )
  112. def test_to_seconds_day(self):
  113. self.assertEqual(
  114. dnsutil._to_seconds("1D"), 86400, msg="Did not detect valid day as invalid"
  115. )
  116. def test_to_seconds_week(self):
  117. self.assertEqual(
  118. dnsutil._to_seconds("2W"),
  119. 604800,
  120. msg="Did not set time greater than one week to one week",
  121. )
  122. def test_to_seconds_empty(self):
  123. self.assertEqual(
  124. dnsutil._to_seconds(""), 604800, msg="Did not set empty time to one week"
  125. )
  126. def test_to_seconds_large(self):
  127. self.assertEqual(
  128. dnsutil._to_seconds("604801"),
  129. 604800,
  130. msg="Did not set time greater than one week to one week",
  131. )