mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
service/{dead/running} optimizations & test fixes
Signed-off-by: Mathieu Le Marec - Pasquet <kiorky@cryptelium.net>
This commit is contained in:
parent
585d8981d4
commit
bde7867c98
@ -277,12 +277,15 @@ def running(name, enable=None, sig=None, init_delay=None, **kwargs):
|
||||
|
||||
# lot of custom init script wont or mis implement the status
|
||||
# command, so it is just an indicator but can not be fully trusted
|
||||
before_toggle_status = __salt__['service.status'](name)
|
||||
before_toggle_status = __salt__['service.status'](name, sig)
|
||||
before_toggle_enable_status = __salt__['service.enabled'](name)
|
||||
|
||||
# See if the service is already running
|
||||
if __salt__['service.status'](name, sig):
|
||||
if before_toggle_status:
|
||||
ret['comment'] = 'The service {0} is already running'.format(name)
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
return ret
|
||||
if enable is True and not before_toggle_enable_status:
|
||||
ret.update(_enable(name, None, **kwargs))
|
||||
return ret
|
||||
@ -363,14 +366,18 @@ def dead(name, enable=None, sig=None, **kwargs):
|
||||
|
||||
# lot of custom init script wont or mis implement the status
|
||||
# command, so it is just an indicator but can not be fully trusted
|
||||
before_toggle_status = __salt__['service.status'](name)
|
||||
before_toggle_status = __salt__['service.status'](name, sig)
|
||||
before_toggle_enable_status = __salt__['service.enabled'](name)
|
||||
if not __salt__['service.status'](name, sig):
|
||||
if not before_toggle_status:
|
||||
ret['comment'] = 'The service {0} is already dead'.format(name)
|
||||
if enable is True and not before_toggle_enable_status:
|
||||
ret.update(_enable(name, None, **kwargs))
|
||||
elif enable is False and before_toggle_enable_status:
|
||||
ret.update(_disable(name, None, **kwargs))
|
||||
if not __opts__['test']:
|
||||
if enable is True and not before_toggle_enable_status:
|
||||
ret.update(_enable(name, None, **kwargs))
|
||||
elif enable is False and before_toggle_enable_status:
|
||||
ret.update(_disable(name, None, **kwargs))
|
||||
return ret
|
||||
else:
|
||||
ret['result'] = None
|
||||
return ret
|
||||
|
||||
if __opts__['test']:
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import
|
||||
import contextlib
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from salttesting import TestCase, skipIf
|
||||
@ -54,39 +55,84 @@ class ServiceTestCase(TestCase):
|
||||
'result': None},
|
||||
{'changes': 'saltstack',
|
||||
'comment': 'Started Service salt', 'name': 'salt',
|
||||
'result': True}]
|
||||
'result': True},
|
||||
{'changes': {},
|
||||
'comment': 'The service salt is already running',
|
||||
'name': 'salt', 'result': None}]
|
||||
|
||||
mock = MagicMock(return_value="salt")
|
||||
with patch.object(service, '_enabled_used_error', mock):
|
||||
tmock = MagicMock(return_value=True)
|
||||
fmock = MagicMock(return_value=False)
|
||||
vmock = MagicMock(return_value="salt")
|
||||
with patch.object(service, '_enabled_used_error', vmock):
|
||||
self.assertEqual(service.running("salt", enabled=1), 'salt')
|
||||
|
||||
mock = MagicMock(side_effect=[False, True, True, True, True, True])
|
||||
with patch.object(service, '_available', mock):
|
||||
with patch.object(service, '_available', fmock):
|
||||
self.assertDictEqual(service.running("salt"), ret[0])
|
||||
|
||||
mock = MagicMock(side_effect=[True, True, True, False, False])
|
||||
with patch.dict(service.__salt__, {'service.status': mock}):
|
||||
with patch.object(service, '_available', tmock):
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': False}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled': tmock, 'service.status': tmock}
|
||||
)
|
||||
):
|
||||
self.assertDictEqual(service.running("salt"), ret[1])
|
||||
|
||||
# service was enabled and running, no changes
|
||||
mock = MagicMock(return_value={'changes': 'saltstack'})
|
||||
with patch.object(service, '_enable', mock):
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': False}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled':
|
||||
MagicMock(side_effect=[False, True]),
|
||||
'service.status': tmock}
|
||||
),
|
||||
patch.object(service, '_enable', mock)
|
||||
):
|
||||
self.assertDictEqual(service.running("salt", True), ret[2])
|
||||
|
||||
mock = MagicMock(return_value={'changes': 'saltstack'})
|
||||
with patch.object(service, '_disable', mock):
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': False}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled':
|
||||
MagicMock(side_effect=[True, False]),
|
||||
'service.status': tmock}
|
||||
),
|
||||
patch.object(service, '_disable', mock)
|
||||
):
|
||||
self.assertDictEqual(service.running("salt", False),
|
||||
ret[2])
|
||||
|
||||
with patch.dict(service.__opts__, {'test': True}):
|
||||
self.assertDictEqual(service.running("salt"), ret[3])
|
||||
self.assertDictEqual(service.running("salt"), ret[5])
|
||||
|
||||
with patch.dict(service.__opts__, {'test': False}):
|
||||
mock = MagicMock(return_value="stack")
|
||||
with patch.dict(service.__salt__, {'service.start': mock}):
|
||||
mock = MagicMock(return_value={'changes': 'saltstack'})
|
||||
with patch.object(service, '_enable', mock):
|
||||
self.assertDictEqual(service.running("salt", True),
|
||||
ret[4])
|
||||
with contextlib.nested(
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.status': fmock}
|
||||
),
|
||||
patch.dict(service.__opts__, {'test': True})
|
||||
):
|
||||
self.assertDictEqual(service.running("salt"), ret[3]),
|
||||
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': False}),
|
||||
patch.dict(
|
||||
service.__salt__, {
|
||||
'service.status':
|
||||
MagicMock(side_effect=[False, True]),
|
||||
'service.enabled':
|
||||
MagicMock(side_effect=[False, True]),
|
||||
'service.start':
|
||||
MagicMock(return_value="stack")}),
|
||||
patch.object(
|
||||
service, '_enable',
|
||||
MagicMock(return_value={'changes': 'saltstack'}))
|
||||
):
|
||||
self.assertDictEqual(service.running("salt", True), ret[4])
|
||||
|
||||
def test_dead(self):
|
||||
'''
|
||||
@ -101,32 +147,85 @@ class ServiceTestCase(TestCase):
|
||||
'result': None},
|
||||
{'changes': 'saltstack',
|
||||
'comment': 'Service salt was killed', 'name': 'salt',
|
||||
'result': True}]
|
||||
'result': True},
|
||||
{'changes': {},
|
||||
'comment': 'Service salt was killed', 'name': 'salt',
|
||||
'result': True},
|
||||
{'changes': {},
|
||||
'comment': 'The service salt is already dead', 'name': 'salt',
|
||||
'result': None}]
|
||||
|
||||
mock = MagicMock(return_value="salt")
|
||||
with patch.object(service, '_enabled_used_error', mock):
|
||||
self.assertEqual(service.dead("salt", enabled=1), 'salt')
|
||||
|
||||
mock = MagicMock(side_effect=[False, True, True, True])
|
||||
with patch.object(service, '_available', mock):
|
||||
tmock = MagicMock(return_value=True)
|
||||
fmock = MagicMock(return_value=False)
|
||||
with patch.object(service, '_available', fmock):
|
||||
self.assertDictEqual(service.dead("salt"), ret[0])
|
||||
|
||||
mock = MagicMock(side_effect=[False, True, True])
|
||||
with patch.dict(service.__salt__, {'service.status': mock}):
|
||||
mock = MagicMock(return_value={'changes': 'saltstack'})
|
||||
with patch.object(service, '_enable', mock):
|
||||
self.assertDictEqual(service.dead("salt", True), ret[1])
|
||||
with patch.object(service, '_available', tmock):
|
||||
mock = MagicMock(return_value={'changes': 'saltstack'})
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': True}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled': MagicMock(return_value=False),
|
||||
'service.stop': MagicMock(return_value=True),
|
||||
'service.status': MagicMock(return_value=False)}),
|
||||
patch.object(service, '_enable', mock)
|
||||
):
|
||||
self.assertDictEqual(service.dead("salt", True), ret[5])
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': False}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled': MagicMock(return_value=False),
|
||||
'service.stop': MagicMock(return_value=True),
|
||||
'service.status': MagicMock(return_value=False)}),
|
||||
patch.object(service, '_enable', mock)
|
||||
):
|
||||
self.assertDictEqual(service.dead("salt", True), ret[1])
|
||||
|
||||
with patch.dict(service.__opts__, {'test': True}):
|
||||
self.assertDictEqual(service.dead("salt"), ret[2])
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': True}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled': MagicMock(return_value=True),
|
||||
'service.status': MagicMock(return_value=True)}),
|
||||
):
|
||||
self.assertDictEqual(service.dead("salt"), ret[2])
|
||||
|
||||
with patch.dict(service.__opts__, {'test': False}):
|
||||
mock = MagicMock(return_value="stack")
|
||||
with patch.dict(service.__salt__, {'service.stop': mock}):
|
||||
mock = MagicMock(return_value={'changes': 'saltstack'})
|
||||
with patch.object(service, '_enable', mock):
|
||||
self.assertDictEqual(service.dead("salt", True),
|
||||
ret[3])
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': False}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled':
|
||||
MagicMock(side_effect=[True, True, False]),
|
||||
'service.status':
|
||||
MagicMock(side_effect=[True, True, False]),
|
||||
'service.stop': MagicMock(return_value="stack")}),
|
||||
patch.object(
|
||||
service, '_enable',
|
||||
MagicMock(return_value={'changes': 'saltstack'}))
|
||||
):
|
||||
self.assertDictEqual(service.dead("salt", True), ret[3])
|
||||
|
||||
# test an initd which a wrong status (True even if dead)
|
||||
with contextlib.nested(
|
||||
patch.dict(service.__opts__, {'test': False}),
|
||||
patch.dict(
|
||||
service.__salt__,
|
||||
{'service.enabled':
|
||||
MagicMock(side_effect=[False, False, False]),
|
||||
'service.status':
|
||||
MagicMock(side_effect=[True, True, True]),
|
||||
'service.stop': MagicMock(return_value="stack")}),
|
||||
patch.object(
|
||||
service, '_disable',
|
||||
MagicMock(return_value={}))
|
||||
):
|
||||
self.assertDictEqual(service.dead("salt", False), ret[4])
|
||||
|
||||
def test_enabled(self):
|
||||
'''
|
||||
|
Loading…
Reference in New Issue
Block a user