test_mac_group.py 6.4 KB

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