Merge pull request #29399 from jfindlay/second_up

modules.status: add human_readable option to uptime
This commit is contained in:
Mike Place 2015-12-07 12:53:52 -07:00
commit 7efd6dd140
3 changed files with 81 additions and 9 deletions

View file

@ -24,15 +24,14 @@ from salt.utils.network import host_to_ip as _host_to_ip
from salt.utils.network import remote_port_tcp as _remote_port_tcp
from salt.ext.six.moves import zip
__virtualname__ = 'status'
__opts__ = {}
# TODO: Make this module support windows hosts
def __virtual__():
if salt.utils.is_windows():
return False
return True
return (False, 'Cannot load status module on windows')
return __virtualname__
def _number(text):
@ -121,17 +120,32 @@ def custom():
return ret
def uptime():
def uptime(human_readable=True):
'''
Return the uptime for this minion
human_readable: True
If ``True`` return the output provided by the system. If ``False``
return the output in seconds.
.. versionadded:: 2015.8.4
CLI Example:
.. code-block:: bash
salt '*' status.uptime
'''
return __salt__['cmd.run']('uptime')
if human_readable:
return __salt__['cmd.run']('uptime')
else:
if os.path.exists('/proc/uptime'):
out = __salt__['cmd.run']('cat /proc/uptime').split()
if len(out):
return out[0]
else:
return 'unexpected format in /proc/uptime'
return 'cannot find /proc/uptime'
def loadavg():

View file

@ -52,7 +52,7 @@ def __virtual__():
'''
if salt.utils.is_windows() and has_required_packages:
return __virtualname__
return False
return (False, 'Cannot load win_status module on non-windows')
def cpuload():
@ -192,8 +192,7 @@ def uptime(human_readable=False):
Return the system uptime for this machine in seconds
human_readable : False
If ``True``, then the number of seconds will be translated into years,
months, days, etc.
If ``True``, then return uptime in years, days, and seconds.
CLI Example:

View file

@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import
# Import Salt Libs
from salt.modules import status
# Import Salt Testing Libs
from salttesting import skipIf, TestCase
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
ensure_in_syspath('../../')
# Globals
status.__salt__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
class StatusTestCase(TestCase):
'''
test modules.status functions
'''
def test_uptime(self):
'''
test modules.status.uptime function
'''
mock_uptime = 'very often'
mock_run = MagicMock(return_value=mock_uptime)
with patch.dict(status.__salt__, {'cmd.run': mock_run}):
self.assertEqual(status.uptime(), mock_uptime)
mock_uptime = 'very idle'
mock_run = MagicMock(return_value=mock_uptime)
with patch.dict(status.__salt__, {'cmd.run': mock_run}):
with patch('os.path.exists', MagicMock(return_value=True)):
self.assertEqual(status.uptime(human_readable=False), mock_uptime.split()[0])
mock_uptime = ''
mock_return = 'unexpected format in /proc/uptime'
mock_run = MagicMock(return_value=mock_uptime)
with patch.dict(status.__salt__, {'cmd.run': mock_run}):
with patch('os.path.exists', MagicMock(return_value=True)):
self.assertEqual(status.uptime(human_readable=False), mock_return)
mock_return = 'cannot find /proc/uptime'
with patch('os.path.exists', MagicMock(return_value=False)):
self.assertEqual(status.uptime(human_readable=False), mock_return)
if __name__ == '__main__':
from integration import run_tests
run_tests(StatusTestCase, needs_daemon=False)