service/{dead/running} optimizations & test fixes

Signed-off-by: Mathieu Le Marec - Pasquet <kiorky@cryptelium.net>
This commit is contained in:
Mathieu Le Marec - Pasquet 2015-05-16 13:14:53 +02:00
parent 585d8981d4
commit bde7867c98
2 changed files with 149 additions and 43 deletions

View File

@ -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 # lot of custom init script wont or mis implement the status
# command, so it is just an indicator but can not be fully trusted # 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) before_toggle_enable_status = __salt__['service.enabled'](name)
# See if the service is already running # 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) 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: if enable is True and not before_toggle_enable_status:
ret.update(_enable(name, None, **kwargs)) ret.update(_enable(name, None, **kwargs))
return ret 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 # lot of custom init script wont or mis implement the status
# command, so it is just an indicator but can not be fully trusted # 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) 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) ret['comment'] = 'The service {0} is already dead'.format(name)
if enable is True and not before_toggle_enable_status: if not __opts__['test']:
ret.update(_enable(name, None, **kwargs)) if enable is True and not before_toggle_enable_status:
elif enable is False and before_toggle_enable_status: ret.update(_enable(name, None, **kwargs))
ret.update(_disable(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 return ret
if __opts__['test']: if __opts__['test']:

View File

@ -5,6 +5,7 @@
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import contextlib
# Import Salt Testing Libs # Import Salt Testing Libs
from salttesting import TestCase, skipIf from salttesting import TestCase, skipIf
@ -54,39 +55,84 @@ class ServiceTestCase(TestCase):
'result': None}, 'result': None},
{'changes': 'saltstack', {'changes': 'saltstack',
'comment': 'Started Service salt', 'name': 'salt', '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") tmock = MagicMock(return_value=True)
with patch.object(service, '_enabled_used_error', mock): 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') self.assertEqual(service.running("salt", enabled=1), 'salt')
mock = MagicMock(side_effect=[False, True, True, True, True, True]) with patch.object(service, '_available', fmock):
with patch.object(service, '_available', mock):
self.assertDictEqual(service.running("salt"), ret[0]) self.assertDictEqual(service.running("salt"), ret[0])
mock = MagicMock(side_effect=[True, True, True, False, False]) with patch.object(service, '_available', tmock):
with patch.dict(service.__salt__, {'service.status': mock}): 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]) self.assertDictEqual(service.running("salt"), ret[1])
# service was enabled and running, no changes
mock = MagicMock(return_value={'changes': 'saltstack'}) 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]) self.assertDictEqual(service.running("salt", True), ret[2])
mock = MagicMock(return_value={'changes': 'saltstack'}) with contextlib.nested(
with patch.object(service, '_disable', mock): 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), self.assertDictEqual(service.running("salt", False),
ret[2]) ret[2])
with patch.dict(service.__opts__, {'test': True}): 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}): with contextlib.nested(
mock = MagicMock(return_value="stack") patch.dict(
with patch.dict(service.__salt__, {'service.start': mock}): service.__salt__,
mock = MagicMock(return_value={'changes': 'saltstack'}) {'service.status': fmock}
with patch.object(service, '_enable', mock): ),
self.assertDictEqual(service.running("salt", True), patch.dict(service.__opts__, {'test': True})
ret[4]) ):
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): def test_dead(self):
''' '''
@ -101,32 +147,85 @@ class ServiceTestCase(TestCase):
'result': None}, 'result': None},
{'changes': 'saltstack', {'changes': 'saltstack',
'comment': 'Service salt was killed', 'name': 'salt', '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") mock = MagicMock(return_value="salt")
with patch.object(service, '_enabled_used_error', mock): with patch.object(service, '_enabled_used_error', mock):
self.assertEqual(service.dead("salt", enabled=1), 'salt') self.assertEqual(service.dead("salt", enabled=1), 'salt')
mock = MagicMock(side_effect=[False, True, True, True]) tmock = MagicMock(return_value=True)
with patch.object(service, '_available', mock): fmock = MagicMock(return_value=False)
with patch.object(service, '_available', fmock):
self.assertDictEqual(service.dead("salt"), ret[0]) self.assertDictEqual(service.dead("salt"), ret[0])
mock = MagicMock(side_effect=[False, True, True]) with patch.object(service, '_available', tmock):
with patch.dict(service.__salt__, {'service.status': mock}): mock = MagicMock(return_value={'changes': 'saltstack'})
mock = MagicMock(return_value={'changes': 'saltstack'}) with contextlib.nested(
with patch.object(service, '_enable', mock): patch.dict(service.__opts__, {'test': True}),
self.assertDictEqual(service.dead("salt", True), ret[1]) 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}): with contextlib.nested(
self.assertDictEqual(service.dead("salt"), ret[2]) 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}): with contextlib.nested(
mock = MagicMock(return_value="stack") patch.dict(service.__opts__, {'test': False}),
with patch.dict(service.__salt__, {'service.stop': mock}): patch.dict(
mock = MagicMock(return_value={'changes': 'saltstack'}) service.__salt__,
with patch.object(service, '_enable', mock): {'service.enabled':
self.assertDictEqual(service.dead("salt", True), MagicMock(side_effect=[True, True, False]),
ret[3]) '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): def test_enabled(self):
''' '''