1
0

test_mac_group.py 6.7 KB

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