1
0

test_mac_xattr.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: utf-8 -*-
  2. '''
  3. integration tests for mac_xattr
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. # Import Salt Testing libs
  9. from tests.support.runtests import RUNTIME_VARS
  10. from tests.support.case import ModuleCase
  11. # Import Salt libs
  12. import salt.utils.path
  13. import salt.utils.platform
  14. class MacXattrModuleTest(ModuleCase):
  15. '''
  16. Validate the mac_xattr module
  17. '''
  18. @classmethod
  19. def setUpClass(cls):
  20. cls.test_file = os.path.join(RUNTIME_VARS.TMP, 'xattr_test_file.txt')
  21. cls.no_file = os.path.join(RUNTIME_VARS.TMP, 'xattr_no_file.txt')
  22. def setUp(self):
  23. '''
  24. Create test file for testing extended attributes
  25. '''
  26. if not salt.utils.platform.is_darwin():
  27. self.skipTest('Test only available on macOS')
  28. if not salt.utils.path.which('xattr'):
  29. self.skipTest('Test requires xattr binary')
  30. self.run_function('file.touch', [self.test_file])
  31. def tearDown(self):
  32. '''
  33. Clean up test file
  34. '''
  35. if os.path.exists(self.test_file):
  36. os.remove(self.test_file)
  37. def test_list_no_xattr(self):
  38. '''
  39. Make sure there are no attributes
  40. '''
  41. # Clear existing attributes
  42. self.assertTrue(self.run_function('xattr.clear', [self.test_file]))
  43. # Test no attributes
  44. self.assertEqual(self.run_function('xattr.list', [self.test_file]), {})
  45. # Test file not found
  46. self.assertEqual(self.run_function('xattr.list', [self.no_file]),
  47. 'ERROR: File not found: {0}'.format(self.no_file))
  48. def test_write(self):
  49. '''
  50. Write an attribute
  51. '''
  52. # Clear existing attributes
  53. self.assertTrue(self.run_function('xattr.clear', [self.test_file]))
  54. # Write some attributes
  55. self.assertTrue(
  56. self.run_function('xattr.write',
  57. [self.test_file, 'spongebob', 'squarepants']))
  58. self.assertTrue(
  59. self.run_function('xattr.write',
  60. [self.test_file, 'squidward', 'plankton']))
  61. self.assertTrue(
  62. self.run_function('xattr.write',
  63. [self.test_file, 'crabby', 'patty']))
  64. # Test that they were actually added
  65. self.assertEqual(
  66. self.run_function('xattr.list', [self.test_file]),
  67. {'spongebob': 'squarepants',
  68. 'squidward': 'plankton',
  69. 'crabby': 'patty'})
  70. # Test file not found
  71. self.assertEqual(
  72. self.run_function('xattr.write', [self.no_file, 'patrick', 'jellyfish']),
  73. 'ERROR: File not found: {0}'.format(self.no_file))
  74. def test_read(self):
  75. '''
  76. Test xattr.read
  77. '''
  78. # Clear existing attributes
  79. self.assertTrue(self.run_function('xattr.clear', [self.test_file]))
  80. # Write an attribute
  81. self.assertTrue(
  82. self.run_function('xattr.write',
  83. [self.test_file, 'spongebob', 'squarepants']))
  84. # Read the attribute
  85. self.assertEqual(
  86. self.run_function('xattr.read', [self.test_file, 'spongebob']),
  87. 'squarepants')
  88. # Test file not found
  89. self.assertEqual(
  90. self.run_function('xattr.read', [self.no_file, 'spongebob']),
  91. 'ERROR: File not found: {0}'.format(self.no_file))
  92. # Test attribute not found
  93. self.assertEqual(
  94. self.run_function('xattr.read', [self.test_file, 'patrick']),
  95. 'ERROR: Attribute not found: patrick')
  96. def test_delete(self):
  97. '''
  98. Test xattr.delete
  99. '''
  100. # Clear existing attributes
  101. self.assertTrue(self.run_function('xattr.clear', [self.test_file]))
  102. # Write some attributes
  103. self.assertTrue(
  104. self.run_function('xattr.write',
  105. [self.test_file, 'spongebob', 'squarepants']))
  106. self.assertTrue(
  107. self.run_function('xattr.write',
  108. [self.test_file, 'squidward', 'plankton']))
  109. self.assertTrue(
  110. self.run_function('xattr.write',
  111. [self.test_file, 'crabby', 'patty']))
  112. # Delete an attribute
  113. self.assertTrue(
  114. self.run_function('xattr.delete', [self.test_file, 'squidward']))
  115. # Make sure it was actually deleted
  116. self.assertEqual(
  117. self.run_function('xattr.list', [self.test_file]),
  118. {'spongebob': 'squarepants', 'crabby': 'patty'})
  119. # Test file not found
  120. self.assertEqual(
  121. self.run_function('xattr.delete', [self.no_file, 'spongebob']),
  122. 'ERROR: File not found: {0}'.format(self.no_file))
  123. # Test attribute not found
  124. self.assertEqual(
  125. self.run_function('xattr.delete', [self.test_file, 'patrick']),
  126. 'ERROR: Attribute not found: patrick')
  127. def test_clear(self):
  128. '''
  129. Test xattr.clear
  130. '''
  131. # Clear existing attributes
  132. self.assertTrue(self.run_function('xattr.clear', [self.test_file]))
  133. # Write some attributes
  134. self.assertTrue(
  135. self.run_function('xattr.write',
  136. [self.test_file, 'spongebob', 'squarepants']))
  137. self.assertTrue(
  138. self.run_function('xattr.write',
  139. [self.test_file, 'squidward', 'plankton']))
  140. self.assertTrue(
  141. self.run_function('xattr.write',
  142. [self.test_file, 'crabby', 'patty']))
  143. # Test Clear
  144. self.assertTrue(self.run_function('xattr.clear', [self.test_file]))
  145. # Test file not found
  146. self.assertEqual(self.run_function('xattr.clear', [self.no_file]),
  147. 'ERROR: File not found: {0}'.format(self.no_file))