Fixing a bug in the diskusage beacon that prevented it from working on Windows. Adding a couple tests to test functionality on Windows.

This commit is contained in:
Gareth J. Greenaway 2018-10-23 19:03:19 -07:00
parent 5f904dd5c5
commit f65fa57a10
No known key found for this signature in database
GPG key ID: 10B62F8A7CAD7A41
2 changed files with 54 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,26 @@ 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 +109,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)