test_mac_xattr.py 5.0 KB

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