diff --git a/salt/modules/win_groupadd.py b/salt/modules/win_groupadd.py index 26c7f34f66..d466380d70 100644 --- a/salt/modules/win_groupadd.py +++ b/salt/modules/win_groupadd.py @@ -374,3 +374,32 @@ def __fixlocaluser(username): username = ('{0}\\{1}').format(__salt__['grains.get']('host'), username) return username.lower() + + +def list_groups(refresh=False): + ''' + Return a list of groups + + CLI Example: + + .. code-block:: bash + + salt '*' group.getent + ''' + if 'group.list_groups' in __context__ and not refresh: + return __context__['group.getent'] + + ret = [] + + pythoncom.CoInitialize() + nt = win32com.client.Dispatch('AdsNameSpaces') + + results = nt.GetObject('', 'WinNT://.') + results.Filter = ['group'] + + for result in results: + ret.append(result.name) + + __context__['group.list_groups'] = ret + + return ret diff --git a/tests/integration/modules/useradd.py b/tests/integration/modules/useradd.py index 85def4f13f..62682d6210 100644 --- a/tests/integration/modules/useradd.py +++ b/tests/integration/modules/useradd.py @@ -22,16 +22,25 @@ import integration # Import 3rd-party libs from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin +IS_ADMIN = False +if salt.utils.is_windows(): + import salt.utils.win_functions + current_user = salt.utils.win_functions.get_current_user() + if current_user == 'SYSTEM': + IS_ADMIN = True + else: + IS_ADMIN = salt.utils.win_functions.is_admin(current_user) +else: + IS_ADMIN = os.geteuid() == 0 + @destructiveTest -@skipIf(os.geteuid() != 0, 'you must be root to run these tests') -# Only run on linux for now until or if we can figure out a way to use -# __grains__ inside of useradd.__virtual__ @skipIf(not salt.utils.is_linux(), 'These tests can only be run on linux') -class UseraddModuleTest(integration.ModuleCase): +@skipIf(not IS_ADMIN, 'You must be root to run these tests') +class UseraddModuleTestLinux(integration.ModuleCase): def setUp(self): - super(UseraddModuleTest, self).setUp() + super(UseraddModuleTestLinux, self).setUp() os_grain = self.run_function('grains.item', ['kernel']) if os_grain['kernel'] not in ('Linux', 'Darwin'): self.skipTest( @@ -89,7 +98,7 @@ class UseraddModuleTest(integration.ModuleCase): self.run_function('user.delete', [uname, True, True]) raise - def test_linux_user_primary_group(self, grains=None): + def test_user_primary_group(self, grains=None): ''' Tests the primary_group function ''' @@ -111,6 +120,88 @@ class UseraddModuleTest(integration.ModuleCase): raise +@destructiveTest +@skipIf(not salt.utils.is_windows(), 'These tests can only be run on Windows') +@skipIf(not IS_ADMIN, 'You must be Administrator to run these tests') +class UseraddModuleTestWindows(integration.ModuleCase): + + def __random_string(self, size=6): + return 'RS-' + ''.join( + random.choice(string.ascii_uppercase + string.digits) + for x in range(size)) + + def test_add_user(self): + ''' + Test adding a user + ''' + user_name = self.__random_string() + + try: + if self.run_function('user.add', [user_name]) is False: + self.run_function('user.delete', [user_name, True, True]) + self.skipTest('Failed to create user') + + user_list = self.run_function('user.list_users') + self.assertIn(user_name, user_list) + + except AssertionError: + raise + + finally: + self.run_function('user.delete', [user_name, True, True]) + + def test_add_group(self): + ''' + Test adding a user + ''' + group_name = self.__random_string() + try: + if self.run_function('group.add', [group_name]) is False: + # Skip because creating is not what we're testing here + self.run_function('group.delete', [group_name, True, True]) + self.skipTest('Failed to create group') + + group_list = self.run_function('group.list_groups') + self.assertIn(group_name, group_list) + + except AssertionError: + raise + + finally: + self.run_function('group.delete', [group_name]) + + def test_add_user_to_group(self): + ''' + Test adding a user to a group + ''' + group_name = self.__random_string() + user_name = self.__random_string() + try: + # Let's create a group + if self.run_function( + 'group.add', [group_name])['result'] is not True: + self.run_function('group.delete', [group_name, True, True]) + self.skipTest('Failed to create group') + + # And create the user as a member of that group + if self.run_function( + 'user.add', + [user_name, 'groups={0}'.format(group_name)]) is False: + self.run_function('user.delete', [user_name, True, True]) + self.skipTest('Failed to create user') + + user_info = self.run_function('user.info', [user_name]) + self.assertIn(group_name, user_info['groups']) + + except AssertionError: + raise + + finally: + self.run_function('user.delete', [user_name, True, True]) + self.run_function('group.delete', [group_name]) + + if __name__ == '__main__': from integration import run_tests - run_tests(UseraddModuleTest) + run_tests(UseraddModuleTestLinux, + UseraddModuleTestWindows)