mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #48718 from garethgreenaway/48536_beacon_diskusage_fixes
[2018.3] beacon diskusage fixes
This commit is contained in:
commit
062fe7cccf
2 changed files with 34 additions and 20 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
@ -60,10 +68,10 @@ class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
|||
self.assertEqual(ret, [{'diskusage': 50, 'mount': '/'}])
|
||||
|
||||
def test_diskusage_nomatch(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 = [{'/': '70%'}]
|
||||
|
||||
ret = diskusage.validate(config)
|
||||
|
@ -74,10 +82,10 @@ class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
|||
self.assertNotEqual(ret, [{'diskusage': 50, 'mount': '/'}])
|
||||
|
||||
def test_diskusage_match_regex(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 = [{r'^\/': '50%'}]
|
||||
|
||||
ret = diskusage.validate(config)
|
||||
|
|
Loading…
Add table
Reference in a new issue