test_mac_xattr.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.states.mac_xattr as xattr
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class XAttrTestCase(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {xattr: {}}
  13. def test_exists_not(self):
  14. """
  15. Test adding an attribute when it doesn't exist
  16. """
  17. with patch("os.path.exists") as exists_mock:
  18. expected = {
  19. "changes": {"key": "value"},
  20. "comment": "",
  21. "name": "/path/to/file",
  22. "result": True,
  23. }
  24. exists_mock.return_value = True
  25. list_mock = MagicMock(return_value={"other.id": "value2"})
  26. write_mock = MagicMock()
  27. with patch.dict(
  28. xattr.__salt__, {"xattr.list": list_mock, "xattr.write": write_mock}
  29. ):
  30. out = xattr.exists("/path/to/file", ["key=value"])
  31. list_mock.assert_called_once_with("/path/to/file")
  32. write_mock.assert_called_once_with(
  33. "/path/to/file", "key", "value", False
  34. )
  35. self.assertEqual(out, expected)
  36. def test_exists_change(self):
  37. """
  38. Test changing and attribute value
  39. """
  40. with patch("os.path.exists") as exists_mock:
  41. expected = {
  42. "changes": {"key": "other_value"},
  43. "comment": "",
  44. "name": "/path/to/file",
  45. "result": True,
  46. }
  47. exists_mock.return_value = True
  48. list_mock = MagicMock(return_value={"key": "value"})
  49. write_mock = MagicMock()
  50. with patch.dict(
  51. xattr.__salt__, {"xattr.list": list_mock, "xattr.write": write_mock}
  52. ):
  53. out = xattr.exists("/path/to/file", ["key=other_value"])
  54. list_mock.assert_called_once_with("/path/to/file")
  55. write_mock.assert_called_once_with(
  56. "/path/to/file", "key", "other_value", False
  57. )
  58. self.assertEqual(out, expected)
  59. def test_exists_already(self):
  60. """
  61. Test that having the same value does nothing
  62. """
  63. with patch("os.path.exists") as exists_mock:
  64. expected = {
  65. "changes": {},
  66. "comment": "All values existed correctly.",
  67. "name": "/path/to/file",
  68. "result": True,
  69. }
  70. exists_mock.return_value = True
  71. list_mock = MagicMock(return_value={"key": "value"})
  72. write_mock = MagicMock()
  73. with patch.dict(
  74. xattr.__salt__, {"xattr.list": list_mock, "xattr.write": write_mock}
  75. ):
  76. out = xattr.exists("/path/to/file", ["key=value"])
  77. list_mock.assert_called_once_with("/path/to/file")
  78. assert not write_mock.called
  79. self.assertEqual(out, expected)
  80. def test_delete(self):
  81. """
  82. Test deleting an attribute from a file
  83. """
  84. with patch("os.path.exists") as exists_mock:
  85. expected = {
  86. "changes": {"key": "delete"},
  87. "comment": "",
  88. "name": "/path/to/file",
  89. "result": True,
  90. }
  91. exists_mock.return_value = True
  92. list_mock = MagicMock(return_value={"key": "value2"})
  93. delete_mock = MagicMock()
  94. with patch.dict(
  95. xattr.__salt__, {"xattr.list": list_mock, "xattr.delete": delete_mock}
  96. ):
  97. out = xattr.delete("/path/to/file", ["key"])
  98. list_mock.assert_called_once_with("/path/to/file")
  99. delete_mock.assert_called_once_with("/path/to/file", "key")
  100. self.assertEqual(out, expected)
  101. def test_delete_not(self):
  102. """
  103. Test deleting an attribute that doesn't exist from a file
  104. """
  105. with patch("os.path.exists") as exists_mock:
  106. expected = {
  107. "changes": {},
  108. "comment": "All attributes were already deleted.",
  109. "name": "/path/to/file",
  110. "result": True,
  111. }
  112. exists_mock.return_value = True
  113. list_mock = MagicMock(return_value={"other.key": "value2"})
  114. delete_mock = MagicMock()
  115. with patch.dict(
  116. xattr.__salt__, {"xattr.list": list_mock, "xattr.delete": delete_mock}
  117. ):
  118. out = xattr.delete("/path/to/file", ["key"])
  119. list_mock.assert_called_once_with("/path/to/file")
  120. assert not delete_mock.called
  121. self.assertEqual(out, expected)