Skip to content

Commit

Permalink
Check if gid exists before adding group and associated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rallytime committed Mar 18, 2014
1 parent 5eb6396 commit f2cff53
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
21 changes: 20 additions & 1 deletion salt/modules/mac_group.py
Expand Up @@ -49,16 +49,35 @@ 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)
cmd += str(name)
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
Expand Down
89 changes: 89 additions & 0 deletions tests/integration/modules/mac_group.py
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''

# 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)
11 changes: 10 additions & 1 deletion tests/unit/modules/mac_group_test.py
Expand Up @@ -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):
Expand Down Expand Up @@ -57,6 +57,15 @@ def test_add_gid_int(self):
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
Expand Down

0 comments on commit f2cff53

Please sign in to comment.