2014-12-19 06:33:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
2015-01-08 02:36:39 +00:00
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-12-19 06:33:45 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
|
2014-12-19 06:33:45 +00:00
|
|
|
|
|
|
|
# Import Salt Libs
|
|
|
|
from salt.modules import groupadd
|
|
|
|
|
|
|
|
# Import python Libs
|
|
|
|
import grp
|
|
|
|
|
|
|
|
|
2015-08-22 02:35:11 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2014-12-19 06:33:45 +00:00
|
|
|
class GroupAddTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
TestCase for salt.modules.groupadd
|
|
|
|
'''
|
|
|
|
groupadd.__grains__ = {}
|
|
|
|
groupadd.__salt__ = {}
|
|
|
|
groupadd.__context__ = {}
|
|
|
|
mock_group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
|
|
|
|
mock_getgrnam = grp.struct_group(('foo', '*', 20, ['test']))
|
|
|
|
|
|
|
|
# 'add' function tests: 1
|
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
'''
|
|
|
|
Tests if specified group was added
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value={'retcode': 0})
|
|
|
|
with patch.dict(groupadd.__salt__, {'cmd.run_all': mock}):
|
|
|
|
self.assertTrue(groupadd.add('test', 100))
|
|
|
|
|
|
|
|
with patch.dict(groupadd.__grains__, {'kernel': 'Linux'}):
|
|
|
|
with patch.dict(groupadd.__salt__, {'cmd.run_all': mock}):
|
|
|
|
self.assertTrue(groupadd.add('test', 100, True))
|
|
|
|
|
|
|
|
# 'info' function tests: 1
|
|
|
|
|
|
|
|
@patch('grp.getgrnam', MagicMock(return_value=mock_getgrnam))
|
|
|
|
def test_info(self):
|
|
|
|
'''
|
|
|
|
Tests the return of group information
|
|
|
|
'''
|
|
|
|
ret = {'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}
|
|
|
|
self.assertEqual(groupadd.info('foo'), ret)
|
|
|
|
|
|
|
|
# '_format_info' function tests: 1
|
|
|
|
|
|
|
|
@patch('salt.modules.groupadd._format_info',
|
|
|
|
MagicMock(return_value=mock_group))
|
|
|
|
def test_format_info(self):
|
|
|
|
'''
|
|
|
|
Tests the formatting of returned group information
|
|
|
|
'''
|
|
|
|
data = grp.struct_group(('wheel', '*', 0, ['root']))
|
|
|
|
ret = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
|
|
|
|
self.assertDictEqual(groupadd._format_info(data), ret)
|
|
|
|
|
|
|
|
# 'getent' function tests: 1
|
|
|
|
|
|
|
|
@patch('grp.getgrall', MagicMock(return_value=[mock_getgrnam]))
|
|
|
|
def test_getent(self):
|
|
|
|
'''
|
|
|
|
Tests the return of information on all groups
|
|
|
|
'''
|
|
|
|
ret = [{'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}]
|
|
|
|
self.assertEqual(groupadd.getent(), ret)
|
|
|
|
|
|
|
|
# 'chgid' function tests: 2
|
|
|
|
|
|
|
|
def test_chgid_gid_same(self):
|
|
|
|
'''
|
|
|
|
Tests if the group id is the same as argument
|
|
|
|
'''
|
|
|
|
mock_pre_gid = MagicMock(return_value=10)
|
|
|
|
with patch.dict(groupadd.__salt__,
|
|
|
|
{'file.group_to_gid': mock_pre_gid}):
|
|
|
|
self.assertTrue(groupadd.chgid('test', 10))
|
|
|
|
|
|
|
|
def test_chgid(self):
|
|
|
|
'''
|
|
|
|
Tests the gid for a named group was changed
|
|
|
|
'''
|
|
|
|
mock_pre_gid = MagicMock(return_value=0)
|
|
|
|
mock_cmdrun = MagicMock(return_value=0)
|
|
|
|
with patch.dict(groupadd.__salt__,
|
|
|
|
{'file.group_to_gid': mock_pre_gid}):
|
|
|
|
with patch.dict(groupadd.__salt__, {'cmd.run': mock_cmdrun}):
|
|
|
|
self.assertFalse(groupadd.chgid('test', 500))
|
|
|
|
|
|
|
|
# 'delete' function tests: 1
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
'''
|
|
|
|
Tests if the specified group was deleted
|
|
|
|
'''
|
|
|
|
mock_ret = MagicMock(return_value={'retcode': 0})
|
|
|
|
with patch.dict(groupadd.__salt__, {'cmd.run_all': mock_ret}):
|
|
|
|
self.assertTrue(groupadd.delete('test'))
|
|
|
|
|
|
|
|
# 'adduser' function tests: 1
|
|
|
|
|
|
|
|
def test_adduser(self):
|
|
|
|
'''
|
|
|
|
Tests if specified user gets added in the group.
|
|
|
|
'''
|
2016-06-03 12:17:13 +00:00
|
|
|
os_version_list = [
|
2016-09-01 21:40:15 +00:00
|
|
|
{'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('gpasswd', '-a', 'root', 'test')},
|
2016-06-03 12:17:13 +00:00
|
|
|
|
2016-09-01 21:40:15 +00:00
|
|
|
{'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osmajorrelease': '11'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('usermod', '-A', 'test', 'root')},
|
2016-06-03 12:17:13 +00:00
|
|
|
|
|
|
|
{'grains': {'kernel': 'Linux'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('gpasswd', '--add', 'root', 'test')},
|
2014-12-19 06:33:45 +00:00
|
|
|
|
2016-06-03 12:17:13 +00:00
|
|
|
{'grains': {'kernel': 'OTHERKERNEL'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('usermod', '-G', 'test', 'root')},
|
2016-06-03 12:17:13 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for os_version in os_version_list:
|
|
|
|
mock = MagicMock(return_value={'retcode': 0})
|
|
|
|
with patch.dict(groupadd.__grains__, os_version['grains']):
|
|
|
|
with patch.dict(groupadd.__salt__, {'cmd.retcode': mock}):
|
|
|
|
self.assertFalse(groupadd.adduser('test', 'root'))
|
|
|
|
groupadd.__salt__['cmd.retcode'].assert_called_once_with(os_version['cmd'], python_shell=False)
|
2014-12-19 06:33:45 +00:00
|
|
|
|
|
|
|
# 'deluser' function tests: 1
|
|
|
|
|
|
|
|
def test_deluser(self):
|
|
|
|
'''
|
|
|
|
Tests if specified user gets deleted from the group.
|
|
|
|
'''
|
2016-06-03 12:17:13 +00:00
|
|
|
os_version_list = [
|
2016-09-01 21:40:15 +00:00
|
|
|
{'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('gpasswd', '-d', 'root', 'test')},
|
2014-12-19 06:33:45 +00:00
|
|
|
|
2016-09-01 21:40:15 +00:00
|
|
|
{'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osmajorrelease': '11'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('usermod', '-R', 'test', 'root')},
|
2016-06-03 12:17:13 +00:00
|
|
|
|
|
|
|
{'grains': {'kernel': 'Linux'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('gpasswd', '--del', 'root', 'test')},
|
2016-06-03 12:17:13 +00:00
|
|
|
|
|
|
|
{'grains': {'kernel': 'OpenBSD'},
|
|
|
|
'cmd': 'usermod -S foo root'},
|
|
|
|
]
|
|
|
|
|
|
|
|
for os_version in os_version_list:
|
|
|
|
mock_ret = MagicMock(return_value={'retcode': 0})
|
|
|
|
mock_stdout = MagicMock(return_value='test foo')
|
|
|
|
mock_info = MagicMock(return_value={'passwd': '*',
|
|
|
|
'gid': 0,
|
|
|
|
'name': 'test',
|
|
|
|
'members': ['root']})
|
|
|
|
|
|
|
|
with patch.dict(groupadd.__grains__, os_version['grains']):
|
|
|
|
with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_ret,
|
|
|
|
'group.info': mock_info,
|
|
|
|
'cmd.run_stdout': mock_stdout}):
|
|
|
|
self.assertFalse(groupadd.deluser('test', 'root'))
|
|
|
|
groupadd.__salt__['cmd.retcode'].assert_called_once_with(os_version['cmd'], python_shell=False)
|
2014-12-19 06:33:45 +00:00
|
|
|
|
|
|
|
# 'deluser' function tests: 1
|
|
|
|
|
|
|
|
def test_members(self):
|
|
|
|
'''
|
|
|
|
Tests if members of the group, get replaced with a provided list.
|
|
|
|
'''
|
2016-06-03 12:17:13 +00:00
|
|
|
os_version_list = [
|
2016-09-01 21:40:15 +00:00
|
|
|
{'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('gpasswd', '-M', 'foo', 'test')},
|
2016-06-03 12:17:13 +00:00
|
|
|
|
2016-09-01 21:40:15 +00:00
|
|
|
{'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osmajorrelease': '11'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('groupmod', '-A', 'foo', 'test')},
|
2016-06-03 12:17:13 +00:00
|
|
|
|
|
|
|
{'grains': {'kernel': 'Linux'},
|
2016-06-08 18:42:52 +00:00
|
|
|
'cmd': ('gpasswd', '--members', 'foo', 'test')},
|
2016-06-03 12:25:05 +00:00
|
|
|
|
|
|
|
{'grains': {'kernel': 'OpenBSD'},
|
|
|
|
'cmd': 'usermod -G test foo'},
|
2016-06-03 12:17:13 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for os_version in os_version_list:
|
|
|
|
mock_ret = MagicMock(return_value={'retcode': 0})
|
2016-06-03 12:25:05 +00:00
|
|
|
mock_stdout = MagicMock(return_value={'cmd.run_stdout': 1})
|
2016-06-03 12:17:13 +00:00
|
|
|
mock_info = MagicMock(return_value={'passwd': '*',
|
|
|
|
'gid': 0,
|
|
|
|
'name': 'test',
|
|
|
|
'members': ['root']})
|
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
|
|
|
|
with patch.dict(groupadd.__grains__, os_version['grains']):
|
|
|
|
with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_ret,
|
|
|
|
'group.info': mock_info,
|
2016-06-03 12:25:05 +00:00
|
|
|
'cmd.run_stdout': mock_stdout,
|
2016-06-03 12:17:13 +00:00
|
|
|
'cmd.run': mock}):
|
|
|
|
self.assertFalse(groupadd.members('test', 'foo'))
|
|
|
|
groupadd.__salt__['cmd.retcode'].assert_called_once_with(os_version['cmd'], python_shell=False)
|