test_groupadd.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. try:
  8. import grp
  9. except ImportError:
  10. pass
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.unit import TestCase, skipIf
  14. from tests.support.mock import MagicMock, patch
  15. # Import Salt Libs
  16. import salt.modules.groupadd as groupadd
  17. import salt.utils.platform
  18. @skipIf(salt.utils.platform.is_windows(), "Module not available on Windows")
  19. class GroupAddTestCase(TestCase, LoaderModuleMockMixin):
  20. '''
  21. TestCase for salt.modules.groupadd
  22. '''
  23. def setup_loader_modules(self):
  24. return {groupadd: {}}
  25. # 'add' function tests: 1
  26. def test_add(self):
  27. '''
  28. Tests if specified group was added
  29. '''
  30. mock = MagicMock(return_value={'retcode': 0})
  31. with patch.dict(groupadd.__salt__, {'cmd.run_all': mock}):
  32. self.assertTrue(groupadd.add('test', 100))
  33. with patch.dict(groupadd.__grains__, {'kernel': 'Linux'}):
  34. with patch.dict(groupadd.__salt__, {'cmd.run_all': mock}):
  35. self.assertTrue(groupadd.add('test', 100, True))
  36. # 'info' function tests: 1
  37. def test_info(self):
  38. '''
  39. Tests the return of group information
  40. '''
  41. getgrnam = grp.struct_group(('foo', '*', 20, ['test']))
  42. with patch('grp.getgrnam', MagicMock(return_value=getgrnam)):
  43. ret = {'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}
  44. self.assertEqual(groupadd.info('foo'), ret)
  45. # '_format_info' function tests: 1
  46. def test_format_info(self):
  47. '''
  48. Tests the formatting of returned group information
  49. '''
  50. group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
  51. with patch('salt.modules.groupadd._format_info', MagicMock(return_value=group)):
  52. data = grp.struct_group(('wheel', '*', 0, ['root']))
  53. ret = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
  54. self.assertDictEqual(groupadd._format_info(data), ret)
  55. # 'getent' function tests: 1
  56. def test_getent(self):
  57. '''
  58. Tests the return of information on all groups
  59. '''
  60. getgrnam = grp.struct_group(('foo', '*', 20, ['test']))
  61. with patch('grp.getgrall', MagicMock(return_value=[getgrnam])):
  62. ret = [{'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}]
  63. self.assertEqual(groupadd.getent(), ret)
  64. # 'chgid' function tests: 2
  65. def test_chgid_gid_same(self):
  66. '''
  67. Tests if the group id is the same as argument
  68. '''
  69. mock_pre_gid = MagicMock(return_value=10)
  70. with patch.dict(groupadd.__salt__,
  71. {'file.group_to_gid': mock_pre_gid}):
  72. self.assertTrue(groupadd.chgid('test', 10))
  73. def test_chgid(self):
  74. '''
  75. Tests the gid for a named group was changed
  76. '''
  77. mock_pre_gid = MagicMock(return_value=0)
  78. mock_cmdrun = MagicMock(return_value=0)
  79. with patch.dict(groupadd.__salt__,
  80. {'file.group_to_gid': mock_pre_gid}):
  81. with patch.dict(groupadd.__salt__, {'cmd.run': mock_cmdrun}):
  82. self.assertFalse(groupadd.chgid('test', 500))
  83. # 'delete' function tests: 1
  84. def test_delete(self):
  85. '''
  86. Tests if the specified group was deleted
  87. '''
  88. mock_ret = MagicMock(return_value={'retcode': 0})
  89. with patch.dict(groupadd.__salt__, {'cmd.run_all': mock_ret}):
  90. self.assertTrue(groupadd.delete('test'))
  91. # 'adduser' function tests: 1
  92. def test_adduser(self):
  93. '''
  94. Tests if specified user gets added in the group.
  95. '''
  96. os_version_list = [
  97. {'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'},
  98. 'cmd': ['gpasswd', '-a', 'root', 'test']},
  99. {'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osmajorrelease': '11'},
  100. 'cmd': ['usermod', '-A', 'test', 'root']},
  101. {'grains': {'kernel': 'Linux'},
  102. 'cmd': ['gpasswd', '--add', 'root', 'test']},
  103. {'grains': {'kernel': 'OTHERKERNEL'},
  104. 'cmd': ['usermod', '-G', 'test', 'root']},
  105. ]
  106. for os_version in os_version_list:
  107. mock = MagicMock(return_value={'retcode': 0})
  108. with patch.dict(groupadd.__grains__, os_version['grains']):
  109. with patch.dict(groupadd.__salt__, {'cmd.retcode': mock}):
  110. self.assertFalse(groupadd.adduser('test', 'root'))
  111. groupadd.__salt__['cmd.retcode'].assert_called_once_with(os_version['cmd'], python_shell=False)
  112. # 'deluser' function tests: 1
  113. def test_deluser(self):
  114. '''
  115. Tests if specified user gets deleted from the group.
  116. '''
  117. os_version_list = [
  118. {'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'},
  119. 'cmd': ['gpasswd', '-d', 'root', 'test']},
  120. {'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osmajorrelease': '11'},
  121. 'cmd': ['usermod', '-R', 'test', 'root']},
  122. {'grains': {'kernel': 'Linux'},
  123. 'cmd': ['gpasswd', '--del', 'root', 'test']},
  124. {'grains': {'kernel': 'OpenBSD'},
  125. 'cmd': ['usermod', '-S', 'foo', 'root']},
  126. ]
  127. for os_version in os_version_list:
  128. mock_retcode = MagicMock(return_value=0)
  129. mock_stdout = MagicMock(return_value='test foo')
  130. mock_info = MagicMock(return_value={'passwd': '*',
  131. 'gid': 0,
  132. 'name': 'test',
  133. 'members': ['root']})
  134. with patch.dict(groupadd.__grains__, os_version['grains']):
  135. with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_retcode,
  136. 'group.info': mock_info,
  137. 'cmd.run_stdout': mock_stdout}):
  138. self.assertTrue(groupadd.deluser('test', 'root'))
  139. groupadd.__salt__['cmd.retcode'].assert_called_once_with(os_version['cmd'], python_shell=False)
  140. # 'deluser' function tests: 1
  141. def test_members(self):
  142. '''
  143. Tests if members of the group, get replaced with a provided list.
  144. '''
  145. os_version_list = [
  146. {'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'},
  147. 'cmd': ['gpasswd', '-M', 'foo', 'test']},
  148. {'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osmajorrelease': '11'},
  149. 'cmd': ['groupmod', '-A', 'foo', 'test']},
  150. {'grains': {'kernel': 'Linux'},
  151. 'cmd': ['gpasswd', '--members', 'foo', 'test']},
  152. {'grains': {'kernel': 'OpenBSD'},
  153. 'cmd': ['usermod', '-G', 'test', 'foo']},
  154. ]
  155. for os_version in os_version_list:
  156. mock_ret = MagicMock(return_value={'retcode': 0})
  157. mock_stdout = MagicMock(return_value={'cmd.run_stdout': 1})
  158. mock_info = MagicMock(return_value={'passwd': '*',
  159. 'gid': 0,
  160. 'name': 'test',
  161. 'members': ['root']})
  162. mock = MagicMock(return_value=True)
  163. with patch.dict(groupadd.__grains__, os_version['grains']):
  164. with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_ret,
  165. 'group.info': mock_info,
  166. 'cmd.run_stdout': mock_stdout,
  167. 'cmd.run': mock}):
  168. self.assertFalse(groupadd.members('test', 'foo'))
  169. groupadd.__salt__['cmd.retcode'].assert_called_once_with(os_version['cmd'], python_shell=False)