test_hosts.py 6.3 KB

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