test_linux_acl.py 3.9 KB

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