2015-04-14 07:05:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Rahul Handay <rahulha@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
2018-01-19 05:49:04 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-04-14 07:05:50 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 22:28:46 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-04-14 07:05:50 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.service as service
|
2015-04-14 07:05:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def func(name):
|
|
|
|
'''
|
|
|
|
Mock func method
|
|
|
|
'''
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 22:28:46 +00:00
|
|
|
class ServiceTestCase(TestCase, LoaderModuleMockMixin):
|
2015-04-14 07:05:50 +00:00
|
|
|
'''
|
|
|
|
Validate the service state
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {service: {}}
|
2017-02-19 22:28:46 +00:00
|
|
|
|
2015-04-14 07:05:50 +00:00
|
|
|
def test_running(self):
|
|
|
|
'''
|
|
|
|
Test to verify that the service is running
|
|
|
|
'''
|
|
|
|
ret = [{'comment': '', 'changes': {}, 'name': 'salt', 'result': True},
|
|
|
|
{'changes': {},
|
|
|
|
'comment': 'The service salt is already running',
|
|
|
|
'name': 'salt', 'result': True},
|
|
|
|
{'changes': 'saltstack',
|
|
|
|
'comment': 'The service salt is already running',
|
|
|
|
'name': 'salt', 'result': True},
|
|
|
|
{'changes': {},
|
|
|
|
'comment': 'Service salt is set to start', 'name': 'salt',
|
|
|
|
'result': None},
|
|
|
|
{'changes': 'saltstack',
|
|
|
|
'comment': 'Started Service salt', 'name': 'salt',
|
2015-05-16 11:14:53 +00:00
|
|
|
'result': True},
|
|
|
|
{'changes': {},
|
|
|
|
'comment': 'The service salt is already running',
|
2016-09-27 14:41:53 +00:00
|
|
|
'name': 'salt', 'result': True},
|
|
|
|
{'changes': 'saltstack',
|
|
|
|
'comment': 'Service salt failed to start', 'name': 'salt',
|
2017-05-08 14:25:21 +00:00
|
|
|
'result': False}]
|
2015-04-14 07:05:50 +00:00
|
|
|
|
2015-05-16 11:14:53 +00:00
|
|
|
tmock = MagicMock(return_value=True)
|
|
|
|
fmock = MagicMock(return_value=False)
|
|
|
|
vmock = MagicMock(return_value="salt")
|
|
|
|
with patch.object(service, '_enabled_used_error', vmock):
|
2015-04-14 07:05:50 +00:00
|
|
|
self.assertEqual(service.running("salt", enabled=1), 'salt')
|
|
|
|
|
2015-05-16 11:14:53 +00:00
|
|
|
with patch.object(service, '_available', fmock):
|
2015-04-14 07:05:50 +00:00
|
|
|
self.assertDictEqual(service.running("salt"), ret[0])
|
|
|
|
|
2015-05-16 11:14:53 +00:00
|
|
|
with patch.object(service, '_available', tmock):
|
2016-08-03 20:49:24 +00:00
|
|
|
with patch.dict(service.__opts__, {'test': False}):
|
|
|
|
with patch.dict(service.__salt__, {'service.enabled': tmock,
|
|
|
|
'service.status': tmock}):
|
|
|
|
self.assertDictEqual(service.running("salt"), ret[1])
|
|
|
|
|
2015-04-14 07:05:50 +00:00
|
|
|
mock = MagicMock(return_value={'changes': 'saltstack'})
|
2016-08-03 20:49:24 +00:00
|
|
|
with patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[False, True]),
|
|
|
|
'service.status': tmock}):
|
|
|
|
with patch.object(service, '_enable', mock):
|
|
|
|
self.assertDictEqual(service.running("salt", True), ret[2])
|
|
|
|
|
|
|
|
with patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[True, False]),
|
|
|
|
'service.status': tmock}):
|
|
|
|
with patch.object(service, '_disable', mock):
|
|
|
|
self.assertDictEqual(service.running("salt", False), ret[2])
|
|
|
|
|
|
|
|
with patch.dict(service.__salt__, {'service.status': MagicMock(side_effect=[False, True]),
|
|
|
|
'service.enabled': MagicMock(side_effect=[False, True]),
|
|
|
|
'service.start': MagicMock(return_value="stack")}):
|
|
|
|
with patch.object(service, '_enable', MagicMock(return_value={'changes': 'saltstack'})):
|
|
|
|
self.assertDictEqual(service.running("salt", True), ret[4])
|
|
|
|
|
|
|
|
with patch.dict(service.__opts__, {'test': True}):
|
|
|
|
with patch.dict(service.__salt__, {'service.status': tmock}):
|
2015-05-16 11:14:53 +00:00
|
|
|
self.assertDictEqual(service.running("salt"), ret[5])
|
|
|
|
|
2016-08-03 20:49:24 +00:00
|
|
|
with patch.dict(service.__salt__, {'service.status': fmock}):
|
2015-05-16 12:21:17 +00:00
|
|
|
self.assertDictEqual(service.running("salt"), ret[3])
|
2015-05-16 11:14:53 +00:00
|
|
|
|
2016-09-29 17:25:46 +00:00
|
|
|
with patch.dict(service.__opts__, {'test': False}):
|
|
|
|
with patch.dict(service.__salt__, {'service.status': MagicMock(side_effect=[False, False]),
|
|
|
|
'service.enabled': MagicMock(side_effecct=[True, True]),
|
|
|
|
'service.start': MagicMock(return_value='stack')}):
|
|
|
|
with patch.object(service, '_enable', MagicMock(return_value={'changes': 'saltstack'})):
|
|
|
|
self.assertDictEqual(service.running('salt', True), ret[6])
|
2016-09-27 14:41:53 +00:00
|
|
|
|
2015-04-14 07:05:50 +00:00
|
|
|
def test_dead(self):
|
|
|
|
'''
|
|
|
|
Test to ensure that the named service is dead
|
|
|
|
'''
|
|
|
|
ret = [{'changes': {}, 'comment': '', 'name': 'salt', 'result': True},
|
|
|
|
{'changes': 'saltstack',
|
|
|
|
'comment': 'The service salt is already dead', 'name': 'salt',
|
|
|
|
'result': True},
|
|
|
|
{'changes': {},
|
|
|
|
'comment': 'Service salt is set to be killed', 'name': 'salt',
|
|
|
|
'result': None},
|
|
|
|
{'changes': 'saltstack',
|
|
|
|
'comment': 'Service salt was killed', 'name': 'salt',
|
2015-05-16 11:14:53 +00:00
|
|
|
'result': True},
|
|
|
|
{'changes': {},
|
2016-09-27 14:41:53 +00:00
|
|
|
'comment': 'Service salt failed to die', 'name': 'salt',
|
|
|
|
'result': False},
|
2015-11-26 05:54:30 +00:00
|
|
|
{'changes': 'saltstack',
|
2015-05-16 11:14:53 +00:00
|
|
|
'comment': 'The service salt is already dead', 'name': 'salt',
|
2015-08-04 15:48:43 +00:00
|
|
|
'result': True}]
|
2015-04-14 07:05:50 +00:00
|
|
|
|
|
|
|
mock = MagicMock(return_value="salt")
|
|
|
|
with patch.object(service, '_enabled_used_error', mock):
|
|
|
|
self.assertEqual(service.dead("salt", enabled=1), 'salt')
|
|
|
|
|
2015-05-16 11:14:53 +00:00
|
|
|
tmock = MagicMock(return_value=True)
|
|
|
|
fmock = MagicMock(return_value=False)
|
|
|
|
with patch.object(service, '_available', fmock):
|
2015-04-14 07:05:50 +00:00
|
|
|
self.assertDictEqual(service.dead("salt"), ret[0])
|
|
|
|
|
2015-05-16 11:14:53 +00:00
|
|
|
with patch.object(service, '_available', tmock):
|
|
|
|
mock = MagicMock(return_value={'changes': 'saltstack'})
|
2016-08-03 20:49:24 +00:00
|
|
|
with patch.dict(service.__opts__, {'test': True}):
|
|
|
|
with patch.dict(service.__salt__, {'service.enabled': fmock,
|
|
|
|
'service.stop': tmock,
|
|
|
|
'service.status': fmock}):
|
|
|
|
with patch.object(service, '_enable', mock):
|
|
|
|
self.assertDictEqual(service.dead("salt", True), ret[5])
|
|
|
|
|
|
|
|
with patch.dict(service.__salt__, {'service.enabled': tmock,
|
|
|
|
'service.status': tmock}):
|
|
|
|
self.assertDictEqual(service.dead("salt"), ret[2])
|
|
|
|
|
|
|
|
with patch.dict(service.__opts__, {'test': False}):
|
2016-08-08 15:53:29 +00:00
|
|
|
with patch.dict(service.__salt__, {'service.enabled': fmock,
|
|
|
|
'service.stop': tmock,
|
|
|
|
'service.status': fmock}):
|
2016-08-03 20:49:24 +00:00
|
|
|
with patch.object(service, '_enable', mock):
|
|
|
|
self.assertDictEqual(service.dead("salt", True), ret[1])
|
|
|
|
|
|
|
|
with patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[True, True, False]),
|
2016-09-29 17:26:50 +00:00
|
|
|
'service.status': MagicMock(side_effect=[True, False, False]),
|
2016-08-03 20:49:24 +00:00
|
|
|
'service.stop': MagicMock(return_value="stack")}):
|
|
|
|
with 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 patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[False, False, False]),
|
2016-09-29 17:26:50 +00:00
|
|
|
'service.status': MagicMock(side_effect=[True, True, True]),
|
2016-08-03 20:49:24 +00:00
|
|
|
'service.stop': MagicMock(return_value="stack")}):
|
|
|
|
with patch.object(service, '_disable', MagicMock(return_value={})):
|
|
|
|
self.assertDictEqual(service.dead("salt", False), ret[4])
|
2015-04-14 07:05:50 +00:00
|
|
|
|
2016-11-08 21:58:33 +00:00
|
|
|
def test_dead_with_missing_service(self):
|
|
|
|
'''
|
|
|
|
Tests the case in which a service.dead state is executed on a state
|
|
|
|
which does not exist.
|
|
|
|
|
|
|
|
See https://github.com/saltstack/salt/issues/37511
|
|
|
|
'''
|
|
|
|
name = 'thisisnotarealservice'
|
|
|
|
with patch.dict(service.__salt__,
|
|
|
|
{'service.available': MagicMock(return_value=False)}):
|
|
|
|
ret = service.dead(name=name)
|
|
|
|
self.assertDictEqual(
|
|
|
|
ret,
|
|
|
|
{'changes': {},
|
|
|
|
'comment': 'The named service {0} is not available'.format(name),
|
|
|
|
'result': True,
|
|
|
|
'name': name}
|
|
|
|
)
|
|
|
|
|
2015-04-14 07:05:50 +00:00
|
|
|
def test_enabled(self):
|
|
|
|
'''
|
|
|
|
Test to verify that the service is enabled
|
|
|
|
'''
|
|
|
|
ret = {'changes': 'saltstack', 'comment': '', 'name': 'salt',
|
|
|
|
'result': True}
|
|
|
|
mock = MagicMock(return_value={'changes': 'saltstack'})
|
|
|
|
with patch.object(service, '_enable', mock):
|
|
|
|
self.assertDictEqual(service.enabled("salt"), ret)
|
|
|
|
|
|
|
|
def test_disabled(self):
|
|
|
|
'''
|
|
|
|
Test to verify that the service is disabled
|
|
|
|
'''
|
|
|
|
ret = {'changes': 'saltstack', 'comment': '', 'name': 'salt',
|
|
|
|
'result': True}
|
|
|
|
mock = MagicMock(return_value={'changes': 'saltstack'})
|
|
|
|
with patch.object(service, '_disable', mock):
|
|
|
|
self.assertDictEqual(service.disabled("salt"), ret)
|
|
|
|
|
|
|
|
def test_mod_watch(self):
|
|
|
|
'''
|
|
|
|
Test to the service watcher, called to invoke the watch command.
|
|
|
|
'''
|
|
|
|
ret = [{'changes': {},
|
|
|
|
'comment': 'Service is already stopped', 'name': 'salt',
|
|
|
|
'result': True},
|
|
|
|
{'changes': {},
|
2016-09-02 12:38:15 +00:00
|
|
|
'comment': 'Unable to trigger watch for service.stack',
|
2015-04-14 07:05:50 +00:00
|
|
|
'name': 'salt', 'result': False},
|
|
|
|
{'changes': {},
|
|
|
|
'comment': 'Service is set to be started', 'name': 'salt',
|
|
|
|
'result': None},
|
|
|
|
{'changes': {'salt': 'salt'},
|
|
|
|
'comment': 'Service started', 'name': 'salt',
|
|
|
|
'result': 'salt'}]
|
|
|
|
|
|
|
|
mock = MagicMock(return_value=False)
|
|
|
|
with patch.dict(service.__salt__, {'service.status': mock}):
|
|
|
|
self.assertDictEqual(service.mod_watch("salt", "dead"), ret[0])
|
|
|
|
|
|
|
|
with patch.dict(service.__salt__, {'service.start': func}):
|
|
|
|
with patch.dict(service.__opts__, {'test': True}):
|
|
|
|
self.assertDictEqual(service.mod_watch("salt", "running"),
|
|
|
|
ret[2])
|
|
|
|
|
|
|
|
with patch.dict(service.__opts__, {'test': False}):
|
|
|
|
self.assertDictEqual(service.mod_watch("salt", "running"),
|
|
|
|
ret[3])
|
|
|
|
|
|
|
|
self.assertDictEqual(service.mod_watch("salt", "stack"), ret[1])
|