Improve macOS service naming support (#57646)

* add in a check so if someone passes in a service name like salt-master the module converts over to the macOS naming convention of com.saltstack.salt.master

* add note to documentation

* blacken mac_service

* add changelog for fixing 57878

* remove comments in test

Co-authored-by: Gareth J. Greenaway <gareth@wiked.org>
Co-authored-by: Sage the Rage <36676171+sagetherage@users.noreply.github.com>
This commit is contained in:
Wesley Whetstone 2020-09-30 13:54:49 -07:00 committed by GitHub
parent 16af697deb
commit a8a7eec011
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 111 additions and 10 deletions

1
changelog/57878.fixed Normal file
View file

@ -0,0 +1 @@
Fixes an issue on macOS where if you try and restart the macOS using serivce.restart salt-minion it would fail because the service names are different on macOS.

View file

@ -20,20 +20,22 @@ This module has support for services in the following locations.
path and a ``runas`` user is NOT specified, the current console user will
be used to properly interact with the service.
.. note::
As of the Magnesium release, if a service name of ``salt-minion`` is passed this
module will convert it over to it's macOS equivalent name, in this case
to ``com.saltstack.salt.minion``. This is true for ``salt-master``
``salt-api``, and ``salt-syndic`` as well.
"""
# Import python libs
import logging
import os
# Import salt libs
import salt.utils.files
import salt.utils.path
import salt.utils.platform
import salt.utils.stringutils
from salt.exceptions import CommandExecutionError
# Import 3rd party libs
from salt.utils.versions import LooseVersion as _LooseVersion
# Define the module's virtual name
@ -45,6 +47,13 @@ __func_alias__ = {
log = logging.getLogger(__name__)
SALT_MAC_SERVICES = {
"salt-minion": "com.saltstack.salt.minion",
"salt-master": "com.saltstack.salt.master",
"salt-api": "com.saltstack.salt.api",
"salt-syndic": "com.saltstack.salt.syndic",
}
def __virtual__():
"""
@ -120,7 +129,9 @@ def _get_service(name):
:rtype: dict
"""
services = __utils__["mac_utils.available_services"]()
name = name.lower()
# fix the name differences between platforms
# salt-minion becomes com.saltstack.salt.minion
name = SALT_MAC_SERVICES.get(name, name).lower()
service = _name_in_services(name, services)

View file

@ -3,14 +3,9 @@
"""
# Import Python libs
import pytest
# Import Salt Libs
import salt.modules.mac_service as mac_service
from salt.exceptions import CommandExecutionError
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
@ -247,6 +242,100 @@ class MacServiceTestCase(TestCase, LoaderModuleMockMixin):
with patch.object(mac_service, "show", MagicMock(return_value=info)):
assert mac_service._always_running_service(srv_name) is False
def test_service_name_change_salt_minion(self):
srv_name = "salt-minion"
info = {
"com.saltstack.salt.minion": {
"file_name": "com.saltstack.salt.minion.plist",
"file_path": "/Library/LaunchDaemons/com.saltstack.salt.minion.plist",
"plist": {
"HardResourceLimits": {"NumberOfFiles": 100000},
"KeepAlive": True,
"Label": "com.saltstack.salt.minion",
"ProgramArguments": ["/opt/salt/bin/start-salt-minion.sh"],
"RunAtLoad": True,
"SoftResourceLimits": {"NumberOfFiles": 100000},
},
}
}
with patch.dict(
mac_service.__utils__,
{"mac_utils.available_services": MagicMock(return_value=info)},
):
assert (
mac_service._get_service(srv_name) == info["com.saltstack.salt.minion"]
)
def test_service_name_change_salt_master(self):
srv_name = "salt-master"
info = {
"com.saltstack.salt.master": {
"file_name": "com.saltstack.salt.master.plist",
"file_path": "/Library/LaunchDaemons/com.saltstack.salt.master.plist",
"plist": {
"HardResourceLimits": {"NumberOfFiles": 100000},
"KeepAlive": True,
"Label": "com.saltstack.salt.master",
"ProgramArguments": ["/opt/salt/bin/start-salt-master.sh"],
"RunAtLoad": True,
"SoftResourceLimits": {"NumberOfFiles": 100000},
},
}
}
with patch.dict(
mac_service.__utils__,
{"mac_utils.available_services": MagicMock(return_value=info)},
):
assert (
mac_service._get_service(srv_name) == info["com.saltstack.salt.master"]
)
def test_service_name_change_salt_api(self):
srv_name = "salt-api"
info = {
"com.saltstack.salt.api": {
"file_name": "com.saltstack.salt.api.plist",
"file_path": "/Library/LaunchDaemons/com.saltstack.salt.api.plist",
"plist": {
"HardResourceLimits": {"NumberOfFiles": 100000},
"KeepAlive": True,
"Label": "com.saltstack.salt.api",
"ProgramArguments": ["/opt/salt/bin/start-salt-api.sh"],
"RunAtLoad": True,
"SoftResourceLimits": {"NumberOfFiles": 100000},
},
}
}
with patch.dict(
mac_service.__utils__,
{"mac_utils.available_services": MagicMock(return_value=info)},
):
assert mac_service._get_service(srv_name) == info["com.saltstack.salt.api"]
def test_service_name_change_salt_syndic(self):
srv_name = "salt-syndic"
info = {
"com.saltstack.salt.syndic": {
"file_name": "com.saltstack.salt.syndic.plist",
"file_path": "/Library/LaunchDaemons/com.saltstack.salt.syndic.plist",
"plist": {
"HardResourceLimits": {"NumberOfFiles": 100000},
"KeepAlive": True,
"Label": "com.saltstack.salt.syndic",
"ProgramArguments": ["/opt/salt/bin/start-salt-syndic.sh"],
"RunAtLoad": True,
"SoftResourceLimits": {"NumberOfFiles": 100000},
},
}
}
with patch.dict(
mac_service.__utils__,
{"mac_utils.available_services": MagicMock(return_value=info)},
):
assert (
mac_service._get_service(srv_name) == info["com.saltstack.salt.syndic"]
)
def test_service_restart_already_loaded(self):
mock_cmd = MagicMock(return_value=True)
salt_dict = {