123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- # -*- coding: utf-8 -*-
- '''
- :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
- '''
- # Import Python libs
- from __future__ import absolute_import, unicode_literals, print_function
- import sys
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.unit import skipIf, TestCase
- from tests.support.mock import (
- MagicMock,
- patch)
- # Import Salt Libs
- import salt.states.linux_acl as linux_acl
- from salt.exceptions import CommandExecutionError
- @skipIf(not sys.platform.startswith('linux'), 'Test for Linux only')
- class LinuxAclTestCase(TestCase, LoaderModuleMockMixin):
- '''
- Test cases for salt.states.linux_acl
- '''
- def setup_loader_modules(self):
- return {linux_acl: {}}
- # 'present' function tests: 1
- def test_present(self):
- '''
- Test to ensure a Linux ACL is present
- '''
- self.maxDiff = None
- name = '/root'
- acl_type = 'users'
- acl_name = 'damian'
- perms = 'rwx'
- mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name:
- {'octal': 5}}]}},
- {name: {acl_type: [{acl_name:
- {'octal': 5}}]}},
- {name: {acl_type: [{acl_name:
- {'octal': 5}}]}},
- {name: {acl_type: [{}]}},
- {name: {acl_type: [{}]}},
- {name: {acl_type: [{}]}},
- {
- name: {acl_type: [{acl_name: {'octal': 7}}]},
- name+"/foo": {acl_type: [{acl_name: {'octal': 5}}]}
- },
- {
- name: {acl_type: [{acl_name: {'octal': 7}}]},
- name+"/foo": {acl_type: [{acl_name: {'octal': 7}}]}
- },
- {name: {acl_type: ''}}])
- mock_modfacl = MagicMock(return_value=True)
- with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
- # Update - test=True
- with patch.dict(linux_acl.__opts__, {'test': True}):
- comt = ('Updated permissions will be applied for {0}: r-x -> {1}'
- ''.format(acl_name, perms))
- 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': 'r-x'}},
- 'result': None}
- 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': 'r-x'}},
- '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': {},
- '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': {'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}},
- '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': {},
- 'result': False}
- self.assertDictEqual(linux_acl.present(name, acl_type,
- acl_name, perms),
- ret)
- # New - recurse true
- with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
- # Update - test=True
- with patch.dict(linux_acl.__opts__, {'test': True}):
- comt = ('Updated permissions will be applied for {0}: rwx -> {1}'
- ''.format(acl_name, perms))
- 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': 'rwx'}},
- 'result': None}
- self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
- perms, recurse=False), ret)
- # New - recurse true - nothing to do
- with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
- # Update - test=True
- with patch.dict(linux_acl.__opts__, {'test': True}):
- comt = ('Permissions are in the desired state')
- ret = {'name': name,
- 'comment': comt,
- 'changes': {},
- 'result': True}
- self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
- perms, recurse=True), ret)
- # No acl type
- comt = ('ACL Type does not exist')
- ret = {'name': name, 'comment': comt, 'result': False, 'changes': {}}
- self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
- perms), ret)
- # 'absent' function tests: 2
- def test_absent(self):
- '''
- Test to ensure a Linux ACL does not exist
- '''
- name = '/root'
- 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)
- # 'list_present' function tests: 1
- def test_list_present(self):
- '''
- Test to ensure a Linux ACL is present
- '''
- self.maxDiff = None
- name = '/root'
- acl_type = 'user'
- acl_names = ['root', 'damian', 'homer']
- acl_comment = {u'owner': u'root',
- u'group': u'root',
- u'file': u'/root'}
- perms = 'rwx'
- mock = MagicMock(side_effect=[{name: {acl_type: [{acl_names[0]:
- {'octal': 'A'}},
- {acl_names[1]:
- {'octal': 'A'}},
- {acl_names[2]:
- {'octal': 'A'}}],
- 'comment': acl_comment}},
- {name: {acl_type: [{acl_names[0]:
- {'octal': 'A'}},
- {acl_names[1]:
- {'octal': 'A'}}],
- 'comment': acl_comment}},
- {name: {acl_type: [{acl_names[0]:
- {'octal': 'A'}},
- {acl_names[1]:
- {'octal': 'A'}}]}},
- {name: {acl_type: [{}]}},
- {name: {acl_type: [{}]}},
- {name: {acl_type: [{}]}},
- {name: {acl_type: ''}}])
- mock_modfacl = MagicMock(return_value=True)
- with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
- # Update - test=True
- with patch.dict(linux_acl.__opts__, {'test': True}):
- comt = ('Updated permissions will be applied for {0}: A -> {1}'
- ''.format(acl_names, perms))
- expected = {'name': name,
- 'comment': comt,
- 'changes': {},
- 'pchanges': {'new': {'acl_name': ', '.join(acl_names),
- 'acl_type': acl_type,
- 'perms': 7},
- 'old': {'acl_name': ', '.join(acl_names),
- 'acl_type': acl_type,
- 'perms': 'A'}},
- 'result': None}
- ret = linux_acl.list_present(name, acl_type, acl_names, perms)
- self.assertDictEqual(ret, expected)
- # Update - 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(', '.join(acl_names)))
- expected = {'name': name,
- 'comment': comt,
- 'changes': {'new': {'acl_name': ', '.join(acl_names),
- 'acl_type': acl_type,
- 'perms': 'rwx'}},
- 'pchanges': {},
- 'result': True}
- ret = linux_acl.list_present(name, acl_type, acl_names, perms)
- self.assertDictEqual(expected, 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_names))
- expected = {'name': name,
- 'comment': comt,
- 'changes': {},
- 'pchanges': {},
- 'result': False}
- ret = linux_acl.list_present(name, acl_type, acl_names, perms)
- self.assertDictEqual(expected, 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_names, perms))
- expected = {'name': name,
- 'comment': comt,
- 'changes': {},
- 'pchanges': {'new': {'acl_name': ', '.join(acl_names),
- 'acl_type': acl_type,
- 'perms': perms}},
- 'result': None}
- ret = linux_acl.list_present(name, acl_type, acl_names, perms)
- self.assertDictEqual(expected, 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(', '.join(acl_names)))
- expected = {'name': name,
- 'comment': comt,
- 'changes': {'new': {'acl_name': ', '.join(acl_names),
- 'acl_type': acl_type,
- 'perms': perms}},
- 'pchanges': {},
- 'result': True}
- ret = linux_acl.list_present(name, acl_type, acl_names, perms)
- self.assertDictEqual(expected, 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_names))
- expected = {'name': name,
- 'comment': comt,
- 'changes': {},
- 'pchanges': {},
- 'result': False}
- ret = linux_acl.list_present(name, acl_type, acl_names, perms)
- self.assertDictEqual(expected, ret)
- # No acl type
- comt = ('ACL Type does not exist')
- expected = {'name': name, 'comment': comt, 'result': False,
- 'changes': {}, 'pchanges': {}}
- ret = linux_acl.list_present(name, acl_type, acl_names, perms)
- self.assertDictEqual(expected, ret)
- # 'list_absent' function tests: 2
- def test_list_absent(self):
- '''
- Test to ensure a Linux ACL does not exist
- '''
- name = '/root'
- acl_type = 'users'
- acl_names = ['damian', 'homer']
- perms = 'rwx'
- ret = {'name': name,
- 'result': None,
- 'comment': '',
- 'changes': {}}
- mock = MagicMock(side_effect=[{name: {acl_type: [{acl_names[0]: {'octal': 'A'},
- acl_names[1]: {'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.list_absent(name, acl_type, acl_names, perms), ret)
- comt = ('ACL Type does not exist')
- ret.update({'comment': comt, 'result': False})
- self.assertDictEqual(linux_acl.list_absent(name, acl_type, acl_names), ret)
- def test_absent_recursive(self):
- '''
- Test to ensure a Linux ACL does not exist
- '''
- name = '/root'
- 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': 7}}]}, name+"/foo": {acl_type: [{acl_name: {'octal': 'A'}}]}}
- ])
- 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, recurse=True), ret)
|