Add mac_desktop integration tests

- Updates mac_desktop module to use cmd.run_all
- Updates current mac_desktop_test unit tests with cmd.run_all change
- Adds integration tests for mac_desktop execution module
This commit is contained in:
rallytime 2016-03-23 17:31:41 -06:00
parent 1af2e41a17
commit 6b92ef1924
3 changed files with 233 additions and 31 deletions

View File

@ -6,6 +6,7 @@ from __future__ import absolute_import
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError
# Define the module's virtual name
__virtualname__ = 'desktop'
@ -17,7 +18,7 @@ def __virtual__():
'''
if salt.utils.is_darwin():
return __virtualname__
return (False, 'Cannot load osxdesktop module: This is not a OSX host.')
return False, 'Cannot load OSX desktop module: This is not an OSX host.'
def get_output_volume():
@ -31,13 +32,22 @@ def get_output_volume():
salt '*' desktop.get_output_volume
'''
cmd = 'osascript -e "get output volume of (get volume settings)"'
call = __salt__['cmd.run_all'](
cmd,
output_loglevel='debug',
python_shell=False
)
_check_cmd(call)
return __salt__['cmd.run'](cmd)
return call.get('stdout')
def set_output_volume(volume):
'''
Set the volume of sound (range 0 to 100)
Set the volume of sound.
volume
The level of volume. Can range from 0 to 100.
CLI Example:
@ -46,15 +56,19 @@ def set_output_volume(volume):
salt '*' desktop.set_output_volume <volume>
'''
cmd = 'osascript -e "set volume output volume {0}"'.format(volume)
__salt__['cmd.run'](cmd, python_shell=False)
call = __salt__['cmd.run_all'](
cmd,
output_loglevel='debug',
python_shell=False
)
_check_cmd(call)
return get_output_volume()
def screensaver():
'''
Launch the screensaver
Launch the screensaver.
CLI Example:
@ -63,8 +77,14 @@ def screensaver():
salt '*' desktop.screensaver
'''
cmd = 'open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app'
call = __salt__['cmd.run_all'](
cmd,
output_loglevel='debug',
python_shell=False
)
_check_cmd(call)
return __salt__['cmd.run'](cmd, python_shell=False)
return True
def lock():
@ -78,14 +98,23 @@ def lock():
salt '*' desktop.lock
'''
cmd = '/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend'
call = __salt__['cmd.run_all'](
cmd,
output_loglevel='debug',
python_shell=False
)
_check_cmd(call)
return __salt__['cmd.run'](cmd, python_shell=False)
return True
def say(*words):
'''
Say some words.
words
The words to execute the say command with.
CLI Example:
.. code-block:: bash
@ -93,4 +122,29 @@ def say(*words):
salt '*' desktop.say <word0> <word1> ... <wordN>
'''
cmd = 'say {0}'.format(' '.join(words))
return __salt__['cmd.run'](cmd, python_shell=False)
call = __salt__['cmd.run_all'](
cmd,
output_loglevel='debug',
python_shell=False
)
_check_cmd(call)
return True
def _check_cmd(call):
'''
Check the output of the cmd.run_all function call.
'''
if call['retcode'] != 0:
comment = ''
std_err = call.get('stderr')
std_out = call.get('stdout')
if std_err:
comment += std_err
if std_out:
comment += std_out
raise CommandExecutionError('Error running command: {0}'.format(comment))
return call

View File

@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
'''
Integration tests for the mac_desktop execution module.
'''
# Import Python Libs
from __future__ import absolute_import
import os
# Import Salt Testing Libs
from salttesting import skipIf
from salttesting.helpers import (
destructiveTest,
ensure_in_syspath,
requires_system_grains
)
ensure_in_syspath('../../')
# Import Salt Libs
import integration
@skipIf(os.geteuid() != 0, 'You must be logged in as root to run this test')
class MacDesktopTestCase(integration.ModuleCase):
'''
Integration tests for the mac_desktop module.
'''
def setUp(self):
'''
Sets up test requirements.
'''
os_grain = self.run_function('grains.item', ['kernel'])
if os_grain['kernel'] not in 'Darwin':
self.skipTest(
'Test not applicable to \'{kernel}\' kernel'.format(
**os_grain
)
)
@requires_system_grains
def test_get_output_volume(self, grains=None):
'''
Tests the return of get_output_volume.
'''
ret = self.run_function('desktop.get_output_volume')
self.assertIsNotNone(ret)
@destructiveTest
@requires_system_grains
def test_set_output_volume(self, grains=None):
'''
Tests the return of set_output_volume.
'''
current_vol = self.run_function('desktop.get_output_volume')
to_set = 10
if current_vol == str(to_set):
to_set += 2
new_vol = self.run_function('desktop.set_output_volume', [str(to_set)])
check_vol = self.run_function('desktop.get_output_volume')
self.assertEqual(new_vol, check_vol)
# Set volume back to what it was before
self.run_function('desktop.set_output_volume', [current_vol])
@destructiveTest
@requires_system_grains
def test_screensaver(self, grains=None):
'''
Tests the return of the screensaver function.
'''
self.assertTrue(
self.run_function('desktop.screensaver')
)
@destructiveTest
@requires_system_grains
def test_lock(self, grains=None):
'''
Tests the return of the lock function.
'''
self.assertTrue(
self.run_function('desktop.lock')
)
@destructiveTest
@requires_system_grains
def test_say(self, grains=None):
'''
Tests the return of the say function.
'''
self.assertTrue(
self.run_function('desktop.say', ['hello', 'world'])
)
if __name__ == '__main__':
from integration import run_tests
run_tests(MacDesktopTestCase)

View File

@ -1,14 +1,11 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
Unit Tests for the mac_desktop execution module.
'''
# Import Python Libs
from __future__ import absolute_import
# Import Salt Libs
from salt.modules import mac_desktop
# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import (
@ -17,11 +14,14 @@ from salttesting.mock import (
NO_MOCK,
NO_MOCK_REASON
)
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt Libs
from salt.modules import mac_desktop
from salt.exceptions import CommandExecutionError
# Globals
mac_desktop.__salt__ = {}
@ -31,56 +31,104 @@ class MacDesktopTestCase(TestCase):
'''
Test cases for salt.modules.mac_desktop
'''
# 'get_output_volume' function tests: 1
# 'get_output_volume' function tests: 2
def test_get_output_volume(self):
'''
Test if it get the output volume (range 0 to 100)
'''
mock = MagicMock(return_value=True)
with patch.dict(mac_desktop.__salt__, {'cmd.run': mock}):
self.assertTrue(mac_desktop.get_output_volume())
mock = MagicMock(return_value={'retcode': 0, 'stdout': '25'})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertEqual(mac_desktop.get_output_volume(), '25')
# 'set_output_volume' function tests: 1
def test_get_output_volume_error(self):
'''
Tests that an error is raised when cmd.run_all errors
'''
mock = MagicMock(return_value={'retcode': 1})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertRaises(CommandExecutionError,
mac_desktop.get_output_volume)
# 'set_output_volume' function tests: 2
@patch('salt.modules.mac_desktop.get_output_volume',
MagicMock(return_value='25'))
def test_set_output_volume(self):
'''
Test if it set the volume of sound (range 0 to 100)
'''
mock = MagicMock(return_value=True)
with patch.dict(mac_desktop.__salt__, {'cmd.run': mock}):
self.assertTrue(mac_desktop.set_output_volume('my-volume'))
mock = MagicMock(return_value={'retcode': 0})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertTrue(mac_desktop.set_output_volume('25'))
# 'screensaver' function tests: 1
def test_set_output_volume_error(self):
'''
Tests that an error is raised when cmd.run_all errors
'''
mock = MagicMock(return_value={'retcode': 1})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertRaises(CommandExecutionError,
mac_desktop.set_output_volume,
'25')
# 'screensaver' function tests: 2
def test_screensaver(self):
'''
Test if it launch the screensaver
'''
mock = MagicMock(return_value=True)
with patch.dict(mac_desktop.__salt__, {'cmd.run': mock}):
mock = MagicMock(return_value={'retcode': 0})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertTrue(mac_desktop.screensaver())
# 'lock' function tests: 1
def test_screensaver_error(self):
'''
Tests that an error is raised when cmd.run_all errors
'''
mock = MagicMock(return_value={'retcode': 1})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertRaises(CommandExecutionError,
mac_desktop.screensaver)
# 'lock' function tests: 2
def test_lock(self):
'''
Test if it lock the desktop session
'''
mock = MagicMock(return_value=True)
with patch.dict(mac_desktop.__salt__, {'cmd.run': mock}):
mock = MagicMock(return_value={'retcode': 0})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertTrue(mac_desktop.lock())
# 'say' function tests: 1
def test_lock_error(self):
'''
Tests that an error is raised when cmd.run_all errors
'''
mock = MagicMock(return_value={'retcode': 1})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertRaises(CommandExecutionError,
mac_desktop.lock)
# 'say' function tests: 2
def test_say(self):
'''
Test if it says some words.
'''
mock = MagicMock(return_value=True)
with patch.dict(mac_desktop.__salt__, {'cmd.run': mock}):
mock = MagicMock(return_value={'retcode': 0})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertTrue(mac_desktop.say())
def test_say_error(self):
'''
Tests that an error is raised when cmd.run_all errors
'''
mock = MagicMock(return_value={'retcode': 1})
with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
self.assertRaises(CommandExecutionError,
mac_desktop.say)
if __name__ == '__main__':
from integration import run_tests