test_hosts.py 6.2 KB

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