1
0

test_hosts.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test the hosts module
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import logging
  7. import os
  8. import shutil
  9. import pytest
  10. import salt.utils.files
  11. import salt.utils.stringutils
  12. from tests.support.case import ModuleCase
  13. from tests.support.helpers import slowTest
  14. from tests.support.runtests import RUNTIME_VARS
  15. log = logging.getLogger(__name__)
  16. @pytest.mark.windows_whitelisted
  17. class HostsModuleTest(ModuleCase):
  18. """
  19. Test the hosts module
  20. """
  21. maxDiff = None
  22. @classmethod
  23. def setUpClass(cls):
  24. cls.hosts_file = os.path.join(RUNTIME_VARS.TMP, "hosts")
  25. def __clear_hosts(self):
  26. """
  27. Delete the tmp hosts file
  28. """
  29. if os.path.isfile(self.hosts_file):
  30. os.remove(self.hosts_file)
  31. def setUp(self):
  32. shutil.copyfile(os.path.join(RUNTIME_VARS.FILES, "hosts"), self.hosts_file)
  33. self.addCleanup(self.__clear_hosts)
  34. @slowTest
  35. def test_list_hosts(self):
  36. """
  37. hosts.list_hosts
  38. """
  39. hosts = self.run_function("hosts.list_hosts")
  40. self.assertEqual(len(hosts), 10)
  41. self.assertEqual(hosts["::1"], {"aliases": ["ip6-localhost", "ip6-loopback"]})
  42. self.assertEqual(hosts["127.0.0.1"], {"aliases": ["localhost", "myname"]})
  43. @slowTest
  44. def test_list_hosts_nofile(self):
  45. """
  46. hosts.list_hosts
  47. without a hosts file
  48. """
  49. if os.path.isfile(self.hosts_file):
  50. os.remove(self.hosts_file)
  51. hosts = self.run_function("hosts.list_hosts")
  52. self.assertEqual(hosts, {})
  53. @slowTest
  54. def test_get_ip(self):
  55. """
  56. hosts.get_ip
  57. """
  58. self.assertEqual(self.run_function("hosts.get_ip", ["myname"]), "127.0.0.1")
  59. self.assertEqual(self.run_function("hosts.get_ip", ["othername"]), "")
  60. self.__clear_hosts()
  61. self.assertEqual(self.run_function("hosts.get_ip", ["othername"]), "")
  62. @slowTest
  63. def test_get_alias(self):
  64. """
  65. hosts.get_alias
  66. """
  67. self.assertEqual(
  68. self.run_function("hosts.get_alias", ["127.0.0.1"]), ["localhost", "myname"]
  69. )
  70. self.assertEqual(self.run_function("hosts.get_alias", ["127.0.0.2"]), [])
  71. self.__clear_hosts()
  72. self.assertEqual(self.run_function("hosts.get_alias", ["127.0.0.1"]), [])
  73. @slowTest
  74. def test_has_pair(self):
  75. """
  76. hosts.has_pair
  77. """
  78. self.assertTrue(self.run_function("hosts.has_pair", ["127.0.0.1", "myname"]))
  79. self.assertFalse(
  80. self.run_function("hosts.has_pair", ["127.0.0.1", "othername"])
  81. )
  82. @slowTest
  83. def test_set_host(self):
  84. """
  85. hosts.set_hosts
  86. """
  87. self.assertTrue(self.run_function("hosts.set_host", ["192.168.1.123", "newip"]))
  88. self.assertTrue(self.run_function("hosts.has_pair", ["192.168.1.123", "newip"]))
  89. self.assertTrue(self.run_function("hosts.set_host", ["127.0.0.1", "localhost"]))
  90. self.assertEqual(len(self.run_function("hosts.list_hosts")), 11)
  91. self.assertFalse(
  92. self.run_function("hosts.has_pair", ["127.0.0.1", "myname"]),
  93. "should remove second entry",
  94. )
  95. @slowTest
  96. def test_add_host(self):
  97. """
  98. hosts.add_host
  99. """
  100. self.assertTrue(self.run_function("hosts.add_host", ["192.168.1.123", "newip"]))
  101. self.assertTrue(self.run_function("hosts.has_pair", ["192.168.1.123", "newip"]))
  102. self.assertEqual(len(self.run_function("hosts.list_hosts")), 11)
  103. self.assertTrue(
  104. self.run_function("hosts.add_host", ["127.0.0.1", "othernameip"])
  105. )
  106. self.assertEqual(len(self.run_function("hosts.list_hosts")), 11)
  107. @slowTest
  108. def test_rm_host(self):
  109. self.assertTrue(self.run_function("hosts.has_pair", ["127.0.0.1", "myname"]))
  110. self.assertTrue(self.run_function("hosts.rm_host", ["127.0.0.1", "myname"]))
  111. self.assertFalse(self.run_function("hosts.has_pair", ["127.0.0.1", "myname"]))
  112. self.assertTrue(self.run_function("hosts.rm_host", ["127.0.0.1", "unknown"]))
  113. @slowTest
  114. def test_add_host_formatting(self):
  115. """
  116. Ensure that hosts.add_host isn't adding duplicates and that
  117. it's formatting the output correctly
  118. """
  119. # instead of using the 'clean' hosts file we're going to
  120. # use an empty one so we can prove the syntax of the entries
  121. # being added by the hosts module
  122. self.__clear_hosts()
  123. with salt.utils.files.fopen(self.hosts_file, "w"):
  124. pass
  125. self.assertTrue(
  126. self.run_function("hosts.add_host", ["192.168.1.3", "host3.fqdn.com"])
  127. )
  128. self.assertTrue(
  129. self.run_function("hosts.add_host", ["192.168.1.1", "host1.fqdn.com"])
  130. )
  131. self.assertTrue(self.run_function("hosts.add_host", ["192.168.1.1", "host1"]))
  132. self.assertTrue(
  133. self.run_function("hosts.add_host", ["192.168.1.2", "host2.fqdn.com"])
  134. )
  135. self.assertTrue(self.run_function("hosts.add_host", ["192.168.1.2", "host2"]))
  136. self.assertTrue(
  137. self.run_function("hosts.add_host", ["192.168.1.2", "oldhost2"])
  138. )
  139. self.assertTrue(
  140. self.run_function("hosts.add_host", ["192.168.1.2", "host2-reorder"])
  141. )
  142. self.assertTrue(
  143. self.run_function("hosts.add_host", ["192.168.1.1", "host1-reorder"])
  144. )
  145. # now read the lines and ensure they're formatted correctly
  146. with salt.utils.files.fopen(self.hosts_file, "r") as fp_:
  147. lines = salt.utils.stringutils.to_unicode(fp_.read()).splitlines()
  148. self.assertEqual(
  149. lines,
  150. [
  151. "192.168.1.3\t\thost3.fqdn.com",
  152. "192.168.1.1\t\thost1.fqdn.com host1 host1-reorder",
  153. "192.168.1.2\t\thost2.fqdn.com host2 oldhost2 host2-reorder",
  154. ],
  155. )