test_mac_group.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. HAS_GRP = True
  8. try:
  9. import grp
  10. except ImportError:
  11. HAS_GRP = False
  12. # Import Salt Testing Libs
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.unit import TestCase, skipIf
  15. from tests.support.mock import MagicMock, patch
  16. # Import Salt Libs
  17. import salt.modules.mac_group as mac_group
  18. from salt.exceptions import SaltInvocationError, CommandExecutionError
  19. @skipIf(not HAS_GRP, "Missing required library 'grp'")
  20. class MacGroupTestCase(TestCase, LoaderModuleMockMixin):
  21. '''
  22. TestCase for the salt.modules.mac_group module
  23. '''
  24. def setup_loader_modules(self):
  25. return {mac_group: {}}
  26. # 'add' function tests: 6
  27. def test_add_group_exists(self):
  28. '''
  29. Tests if the group already exists or not
  30. '''
  31. mock_group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
  32. with patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group)):
  33. self.assertRaises(CommandExecutionError, mac_group.add, 'test')
  34. def test_add_whitespace(self):
  35. '''
  36. Tests if there is whitespace in the group name
  37. '''
  38. with patch('salt.modules.mac_group.info', MagicMock(return_value={})):
  39. self.assertRaises(SaltInvocationError, mac_group.add, 'white space')
  40. def test_add_underscore(self):
  41. '''
  42. Tests if the group name starts with an underscore or not
  43. '''
  44. with patch('salt.modules.mac_group.info', MagicMock(return_value={})):
  45. self.assertRaises(SaltInvocationError, mac_group.add, '_Test')
  46. def test_add_gid_int(self):
  47. '''
  48. Tests if the gid is an int or not
  49. '''
  50. with patch('salt.modules.mac_group.info', MagicMock(return_value={})):
  51. self.assertRaises(SaltInvocationError, mac_group.add, 'foo', 'foo')
  52. def test_add_gid_exists(self):
  53. '''
  54. Tests if the gid is already in use or not
  55. '''
  56. with patch('salt.modules.mac_group.info', MagicMock(return_value={})), \
  57. patch('salt.modules.mac_group._list_gids', MagicMock(return_value=['3456'])):
  58. self.assertRaises(CommandExecutionError, mac_group.add, 'foo', 3456)
  59. def test_add(self):
  60. '''
  61. Tests if specified group was added
  62. '''
  63. mock_ret = MagicMock(return_value=0)
  64. with patch.dict(mac_group.__salt__, {'cmd.retcode': mock_ret}), \
  65. patch('salt.modules.mac_group.info', MagicMock(return_value={})), \
  66. patch('salt.modules.mac_group._list_gids', MagicMock(return_value=[])):
  67. self.assertTrue(mac_group.add('test', 500))
  68. # 'delete' function tests: 4
  69. def test_delete_whitespace(self):
  70. '''
  71. Tests if there is whitespace in the group name
  72. '''
  73. self.assertRaises(SaltInvocationError, mac_group.delete, 'white space')
  74. def test_delete_underscore(self):
  75. '''
  76. Tests if the group name starts with an underscore or not
  77. '''
  78. self.assertRaises(SaltInvocationError, mac_group.delete, '_Test')
  79. def test_delete_group_exists(self):
  80. '''
  81. Tests if the group to be deleted exists or not
  82. '''
  83. with patch('salt.modules.mac_group.info', MagicMock(return_value={})):
  84. self.assertTrue(mac_group.delete('test'))
  85. def test_delete(self):
  86. '''
  87. Tests if the specified group was deleted
  88. '''
  89. mock_ret = MagicMock(return_value=0)
  90. mock_group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
  91. with patch.dict(mac_group.__salt__, {'cmd.retcode': mock_ret}), \
  92. patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group)):
  93. self.assertTrue(mac_group.delete('test'))
  94. # 'info' function tests: 2
  95. def test_info_whitespace(self):
  96. '''
  97. Tests if there is whitespace in the group name
  98. '''
  99. self.assertRaises(SaltInvocationError, mac_group.info, 'white space')
  100. def test_info(self):
  101. '''
  102. Tests the return of group information
  103. '''
  104. mock_getgrall = [grp.struct_group(('foo', '*', 20, ['test']))]
  105. with patch('grp.getgrall', MagicMock(return_value=mock_getgrall)):
  106. ret = {'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}
  107. self.assertEqual(mac_group.info('foo'), ret)
  108. # '_format_info' function tests: 1
  109. def test_format_info(self):
  110. '''
  111. Tests the formatting of returned group information
  112. '''
  113. data = grp.struct_group(('wheel', '*', 0, ['root']))
  114. ret = {'passwd': '*', 'gid': 0, 'name': 'wheel', 'members': ['root']}
  115. self.assertEqual(mac_group._format_info(data), ret)
  116. # 'getent' function tests: 1
  117. def test_getent(self):
  118. '''
  119. Tests the return of information on all groups
  120. '''
  121. mock_getgrall = [grp.struct_group(('foo', '*', 20, ['test']))]
  122. with patch('grp.getgrall', MagicMock(return_value=mock_getgrall)):
  123. ret = [{'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}]
  124. self.assertEqual(mac_group.getent(), ret)
  125. # 'chgid' function tests: 4
  126. def test_chgid_gid_int(self):
  127. '''
  128. Tests if gid is an integer or not
  129. '''
  130. self.assertRaises(SaltInvocationError, mac_group.chgid, 'foo', 'foo')
  131. def test_chgid_group_exists(self):
  132. '''
  133. Tests if the group id exists or not
  134. '''
  135. mock_pre_gid = MagicMock(return_value='')
  136. with patch.dict(mac_group.__salt__,
  137. {'file.group_to_gid': mock_pre_gid}), \
  138. patch('salt.modules.mac_group.info', MagicMock(return_value={})):
  139. self.assertRaises(CommandExecutionError,
  140. mac_group.chgid, 'foo', 4376)
  141. def test_chgid_gid_same(self):
  142. '''
  143. Tests if the group id is the same as argument
  144. '''
  145. mock_group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
  146. mock_pre_gid = MagicMock(return_value=0)
  147. with patch.dict(mac_group.__salt__,
  148. {'file.group_to_gid': mock_pre_gid}), \
  149. patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group)):
  150. self.assertTrue(mac_group.chgid('test', 0))
  151. def test_chgid(self):
  152. '''
  153. Tests the gid for a named group was changed
  154. '''
  155. mock_group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
  156. mock_pre_gid = MagicMock(return_value=0)
  157. mock_ret = MagicMock(return_value=0)
  158. with patch.dict(mac_group.__salt__,
  159. {'file.group_to_gid': mock_pre_gid}), \
  160. patch.dict(mac_group.__salt__, {'cmd.retcode': mock_ret}), \
  161. patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group)):
  162. self.assertTrue(mac_group.chgid('test', 500))