mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 00:55:19 +00:00
Merge pull request #50188 from garethgreenaway/49965_beacon_diskusage_windows
[2018.3] fixes to diskusage beacon
This commit is contained in:
commit
01b083e20b
@ -12,6 +12,8 @@ from __future__ import absolute_import, unicode_literals
|
||||
import logging
|
||||
import re
|
||||
|
||||
import salt.utils.platform
|
||||
|
||||
# Import Third Party Libs
|
||||
try:
|
||||
import psutil
|
||||
@ -94,6 +96,9 @@ def beacon(config):
|
||||
if not mount.endswith('$'):
|
||||
mount_re = '{0}$'.format(mount)
|
||||
|
||||
if salt.utils.platform.is_windows():
|
||||
mount_re = re.sub('\\$', '\\\\', mount_re)
|
||||
|
||||
for part in parts:
|
||||
if re.match(mount_re, part.mountpoint):
|
||||
_mount = part.mountpoint
|
||||
@ -106,7 +111,6 @@ def beacon(config):
|
||||
|
||||
current_usage = _current_usage.percent
|
||||
monitor_usage = mounts[mount]
|
||||
log.debug('current_usage %s', current_usage)
|
||||
if '%' in monitor_usage:
|
||||
monitor_usage = re.sub('%', '', monitor_usage)
|
||||
monitor_usage = float(monitor_usage)
|
||||
|
@ -23,11 +23,30 @@ STUB_DISK_PARTITION = [
|
||||
'device mountpoint fstype, opts')(
|
||||
'/dev/disk0s2', '/', 'hfs',
|
||||
'rw,local,rootfs,dovolfs,journaled,multilabel')]
|
||||
|
||||
WINDOWS_STUB_DISK_PARTITION = [
|
||||
namedtuple(
|
||||
'partition',
|
||||
'device mountpoint fstype, opts')(
|
||||
'C:\\', 'C:\\', 'NTFS',
|
||||
'rw,fixed'),
|
||||
namedtuple(
|
||||
'partition',
|
||||
'device mountpoint fstype, opts')(
|
||||
'D:\\', 'D:\\', 'CDFS',
|
||||
'ro,cdrom')]
|
||||
|
||||
STUB_DISK_USAGE = [namedtuple('usage',
|
||||
'total used free percent')(1000, 500, 500, 50),
|
||||
namedtuple('usage',
|
||||
'total used free percent')(100, 75, 25, 25)]
|
||||
|
||||
WINDOWS_STUB_DISK_USAGE = namedtuple('usage',
|
||||
'total used free percent')(1000,
|
||||
500,
|
||||
500,
|
||||
50)
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
||||
@ -94,3 +113,37 @@ class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
||||
|
||||
ret = diskusage.beacon(config)
|
||||
self.assertEqual(ret, [{'diskusage': 50, 'mount': '/'}])
|
||||
|
||||
def test_diskusage_windows(self):
|
||||
disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
|
||||
with patch('salt.utils.platform.is_windows',
|
||||
MagicMock(return_value=True)):
|
||||
with patch('psutil.disk_partitions',
|
||||
MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION)), \
|
||||
patch('psutil.disk_usage', disk_usage_mock):
|
||||
config = [{'C:\\': '50%'}]
|
||||
|
||||
ret = diskusage.validate(config)
|
||||
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
ret = diskusage.beacon(config)
|
||||
self.assertEqual(ret, [{'diskusage': 50, 'mount': 'C:\\'}])
|
||||
|
||||
def test_diskusage_windows_match_regex(self):
|
||||
disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
|
||||
with patch('salt.utils.platform.is_windows',
|
||||
MagicMock(return_value=True)):
|
||||
with patch('psutil.disk_partitions',
|
||||
MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION)), \
|
||||
patch('psutil.disk_usage', disk_usage_mock):
|
||||
config = [{'^[a-zA-Z]:\\': '50%'}]
|
||||
|
||||
ret = diskusage.validate(config)
|
||||
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
ret = diskusage.beacon(config)
|
||||
_expected = [{u'diskusage': 50, u'mount': 'C:\\'},
|
||||
{u'diskusage': 50, u'mount': 'D:\\'}]
|
||||
self.assertEqual(ret, _expected)
|
||||
|
Loading…
Reference in New Issue
Block a user