mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add tests for mac_utils
Need help with the `available_services` tests
This commit is contained in:
parent
b5f67130cc
commit
b429fc3e74
2 changed files with 149 additions and 3 deletions
|
@ -274,7 +274,7 @@ def launchctl(sub_cmd, *args, **kwargs):
|
|||
out = 'Failed to {0} service:\n'.format(sub_cmd)
|
||||
out += 'stdout: {0}\n'.format(ret['stdout'])
|
||||
out += 'stderr: {0}\n'.format(ret['stderr'])
|
||||
out += 'retcode: {0}\n'.format(ret['retcode'])
|
||||
out += 'retcode: {0}'.format(ret['retcode'])
|
||||
raise CommandExecutionError(out)
|
||||
else:
|
||||
return ret['stdout'] if return_stdout else True
|
||||
|
@ -321,8 +321,7 @@ def available_services():
|
|||
try:
|
||||
# This assumes most of the plist files
|
||||
# will be already in XML format
|
||||
with salt.utils.files.fopen(file_path):
|
||||
plist = plistlib.readPlist(true_path)
|
||||
plist = plistlib.readPlist(true_path)
|
||||
|
||||
except Exception:
|
||||
# If plistlib is unable to read the file we'll need to use
|
||||
|
|
|
@ -5,6 +5,7 @@ mac_utils tests
|
|||
|
||||
# Import python libs
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
import os
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
|
@ -165,3 +166,149 @@ class MacUtilsTestCase(TestCase):
|
|||
'''
|
||||
self.assertEqual(mac_utils.validate_enabled(False),
|
||||
'off')
|
||||
|
||||
def test_launchctl(self):
|
||||
'''
|
||||
test launchctl function
|
||||
'''
|
||||
mock_cmd = MagicMock(return_value={'retcode': 0,
|
||||
'stdout': 'success',
|
||||
'stderr': 'none'})
|
||||
with patch('salt.modules.cmdmod.run_all', mock_cmd) as m_run_all:
|
||||
ret = mac_utils.launchctl('enable', 'org.salt.minion')
|
||||
m_run_all.assert_called_with(
|
||||
['launchctl', 'enable', 'org.salt.minion'],
|
||||
python_shell=False)
|
||||
self.assertEqual(ret, True)
|
||||
|
||||
def test_launchctl_return_stdout(self):
|
||||
'''
|
||||
test launchctl function and return stdout
|
||||
'''
|
||||
mock_cmd = MagicMock(return_value={'retcode': 0,
|
||||
'stdout': 'success',
|
||||
'stderr': 'none'})
|
||||
with patch('salt.modules.cmdmod.run_all', mock_cmd) as m_run_all:
|
||||
ret = mac_utils.launchctl('enable',
|
||||
'org.salt.minion',
|
||||
return_stdout=True)
|
||||
m_run_all.assert_called_with(['launchctl', 'enable', 'org.salt.minion'],
|
||||
python_shell=False)
|
||||
self.assertEqual(ret, 'success')
|
||||
|
||||
def test_launchctl_error(self):
|
||||
'''
|
||||
test launchctl function returning an error
|
||||
'''
|
||||
mock_cmd = MagicMock(return_value={'retcode': 1,
|
||||
'stdout': 'failure',
|
||||
'stderr': 'test failure'})
|
||||
error = 'Failed to enable service:\n' \
|
||||
'stdout: failure\n' \
|
||||
'stderr: test failure\n' \
|
||||
'retcode: 1'
|
||||
with patch('salt.modules.cmdmod.run_all', mock_cmd) as m_run_all:
|
||||
try:
|
||||
mac_utils.launchctl('enable', 'org.salt.minion')
|
||||
except CommandExecutionError as exc:
|
||||
self.assertEqual(exc.message, error)
|
||||
m_run_all.assert_called_with(['launchctl', 'enable', 'org.salt.minion'],
|
||||
python_shell=False)
|
||||
|
||||
@patch('salt.utils.path.os_walk')
|
||||
@patch('os.path.exists')
|
||||
@patch('plistlib.readPlist')
|
||||
def test_available_services(self, mock_read_plist, mock_exists, mock_os_walk):
|
||||
'''
|
||||
test available_services
|
||||
'''
|
||||
mock_os_walk.side_effect = [
|
||||
[('/Library/LaunchAgents', [], ['com.apple.lla1.plist', 'com.apple.lla2.plist'])],
|
||||
[('/Library/LaunchDaemons', [], ['com.apple.lld1.plist', 'com.apple.lld2.plist'])],
|
||||
[('/System/Library/LaunchAgents', [], ['com.apple.slla1.plist', 'com.apple.slla2.plist'])],
|
||||
[('/System/Library/LaunchDaemons', [], ['com.apple.slld1.plist', 'com.apple.slld2.plist'])],
|
||||
]
|
||||
|
||||
mock_read_plist.side_effect = [
|
||||
MagicMock(Label='com.apple.lla1'),
|
||||
MagicMock(Label='com.apple.lla2'),
|
||||
MagicMock(Label='com.apple.lld1'),
|
||||
MagicMock(Label='com.apple.lld2'),
|
||||
MagicMock(Label='com.apple.slla1'),
|
||||
MagicMock(Label='com.apple.slla2'),
|
||||
MagicMock(Label='com.apple.slld1'),
|
||||
MagicMock(Label='com.apple.slld2'),
|
||||
]
|
||||
|
||||
mock_exists.return_value = True
|
||||
ret = mac_utils.available_services()
|
||||
|
||||
# Make sure it's a dict with 8 items
|
||||
self.assertTrue(isinstance(ret, dict))
|
||||
self.assertEqual(len(ret), 8)
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.lla1']['file_name'],
|
||||
'com.apple.lla1.plist')
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.lla1']['file_path'],
|
||||
os.path.realpath(
|
||||
os.path.join('/Library/LaunchAgents', 'com.apple.lla1.plist')))
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.slld2']['file_name'],
|
||||
'com.apple.slld2.plist')
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.slld2']['file_path'],
|
||||
os.path.realpath(
|
||||
os.path.join('/System/Library/LaunchDaemons', 'com.apple.slld2.plist')))
|
||||
|
||||
@patch('salt.utils.path.os_walk')
|
||||
@patch('os.path.exists')
|
||||
@patch('plistlib.readPlist')
|
||||
def test_available_services_broken_symlink(self, mock_read_plist, mock_exists, mock_os_walk):
|
||||
'''
|
||||
test available_services
|
||||
'''
|
||||
mock_os_walk.side_effect = [
|
||||
[('/Library/LaunchAgents', [], ['com.apple.lla1.plist', 'com.apple.lla2.plist'])],
|
||||
[('/Library/LaunchDaemons', [], ['com.apple.lld1.plist', 'com.apple.lld2.plist'])],
|
||||
[('/System/Library/LaunchAgents', [], ['com.apple.slla1.plist', 'com.apple.slla2.plist'])],
|
||||
[('/System/Library/LaunchDaemons', [], ['com.apple.slld1.plist', 'com.apple.slld2.plist'])],
|
||||
]
|
||||
|
||||
mock_read_plist.side_effect = [
|
||||
MagicMock(Label='com.apple.lla1'),
|
||||
MagicMock(Label='com.apple.lla2'),
|
||||
MagicMock(Label='com.apple.lld1'),
|
||||
MagicMock(Label='com.apple.lld2'),
|
||||
MagicMock(Label='com.apple.slld1'),
|
||||
MagicMock(Label='com.apple.slld2'),
|
||||
]
|
||||
|
||||
mock_exists.side_effect = [True, True, True, True, False, False, True, True]
|
||||
ret = mac_utils.available_services()
|
||||
|
||||
# Make sure it's a dict with 6 items
|
||||
self.assertTrue(isinstance(ret, dict))
|
||||
self.assertEqual(len(ret), 6)
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.lla1']['file_name'],
|
||||
'com.apple.lla1.plist')
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.lla1']['file_path'],
|
||||
os.path.realpath(
|
||||
os.path.join('/Library/LaunchAgents', 'com.apple.lla1.plist')))
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.slld2']['file_name'],
|
||||
'com.apple.slld2.plist')
|
||||
|
||||
self.assertEqual(
|
||||
ret['com.apple.slld2']['file_path'],
|
||||
os.path.realpath(
|
||||
os.path.join('/System/Library/LaunchDaemons', 'com.apple.slld2.plist')))
|
||||
|
|
Loading…
Add table
Reference in a new issue