test_mac_xattr.py 5.6 KB

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