salt/tests/unit/modules/mac_xattr_test.py

132 lines
4.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import TestCase
from salttesting.helpers import ensure_in_syspath
2016-03-25 21:27:28 +00:00
from salttesting.mock import MagicMock, patch
ensure_in_syspath('../../')
2016-03-24 20:37:32 +00:00
# Import Salt Libs
from salt.exceptions import CommandExecutionError
from salt.modules import mac_xattr as xattr
import salt.utils.mac_utils
xattr.__salt__ = {}
class XAttrTestCase(TestCase):
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_result',
MagicMock(return_value='spongebob\nsquidward'))
def test_list(self):
'''
2016-03-24 20:37:32 +00:00
Test xattr.list
'''
2016-03-24 20:37:32 +00:00
expected = {'spongebob': 'squarepants',
'squidward': 'patrick'}
2016-03-25 21:27:28 +00:00
with patch.object(xattr, 'read', MagicMock(side_effect=['squarepants',
'patrick'])):
2016-03-24 20:37:32 +00:00
self.assertEqual(xattr.list('path/to/file'), expected)
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_result',
MagicMock(side_effect=CommandExecutionError('No such file')))
def test_list_missing(self):
'''
2016-03-24 20:37:32 +00:00
Test listing attributes of a missing file
'''
2016-03-24 20:37:32 +00:00
self.assertRaises(CommandExecutionError, xattr.list, '/path/to/file')
2016-01-29 14:29:06 +00:00
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_result',
MagicMock(return_value='expected results'))
def test_read(self):
'''
2016-03-24 20:37:32 +00:00
Test reading a specific attribute from a file
'''
2016-03-24 20:37:32 +00:00
self.assertEqual(xattr.read('/path/to/file', 'com.attr'),
'expected results')
2016-01-29 14:29:06 +00:00
2016-03-24 20:37:32 +00:00
def test_read_hex(self):
'''
2016-03-24 20:37:32 +00:00
Test reading a specific attribute from a file
'''
2016-03-24 20:37:32 +00:00
with patch.object(salt.utils.mac_utils, 'execute_return_result',
MagicMock(return_value='expected results')) as mock:
self.assertEqual(xattr.read('/path/to/file', 'com.attr', True),
'expected results')
mock.assert_called_once_with('xattr -p -x "com.attr" "/path/to/file"')
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_result',
MagicMock(side_effect=CommandExecutionError('No such file')))
def test_read_missing(self):
'''
2016-03-24 20:37:32 +00:00
Test reading a specific attribute from a file
'''
2016-03-24 20:37:32 +00:00
self.assertRaises(CommandExecutionError,
xattr.read,
'/path/to/file',
'attribute')
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_success',
MagicMock(return_value=True))
def test_write(self):
'''
2016-03-24 20:37:32 +00:00
Test writing a specific attribute to a file
'''
2016-03-24 20:37:32 +00:00
self.assertTrue(xattr.write('/path/to/file', 'attribute', 'value'))
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_success',
MagicMock(side_effect=CommandExecutionError('No such file')))
def test_write_missing(self):
'''
Test writing a specific attribute to a file
'''
self.assertRaises(CommandExecutionError,
xattr.write,
'/path/to/file',
'attribute',
'value')
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_success',
MagicMock(return_value=True))
def test_delete(self):
'''
2016-03-24 20:37:32 +00:00
Test deleting a specific attribute from a file
'''
2016-03-24 20:37:32 +00:00
self.assertTrue(xattr.delete('/path/to/file', 'attribute'))
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_success',
MagicMock(side_effect=CommandExecutionError('No such file')))
def test_delete_missing(self):
'''
Test deleting a specific attribute from a file
'''
self.assertRaises(CommandExecutionError,
xattr.delete,
'/path/to/file',
'attribute')
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_success',
MagicMock(return_value=True))
def test_clear(self):
'''
2016-03-24 20:37:32 +00:00
Test clearing all attributes on a file
'''
2016-03-24 20:37:32 +00:00
self.assertTrue(xattr.clear('/path/to/file'))
2016-03-24 20:37:32 +00:00
@patch('salt.utils.mac_utils.execute_return_success',
MagicMock(side_effect=CommandExecutionError('No such file')))
def test_clear_missing(self):
'''
Test clearing all attributes on a file
'''
self.assertRaises(CommandExecutionError, xattr.clear, '/path/to/file')
if __name__ == '__main__':
from integration import run_tests
run_tests(XAttrTestCase, needs_daemon=False)