Merge pull request #10836 from rallytime/mac_group_test

Wrote the rest of the mac_group unit tests
This commit is contained in:
Mike Place 2014-02-27 14:13:58 -07:00
commit 6a318faee3

View File

@ -19,19 +19,21 @@ class MacGroupTestCase(TestCase):
'''
TestCase for the salt.modules.mac_group module
'''
mock_group = {'passwd': '*', 'gid': 0, 'name': 'wheel', 'members': ['root']}
mac_group.__context__ = {}
mac_group.__salt__ = {}
mock_group = {'passwd': '*', 'gid': 0, 'name': 'test', 'members': ['root']}
mock_getgrall = [grp.struct_group(('foo', '*', 20, ['test']))]
# 'add' function tests: 4
# Only tested error handling
# Full functionality tests covered in integration testing
# 'add' function tests: 5
@patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group))
def test_add_group_exists(self):
'''
Tests if the group already exists or not
'''
self.assertRaises(CommandExecutionError, mac_group.add, 'wheel')
self.assertRaises(CommandExecutionError, mac_group.add, 'test')
@patch('salt.modules.mac_group.info', MagicMock(return_value={}))
def test_add_whitespace(self):
@ -54,6 +56,47 @@ class MacGroupTestCase(TestCase):
'''
self.assertRaises(SaltInvocationError, mac_group.add, 'foo', 'foo')
@patch('salt.modules.mac_group.info', MagicMock(return_value={}))
def test_add(self):
'''
Tests if specified group was added
'''
mock_ret = MagicMock(return_value=0)
with patch.dict(mac_group.__salt__, {'cmd.retcode': mock_ret}):
self.assertTrue(mac_group.add('test', 500))
# 'delete' function tests: 4
def test_delete_whitespace(self):
'''
Tests if there is whitespace in the group name
'''
self.assertRaises(SaltInvocationError, mac_group.delete, 'white space')
def test_delete_underscore(self):
'''
Tests if the group name starts with an underscore or not
'''
self.assertRaises(SaltInvocationError, mac_group.delete, '_Test')
@patch('salt.modules.mac_group.info', MagicMock(return_value={}))
def test_delete_group_exists(self):
'''
Tests if the group to be deleted exists or not
'''
self.assertTrue(mac_group.delete('test'))
@patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group))
def test_delete(self):
'''
Tests if the specified group was deleted
'''
mock_ret = MagicMock(return_value=0)
with patch.dict(mac_group.__salt__, {'cmd.retcode': mock_ret}):
self.assertTrue(mac_group.delete('test'))
# 'info' function tests: 2
def test_info_whitespace(self):
'''
Tests if there is whitespace in the group name
@ -68,6 +111,8 @@ class MacGroupTestCase(TestCase):
ret = {'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}
self.assertEqual(mac_group.info('foo'), ret)
# '_format_info' function tests: 1
def test_format_info(self):
'''
Tests the formatting of returned group information
@ -76,6 +121,57 @@ class MacGroupTestCase(TestCase):
ret = {'passwd': '*', 'gid': 0, 'name': 'wheel', 'members': ['root']}
self.assertEqual(mac_group._format_info(data), ret)
# 'getent' function tests: 1
@patch('grp.getgrall', MagicMock(return_value=mock_getgrall))
def test_getent(self):
'''
Tests the return of information on all groups
'''
ret = [{'passwd': '*', 'gid': 20, 'name': 'foo', 'members': ['test']}]
self.assertEqual(mac_group.getent(), ret)
# 'chgid' function tests: 4
def test_chgid_gid_int(self):
'''
Tests if gid is an integer or not
'''
self.assertRaises(SaltInvocationError, mac_group.chgid, 'foo', 'foo')
@patch('salt.modules.mac_group.info', MagicMock(return_value={}))
def test_chgid_group_exists(self):
'''
Tests if the group id exists or not
'''
mock_pre_gid = MagicMock(return_value='')
with patch.dict(mac_group.__salt__,
{'file.group_to_gid': mock_pre_gid}):
self.assertRaises(CommandExecutionError,
mac_group.chgid, 'foo', 4376)
@patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group))
def test_chgid_gid_same(self):
'''
Tests if the group id is the same as argument
'''
mock_pre_gid = MagicMock(return_value=0)
with patch.dict(mac_group.__salt__,
{'file.group_to_gid': mock_pre_gid}):
self.assertTrue(mac_group.chgid('test', 0))
@patch('salt.modules.mac_group.info', MagicMock(return_value=mock_group))
def test_chgid(self):
'''
Tests the gid for a named group was changed
'''
mock_pre_gid = MagicMock(return_value=0)
mock_ret = MagicMock(return_value=0)
with patch.dict(mac_group.__salt__,
{'file.group_to_gid': mock_pre_gid}):
with patch.dict(mac_group.__salt__, {'cmd.retcode': mock_ret}):
self.assertTrue(mac_group.chgid('test', 500))
if __name__ == '__main__':
from integration import run_tests