1
0

test_linux_acl.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. from tests.support.unit import skipIf
  13. # Acl package should be installed to test linux_acl module
  14. @skip_if_binaries_missing(["getfacl"])
  15. # Doesn't work. Why?
  16. # @requires_salt_modules('acl')
  17. # @requires_salt_modules('linux_acl')
  18. @pytest.mark.windows_whitelisted
  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(RUNTIME_VARS.TMP, "myfile")
  26. with salt.utils.files.fopen(self.myfile, "w+") as fp:
  27. fp.write("Hello\n")
  28. self.mydir = os.path.join(RUNTIME_VARS.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(RUNTIME_VARS.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(RUNTIME_VARS.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. @skipIf(salt.utils.platform.is_freebsd(), "Skip on FreeBSD")
  51. def test_version(self):
  52. self.assertRegex(self.run_function("acl.version"), r"\d+\.\d+\.\d+")
  53. @skipIf(salt.utils.platform.is_freebsd(), "Skip on FreeBSD")
  54. def test_getfacl_w_single_file_without_acl(self):
  55. ret = self.run_function("acl.getfacl", arg=[self.myfile])
  56. user = salt.utils.user.get_user()
  57. group = salt.utils.user.get_default_group(user)
  58. self.maxDiff = None
  59. self.assertEqual(
  60. ret,
  61. {
  62. self.myfile: {
  63. "other": [
  64. {
  65. "": {
  66. "octal": 4,
  67. "permissions": {
  68. "read": True,
  69. "write": False,
  70. "execute": False,
  71. },
  72. }
  73. }
  74. ],
  75. "user": [
  76. {
  77. user: {
  78. "octal": 6,
  79. "permissions": {
  80. "read": True,
  81. "write": True,
  82. "execute": False,
  83. },
  84. }
  85. }
  86. ],
  87. "group": [
  88. {
  89. group: {
  90. "octal": 4,
  91. "permissions": {
  92. "read": True,
  93. "write": False,
  94. "execute": False,
  95. },
  96. }
  97. }
  98. ],
  99. "comment": {"owner": user, "group": group, "file": self.myfile},
  100. }
  101. },
  102. )