1
0

test_file.py 11 KB

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