test_linux_acl.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import os
  4. import shutil
  5. import pytest
  6. import salt.utils.files
  7. import salt.utils.user
  8. from tests.support.case import ModuleCase
  9. from tests.support.mixins import AdaptedConfigurationTestCaseMixin
  10. from tests.support.runtests import RUNTIME_VARS
  11. # Acl package should be installed to test linux_acl module
  12. @pytest.mark.skip_if_binaries_missing("getfacl")
  13. # Doesn't work. Why?
  14. # @pytest.mark.requires_salt_modules('acl')
  15. # @pytest.mark.requires_salt_modules('linux_acl')
  16. @pytest.mark.windows_whitelisted
  17. class LinuxAclModuleTest(ModuleCase, AdaptedConfigurationTestCaseMixin):
  18. """
  19. Validate the linux_acl module
  20. """
  21. def setUp(self):
  22. # Blindly copied from tests.integration.modules.file; Refactoring?
  23. self.myfile = os.path.join(RUNTIME_VARS.TMP, "myfile")
  24. with salt.utils.files.fopen(self.myfile, "w+") as fp:
  25. fp.write("Hello\n")
  26. self.mydir = os.path.join(RUNTIME_VARS.TMP, "mydir/isawesome")
  27. if not os.path.isdir(self.mydir):
  28. # left behind... Don't fail because of this!
  29. os.makedirs(self.mydir)
  30. self.mysymlink = os.path.join(RUNTIME_VARS.TMP, "mysymlink")
  31. if os.path.islink(self.mysymlink):
  32. os.remove(self.mysymlink)
  33. os.symlink(self.myfile, self.mysymlink)
  34. self.mybadsymlink = os.path.join(RUNTIME_VARS.TMP, "mybadsymlink")
  35. if os.path.islink(self.mybadsymlink):
  36. os.remove(self.mybadsymlink)
  37. os.symlink("/nonexistentpath", self.mybadsymlink)
  38. super(LinuxAclModuleTest, self).setUp()
  39. def tearDown(self):
  40. if os.path.isfile(self.myfile):
  41. os.remove(self.myfile)
  42. if os.path.islink(self.mysymlink):
  43. os.remove(self.mysymlink)
  44. if os.path.islink(self.mybadsymlink):
  45. os.remove(self.mybadsymlink)
  46. shutil.rmtree(self.mydir, ignore_errors=True)
  47. super(LinuxAclModuleTest, self).tearDown()
  48. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  49. def test_version(self):
  50. self.assertRegex(self.run_function("acl.version"), r"\d+\.\d+\.\d+")
  51. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  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. {
  60. self.myfile: {
  61. "other": [
  62. {
  63. "": {
  64. "octal": 4,
  65. "permissions": {
  66. "read": True,
  67. "write": False,
  68. "execute": False,
  69. },
  70. }
  71. }
  72. ],
  73. "user": [
  74. {
  75. user: {
  76. "octal": 6,
  77. "permissions": {
  78. "read": True,
  79. "write": True,
  80. "execute": False,
  81. },
  82. }
  83. }
  84. ],
  85. "group": [
  86. {
  87. group: {
  88. "octal": 4,
  89. "permissions": {
  90. "read": True,
  91. "write": False,
  92. "execute": False,
  93. },
  94. }
  95. }
  96. ],
  97. "comment": {"owner": user, "group": group, "file": self.myfile},
  98. }
  99. },
  100. )