2015-02-09 12:51:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
2015-02-09 12:51:03 +00:00
|
|
|
'''
|
2017-02-19 22:28:46 +00:00
|
|
|
# Import python libs
|
2018-01-17 23:05:08 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 22:28:46 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2016-03-03 21:54:19 +00:00
|
|
|
mock_open,
|
2015-02-09 12:51:03 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
Use explicit unicode strings + break up salt.utils
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.
2017-07-25 01:47:15 +00:00
|
|
|
import salt.utils.files
|
2015-02-09 12:51:03 +00:00
|
|
|
import salt.utils.fsutils
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.btrfs as btrfs
|
2015-02-09 12:51:03 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 22:28:46 +00:00
|
|
|
class BtrfsTestCase(TestCase, LoaderModuleMockMixin):
|
2015-02-09 12:51:03 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.btrfs
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {btrfs: {}}
|
2015-02-09 12:51:03 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
# 'version' function tests: 1
|
2015-02-09 12:51:03 +00:00
|
|
|
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.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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'})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'devices' function tests: 1
|
|
|
|
|
|
|
|
def test_devices(self):
|
|
|
|
'''
|
|
|
|
Test if it get known BTRFS formatted devices on the system.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'defragment' function tests: 2
|
|
|
|
|
|
|
|
def test_defragment(self):
|
|
|
|
'''
|
|
|
|
Test if it defragment mounted BTRFS filesystem.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
Use explicit unicode strings + break up salt.utils
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.
2017-07-25 01:47:15 +00:00
|
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertListEqual(btrfs.defragment('/dev/sda1'), ret)
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
def test_defragment_error(self):
|
|
|
|
'''
|
|
|
|
Test if it gives device not mount error
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
Use explicit unicode strings + break up salt.utils
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.
2017-07-25 01:47:15 +00:00
|
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertRaises(CommandExecutionError, btrfs.defragment,
|
|
|
|
'/dev/sda1')
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'features' function tests: 1
|
|
|
|
|
|
|
|
def test_features(self):
|
|
|
|
'''
|
|
|
|
Test if it list currently available BTRFS features.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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(), {})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'usage' function tests: 1
|
|
|
|
|
|
|
|
def test_usage(self):
|
|
|
|
'''
|
|
|
|
Test if it shows in which disk the chunks are allocated.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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'})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
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}})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'mkfs' function tests: 3
|
|
|
|
|
|
|
|
def test_mkfs(self):
|
|
|
|
'''
|
|
|
|
Test if it create a file system on the specified device.
|
|
|
|
'''
|
2016-03-03 21:54:19 +00:00
|
|
|
mock_cmd = MagicMock(return_value={'retcode': 1,
|
|
|
|
'stderr': '',
|
|
|
|
'stdout': 'Salt'})
|
2015-02-09 12:51:03 +00:00
|
|
|
mock_info = MagicMock(return_value=[])
|
2016-03-03 21:54:19 +00:00
|
|
|
with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_cmd,
|
2015-02-09 12:51:03 +00:00
|
|
|
'btrfs.info': mock_info}):
|
2016-03-03 21:54:19 +00:00
|
|
|
mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
|
Use explicit unicode strings + break up salt.utils
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.
2017-07-25 01:47:15 +00:00
|
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
2016-03-03 21:54:19 +00:00
|
|
|
self.assertDictEqual(btrfs.mkfs('/dev/sda1'), {'log': 'Salt'})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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'})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
def test_resize_valid_error(self):
|
|
|
|
'''
|
|
|
|
Test if it gives device should be mounted error
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
def test_resize_mount_error(self):
|
|
|
|
'''
|
|
|
|
Test if it gives mount point error
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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)
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
def test_convert_error(self):
|
|
|
|
'''
|
|
|
|
Test if it gives error cannot convert root
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
def test_convert_migration_error(self):
|
|
|
|
'''
|
|
|
|
Test if it gives migration error
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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')
|
Use explicit unicode strings + break up salt.utils
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.
2017-07-25 01:47:15 +00:00
|
|
|
with patch.object(salt.utils.files, 'fopen', mock_file):
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertRaises(CommandExecutionError, btrfs.convert,
|
|
|
|
'/dev/sda1')
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'add' function tests: 1
|
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
'''
|
|
|
|
Test if it add a devices to a BTRFS filesystem.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.btrfs._restripe', MagicMock(return_value={})):
|
|
|
|
self.assertDictEqual(btrfs.add('/mountpoint', '/dev/sda1', '/dev/sda2'), {})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'delete' function tests: 1
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
'''
|
|
|
|
Test if it delete a devices to a BTRFS filesystem.
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.btrfs._restripe', MagicMock(return_value={})):
|
|
|
|
self.assertDictEqual(btrfs.delete('/mountpoint', '/dev/sda1',
|
|
|
|
'/dev/sda2'), {})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
# 'properties' function tests: 1
|
|
|
|
|
|
|
|
def test_properties(self):
|
|
|
|
'''
|
|
|
|
Test if list properties for given btrfs object
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
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'), {})
|
2015-02-09 12:51:03 +00:00
|
|
|
|
|
|
|
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)
|