Updating psutil.disk_partitions to pull in all mounts not just the physical ones. Check to see if the mount point from the configuration ends with a $ (regular expression end of line) if not we add one in to ensure that a simple / does not end up matching all mount points. Updating tests accordingly.

This commit is contained in:
Gareth J. Greenaway 2018-07-19 12:08:33 -07:00
parent e1dd10be70
commit 6ecca166ff
No known key found for this signature in database
GPG key ID: 10B62F8A7CAD7A41
2 changed files with 30 additions and 16 deletions

View file

@ -83,13 +83,19 @@ def beacon(config):
it will override the previously defined threshold.
'''
parts = psutil.disk_partitions(all=False)
parts = psutil.disk_partitions(all=True)
ret = []
for mounts in config:
mount = next(iter(mounts))
# Because we're using regular expressions
# if our mount doesn't end with a $, insert one.
mount_re = mount
if not mount.endswith('$'):
mount_re = '{0}$'.format(mount)
for part in parts:
if re.match(mount, part.mountpoint):
if re.match(mount_re, part.mountpoint):
_mount = part.mountpoint
try:
@ -100,7 +106,7 @@ def beacon(config):
current_usage = _current_usage.percent
monitor_usage = mounts[mount]
log.info('current_usage %s', current_usage)
log.debug('current_usage %s', current_usage)
if '%' in monitor_usage:
monitor_usage = re.sub('%', '', monitor_usage)
monitor_usage = float(monitor_usage)

View file

@ -6,19 +6,27 @@ from collections import namedtuple
# Salt testing libs
from tests.support.unit import skipIf, TestCase
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock, Mock
from tests.support.mixins import LoaderModuleMockMixin
# Salt libs
import salt.beacons.diskusage as diskusage
STUB_DISK_PARTITION = namedtuple(
'partition',
'device mountpoint fstype, opts')(
'/dev/disk0s2', '/', 'hfs',
'rw,local,rootfs,dovolfs,journaled,multilabel')
STUB_DISK_USAGE = namedtuple('usage',
'total used free percent')(1000, 500, 500, 50)
STUB_DISK_PARTITION = [
namedtuple(
'partition',
'device mountpoint fstype, opts')(
'tmpfs', '/mnt/tmp', 'tmpfs',
'rw,nosuid,nodev,relatime,size=10240k'),
namedtuple(
'partition',
'device mountpoint fstype, opts')(
'/dev/disk0s2', '/', 'hfs',
'rw,local,rootfs,dovolfs,journaled,multilabel')]
STUB_DISK_USAGE = [namedtuple('usage',
'total used free percent')(1000, 500, 500, 50),
namedtuple('usage',
'total used free percent')(100, 75, 25, 25)]
@skipIf(NO_MOCK, NO_MOCK_REASON)
@ -46,10 +54,10 @@ class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
self.assertEqual(ret, (True, 'Valid beacon configuration'))
def test_diskusage_match(self):
disk_usage_mock = Mock(side_effect=STUB_DISK_USAGE)
with patch('psutil.disk_partitions',
MagicMock(return_value=[STUB_DISK_PARTITION])), \
patch('psutil.disk_usage',
MagicMock(return_value=STUB_DISK_USAGE)):
MagicMock(return_value=STUB_DISK_PARTITION)), \
patch('psutil.disk_usage', disk_usage_mock):
config = [{'/': '50%'}]
ret = diskusage.validate(config)
@ -61,7 +69,7 @@ class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
def test_diskusage_nomatch(self):
with patch('psutil.disk_partitions',
MagicMock(return_value=[STUB_DISK_PARTITION])), \
MagicMock(return_value=STUB_DISK_PARTITION)), \
patch('psutil.disk_usage',
MagicMock(return_value=STUB_DISK_USAGE)):
config = [{'/': '70%'}]
@ -75,7 +83,7 @@ class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
def test_diskusage_match_regex(self):
with patch('psutil.disk_partitions',
MagicMock(return_value=[STUB_DISK_PARTITION])), \
MagicMock(return_value=STUB_DISK_PARTITION)), \
patch('psutil.disk_usage',
MagicMock(return_value=STUB_DISK_USAGE)):
config = [{r'^\/': '50%'}]