mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
3273bbdab7
Conflicts: - doc/ref/configuration/master.rst - doc/ref/modules/all/index.rst - doc/topics/grains/index.rst - doc/topics/releases/2016.3.4.rst - doc/topics/spm/spm_formula.rst - doc/topics/tutorials/cron.rst - doc/topics/tutorials/index.rst - doc/topics/tutorials/stormpath.rst - salt/engines/slack.py - salt/log/handlers/fluent_mod.py - salt/modules/cyg.py - salt/modules/junos.py - salt/modules/namecheap_dns.py - salt/modules/namecheap_domains.py - salt/modules/namecheap_ns.py - salt/modules/namecheap_ssl.py - salt/modules/namecheap_users.py - salt/modules/reg.py - salt/modules/tomcat.py - salt/modules/vault.py - salt/modules/win_file.py - salt/modules/zpool.py - salt/output/highstate.py - salt/renderers/pass.py - salt/runners/cache.py - salt/states/boto_apigateway.py - salt/states/boto_iam.py - salt/states/boto_route53.py - salt/states/msteams.py - salt/states/reg.py - salt/states/win_iis.py - tests/integration/modules/test_cmdmod.py - tests/integration/states/test_user.py - tests/support/helpers.py - tests/unit/cloud/clouds/test_openstack.py - tests/unit/fileserver/test_gitfs.py - tests/unit/modules/test_junos.py - tests/unit/pillar/test_git.py - tests/unit/states/test_win_path.py - tests/unit/test_pillar.py - tests/unit/utils/test_format_call.py - tests/unit/utils/test_utils.py - tests/unit/utils/test_warnings.py
111 lines
4.3 KiB
Python
111 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
:codeauthor: Rahul Handay <rahulha@saltstack.com>
|
|
'''
|
|
|
|
# Import Python Libs
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
# Import Salt Testing Libs
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
|
from tests.support.unit import TestCase, skipIf
|
|
from tests.support.mock import (
|
|
MagicMock,
|
|
patch,
|
|
NO_MOCK,
|
|
NO_MOCK_REASON
|
|
)
|
|
|
|
# Import Salt Libs
|
|
import salt.states.group as group
|
|
from salt.utils.odict import OrderedDict
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
class GroupTestCase(TestCase, LoaderModuleMockMixin):
|
|
'''
|
|
Validate the group state
|
|
'''
|
|
def setup_loader_modules(self):
|
|
return {group: {}}
|
|
|
|
def test_present(self):
|
|
'''
|
|
Test to ensure that a group is present
|
|
'''
|
|
ret = {'name': 'salt',
|
|
'changes': {},
|
|
'result': True,
|
|
'comment': {}
|
|
}
|
|
|
|
ret.update({'comment': 'Error: Conflicting options "members" with'
|
|
' "addusers" and/or "delusers" can not be used together. ',
|
|
'result': None})
|
|
self.assertDictEqual(group.present("salt", delusers=True,
|
|
members=True), ret)
|
|
|
|
ret.update({'comment': 'Error. Same user(s) can not be'
|
|
' added and deleted simultaneously'})
|
|
self.assertDictEqual(group.present("salt", addusers=['a'],
|
|
delusers=['a']), ret)
|
|
|
|
ret.update({'comment': 'The following group attributes are set'
|
|
' to be changed:\nkey0: value0\nkey1: value1\n'})
|
|
|
|
mock = MagicMock(side_effect=[OrderedDict((('key0', 'value0'), ('key1', 'value1'))),
|
|
False,
|
|
False,
|
|
False])
|
|
with patch.object(group, '_changes', mock):
|
|
with patch.dict(group.__opts__, {"test": True}):
|
|
self.assertDictEqual(group.present("salt"), ret)
|
|
|
|
ret.update({'comment': 'Group salt set to be added'})
|
|
self.assertDictEqual(group.present("salt"), ret)
|
|
|
|
with patch.dict(group.__opts__, {"test": False}):
|
|
mock = MagicMock(return_value=[{'gid': 1, 'name': 'stack'}])
|
|
with patch.dict(group.__salt__, {'group.getent': mock}):
|
|
ret.update({'result': False,
|
|
'comment': 'Group salt is not present but'
|
|
' gid 1 is already taken by group stack'})
|
|
self.assertDictEqual(group.present("salt", 1), ret)
|
|
|
|
mock = MagicMock(return_value=False)
|
|
with patch.dict(group.__salt__, {'group.add': mock}):
|
|
ret.update({'comment':
|
|
'Failed to create new group salt'})
|
|
self.assertDictEqual(group.present("salt"), ret)
|
|
|
|
def test_absent(self):
|
|
'''
|
|
Test to ensure that the named group is absent
|
|
'''
|
|
ret = {'name': 'salt',
|
|
'changes': {},
|
|
'result': True,
|
|
'comment': {}
|
|
}
|
|
mock = MagicMock(side_effect=[True, True, True, False])
|
|
with patch.dict(group.__salt__, {'group.info': mock}):
|
|
with patch.dict(group.__opts__, {"test": True}):
|
|
ret.update({'result': None,
|
|
'comment': 'Group salt is set for removal'})
|
|
self.assertDictEqual(group.absent("salt"), ret)
|
|
|
|
with patch.dict(group.__opts__, {"test": False}):
|
|
mock = MagicMock(side_effect=[True, False])
|
|
with patch.dict(group.__salt__, {'group.delete': mock}):
|
|
ret.update({'result': True, 'changes': {'salt': ''},
|
|
'comment': 'Removed group salt'})
|
|
self.assertDictEqual(group.absent('salt'), ret)
|
|
|
|
ret.update({'changes': {}, 'result': False,
|
|
'comment': 'Failed to remove group salt'})
|
|
self.assertDictEqual(group.absent('salt'), ret)
|
|
|
|
ret.update({'result': True,
|
|
'comment': 'Group not present'})
|
|
self.assertDictEqual(group.absent('salt'), ret)
|