test_mac_group.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import pytest
  7. from salt.exceptions import CommandExecutionError
  8. from salt.ext import six
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import random_string
  11. # Create group name strings for tests
  12. ADD_GROUP = random_string("RS-", lowercase=False)
  13. DEL_GROUP = random_string("RS-", lowercase=False)
  14. CHANGE_GROUP = random_string("RS-", lowercase=False)
  15. ADD_USER = random_string("RS-", lowercase=False)
  16. REP_USER_GROUP = random_string("RS-", lowercase=False)
  17. @pytest.mark.destructive_test
  18. @pytest.mark.skip_if_not_root
  19. @pytest.mark.skip_unless_on_darwin
  20. class MacGroupModuleTest(ModuleCase):
  21. """
  22. Integration tests for the mac_group module
  23. """
  24. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  25. def test_mac_group_add(self):
  26. """
  27. Tests the add group function
  28. """
  29. try:
  30. self.run_function("group.add", [ADD_GROUP, 3456])
  31. group_info = self.run_function("group.info", [ADD_GROUP])
  32. self.assertEqual(group_info["name"], ADD_GROUP)
  33. except CommandExecutionError:
  34. self.run_function("group.delete", [ADD_GROUP])
  35. raise
  36. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  37. def test_mac_group_delete(self):
  38. """
  39. Tests the delete group function
  40. """
  41. # Create a group to delete - If unsuccessful, skip the test
  42. if self.run_function("group.add", [DEL_GROUP, 4567]) is not True:
  43. self.run_function("group.delete", [DEL_GROUP])
  44. self.skipTest("Failed to create a group to delete")
  45. # Now try to delete the added group
  46. ret = self.run_function("group.delete", [DEL_GROUP])
  47. self.assertTrue(ret)
  48. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  49. def test_mac_group_chgid(self):
  50. """
  51. Tests changing the group id
  52. """
  53. # Create a group to delete - If unsuccessful, skip the test
  54. if self.run_function("group.add", [CHANGE_GROUP, 5678]) is not True:
  55. self.run_function("group.delete", [CHANGE_GROUP])
  56. self.skipTest("Failed to create a group to manipulate")
  57. try:
  58. self.run_function("group.chgid", [CHANGE_GROUP, 6789])
  59. group_info = self.run_function("group.info", [CHANGE_GROUP])
  60. self.assertEqual(group_info["gid"], 6789)
  61. except AssertionError:
  62. self.run_function("group.delete", [CHANGE_GROUP])
  63. raise
  64. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  65. def test_mac_adduser(self):
  66. """
  67. Tests adding user to the group
  68. """
  69. # Create a group to use for test - If unsuccessful, skip the test
  70. if self.run_function("group.add", [ADD_GROUP, 5678]) is not True:
  71. self.run_function("group.delete", [ADD_GROUP])
  72. self.skipTest("Failed to create a group to manipulate")
  73. try:
  74. self.run_function("group.adduser", [ADD_GROUP, ADD_USER])
  75. group_info = self.run_function("group.info", [ADD_GROUP])
  76. self.assertEqual(ADD_USER, "".join(group_info["members"]))
  77. except AssertionError:
  78. self.run_function("group.delete", [ADD_GROUP])
  79. raise
  80. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  81. def test_mac_deluser(self):
  82. """
  83. Test deleting user from a group
  84. """
  85. # Create a group to use for test - If unsuccessful, skip the test
  86. if (
  87. self.run_function("group.add", [ADD_GROUP, 5678])
  88. and self.run_function("group.adduser", [ADD_GROUP, ADD_USER]) is not True
  89. ):
  90. self.run_function("group.delete", [ADD_GROUP])
  91. self.skipTest("Failed to create a group to manipulate")
  92. delusr = self.run_function("group.deluser", [ADD_GROUP, ADD_USER])
  93. self.assertTrue(delusr)
  94. group_info = self.run_function("group.info", [ADD_GROUP])
  95. self.assertNotIn(ADD_USER, "".join(group_info["members"]))
  96. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  97. def test_mac_members(self):
  98. """
  99. Test replacing members of a group
  100. """
  101. if (
  102. self.run_function("group.add", [ADD_GROUP, 5678])
  103. and self.run_function("group.adduser", [ADD_GROUP, ADD_USER]) is not True
  104. ):
  105. self.run_function("group.delete", [ADD_GROUP])
  106. self.skipTest(
  107. "Failed to create the {0} group or add user {1} to group "
  108. "to manipulate".format(ADD_GROUP, ADD_USER)
  109. )
  110. rep_group_mem = self.run_function("group.members", [ADD_GROUP, REP_USER_GROUP])
  111. self.assertTrue(rep_group_mem)
  112. # ensure new user is added to group and previous user is removed
  113. group_info = self.run_function("group.info", [ADD_GROUP])
  114. self.assertIn(REP_USER_GROUP, six.text_type(group_info["members"]))
  115. self.assertNotIn(ADD_USER, six.text_type(group_info["members"]))
  116. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  117. def test_mac_getent(self):
  118. """
  119. Test returning info on all groups
  120. """
  121. if (
  122. self.run_function("group.add", [ADD_GROUP, 5678])
  123. and self.run_function("group.adduser", [ADD_GROUP, ADD_USER]) is not True
  124. ):
  125. self.run_function("group.delete", [ADD_GROUP])
  126. self.skipTest(
  127. "Failed to create the {0} group or add user {1} to group "
  128. "to manipulate".format(ADD_GROUP, ADD_USER)
  129. )
  130. getinfo = self.run_function("group.getent")
  131. self.assertTrue(getinfo)
  132. self.assertIn(ADD_GROUP, six.text_type(getinfo))
  133. self.assertIn(ADD_USER, six.text_type(getinfo))
  134. def tearDown(self):
  135. """
  136. Clean up after tests
  137. """
  138. # Delete ADD_GROUP
  139. add_info = self.run_function("group.info", [ADD_GROUP])
  140. if add_info:
  141. self.run_function("group.delete", [ADD_GROUP])
  142. # Delete DEL_GROUP if something failed
  143. del_info = self.run_function("group.info", [DEL_GROUP])
  144. if del_info:
  145. self.run_function("group.delete", [DEL_GROUP])
  146. # Delete CHANGE_GROUP
  147. change_info = self.run_function("group.info", [CHANGE_GROUP])
  148. if change_info:
  149. self.run_function("group.delete", [CHANGE_GROUP])