1
0

test_file.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. import os
  5. import sys
  6. # Posix only
  7. try:
  8. import grp
  9. import pwd
  10. except ImportError:
  11. pass
  12. # Import 3rd-party libs
  13. import pytest
  14. # Import Salt libs
  15. import salt.utils.files
  16. import salt.utils.platform
  17. import salt.utils.stringutils
  18. # Import testing libs
  19. from tests.support.unit import skipIf
  20. from tests.support.runtime import RUNTIME_VARS
  21. @pytest.fixture
  22. def myfile():
  23. _myfile = os.path.join(RUNTIME_VARS.TMP, 'myfile')
  24. with salt.utils.files.fopen(_myfile, 'w+') as fp:
  25. fp.write(salt.utils.stringutils.to_str('Hello' + os.linesep))
  26. yield _myfile
  27. if os.path.exists(_myfile):
  28. os.unlink(_myfile)
  29. @skipIf(salt.utils.platform.is_windows(), reason='No chgrp on Windows')
  30. def test_chown(running_username, myfile, modules):
  31. if sys.platform == 'darwin':
  32. group = 'staff'
  33. elif sys.platform.startswith(('linux', 'freebsd', 'openbsd')):
  34. group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
  35. assert modules.file.chown(myfile, running_username, group) is None
  36. fstat = os.stat(myfile)
  37. assert fstat.st_uid == os.getuid()
  38. assert fstat.st_gid == grp.getgrnam(group).gr_gid
  39. def test_remove_file(modules, myfile):
  40. ret = modules.file.remove(myfile)
  41. assert ret is True