Merge pull request #50188 from garethgreenaway/49965_beacon_diskusage_windows

[2018.3] fixes to diskusage beacon
This commit is contained in:
Nicole Thomas 2018-10-25 09:41:18 -04:00 committed by GitHub
commit 01b083e20b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -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)

View File

@ -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)