mirror of
https://github.com/valitydev/salt.git
synced 2024-11-09 01:36:48 +00:00
3184168365
This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
365 lines
16 KiB
Python
365 lines
16 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
'''
|
|
# Import python libs
|
|
from __future__ import absolute_import
|
|
|
|
# Import Salt Testing Libs
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
|
from tests.support.unit import TestCase, skipIf
|
|
from tests.support.mock import (
|
|
mock_open,
|
|
MagicMock,
|
|
patch,
|
|
NO_MOCK,
|
|
NO_MOCK_REASON
|
|
)
|
|
|
|
# Import Salt Libs
|
|
import salt.utils.files
|
|
import salt.utils.fsutils
|
|
import salt.modules.btrfs as btrfs
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
class BtrfsTestCase(TestCase, LoaderModuleMockMixin):
|
|
'''
|
|
Test cases for salt.modules.btrfs
|
|
'''
|
|
def setup_loader_modules(self):
|
|
return {btrfs: {}}
|
|
|
|
# 'version' function tests: 1
|
|
def test_version(self):
|
|
'''
|
|
Test if it return BTRFS version.
|
|
'''
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
self.assertDictEqual(btrfs.version(), {'version': 'Salt'})
|
|
|
|
# 'info' function tests: 1
|
|
|
|
def test_info(self):
|
|
'''
|
|
Test if it get BTRFS filesystem information.
|
|
'''
|
|
with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'Salt': 'salt'})
|
|
with patch.object(btrfs, '_parse_btrfs_info', mock):
|
|
self.assertDictEqual(btrfs.info('/dev/sda1'),
|
|
{'Salt': 'salt'})
|
|
|
|
# 'devices' function tests: 1
|
|
|
|
def test_devices(self):
|
|
'''
|
|
Test if it get known BTRFS formatted devices on the system.
|
|
'''
|
|
with patch('salt.utils.fsutils._blkid_output',
|
|
MagicMock(return_value='Salt')):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
self.assertEqual(btrfs.devices(), 'Salt')
|
|
|
|
# 'defragment' function tests: 2
|
|
|
|
def test_defragment(self):
|
|
'''
|
|
Test if it defragment mounted BTRFS filesystem.
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=False)):
|
|
with patch('os.path.exists', MagicMock(return_value=True)):
|
|
ret = [{'range': '/dev/sda1',
|
|
'mount_point': False,
|
|
'log': False, 'passed': True}]
|
|
mock_run = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_run}):
|
|
mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
|
self.assertListEqual(btrfs.defragment('/dev/sda1'), ret)
|
|
|
|
def test_defragment_error(self):
|
|
'''
|
|
Test if it gives device not mount error
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
|
|
mock_run = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_run}):
|
|
mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
|
self.assertRaises(CommandExecutionError, btrfs.defragment,
|
|
'/dev/sda1')
|
|
|
|
# 'features' function tests: 1
|
|
|
|
def test_features(self):
|
|
'''
|
|
Test if it list currently available BTRFS features.
|
|
'''
|
|
with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
self.assertDictEqual(btrfs.features(), {})
|
|
|
|
# 'usage' function tests: 1
|
|
|
|
def test_usage(self):
|
|
'''
|
|
Test if it shows in which disk the chunks are allocated.
|
|
'''
|
|
with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'Salt': 'salt'})
|
|
with patch.object(btrfs, '_usage_specific', mock):
|
|
self.assertDictEqual(btrfs.usage('/dev/sda1'),
|
|
{'Salt': 'salt'})
|
|
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Unallocated:\n'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'/dev/sda1': True})
|
|
with patch.object(btrfs, '_usage_unallocated', mock):
|
|
self.assertDictEqual(btrfs.usage('/dev/sda1'),
|
|
{'unallocated': {'/dev/sda1': True}})
|
|
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Overall:\n'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'/dev/sda1': True})
|
|
with patch.object(btrfs, '_usage_overall', mock):
|
|
self.assertDictEqual(btrfs.usage('/dev/sda1'),
|
|
{'overall': {'/dev/sda1': True}})
|
|
|
|
# 'mkfs' function tests: 3
|
|
|
|
def test_mkfs(self):
|
|
'''
|
|
Test if it create a file system on the specified device.
|
|
'''
|
|
mock_cmd = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
mock_info = MagicMock(return_value=[])
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_cmd,
|
|
'btrfs.info': mock_info}):
|
|
mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
|
self.assertDictEqual(btrfs.mkfs('/dev/sda1'), {'log': 'Salt'})
|
|
|
|
def test_mkfs_error(self):
|
|
'''
|
|
Test if it No devices specified error
|
|
'''
|
|
self.assertRaises(CommandExecutionError, btrfs.mkfs)
|
|
|
|
def test_mkfs_mount_error(self):
|
|
'''
|
|
Test if it device mount error
|
|
'''
|
|
mock = MagicMock(return_value={'/dev/sda1': True})
|
|
with patch.object(salt.utils.fsutils, '_get_mounts', mock):
|
|
self.assertRaises(CommandExecutionError, btrfs.mkfs, '/dev/sda1')
|
|
|
|
# 'resize' function tests: 4
|
|
|
|
def test_resize(self):
|
|
'''
|
|
Test if it resize filesystem.
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
mock_info = MagicMock(return_value=[])
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock,
|
|
'btrfs.info': mock_info}):
|
|
mock = MagicMock(return_value={'/dev/sda1': True})
|
|
with patch.object(salt.utils.fsutils, '_get_mounts', mock):
|
|
self.assertDictEqual(btrfs.resize('/dev/sda1', 'max'),
|
|
{'log': 'Salt'})
|
|
|
|
def test_resize_valid_error(self):
|
|
'''
|
|
Test if it gives device should be mounted error
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=False)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
self.assertRaises(CommandExecutionError, btrfs.resize,
|
|
'/dev/sda1', 'max')
|
|
|
|
def test_resize_mount_error(self):
|
|
'''
|
|
Test if it gives mount point error
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'/dev/sda1': False})
|
|
with patch.object(salt.utils.fsutils, '_get_mounts', mock):
|
|
self.assertRaises(CommandExecutionError, btrfs.resize,
|
|
'/dev/sda1', 'max')
|
|
|
|
def test_resize_size_error(self):
|
|
'''
|
|
Test if it gives unknown size error
|
|
'''
|
|
self.assertRaises(CommandExecutionError, btrfs.resize,
|
|
'/dev/sda1', '250m')
|
|
|
|
# 'convert' function tests: 5
|
|
|
|
def test_convert(self):
|
|
'''
|
|
Test if it convert ext2/3/4 to BTRFS
|
|
'''
|
|
with patch('os.path.exists', MagicMock(return_value=True)):
|
|
ret = {'after': {'balance_log': 'Salt',
|
|
'ext4_image': 'removed',
|
|
'ext4_image_info': 'N/A',
|
|
'fsck_status': 'N/A',
|
|
'mount_point': None,
|
|
'type': 'ext4'},
|
|
'before': {'fsck_status': 'Filesystem errors corrected',
|
|
'mount_point': None,
|
|
'type': 'ext4'}}
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'/dev/sda3': {'type': 'ext4'}})
|
|
with patch.object(salt.utils.fsutils, '_blkid_output', mock):
|
|
mock = MagicMock(return_value={'/dev/sda3': [{'mount_point': None}]})
|
|
with patch.object(salt.utils.fsutils, '_get_mounts', mock):
|
|
self.assertDictEqual(btrfs.convert('/dev/sda3', permanent=True),
|
|
ret)
|
|
|
|
def test_convert_device_error(self):
|
|
'''
|
|
Test if it gives device not found error
|
|
'''
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'/dev/sda1': False})
|
|
with patch.object(salt.utils.fsutils, '_blkid_output', mock):
|
|
self.assertRaises(CommandExecutionError, btrfs.convert,
|
|
'/dev/sda1')
|
|
|
|
def test_convert_filesystem_error(self):
|
|
'''
|
|
Test if it gives file system error
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'/dev/sda1': {'type': 'ext'}})
|
|
with patch.object(salt.utils.fsutils, '_blkid_output', mock):
|
|
self.assertRaises(CommandExecutionError, btrfs.convert,
|
|
'/dev/sda1')
|
|
|
|
def test_convert_error(self):
|
|
'''
|
|
Test if it gives error cannot convert root
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
mock = MagicMock(return_value={'/dev/sda1': {'type': 'ext4',
|
|
'mount_point': '/'}})
|
|
with patch.object(salt.utils.fsutils, '_blkid_output', mock):
|
|
mock = MagicMock(return_value={'/dev/sda1':
|
|
[{'mount_point': '/'}]})
|
|
with patch.object(salt.utils.fsutils, '_get_mounts', mock):
|
|
self.assertRaises(CommandExecutionError, btrfs.convert,
|
|
'/dev/sda1')
|
|
|
|
def test_convert_migration_error(self):
|
|
'''
|
|
Test if it gives migration error
|
|
'''
|
|
with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
|
|
mock_run = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_run}):
|
|
mock_blk = MagicMock(return_value={'/dev/sda1': {'type': 'ext4'}})
|
|
with patch.object(salt.utils.fsutils, '_blkid_output', mock_blk):
|
|
mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
|
self.assertRaises(CommandExecutionError, btrfs.convert,
|
|
'/dev/sda1')
|
|
|
|
# 'add' function tests: 1
|
|
|
|
def test_add(self):
|
|
'''
|
|
Test if it add a devices to a BTRFS filesystem.
|
|
'''
|
|
with patch('salt.modules.btrfs._restripe', MagicMock(return_value={})):
|
|
self.assertDictEqual(btrfs.add('/mountpoint', '/dev/sda1', '/dev/sda2'), {})
|
|
|
|
# 'delete' function tests: 1
|
|
|
|
def test_delete(self):
|
|
'''
|
|
Test if it delete a devices to a BTRFS filesystem.
|
|
'''
|
|
with patch('salt.modules.btrfs._restripe', MagicMock(return_value={})):
|
|
self.assertDictEqual(btrfs.delete('/mountpoint', '/dev/sda1',
|
|
'/dev/sda2'), {})
|
|
|
|
# 'properties' function tests: 1
|
|
|
|
def test_properties(self):
|
|
'''
|
|
Test if list properties for given btrfs object
|
|
'''
|
|
with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
|
|
mock = MagicMock(return_value={'retcode': 1,
|
|
'stderr': '',
|
|
'stdout': 'Salt'})
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
|
|
self.assertDictEqual(btrfs.properties('/dev/sda1', 'subvol'), {})
|
|
|
|
def test_properties_unknown_error(self):
|
|
'''
|
|
Test if it gives unknown property error
|
|
'''
|
|
self.assertRaises(CommandExecutionError, btrfs.properties,
|
|
'/dev/sda1', 'a')
|
|
|
|
def test_properties_error(self):
|
|
'''
|
|
Test if it gives exception error
|
|
'''
|
|
self.assertRaises(CommandExecutionError, btrfs.properties,
|
|
'/dev/sda1', 'subvol', True)
|