From f2cff5375682bee62005ce9d49ee058598c25b07 Mon Sep 17 00:00:00 2001 From: rallytime Date: Tue, 18 Mar 2014 13:02:20 -0600 Subject: [PATCH] Check if gid exists before adding group and associated tests --- salt/modules/mac_group.py | 21 +++++- tests/integration/modules/mac_group.py | 89 ++++++++++++++++++++++++++ tests/unit/modules/mac_group_test.py | 11 +++- 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tests/integration/modules/mac_group.py diff --git a/salt/modules/mac_group.py b/salt/modules/mac_group.py index af9483fb99..0aa5b53139 100644 --- a/salt/modules/mac_group.py +++ b/salt/modules/mac_group.py @@ -49,9 +49,15 @@ def add(name, gid=None, **kwargs): raise SaltInvocationError( 'Salt will not create groups beginning with underscores' ) - if gid is not None and not isinstance(gid, int): raise SaltInvocationError('gid must be an integer') + # check if gid is already in use + gid_list = _list_gids() + if str(gid) in gid_list: + raise CommandExecutionError( + 'gid {0!r} already exists'.format(gid) + ) + cmd = 'dseditgroup -o create ' if gid: cmd += '-i {0} '.format(gid) @@ -59,6 +65,19 @@ def add(name, gid=None, **kwargs): return __salt__['cmd.retcode'](cmd) == 0 +def _list_gids(): + ''' + Return a list of gids in use + ''' + cmd = __salt__['cmd.run']('dscacheutil -q group | grep gid:', + output_loglevel='quiet') + data_list = cmd.split() + for item in data_list: + if item == 'gid:': + data_list.remove(item) + return sorted(set(data_list)) + + def delete(name): ''' Remove the named group diff --git a/tests/integration/modules/mac_group.py b/tests/integration/modules/mac_group.py new file mode 100644 index 0000000000..3f3164412e --- /dev/null +++ b/tests/integration/modules/mac_group.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- +''' + :codeauthor: :email:`Nicole Thomas ` +''' + +# Import Python Libs +import os +import random +import string + +# Import Salt Testing Libs +from salttesting import skipIf +from salttesting.helpers import ( + destructiveTest, + ensure_in_syspath, + requires_system_grains +) +ensure_in_syspath('../../') + +# Import Salt Libs +import integration +from salt.exceptions import CommandExecutionError + + +def __random_string(size=6): + ''' + Generates a random username + ''' + return 'RS-' + ''.join( + random.choice(string.ascii_uppercase + string.digits) + for x in range(size) + ) + +# Create group name strings for tests +ADD_GROUP = __random_string() + + +class MacGroupModuleTest(integration.ModuleCase): + ''' + Integration tests for the mac_group module + ''' + + def setUp(self): + ''' + Sets up test requirements + ''' + super(MacGroupModuleTest, self).setUp() + os_grain = self.run_function('grains.item', ['kernel']) + if os_grain['kernel'] not in 'Darwin': + self.skipTest( + 'Test not applicable to \'{kernel}\' kernel'.format( + **os_grain + ) + ) + + @destructiveTest + @skipIf(os.geteuid() != 0, 'You must be logged in as root to run this test') + @requires_system_grains + def test_mac_group_add(self, grains=None): + ''' + Tests the add group function + ''' + print "In Add" + print "Add Group" + print ADD_GROUP + try: + self.run_function('group.add', [ADD_GROUP, 3456]) + group_info = self.run_function('group.info', [ADD_GROUP]) + self.assertEqual(group_info['name'], ADD_GROUP) + except CommandExecutionError: + self.run_function('group.delete', [ADD_GROUP]) + raise + + @destructiveTest + @skipIf(os.geteuid() != 0, 'You must be logged in as root to run this test') + @requires_system_grains + def tearDown(self, grains=None): + ''' + Clean up after tests + ''' + # Delete the added group + add_info = self.run_function('group.info', [ADD_GROUP]) + if add_info: + self.run_function('group.delete', [ADD_GROUP]) + + +if __name__ == '__main__': + from integration import run_tests + run_tests(MacGroupModuleTest) diff --git a/tests/unit/modules/mac_group_test.py b/tests/unit/modules/mac_group_test.py index a9f897a257..e0bbaf38db 100644 --- a/tests/unit/modules/mac_group_test.py +++ b/tests/unit/modules/mac_group_test.py @@ -26,7 +26,7 @@ class MacGroupTestCase(TestCase): mock_group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']} mock_getgrall = [grp.struct_group(('foo', '*', 20, ['test']))] - # 'add' function tests: 5 + # 'add' function tests: 6 @patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group)) def test_add_group_exists(self): @@ -57,6 +57,15 @@ class MacGroupTestCase(TestCase): self.assertRaises(SaltInvocationError, mac_group.add, 'foo', 'foo') @patch('salt.modules.mac_group.info', MagicMock(return_value={})) + @patch('salt.modules.mac_group._list_gids', MagicMock(return_value=['3456'])) + def test_add_gid_exists(self): + ''' + Tests if the gid is already in use or not + ''' + self.assertRaises(CommandExecutionError, mac_group.add, 'foo', 3456) + + @patch('salt.modules.mac_group.info', MagicMock(return_value={})) + @patch('salt.modules.mac_group._list_gids', MagicMock(return_value=[])) def test_add(self): ''' Tests if specified group was added