test_file.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import getpass
  5. import os
  6. import shutil
  7. import sys
  8. # Posix only
  9. try:
  10. import grp
  11. import pwd
  12. except ImportError:
  13. pass
  14. # Windows only
  15. try:
  16. import win32file
  17. except ImportError:
  18. pass
  19. # Import Salt Testing libs
  20. from tests.support.case import ModuleCase
  21. from tests.support.unit import skipIf
  22. from tests.support.paths import FILES, TMP
  23. # Import salt libs
  24. import salt.utils.files
  25. import salt.utils.platform
  26. def symlink(source, link_name):
  27. '''
  28. Handle symlinks on Windows with Python < 3.2
  29. '''
  30. if salt.utils.platform.is_windows():
  31. win32file.CreateSymbolicLink(link_name, source)
  32. else:
  33. os.symlink(source, link_name)
  34. class FileModuleTest(ModuleCase):
  35. '''
  36. Validate the file module
  37. '''
  38. def setUp(self):
  39. self.myfile = os.path.join(TMP, 'myfile')
  40. with salt.utils.files.fopen(self.myfile, 'w+') as fp:
  41. fp.write(salt.utils.stringutils.to_str('Hello' + os.linesep))
  42. self.mydir = os.path.join(TMP, 'mydir/isawesome')
  43. if not os.path.isdir(self.mydir):
  44. # left behind... Don't fail because of this!
  45. os.makedirs(self.mydir)
  46. self.mysymlink = os.path.join(TMP, 'mysymlink')
  47. if os.path.islink(self.mysymlink) or os.path.isfile(self.mysymlink):
  48. os.remove(self.mysymlink)
  49. symlink(self.myfile, self.mysymlink)
  50. self.mybadsymlink = os.path.join(TMP, 'mybadsymlink')
  51. if os.path.islink(self.mybadsymlink) or os.path.isfile(self.mybadsymlink):
  52. os.remove(self.mybadsymlink)
  53. symlink('/nonexistentpath', self.mybadsymlink)
  54. super(FileModuleTest, self).setUp()
  55. def tearDown(self):
  56. if os.path.isfile(self.myfile):
  57. os.remove(self.myfile)
  58. if os.path.islink(self.mysymlink) or os.path.isfile(self.mysymlink):
  59. os.remove(self.mysymlink)
  60. if os.path.islink(self.mybadsymlink) or os.path.isfile(self.mybadsymlink):
  61. os.remove(self.mybadsymlink)
  62. shutil.rmtree(self.mydir, ignore_errors=True)
  63. super(FileModuleTest, self).tearDown()
  64. @skipIf(salt.utils.platform.is_windows(), 'No chgrp on Windows')
  65. def test_chown(self):
  66. user = getpass.getuser()
  67. if sys.platform == 'darwin':
  68. group = 'staff'
  69. elif sys.platform.startswith(('linux', 'freebsd', 'openbsd')):
  70. group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
  71. ret = self.run_function('file.chown', arg=[self.myfile, user, group])
  72. self.assertIsNone(ret)
  73. fstat = os.stat(self.myfile)
  74. self.assertEqual(fstat.st_uid, os.getuid())
  75. self.assertEqual(fstat.st_gid, grp.getgrnam(group).gr_gid)
  76. @skipIf(salt.utils.platform.is_windows(), 'No chgrp on Windows')
  77. def test_chown_no_user(self):
  78. user = 'notanyuseriknow'
  79. group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
  80. ret = self.run_function('file.chown', arg=[self.myfile, user, group])
  81. self.assertIn('not exist', ret)
  82. @skipIf(salt.utils.platform.is_windows(), 'No chgrp on Windows')
  83. def test_chown_no_user_no_group(self):
  84. user = 'notanyuseriknow'
  85. group = 'notanygroupyoushoulduse'
  86. ret = self.run_function('file.chown', arg=[self.myfile, user, group])
  87. self.assertIn('Group does not exist', ret)
  88. self.assertIn('User does not exist', ret)
  89. @skipIf(salt.utils.platform.is_windows(), 'No chgrp on Windows')
  90. def test_chown_no_path(self):
  91. user = getpass.getuser()
  92. if sys.platform == 'darwin':
  93. group = 'staff'
  94. elif sys.platform.startswith(('linux', 'freebsd', 'openbsd')):
  95. group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
  96. ret = self.run_function('file.chown',
  97. arg=['/tmp/nosuchfile', user, group])
  98. self.assertIn('File not found', ret)
  99. @skipIf(salt.utils.platform.is_windows(), 'No chgrp on Windows')
  100. def test_chown_noop(self):
  101. user = ''
  102. group = ''
  103. ret = self.run_function('file.chown', arg=[self.myfile, user, group])
  104. self.assertIsNone(ret)
  105. fstat = os.stat(self.myfile)
  106. self.assertEqual(fstat.st_uid, os.getuid())
  107. self.assertEqual(fstat.st_gid, os.getgid())
  108. @skipIf(salt.utils.platform.is_windows(), 'No chgrp on Windows')
  109. def test_chgrp(self):
  110. if sys.platform == 'darwin':
  111. group = 'everyone'
  112. elif sys.platform.startswith(('linux', 'freebsd', 'openbsd')):
  113. group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
  114. ret = self.run_function('file.chgrp', arg=[self.myfile, group])
  115. self.assertIsNone(ret)
  116. fstat = os.stat(self.myfile)
  117. self.assertEqual(fstat.st_gid, grp.getgrnam(group).gr_gid)
  118. @skipIf(salt.utils.platform.is_windows(), 'No chgrp on Windows')
  119. def test_chgrp_failure(self):
  120. group = 'thisgroupdoesntexist'
  121. ret = self.run_function('file.chgrp', arg=[self.myfile, group])
  122. self.assertIn('not exist', ret)
  123. def test_patch(self):
  124. if not self.run_function('cmd.has_exec', ['patch']):
  125. self.skipTest('patch is not installed')
  126. src_patch = os.path.join(
  127. FILES, 'file', 'base', 'hello.patch')
  128. src_file = os.path.join(TMP, 'src.txt')
  129. with salt.utils.files.fopen(src_file, 'w+') as fp:
  130. fp.write(salt.utils.stringutils.to_str('Hello\n'))
  131. # dry-run should not modify src_file
  132. ret = self.minion_run('file.patch', src_file, src_patch, dry_run=True)
  133. assert ret['retcode'] == 0, repr(ret)
  134. with salt.utils.files.fopen(src_file) as fp:
  135. self.assertEqual(
  136. salt.utils.stringutils.to_unicode(fp.read()), 'Hello\n')
  137. ret = self.minion_run('file.patch', src_file, src_patch)
  138. assert ret['retcode'] == 0, repr(ret)
  139. with salt.utils.files.fopen(src_file) as fp:
  140. self.assertEqual(
  141. salt.utils.stringutils.to_unicode(fp.read()), 'Hello world\n')
  142. def test_remove_file(self):
  143. ret = self.run_function('file.remove', arg=[self.myfile])
  144. self.assertTrue(ret)
  145. def test_remove_dir(self):
  146. ret = self.run_function('file.remove', arg=[self.mydir])
  147. self.assertTrue(ret)
  148. def test_remove_symlink(self):
  149. ret = self.run_function('file.remove', arg=[self.mysymlink])
  150. self.assertTrue(ret)
  151. def test_remove_broken_symlink(self):
  152. ret = self.run_function('file.remove', arg=[self.mybadsymlink])
  153. self.assertTrue(ret)
  154. def test_cannot_remove(self):
  155. ret = self.run_function('file.remove', arg=['tty'])
  156. self.assertEqual(
  157. 'ERROR executing \'file.remove\': File path must be absolute: tty', ret
  158. )
  159. def test_source_list_for_single_file_returns_unchanged(self):
  160. ret = self.run_function('file.source_list', ['salt://http/httpd.conf',
  161. 'filehash', 'base'])
  162. self.assertEqual(list(ret), ['salt://http/httpd.conf', 'filehash'])
  163. def test_source_list_for_single_local_file_slash_returns_unchanged(self):
  164. ret = self.run_function('file.source_list', [self.myfile,
  165. 'filehash', 'base'])
  166. self.assertEqual(list(ret), [self.myfile, 'filehash'])
  167. def test_source_list_for_single_local_file_proto_returns_unchanged(self):
  168. ret = self.run_function('file.source_list', ['file://' + self.myfile,
  169. 'filehash', 'base'])
  170. self.assertEqual(list(ret), ['file://' + self.myfile, 'filehash'])
  171. def test_file_line_changes_format(self):
  172. '''
  173. Test file.line changes output formatting.
  174. Issue #41474
  175. '''
  176. ret = self.minion_run('file.line', self.myfile, 'Goodbye',
  177. mode='insert', after='Hello')
  178. self.assertIn('Hello' + os.linesep + '+Goodbye', ret)
  179. def test_file_line_content(self):
  180. self.minion_run('file.line', self.myfile, 'Goodbye',
  181. mode='insert', after='Hello')
  182. with salt.utils.files.fopen(self.myfile, 'r') as fp:
  183. content = fp.read()
  184. self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)
  185. def test_file_line_duplicate_insert_after(self):
  186. """
  187. Test file.line duplicates line.
  188. Issue #50254
  189. """
  190. with salt.utils.files.fopen(self.myfile, 'a') as fp:
  191. fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
  192. self.minion_run('file.line', self.myfile, 'Goodbye',
  193. mode='insert', after='Hello')
  194. with salt.utils.files.fopen(self.myfile, 'r') as fp:
  195. content = fp.read()
  196. self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)
  197. def test_file_line_duplicate_insert_before(self):
  198. """
  199. Test file.line duplicates line.
  200. Issue #50254
  201. """
  202. with salt.utils.files.fopen(self.myfile, 'a') as fp:
  203. fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
  204. self.minion_run('file.line', self.myfile, 'Hello',
  205. mode='insert', before='Goodbye')
  206. with salt.utils.files.fopen(self.myfile, 'r') as fp:
  207. content = fp.read()
  208. self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)
  209. def test_file_line_duplicate_ensure_after(self):
  210. """
  211. Test file.line duplicates line.
  212. Issue #50254
  213. """
  214. with salt.utils.files.fopen(self.myfile, 'a') as fp:
  215. fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
  216. self.minion_run('file.line', self.myfile, 'Goodbye',
  217. mode='ensure', after='Hello')
  218. with salt.utils.files.fopen(self.myfile, 'r') as fp:
  219. content = fp.read()
  220. self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)
  221. def test_file_line_duplicate_ensure_before(self):
  222. """
  223. Test file.line duplicates line.
  224. Issue #50254
  225. """
  226. with salt.utils.files.fopen(self.myfile, 'a') as fp:
  227. fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
  228. self.minion_run('file.line', self.myfile, 'Hello',
  229. mode='ensure', before='Goodbye')
  230. with salt.utils.files.fopen(self.myfile, 'r') as fp:
  231. content = fp.read()
  232. self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)