Merge pull request #28891 from jfindlay/chkconfig_check

rh_service module: fix logic in _chkconfig_is_enabled
This commit is contained in:
Mike Place 2015-11-15 19:44:14 -07:00
commit 23eae0d9e0
2 changed files with 41 additions and 12 deletions

View file

@ -156,23 +156,22 @@ def _sysv_is_enabled(name, runlevel=None):
def _chkconfig_is_enabled(name, runlevel=None):
'''
Return True if the service is enabled according to chkconfig; otherwise
return False. If `runlevel` is None, then use the current runlevel.
Return ``True`` if the service is enabled according to chkconfig; otherwise
return ``False``. If ``runlevel`` is ``None``, then use the current
runlevel.
'''
cmdline = '/sbin/chkconfig --list {0}'.format(name)
result = __salt__['cmd.run_all'](cmdline, python_shell=False)
if runlevel is None:
runlevel = _runlevel()
if result['retcode'] == 0:
cols = result['stdout'].splitlines()[0].split()
try:
if cols[0].strip(':') == name:
if runlevel is None:
runlevel = _runlevel()
if len(cols) > 3 and '{0}:on'.format(runlevel) in cols:
for row in result['stdout'].splitlines():
if '{0}:on'.format(runlevel) in row:
if row.split()[0] == name:
return True
elif len(cols) < 3 and cols[1] and cols[1] == 'on':
return True
except IndexError:
pass
elif row.split() == [name + ':', 'on']:
return True
return False

View file

@ -5,6 +5,7 @@
# Import Python Libs
from __future__ import absolute_import
import textwrap
# Import Salt Testing Libs
from salttesting import TestCase, skipIf
@ -91,6 +92,35 @@ class RhServiceTestCase(TestCase):
'''
return MagicMock(return_value=bol)
def test__chkconfig_is_enabled(self):
'''
test _chkconfig_is_enabled function
'''
name = 'atd'
chkconfig_out = textwrap.dedent('''\
{0} 0:off 1:off 2:off 3:on 4:on 5:on 6:off
'''.format(name))
xinetd_out = textwrap.dedent('''\
xinetd based services:
{0}: on
'''.format(name))
with patch.object(rh_service, '_runlevel', MagicMock(return_value=3)):
mock_run = MagicMock(return_value={'retcode': 0,
'stdout': chkconfig_out})
with patch.dict(rh_service.__salt__, {'cmd.run_all': mock_run}):
self.assertTrue(rh_service._chkconfig_is_enabled(name))
self.assertFalse(rh_service._chkconfig_is_enabled(name, 2))
self.assertTrue(rh_service._chkconfig_is_enabled(name, 3))
mock_run = MagicMock(return_value={'retcode': 0,
'stdout': xinetd_out})
with patch.dict(rh_service.__salt__, {'cmd.run_all': mock_run}):
self.assertTrue(rh_service._chkconfig_is_enabled(name))
self.assertTrue(rh_service._chkconfig_is_enabled(name, 2))
self.assertTrue(rh_service._chkconfig_is_enabled(name, 3))
# 'get_enabled' function tests: 1
def test_get_enabled(self):