test_linux_acl.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import shutil
  6. # Import Salt Testing libs
  7. from tests.support.case import ModuleCase
  8. from tests.support.paths import TMP
  9. from tests.support.mixins import AdaptedConfigurationTestCaseMixin
  10. from tests.support.helpers import skip_if_binaries_missing
  11. # Import salt libs
  12. import salt.utils.files
  13. import salt.utils.user
  14. # Acl package should be installed to test linux_acl module
  15. @skip_if_binaries_missing(['getfacl'])
  16. # Doesn't work. Why?
  17. # @requires_salt_modules('acl')
  18. # @requires_salt_modules('linux_acl')
  19. class LinuxAclModuleTest(ModuleCase, AdaptedConfigurationTestCaseMixin):
  20. '''
  21. Validate the linux_acl module
  22. '''
  23. def setUp(self):
  24. # Blindly copied from tests.integration.modules.file; Refactoring?
  25. self.myfile = os.path.join(TMP, 'myfile')
  26. with salt.utils.files.fopen(self.myfile, 'w+') as fp:
  27. fp.write('Hello\n')
  28. self.mydir = os.path.join(TMP, 'mydir/isawesome')
  29. if not os.path.isdir(self.mydir):
  30. # left behind... Don't fail because of this!
  31. os.makedirs(self.mydir)
  32. self.mysymlink = os.path.join(TMP, 'mysymlink')
  33. if os.path.islink(self.mysymlink):
  34. os.remove(self.mysymlink)
  35. os.symlink(self.myfile, self.mysymlink)
  36. self.mybadsymlink = os.path.join(TMP, 'mybadsymlink')
  37. if os.path.islink(self.mybadsymlink):
  38. os.remove(self.mybadsymlink)
  39. os.symlink('/nonexistentpath', self.mybadsymlink)
  40. super(LinuxAclModuleTest, self).setUp()
  41. def tearDown(self):
  42. if os.path.isfile(self.myfile):
  43. os.remove(self.myfile)
  44. if os.path.islink(self.mysymlink):
  45. os.remove(self.mysymlink)
  46. if os.path.islink(self.mybadsymlink):
  47. os.remove(self.mybadsymlink)
  48. shutil.rmtree(self.mydir, ignore_errors=True)
  49. super(LinuxAclModuleTest, self).tearDown()
  50. def test_version(self):
  51. self.assertRegex(self.run_function('acl.version'), r'\d+\.\d+\.\d+')
  52. def test_getfacl_w_single_file_without_acl(self):
  53. ret = self.run_function('acl.getfacl', arg=[self.myfile])
  54. user = salt.utils.user.get_user()
  55. group = salt.utils.user.get_default_group(user)
  56. self.maxDiff = None
  57. self.assertEqual(
  58. ret,
  59. {self.myfile: {'other': [{'': {'octal': 4, 'permissions': {'read': True, 'write': False, 'execute': False}}}],
  60. 'user': [{user: {'octal': 6, 'permissions': {'read': True, 'write': True, 'execute': False}}}],
  61. 'group': [{group: {'octal': 4, 'permissions': {'read': True, 'write': False, 'execute': False}}}],
  62. 'comment': {'owner': user, 'group': group, 'file': self.myfile}}}
  63. )