2014-12-12 11:50:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
2015-01-08 02:28:37 +00:00
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-12-12 11:50:31 +00:00
|
|
|
# Import Salt Testing libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import MagicMock, patch
|
2014-12-12 11:50:31 +00:00
|
|
|
|
2016-08-10 22:32:14 +00:00
|
|
|
# Import Salt libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.disk as disk
|
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.path
|
2016-08-10 22:32:14 +00:00
|
|
|
|
2014-12-12 11:50:31 +00:00
|
|
|
STUB_DISK_USAGE = {
|
2014-12-18 17:15:58 +00:00
|
|
|
'/': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
|
|
|
|
'/dev': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
|
|
|
|
'/run': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
|
2014-12-18 17:33:31 +00:00
|
|
|
'/run/lock': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
|
2014-12-18 17:15:58 +00:00
|
|
|
'/run/shm': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
|
|
|
|
'/run/user': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
|
|
|
|
'/sys/fs/cgroup': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000}
|
2014-12-12 11:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STUB_DISK_INODEUSAGE = {
|
2014-12-18 17:33:31 +00:00
|
|
|
'/': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
|
2014-12-18 17:15:58 +00:00
|
|
|
'/dev': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
|
|
|
|
'/run': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
|
|
|
|
'/run/lock': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
|
|
|
|
'/run/shm': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
|
|
|
|
'/run/user': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
|
|
|
|
'/sys/fs/cgroup': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None}
|
2014-12-12 11:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STUB_DISK_PERCENT = {
|
2014-12-18 17:15:58 +00:00
|
|
|
'/': 50,
|
|
|
|
'/dev': 10,
|
|
|
|
'/run': 10,
|
|
|
|
'/run/lock': 10,
|
|
|
|
'/run/shm': 10,
|
|
|
|
'/run/user': 10,
|
|
|
|
'/sys/fs/cgroup': 10
|
2014-12-12 11:50:31 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 17:33:31 +00:00
|
|
|
STUB_DISK_BLKID = {'/dev/sda': {'TYPE': 'ext4', 'UUID': None}}
|
2014-12-12 11:50:31 +00:00
|
|
|
|
|
|
|
|
2017-02-19 15:35:30 +00:00
|
|
|
class DiskTestCase(TestCase, LoaderModuleMockMixin):
|
2014-12-12 11:50:31 +00:00
|
|
|
'''
|
|
|
|
TestCase for salt.modules.disk module
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {disk: {}}
|
2014-12-12 11:50:31 +00:00
|
|
|
|
|
|
|
def test_usage_dict(self):
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
|
|
|
|
patch('salt.modules.disk.usage',
|
|
|
|
MagicMock(return_value=STUB_DISK_USAGE)):
|
2014-12-12 11:50:31 +00:00
|
|
|
mock_cmd = MagicMock(return_value=1)
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run': mock_cmd}):
|
|
|
|
self.assertDictEqual(STUB_DISK_USAGE, disk.usage(args=None))
|
2014-12-18 17:33:31 +00:00
|
|
|
|
2014-12-12 11:50:31 +00:00
|
|
|
def test_usage_none(self):
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
|
|
|
|
patch('salt.modules.disk.usage', MagicMock(return_value='')):
|
2014-12-12 11:50:31 +00:00
|
|
|
mock_cmd = MagicMock(return_value=1)
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run': mock_cmd}):
|
|
|
|
self.assertEqual('', disk.usage(args=None))
|
|
|
|
|
|
|
|
def test_inodeusage(self):
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(disk.__grains__, {'kernel': 'OpenBSD'}), \
|
|
|
|
patch('salt.modules.disk.inodeusage',
|
|
|
|
MagicMock(return_value=STUB_DISK_INODEUSAGE)):
|
2014-12-12 11:50:31 +00:00
|
|
|
mock = MagicMock()
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertDictEqual(STUB_DISK_INODEUSAGE, disk.inodeusage(args=None))
|
|
|
|
|
|
|
|
def test_percent(self):
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
|
|
|
|
patch('salt.modules.disk.percent',
|
|
|
|
MagicMock(return_value=STUB_DISK_PERCENT)):
|
2014-12-12 11:50:31 +00:00
|
|
|
mock = MagicMock()
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertDictEqual(STUB_DISK_PERCENT, disk.percent(args=None))
|
|
|
|
|
|
|
|
def test_percent_args(self):
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
|
|
|
|
patch('salt.modules.disk.percent', MagicMock(return_value='/')):
|
2014-12-12 11:50:31 +00:00
|
|
|
mock = MagicMock()
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertEqual('/', disk.percent('/'))
|
|
|
|
|
|
|
|
def test_blkid(self):
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(disk.__salt__, {'cmd.run_stdout': MagicMock(return_value=1)}), \
|
|
|
|
patch('salt.modules.disk.blkid', MagicMock(return_value=STUB_DISK_BLKID)):
|
2014-12-18 17:33:31 +00:00
|
|
|
self.assertDictEqual(STUB_DISK_BLKID, disk.blkid())
|
2014-12-12 11:50:31 +00:00
|
|
|
|
2015-06-23 14:52:37 +00:00
|
|
|
def test_dump(self):
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run_all': mock}):
|
|
|
|
disk.dump('/dev/sda')
|
|
|
|
mock.assert_called_once_with(
|
|
|
|
'blockdev --getro --getsz --getss --getpbsz --getiomin '
|
|
|
|
'--getioopt --getalignoff --getmaxsect --getsize '
|
|
|
|
'--getsize64 --getra --getfra /dev/sda',
|
|
|
|
python_shell=False
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_wipe(self):
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run_all': mock}):
|
|
|
|
disk.wipe('/dev/sda')
|
|
|
|
mock.assert_called_once_with(
|
2016-08-06 21:57:20 +00:00
|
|
|
'wipefs -a /dev/sda',
|
2015-06-23 14:52:37 +00:00
|
|
|
python_shell=False
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tune(self):
|
|
|
|
mock = MagicMock(return_value='712971264\n512\n512\n512\n0\n0\n88\n712971264\n365041287168\n512\n512')
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run': mock}):
|
|
|
|
mock_dump = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
with patch('salt.modules.disk.dump', mock_dump):
|
2017-06-21 11:48:59 +00:00
|
|
|
kwargs = {'read-ahead': 512, 'filesystem-read-ahead': 1024}
|
2015-06-23 14:52:37 +00:00
|
|
|
disk.tune('/dev/sda', **kwargs)
|
2017-06-21 11:48:59 +00:00
|
|
|
|
2017-07-03 14:30:12 +00:00
|
|
|
self.assert_called_once(mock)
|
2017-06-21 11:48:59 +00:00
|
|
|
|
|
|
|
args, kwargs = mock.call_args
|
|
|
|
|
|
|
|
# Assert called once with either 'blockdev --setra 512 --setfra 512 /dev/sda' or
|
|
|
|
# 'blockdev --setfra 512 --setra 512 /dev/sda' and python_shell=False kwarg.
|
|
|
|
self.assertEqual(len(args), 1)
|
|
|
|
self.assertTrue(args[0].startswith('blockdev '))
|
|
|
|
self.assertTrue(args[0].endswith(' /dev/sda'))
|
|
|
|
self.assertIn(' --setra 512 ', args[0])
|
|
|
|
self.assertIn(' --setfra 1024 ', args[0])
|
|
|
|
self.assertEqual(len(args[0].split()), 6)
|
|
|
|
self.assertEqual(kwargs, {'python_shell': False})
|
2015-06-23 14:52:37 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.path.which('sync'), 'sync not found')
|
|
|
|
@skipIf(not salt.utils.path.which('mkfs'), 'mkfs not found')
|
2016-08-10 22:32:14 +00:00
|
|
|
def test_format(self):
|
|
|
|
'''
|
|
|
|
unit tests for disk.format
|
|
|
|
'''
|
|
|
|
device = '/dev/sdX1'
|
|
|
|
mock = MagicMock(return_value=0)
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.retcode': mock}):
|
|
|
|
self.assertEqual(disk.format_(device), True)
|
|
|
|
|
2017-10-14 01:52:56 +00:00
|
|
|
@skipIf(not salt.utils.path.which('lsblk') and not salt.utils.path.which('df'),
|
2017-09-26 21:52:04 +00:00
|
|
|
'lsblk or df not found')
|
2016-08-10 22:32:14 +00:00
|
|
|
def test_fstype(self):
|
|
|
|
'''
|
|
|
|
unit tests for disk.fstype
|
|
|
|
'''
|
|
|
|
device = '/dev/sdX1'
|
|
|
|
fs_type = 'ext4'
|
|
|
|
mock = MagicMock(return_value='FSTYPE\n{0}'.format(fs_type))
|
2017-04-13 16:04:20 +00:00
|
|
|
with patch.dict(disk.__grains__, {'kernel': 'Linux'}):
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run': mock}):
|
|
|
|
self.assertEqual(disk.fstype(device), fs_type)
|
2016-09-16 19:25:08 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.path.which('resize2fs'), 'resize2fs not found')
|
2016-09-15 20:37:53 +00:00
|
|
|
def test_resize2fs(self):
|
|
|
|
'''
|
|
|
|
unit tests for disk.resize2fs
|
|
|
|
'''
|
|
|
|
device = '/dev/sdX1'
|
|
|
|
mock = MagicMock()
|
|
|
|
with patch.dict(disk.__salt__, {'cmd.run_all': mock}):
|
|
|
|
disk.resize2fs(device)
|
|
|
|
mock.assert_called_once_with('resize2fs {0}'.format(device), python_shell=False)
|