Merge pull request #44915 from jasperla/status/openbsd/cpustats

status.cpustats: add OpenBSD support
This commit is contained in:
Nicole Thomas 2017-12-13 17:17:43 -05:00 committed by GitHub
commit a8fa393459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 0 deletions

View File

@ -299,6 +299,9 @@ def cpustats():
.. versionchanged:: 2016.11.4
Added support for AIX
.. versionchanged:: Oxygen
Added support for OpenBSD
CLI Example:
.. code-block:: bash
@ -407,10 +410,28 @@ def cpustats():
return ret
def openbsd_cpustats():
'''
openbsd specific implementation of cpustats
'''
systat = __salt__['cmd.run']('systat -s 2 -B cpu').splitlines()
fields = systat[3].split()
ret = {}
for cpu in systat[4:]:
cpu_line = cpu.split()
cpu_idx = cpu_line[0]
ret[cpu_idx] = {}
for idx, field in enumerate(fields[1:]):
ret[cpu_idx][field] = cpu_line[idx+1]
return ret
# dict that return a function that does the right thing per platform
get_version = {
'Linux': linux_cpustats,
'FreeBSD': freebsd_cpustats,
'OpenBSD': openbsd_cpustats,
'SunOS': sunos_cpustats,
'AIX': aix_cpustats,
}

View File

@ -155,3 +155,50 @@ class StatusTestCase(TestCase, LoaderModuleMockMixin):
with self.assertRaises(CommandExecutionError):
with patch.dict(status.__salt__, {'cmd.run': exc_mock}):
status.uptime()
def _set_up_test_cpustats_openbsd(self):
'''
Define mock data for status.cpustats on OpenBSD
'''
class MockData(object):
'''
Store mock data
'''
m = MockData()
m.ret = {
'0': {
'User': '0.0%',
'Nice': '0.0%',
'System': '4.5%',
'Interrupt': '0.5%',
'Idle': '95.0%',
}
}
return m
def test_cpustats_openbsd(self):
'''
Test modules.status.cpustats function for OpenBSD
'''
m = self._set_up_test_cpustats_openbsd()
systat = '\n' \
'\n' \
' 1 users Load 0.20 0.07 0.05 salt.localdomain 09:42:42\n' \
'CPU User Nice System Interrupt Idle\n' \
'0 0.0% 0.0% 4.5% 0.5% 95.0%\n'
with patch.multiple(salt.utils.platform,
is_linux=MagicMock(return_value=False),
is_sunos=MagicMock(return_value=False),
is_darwin=MagicMock(return_value=False),
is_freebsd=MagicMock(return_value=False),
is_openbsd=MagicMock(return_value=True),
is_netbsd=MagicMock(return_value=False)), \
patch('salt.utils.path.which', MagicMock(return_value=True)), \
patch.dict(status.__grains__, {'kernel': 'OpenBSD'}), \
patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=systat)}):
ret = status.cpustats()
self.assertDictEqual(ret, m.ret)