From 10d5f4c4a979782efa94837a71d51c0c6fa12316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Thu, 2 Jun 2016 11:50:36 +0100 Subject: [PATCH 13/13] fix groupadd module for sles11 systems one line is better improvements on groupadd unit tests test_members cleanup pylint fix --- salt/modules/groupadd.py | 17 +++++- tests/unit/modules/groupadd_test.py | 116 +++++++++++++++++++++++------------- 2 files changed, 90 insertions(+), 43 deletions(-) diff --git a/salt/modules/groupadd.py b/salt/modules/groupadd.py index 6d963b1d1ebb..36d0e8da6a0d 100644 --- a/salt/modules/groupadd.py +++ b/salt/modules/groupadd.py @@ -166,10 +166,13 @@ def adduser(name, username, root=None): if not then adds it. ''' on_redhat_5 = __grains__.get('os_family') == 'RedHat' and __grains__.get('osmajorrelease') == '5' + on_suse_11 = __grains__.get('os_family') == 'Suse' and __grains__.get('osrelease_info')[0] == 11 if __grains__['kernel'] == 'Linux': if on_redhat_5: - cmd = ('gpasswd', '-a', username, name) + cmd = 'gpasswd -a {0} {1}'.format(username, name) + elif on_suse_11: + cmd = 'usermod -A {0} {1}'.format(name, username) else: cmd = ('gpasswd', '--add', username, name) if root is not None: @@ -198,13 +201,16 @@ def deluser(name, username, root=None): then returns True. ''' on_redhat_5 = __grains__.get('os_family') == 'RedHat' and __grains__.get('osmajorrelease') == '5' + on_suse_11 = __grains__.get('os_family') == 'Suse' and __grains__.get('osrelease_info')[0] == 11 grp_info = __salt__['group.info'](name) try: if username in grp_info['members']: if __grains__['kernel'] == 'Linux': if on_redhat_5: - cmd = ('gpasswd', '-d', username, name) + cmd = 'gpasswd -d {0} {1}'.format(username, name) + elif on_suse_11: + cmd = 'usermod -R {0} {1}'.format(name, username) else: cmd = ('gpasswd', '--del', username, name) if root is not None: @@ -239,10 +245,15 @@ def members(name, members_list, root=None): foo:x:1234:user1,user2,user3,... ''' on_redhat_5 = __grains__.get('os_family') == 'RedHat' and __grains__.get('osmajorrelease') == '5' + on_suse_11 = __grains__.get('os_family') == 'Suse' and __grains__.get('osrelease_info')[0] == 11 if __grains__['kernel'] == 'Linux': if on_redhat_5: - cmd = ('gpasswd', '-M', members_list, name) + cmd = 'gpasswd -M {0} {1}'.format(members_list, name) + elif on_suse_11: + for old_member in __salt__['group.info'](name).get('members'): + __salt__['cmd.run']('groupmod -R {0} {1}'.format(old_member, name), python_shell=False) + cmd = 'groupmod -A {0} {1}'.format(members_list, name) else: cmd = ('gpasswd', '--members', members_list, name) if root is not None: diff --git a/tests/unit/modules/groupadd_test.py b/tests/unit/modules/groupadd_test.py index b1519014cfd0..c0c0a748dbb5 100644 --- a/tests/unit/modules/groupadd_test.py +++ b/tests/unit/modules/groupadd_test.py @@ -112,14 +112,26 @@ class GroupAddTestCase(TestCase): ''' Tests if specified user gets added in the group. ''' - mock = MagicMock(return_value={'retcode': 0}) - with patch.dict(groupadd.__grains__, {'kernel': 'Linux'}): - with patch.dict(groupadd.__salt__, {'cmd.retcode': mock}): - self.assertFalse(groupadd.adduser('test', 'root')) + os_version_list = [ + {'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'}, + 'cmd': 'gpasswd -a root test'}, + + {'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osrelease_info': [11, 2]}, + 'cmd': 'usermod -A test root'}, + + {'grains': {'kernel': 'Linux'}, + 'cmd': 'gpasswd --add root test'}, - with patch.dict(groupadd.__grains__, {'kernel': ''}): - with patch.dict(groupadd.__salt__, {'cmd.retcode': mock}): - self.assertFalse(groupadd.adduser('test', 'root')) + {'grains': {'kernel': 'OTHERKERNEL'}, + 'cmd': 'usermod -G test root'}, + ] + + 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) # 'deluser' function tests: 1 @@ -127,22 +139,34 @@ class GroupAddTestCase(TestCase): ''' Tests if specified user gets deleted from the group. ''' - mock_ret = MagicMock(return_value={'retcode': 0}) - mock_info = MagicMock(return_value={'passwd': '*', - 'gid': 0, - 'name': 'test', - 'members': ['root']}) - with patch.dict(groupadd.__grains__, {'kernel': 'Linux'}): - with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_ret, - 'group.info': mock_info}): - self.assertFalse(groupadd.deluser('test', 'root')) + os_version_list = [ + {'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'}, + 'cmd': 'gpasswd -d root test'}, - mock_stdout = MagicMock(return_value={'cmd.run_stdout': 1}) - with patch.dict(groupadd.__grains__, {'kernel': 'OpenBSD'}): - with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_ret, - 'group.info': mock_info, - 'cmd.run_stdout': mock_stdout}): - self.assertTrue(groupadd.deluser('foo', 'root')) + {'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osrelease_info': [11, 2]}, + 'cmd': 'usermod -R test root'}, + + {'grains': {'kernel': 'Linux'}, + 'cmd': 'gpasswd --del root test'}, + + {'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) # 'deluser' function tests: 1 @@ -150,24 +174,36 @@ class GroupAddTestCase(TestCase): ''' Tests if members of the group, get replaced with a provided list. ''' - mock_ret = MagicMock(return_value={'retcode': 0}) - mock_info = MagicMock(return_value={'passwd': '*', - 'gid': 0, - 'name': 'test', - 'members': ['root']}) - with patch.dict(groupadd.__grains__, {'kernel': 'Linux'}): - with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_ret, - 'group.info': mock_info}): - self.assertFalse(groupadd.members('test', ['foo'])) - - mock_stdout = MagicMock(return_value={'cmd.run_stdout': 1}) - mock = MagicMock() - with patch.dict(groupadd.__grains__, {'kernel': 'OpenBSD'}): - with patch.dict(groupadd.__salt__, {'cmd.retcode': mock_ret, - 'group.info': mock_info, - 'cmd.run_stdout': mock_stdout, - 'cmd.run': mock}): - self.assertFalse(groupadd.members('foo', ['root'])) + os_version_list = [ + {'grains': {'kernel': 'Linux', 'os_family': 'RedHat', 'osmajorrelease': '5'}, + 'cmd': "gpasswd -M foo test"}, + + {'grains': {'kernel': 'Linux', 'os_family': 'Suse', 'osrelease_info': [11, 2]}, + 'cmd': 'groupmod -A foo test'}, + + {'grains': {'kernel': 'Linux'}, + 'cmd': 'gpasswd --members foo test'}, + + {'grains': {'kernel': 'OpenBSD'}, + 'cmd': 'usermod -G test foo'}, + ] + + for os_version in os_version_list: + mock_ret = MagicMock(return_value={'retcode': 0}) + mock_stdout = MagicMock(return_value={'cmd.run_stdout': 1}) + 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, + 'cmd.run_stdout': mock_stdout, + 'cmd.run': mock}): + self.assertFalse(groupadd.members('test', 'foo')) + groupadd.__salt__['cmd.retcode'].assert_called_once_with(os_version['cmd'], python_shell=False) if __name__ == '__main__': -- 2.8.3