2015-01-07 12:26:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Rupesh Tare <rupesht@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
2015-01-08 19:42:55 +00:00
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2015-01-07 12:26:06 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-02-19 15:35:30 +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-01-07 12:26:06 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Import Salt Libs
|
|
|
|
from salt.modules import daemontools
|
|
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class DaemontoolsTestCase(TestCase, LoaderModuleMockMixin):
|
2015-01-07 12:26:06 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.daemontools
|
|
|
|
'''
|
2017-02-19 15:35:30 +00:00
|
|
|
|
|
|
|
loader_module = daemontools
|
|
|
|
|
2015-01-07 12:26:06 +00:00
|
|
|
def test_start(self):
|
|
|
|
'''
|
|
|
|
Test for Starts service via daemontools
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value=None)
|
|
|
|
with patch.dict(daemontools.__salt__, {'file.remove': mock}):
|
|
|
|
mock = MagicMock(return_value='')
|
|
|
|
with patch.object(daemontools, '_service_path', mock):
|
|
|
|
mock = MagicMock(return_value=False)
|
|
|
|
with patch.dict(daemontools.__salt__, {'cmd.retcode': mock}):
|
|
|
|
self.assertTrue(daemontools.start('name'))
|
|
|
|
|
|
|
|
def test_stop(self):
|
|
|
|
'''
|
|
|
|
Test for Stops service via daemontools
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value=None)
|
|
|
|
with patch.dict(daemontools.__salt__, {'file.touch': mock}):
|
|
|
|
mock = MagicMock(return_value='')
|
|
|
|
with patch.object(daemontools, '_service_path', mock):
|
|
|
|
mock = MagicMock(return_value=False)
|
|
|
|
with patch.dict(daemontools.__salt__, {'cmd.retcode': mock}):
|
|
|
|
self.assertTrue(daemontools.stop('name'))
|
|
|
|
|
|
|
|
def test_term(self):
|
|
|
|
'''
|
|
|
|
Test for Send a TERM to service via daemontools
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value='')
|
|
|
|
with patch.object(daemontools, '_service_path', mock):
|
|
|
|
mock = MagicMock(return_value=False)
|
|
|
|
with patch.dict(daemontools.__salt__, {'cmd.retcode': mock}):
|
|
|
|
self.assertTrue(daemontools.term('name'))
|
|
|
|
|
|
|
|
def test_reload_(self):
|
|
|
|
'''
|
|
|
|
Test for Wrapper for term()
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value=None)
|
|
|
|
with patch.object(daemontools, 'term', mock):
|
|
|
|
self.assertEqual(daemontools.reload_('name'), None)
|
|
|
|
|
|
|
|
def test_restart(self):
|
|
|
|
'''
|
|
|
|
Test for Restart service via daemontools. This will stop/start service
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value=False)
|
|
|
|
with patch.object(daemontools, 'stop', mock):
|
|
|
|
self.assertEqual(daemontools.restart('name'), 'restart False')
|
|
|
|
|
|
|
|
def test_full_restart(self):
|
|
|
|
'''
|
|
|
|
Test for Calls daemontools.restart() function
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value=None)
|
|
|
|
with patch.object(daemontools, 'restart', mock):
|
|
|
|
self.assertEqual(daemontools.restart('name'), None)
|
|
|
|
|
|
|
|
@patch('re.search', MagicMock(return_value=1))
|
|
|
|
def test_status(self):
|
|
|
|
'''
|
|
|
|
Test for Return the status for a service via
|
|
|
|
daemontools, return pid if running
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value='')
|
|
|
|
with patch.object(daemontools, '_service_path', mock):
|
|
|
|
mock = MagicMock(return_value='name')
|
|
|
|
with patch.dict(daemontools.__salt__, {'cmd.run_stdout': mock}):
|
|
|
|
self.assertEqual(daemontools.status('name'), '')
|
|
|
|
|
|
|
|
def test_available(self):
|
|
|
|
'''
|
|
|
|
Test for Returns ``True`` if the specified service
|
|
|
|
is available, otherwise returns``False``.
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value=[])
|
|
|
|
with patch.object(daemontools, 'get_all', mock):
|
|
|
|
self.assertFalse(daemontools.available('name'))
|
|
|
|
|
|
|
|
def test_missing(self):
|
|
|
|
'''
|
|
|
|
Test for The inverse of daemontools.available.
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value=[])
|
|
|
|
with patch.object(daemontools, 'get_all', mock):
|
|
|
|
self.assertTrue(daemontools.missing('name'))
|
|
|
|
|
|
|
|
def test_get_all(self):
|
|
|
|
'''
|
|
|
|
Test for Return a list of all available services
|
|
|
|
'''
|
|
|
|
self.assertRaises(CommandExecutionError, daemontools.get_all)
|
|
|
|
|
|
|
|
daemontools.SERVICE_DIR = 'A'
|
|
|
|
mock = MagicMock(return_value='A')
|
|
|
|
with patch.object(os, 'listdir', mock):
|
|
|
|
self.assertEqual(daemontools.get_all(), ['A'])
|