Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression in service states with reload argument #56185

Merged
merged 8 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion salt/states/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def running(name,
time.sleep(init_delay)

# only force a change state if we have explicitly detected them
after_toggle_status = __salt__['service.status'](name, sig, **kwargs)
after_toggle_status = __salt__['service.status'](name, sig, **status_kwargs)
if 'service.enabled' in __salt__:
after_toggle_enable_status = __salt__['service.enabled'](name)
else:
Expand Down
79 changes: 74 additions & 5 deletions tests/unit/states/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from __future__ import absolute_import, print_function, unicode_literals

# Import Salt Testing Libs
from tests.support.helpers import destructiveTest
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase
from tests.support.mock import (
MagicMock,
patch,
)
from tests.support.unit import TestCase, skipIf
from tests.support.mock import MagicMock, patch

# Import Salt Libs
import salt.utils.platform
import salt.config
import salt.loader
import salt.states.service as service


Expand Down Expand Up @@ -251,3 +252,71 @@ def test_mod_watch(self):
ret[3])

self.assertDictEqual(service.mod_watch("salt", "stack"), ret[1])


@destructiveTest
@skipIf(salt.utils.platform.is_darwin(), "service.running is currently failing on OSX")
class ServiceTestCaseFunctional(TestCase, LoaderModuleMockMixin):
'''
Validate the service state
'''
def setup_loader_modules(self):
self.opts = salt.config.DEFAULT_MINION_OPTS.copy()
self.opts['grains'] = salt.loader.grains(self.opts)
self.utils = salt.loader.utils(self.opts)
self.modules = salt.loader.minion_mods(self.opts, utils=self.utils)

self.service_name = 'cron'
cmd_name = 'crontab'
os_family = self.opts['grains']['os_family']
os_release = self.opts['grains']['osrelease']
if os_family == 'RedHat':
self.service_name = 'crond'
elif os_family == 'Arch':
self.service_name = 'sshd'
cmd_name = 'systemctl'
elif os_family == 'MacOS':
self.service_name = 'org.ntp.ntpd'
if int(os_release.split('.')[1]) >= 13:
self.service_name = 'com.openssh.sshd'
elif os_family == 'Windows':
self.service_name = 'Spooler'

if os_family != 'Windows' and salt.utils.path.which(cmd_name) is None:
self.skipTest('{0} is not installed'.format(cmd_name))

return {
service: {
'__grains__': self.opts['grains'],
'__opts__': self.opts,
'__salt__': self.modules,
'__utils__': self.utils,
},
}

def setUp(self):
self.pre_srv_enabled = True if self.service_name in self.modules['service.get_enabled']() else False
self.post_srv_disable = False
if not self.pre_srv_enabled:
self.modules['service.enable'](self.service_name)
self.post_srv_disable = True

def tearDown(self):
if self.post_srv_disable:
self.modules['service.disable'](self.service_name)

def test_running_with_reload(self):
with patch.dict(service.__opts__, {'test': False}):
service.dead(self.service_name, enable=False)
result = service.running(name=self.service_name, enable=True, reload=False)

expected = {
'changes': {
self.service_name: True
},
'comment': 'Service {0} has been enabled, and is '
'running'.format(self.service_name),
'name': self.service_name,
'result': True
}
self.assertDictEqual(result, expected)