From cf6a2086af17e3c10ec5e53d7539858aaf15482f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?=
 <psuarezhernandez@suse.com>
Date: Thu, 2 Jun 2016 11:50:36 +0100
Subject: [PATCH 54/54] fix groupadd module for sles11 systems

one line is better

improvements on groupadd unit tests

test_members cleanup

pylint fix
---
 salt/modules/groupadd.py            |  11 ++++
 tests/unit/modules/groupadd_test.py | 116 +++++++++++++++++++++++-------------
 2 files changed, 87 insertions(+), 40 deletions(-)

diff --git a/salt/modules/groupadd.py b/salt/modules/groupadd.py
index 31ffeb6..07d5db9 100644
--- a/salt/modules/groupadd.py
+++ b/salt/modules/groupadd.py
@@ -147,10 +147,13 @@ def adduser(name, username):
     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 {0} {1}'.format(username, name)
+        elif on_suse_11:
+            cmd = 'usermod -A {0} {1}'.format(name, username)
         else:
             cmd = 'gpasswd --add {0} {1}'.format(username, name)
     else:
@@ -175,6 +178,7 @@ def deluser(name, username):
     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:
@@ -182,6 +186,8 @@ def deluser(name, username):
             if __grains__['kernel'] == 'Linux':
                 if on_redhat_5:
                     cmd = 'gpasswd -d {0} {1}'.format(username, name)
+                elif on_suse_11:
+                    cmd = 'usermod -R {0} {1}'.format(name, username)
                 else:
                     cmd = 'gpasswd --del {0} {1}'.format(username, name)
                 retcode = __salt__['cmd.retcode'](cmd, python_shell=False)
@@ -214,10 +220,15 @@ def members(name, members_list):
         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 {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 {0} {1}'.format(members_list, name)
         retcode = __salt__['cmd.retcode'](cmd, python_shell=False)
diff --git a/tests/unit/modules/groupadd_test.py b/tests/unit/modules/groupadd_test.py
index b151901..c0c0a74 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

