Skip to content
1 change: 1 addition & 0 deletions changelog/58143.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a fix for the mac_service modules where it would fail to load in some new services and crash on Big Sur.
49 changes: 29 additions & 20 deletions tests/unit/utils/test_mac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import salt.utils.mac_utils as mac_utils
import salt.utils.platform
from salt.exceptions import CommandExecutionError, SaltInvocationError
from salt.ext import six
from salt.ext.six.moves import range
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, MockTimedProc, mock_open, patch
from tests.support.unit import TestCase, skipIf
Expand Down Expand Up @@ -267,7 +265,7 @@ def test_available_services_dirs(

@patch("salt.utils.path.os_walk")
@patch("os.path.exists")
@patch("plistlib.readPlist" if six.PY2 else "plistlib.load")
@patch("plistlib.load")
def test_available_services_broken_symlink(
self, mock_read_plist, mock_exists, mock_os_walk
):
Expand Down Expand Up @@ -300,16 +298,9 @@ def test_available_services_broken_symlink(

@patch("salt.utils.path.os_walk")
@patch("os.path.exists")
@patch("plistlib.readPlist")
@patch("salt.utils.mac_utils.__salt__")
@patch("plistlib.readPlistFromString", create=True)
def test_available_services_binary_plist(
self,
mock_read_plist_from_string,
mock_run,
mock_read_plist,
mock_exists,
mock_os_walk,
self, mock_run, mock_exists, mock_os_walk,
):
"""
test available_services handles binary plist files.
Expand All @@ -326,9 +317,6 @@ def test_available_services_binary_plist(
if salt.utils.platform.is_windows():
file_path = "c:" + file_path

# Py3 plistlib knows how to handle binary plists without
# any extra work, so this test doesn't really do anything
# new.
ret = _run_available_services(plists)

expected = {
Expand All @@ -345,9 +333,8 @@ def test_available_services_binary_plist(
def test_available_services_invalid_file(self, mock_exists, mock_os_walk):
"""
test available_services excludes invalid files.

The py3 plistlib raises an InvalidFileException when a plist
file cannot be parsed. This test only asserts things for py3.
file cannot be parsed.
"""
results = {"/Library/LaunchAgents": ["com.apple.lla1.plist"]}
mock_os_walk.side_effect = _get_walk_side_effects(results)
Expand All @@ -364,12 +351,9 @@ def test_available_services_invalid_file(self, mock_exists, mock_os_walk):
self.assertEqual(len(ret), 0)

@patch("salt.utils.mac_utils.__salt__")
@patch("plistlib.readPlist")
@patch("salt.utils.path.os_walk")
@patch("os.path.exists")
def test_available_services_expat_error(
self, mock_exists, mock_os_walk, mock_read_plist, mock_run
):
def test_available_services_expat_error(self, mock_exists, mock_os_walk, mock_run):
"""
test available_services excludes files with expat errors.

Expand All @@ -394,6 +378,31 @@ def test_available_services_expat_error(

self.assertEqual(len(ret), 0)

@patch("salt.utils.mac_utils.__salt__")
@patch("salt.utils.path.os_walk")
@patch("os.path.exists")
def test_available_services_value_error(self, mock_exists, mock_os_walk, mock_run):
"""
test available_services excludes files with ValueErrors.
"""
results = {"/Library/LaunchAgents": ["com.apple.lla1.plist"]}
mock_os_walk.side_effect = _get_walk_side_effects(results)
mock_exists.return_value = True

file_path = os.sep + os.path.join(
"Library", "LaunchAgents", "com.apple.lla1.plist"
)
if salt.utils.platform.is_windows():
file_path = "c:" + file_path

mock_load = MagicMock()
mock_load.side_effect = ValueError
with patch("salt.utils.files.fopen", mock_open()):
with patch("plistlib.load", mock_load):
ret = mac_utils._available_services()

self.assertEqual(len(ret), 0)

def test_bootout_retcode_36_success(self):
"""
Make sure that if we run a `launchctl bootout` cmd and it returns
Expand Down