mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add tests
This commit is contained in:
parent
b0d646d69c
commit
38009660e4
3 changed files with 71 additions and 14 deletions
|
@ -40,6 +40,8 @@ class ServiceTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
self.service_name = 'com.apple.AirPlayXPCHelper'
|
||||
self.stopped = ''
|
||||
self.running = '[0-9]'
|
||||
elif os_family == 'Windows':
|
||||
self.service_name = 'Spooler'
|
||||
|
||||
self.pre_srv_enabled = True if self.service_name in self.run_function('service.get_enabled') else False
|
||||
self.post_srv_disable = False
|
||||
|
@ -47,7 +49,7 @@ class ServiceTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
self.run_function('service.enable', name=self.service_name)
|
||||
self.post_srv_disable = True
|
||||
|
||||
if salt.utils.which(cmd_name) is None:
|
||||
if os_family != 'Windows' and salt.utils.which(cmd_name) is None:
|
||||
self.skipTest('{0} is not installed'.format(cmd_name))
|
||||
|
||||
def tearDown(self):
|
||||
|
|
|
@ -23,6 +23,7 @@ import salt.modules.win_service as win_service
|
|||
try:
|
||||
WINAPI = True
|
||||
import win32serviceutil
|
||||
import pywintypes
|
||||
except ImportError:
|
||||
WINAPI = False
|
||||
|
||||
|
@ -119,18 +120,37 @@ class WinServiceTestCase(TestCase, LoaderModuleMockMixin):
|
|||
'''
|
||||
mock_true = MagicMock(return_value=True)
|
||||
mock_false = MagicMock(return_value=False)
|
||||
mock_info = MagicMock(side_effect=[{'Status': 'Stopped'},
|
||||
{'Status': 'Start Pending'},
|
||||
{'Status': 'Running'}])
|
||||
mock_info = MagicMock(side_effect=[{'Status': 'Running'}])
|
||||
|
||||
with patch.object(win_service, 'status', mock_true):
|
||||
with patch.object(win32serviceutil, 'StartService', mock_true), \
|
||||
patch.object(win_service, 'disabled', mock_false), \
|
||||
patch.object(win_service, 'info', mock_info):
|
||||
self.assertTrue(win_service.start('spongebob'))
|
||||
|
||||
with patch.object(win_service, 'status', mock_false):
|
||||
with patch.object(win32serviceutil, 'StartService', mock_true):
|
||||
with patch.object(win_service, 'info', mock_info):
|
||||
with patch.object(win_service, 'status', mock_true):
|
||||
self.assertTrue(win_service.start('spongebob'))
|
||||
mock_info = MagicMock(side_effect=[{'Status': 'Stopped', 'Status_WaitHint': 0},
|
||||
{'Status': 'Start Pending', 'Status_WaitHint': 0},
|
||||
{'Status': 'Running'}])
|
||||
|
||||
with patch.object(win32serviceutil, 'StartService', mock_true), \
|
||||
patch.object(win_service, 'disabled', mock_false), \
|
||||
patch.object(win_service, 'info', mock_info), \
|
||||
patch.object(win_service, 'status', mock_true):
|
||||
self.assertTrue(win_service.start('spongebob'))
|
||||
|
||||
def test_start_already_running(self):
|
||||
'''
|
||||
Test starting a service that is already running
|
||||
'''
|
||||
mock_false = MagicMock(return_value=False)
|
||||
mock_error = MagicMock(
|
||||
side_effect=pywintypes.error(1056,
|
||||
'StartService',
|
||||
'Service is running'))
|
||||
mock_info = MagicMock(side_effect=[{'Status': 'Running'}])
|
||||
with patch.object(win32serviceutil, 'StartService', mock_error), \
|
||||
patch.object(win_service, 'disabled', mock_false), \
|
||||
patch.object(win_service, '_status_wait', mock_info):
|
||||
self.assertTrue(win_service.start('spongebob'))
|
||||
|
||||
@skipIf(not WINAPI, 'win32serviceutil not available')
|
||||
def test_stop(self):
|
||||
|
@ -141,8 +161,7 @@ class WinServiceTestCase(TestCase, LoaderModuleMockMixin):
|
|||
mock_false = MagicMock(return_value=False)
|
||||
mock_info = MagicMock(side_effect=[{'Status': 'Stopped'}])
|
||||
|
||||
with patch.dict(win_service.__salt__, {'cmd.run': MagicMock(return_value="service was stopped")}), \
|
||||
patch.object(win32serviceutil, 'StopService', mock_true), \
|
||||
with patch.object(win32serviceutil, 'StopService', mock_true), \
|
||||
patch.object(win_service, '_status_wait', mock_info):
|
||||
self.assertTrue(win_service.stop('spongebob'))
|
||||
|
||||
|
@ -150,12 +169,24 @@ class WinServiceTestCase(TestCase, LoaderModuleMockMixin):
|
|||
{'Status': 'Stop Pending', 'Status_WaitHint': 0},
|
||||
{'Status': 'Stopped'}])
|
||||
|
||||
with patch.dict(win_service.__salt__, {'cmd.run': MagicMock(return_value="service was stopped")}), \
|
||||
patch.object(win32serviceutil, 'StopService', mock_true), \
|
||||
with patch.object(win32serviceutil, 'StopService', mock_true), \
|
||||
patch.object(win_service, 'info', mock_info), \
|
||||
patch.object(win_service, 'status', mock_false):
|
||||
self.assertTrue(win_service.stop('spongebob'))
|
||||
|
||||
def test_stop_not_running(self):
|
||||
'''
|
||||
Test stopping a service that is already stopped
|
||||
'''
|
||||
mock_error = MagicMock(
|
||||
side_effect=pywintypes.error(1062,
|
||||
'StopService',
|
||||
'Service is not running'))
|
||||
mock_info = MagicMock(side_effect=[{'Status': 'Stopped'}])
|
||||
with patch.object(win32serviceutil, 'StopService', mock_error), \
|
||||
patch.object(win_service, '_status_wait', mock_info):
|
||||
self.assertTrue(win_service.stop('spongebob'))
|
||||
|
||||
def test_restart(self):
|
||||
'''
|
||||
Test to restart the named service
|
||||
|
@ -264,3 +295,26 @@ class WinServiceTestCase(TestCase, LoaderModuleMockMixin):
|
|||
with patch.object(win_service, 'enabled', mock):
|
||||
self.assertTrue(win_service.disabled('spongebob'))
|
||||
self.assertFalse(win_service.disabled('squarepants'))
|
||||
|
||||
def test_cmd_quote_no_quotes(self):
|
||||
'''
|
||||
Make sure the command gets quoted correctly
|
||||
'''
|
||||
# Should always return command wrapped in double quotes
|
||||
expected = r'"C:\Program Files\salt\test.exe"'
|
||||
|
||||
# test no quotes
|
||||
bin_path = r'C:\Program Files\salt\test.exe'
|
||||
self.assertEqual(win_service._cmd_quote(bin_path), expected)
|
||||
|
||||
# test single quotes
|
||||
bin_path = r"'C:\Program Files\salt\test.exe'"
|
||||
self.assertEqual(win_service._cmd_quote(bin_path), expected)
|
||||
|
||||
# test double quoted single quotes
|
||||
bin_path = '"\'C:\\Program Files\\salt\\test.exe\'"'
|
||||
self.assertEqual(win_service._cmd_quote(bin_path), expected)
|
||||
|
||||
# test single quoted, double quoted, single quotes
|
||||
bin_path = "\'\"\'C:\\Program Files\\salt\\test.exe\'\"\'"
|
||||
self.assertEqual(win_service._cmd_quote(bin_path), expected)
|
||||
|
|
|
@ -56,6 +56,7 @@ integration.states.test_pip_state
|
|||
integration.states.test_pkg
|
||||
integration.states.test_renderers
|
||||
integration.states.test_file
|
||||
integration.states.test_service
|
||||
integration.states.test_user
|
||||
integration.utils.testprogram
|
||||
integration.wheel.test_client
|
||||
|
|
Loading…
Add table
Reference in a new issue