test_mac_xattr.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing Libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase
  7. from tests.support.mock import MagicMock, patch
  8. # Import Salt Libs
  9. from salt.exceptions import CommandExecutionError
  10. import salt.modules.mac_xattr as xattr
  11. import salt.utils.mac_utils
  12. class XAttrTestCase(TestCase, LoaderModuleMockMixin):
  13. def setup_loader_modules(self):
  14. return {xattr: {}}
  15. def test_list(self):
  16. '''
  17. Test xattr.list
  18. '''
  19. expected = {'spongebob': 'squarepants',
  20. 'squidward': 'patrick'}
  21. with patch.object(xattr, 'read', MagicMock(side_effect=['squarepants',
  22. 'patrick'])), \
  23. patch('salt.utils.mac_utils.execute_return_result',
  24. MagicMock(return_value='spongebob\nsquidward')):
  25. self.assertEqual(xattr.list_('path/to/file'), expected)
  26. def test_list_missing(self):
  27. '''
  28. Test listing attributes of a missing file
  29. '''
  30. with patch('salt.utils.mac_utils.execute_return_result',
  31. MagicMock(side_effect=CommandExecutionError('No such file'))):
  32. self.assertRaises(CommandExecutionError, xattr.list_, '/path/to/file')
  33. def test_read(self):
  34. '''
  35. Test reading a specific attribute from a file
  36. '''
  37. with patch('salt.utils.mac_utils.execute_return_result',
  38. MagicMock(return_value='expected results')):
  39. self.assertEqual(xattr.read('/path/to/file', 'com.attr'),
  40. 'expected results')
  41. def test_read_hex(self):
  42. '''
  43. Test reading a specific attribute from a file
  44. '''
  45. with patch.object(salt.utils.mac_utils, 'execute_return_result',
  46. MagicMock(return_value='expected results')) as mock:
  47. self.assertEqual(
  48. xattr.read('/path/to/file', 'com.attr', **{'hex': True}),
  49. 'expected results'
  50. )
  51. mock.assert_called_once_with(
  52. ['xattr', '-p', '-x', 'com.attr', '/path/to/file'])
  53. def test_read_missing(self):
  54. '''
  55. Test reading a specific attribute from a file
  56. '''
  57. with patch('salt.utils.mac_utils.execute_return_result',
  58. MagicMock(side_effect=CommandExecutionError('No such file'))):
  59. self.assertRaises(CommandExecutionError,
  60. xattr.read,
  61. '/path/to/file',
  62. 'attribute')
  63. def test_write(self):
  64. '''
  65. Test writing a specific attribute to a file
  66. '''
  67. mock_cmd = MagicMock(return_value='squarepants')
  68. with patch.object(xattr, 'read', mock_cmd), \
  69. patch('salt.utils.mac_utils.execute_return_success',
  70. MagicMock(return_value=True)):
  71. self.assertTrue(xattr.write('/path/to/file',
  72. 'spongebob',
  73. 'squarepants'))
  74. def test_write_missing(self):
  75. '''
  76. Test writing a specific attribute to a file
  77. '''
  78. with patch('salt.utils.mac_utils.execute_return_success',
  79. MagicMock(side_effect=CommandExecutionError('No such file'))):
  80. self.assertRaises(CommandExecutionError,
  81. xattr.write,
  82. '/path/to/file',
  83. 'attribute',
  84. 'value')
  85. def test_delete(self):
  86. '''
  87. Test deleting a specific attribute from a file
  88. '''
  89. mock_cmd = MagicMock(return_value={'spongebob': 'squarepants'})
  90. with patch.object(xattr, 'list_', mock_cmd), \
  91. patch('salt.utils.mac_utils.execute_return_success',
  92. MagicMock(return_value=True)):
  93. self.assertTrue(xattr.delete('/path/to/file', 'attribute'))
  94. def test_delete_missing(self):
  95. '''
  96. Test deleting a specific attribute from a file
  97. '''
  98. with patch('salt.utils.mac_utils.execute_return_success',
  99. MagicMock(side_effect=CommandExecutionError('No such file'))):
  100. self.assertRaises(CommandExecutionError,
  101. xattr.delete,
  102. '/path/to/file',
  103. 'attribute')
  104. def test_clear(self):
  105. '''
  106. Test clearing all attributes on a file
  107. '''
  108. mock_cmd = MagicMock(return_value={})
  109. with patch.object(xattr, 'list_', mock_cmd), \
  110. patch('salt.utils.mac_utils.execute_return_success',
  111. MagicMock(return_value=True)):
  112. self.assertTrue(xattr.clear('/path/to/file'))
  113. def test_clear_missing(self):
  114. '''
  115. Test clearing all attributes on a file
  116. '''
  117. with patch('salt.utils.mac_utils.execute_return_success',
  118. MagicMock(side_effect=CommandExecutionError('No such file'))):
  119. self.assertRaises(CommandExecutionError, xattr.clear, '/path/to/file')