salt/tests/unit/states/test_linux_acl.py

186 lines
8.4 KiB
Python
Raw Normal View History

2015-05-13 11:42:28 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import
2016-09-08 21:37:21 +00:00
import sys
2015-05-13 11:42:28 +00:00
# Import Salt Testing Libs
2017-03-22 16:42:17 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import (
2015-05-13 11:42:28 +00:00
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch)
2015-05-13 11:42:28 +00:00
# Import Salt Libs
import salt.states.linux_acl as linux_acl
from salt.exceptions import CommandExecutionError
2015-05-13 11:42:28 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2016-09-08 19:46:41 +00:00
@skipIf(not sys.platform.startswith('linux'), 'Test for Linux only')
2017-03-22 16:42:17 +00:00
class LinuxAclTestCase(TestCase, LoaderModuleMockMixin):
2015-05-13 11:42:28 +00:00
'''
Test cases for salt.states.linux_acl
'''
2017-03-22 16:42:17 +00:00
def setup_loader_modules(self):
return {linux_acl: {}}
2015-05-13 11:42:28 +00:00
# 'present' function tests: 1
def test_present(self):
'''
Test to ensure a Linux ACL is present
'''
self.maxDiff = None
2016-09-08 19:46:41 +00:00
name = '/root'
2015-05-13 11:42:28 +00:00
acl_type = 'users'
acl_name = 'damian'
perms = 'rwx'
mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name:
{'octal': 'A'}}]}},
{name: {acl_type: [{acl_name:
{'octal': 'A'}}]}},
{name: {acl_type: [{acl_name:
{'octal': 'A'}}]}},
{name: {acl_type: [{}]}},
{name: {acl_type: [{}]}},
2015-05-13 12:32:35 +00:00
{name: {acl_type: [{}]}},
2015-05-13 11:42:28 +00:00
{name: {acl_type: ''}}])
mock_modfacl = MagicMock(return_value=True)
2015-05-13 11:42:28 +00:00
with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
# Update - test=True
2015-05-13 11:42:28 +00:00
with patch.dict(linux_acl.__opts__, {'test': True}):
comt = ('Updated permissions will be applied for {0}: A -> {1}'
''.format(acl_name, perms))
ret = {'name': name,
'comment': comt,
'changes': {},
'pchanges': {'new': {'acl_name': acl_name,
'acl_type': acl_type,
'perms': perms},
'old': {'acl_name': acl_name,
'acl_type': acl_type,
'perms': 'A'}},
'result': None}
2015-05-13 11:42:28 +00:00
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
perms), ret)
# Update - test=False
with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = ('Updated permissions for {0}'.format(acl_name))
ret = {'name': name,
'comment': comt,
'changes': {'new': {'acl_name': acl_name,
'acl_type': acl_type,
'perms': perms},
'old': {'acl_name': acl_name,
'acl_type': acl_type,
'perms': 'A'}},
'pchanges': {},
'result': True}
self.assertDictEqual(linux_acl.present(name, acl_type,
acl_name, perms),
ret)
# Update - modfacl error
with patch.dict(linux_acl.__salt__, {'acl.modfacl': MagicMock(
side_effect=CommandExecutionError('Custom err'))}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = ('Error updating permissions for {0}: Custom err'
''.format(acl_name))
ret = {'name': name,
'comment': comt,
'changes': {},
'pchanges': {},
'result': False}
self.assertDictEqual(linux_acl.present(name, acl_type,
acl_name, perms),
ret)
# New - test=True
with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
with patch.dict(linux_acl.__opts__, {'test': True}):
comt = ('New permissions will be applied '
'for {0}: {1}'.format(acl_name, perms))
ret = {'name': name,
'comment': comt,
'changes': {},
'pchanges': {'new': {'acl_name': acl_name,
'acl_type': acl_type,
'perms': perms}},
'result': None}
self.assertDictEqual(linux_acl.present(name, acl_type,
acl_name, perms),
ret)
# New - test=False
with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = ('Applied new permissions for {0}'.format(acl_name))
ret = {'name': name,
'comment': comt,
'changes': {'new': {'acl_name': acl_name,
'acl_type': acl_type,
'perms': perms}},
'pchanges': {},
'result': True}
self.assertDictEqual(linux_acl.present(name, acl_type,
acl_name, perms),
ret)
# New - modfacl error
with patch.dict(linux_acl.__salt__, {'acl.modfacl': MagicMock(
side_effect=CommandExecutionError('Custom err'))}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = ('Error updating permissions for {0}: Custom err'
''.format(acl_name))
ret = {'name': name,
'comment': comt,
'changes': {},
'pchanges': {},
'result': False}
self.assertDictEqual(linux_acl.present(name, acl_type,
acl_name, perms),
ret)
# No acl type
2015-05-13 11:42:28 +00:00
comt = ('ACL Type does not exist')
ret = {'name': name, 'comment': comt, 'result': False,
'changes': {}, 'pchanges': {}}
2015-05-13 11:42:28 +00:00
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
perms), ret)
# 'absent' function tests: 1
def test_absent(self):
'''
Test to ensure a Linux ACL does not exist
'''
2016-09-08 19:46:41 +00:00
name = '/root'
2015-05-13 11:42:28 +00:00
acl_type = 'users'
acl_name = 'damian'
perms = 'rwx'
ret = {'name': name,
'result': None,
'comment': '',
'changes': {}}
mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name:
{'octal': 'A'}}]}},
{name: {acl_type: ''}}])
with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
with patch.dict(linux_acl.__opts__, {'test': True}):
comt = ('Removing permissions')
ret.update({'comment': comt})
self.assertDictEqual(linux_acl.absent(name, acl_type, acl_name,
perms), ret)
comt = ('ACL Type does not exist')
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(linux_acl.absent(name, acl_type, acl_name,
perms), ret)