test_mac_desktop.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Unit Tests for the mac_desktop execution module.
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.modules.mac_desktop as mac_desktop
  16. from salt.exceptions import CommandExecutionError
  17. class MacDesktopTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.mac_desktop
  20. '''
  21. def setup_loader_modules(self):
  22. return {mac_desktop: {}}
  23. # 'get_output_volume' function tests: 2
  24. def test_get_output_volume(self):
  25. '''
  26. Test if it get the output volume (range 0 to 100)
  27. '''
  28. mock = MagicMock(return_value={'retcode': 0, 'stdout': '25'})
  29. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  30. self.assertEqual(mac_desktop.get_output_volume(), '25')
  31. def test_get_output_volume_error(self):
  32. '''
  33. Tests that an error is raised when cmd.run_all errors
  34. '''
  35. mock = MagicMock(return_value={'retcode': 1})
  36. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  37. self.assertRaises(CommandExecutionError,
  38. mac_desktop.get_output_volume)
  39. # 'set_output_volume' function tests: 2
  40. def test_set_output_volume(self):
  41. '''
  42. Test if it set the volume of sound (range 0 to 100)
  43. '''
  44. mock = MagicMock(return_value={'retcode': 0})
  45. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}), \
  46. patch('salt.modules.mac_desktop.get_output_volume',
  47. MagicMock(return_value='25')):
  48. self.assertTrue(mac_desktop.set_output_volume('25'))
  49. def test_set_output_volume_error(self):
  50. '''
  51. Tests that an error is raised when cmd.run_all errors
  52. '''
  53. mock = MagicMock(return_value={'retcode': 1})
  54. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  55. self.assertRaises(CommandExecutionError,
  56. mac_desktop.set_output_volume,
  57. '25')
  58. # 'screensaver' function tests: 2
  59. def test_screensaver(self):
  60. '''
  61. Test if it launch the screensaver
  62. '''
  63. mock = MagicMock(return_value={'retcode': 0})
  64. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  65. self.assertTrue(mac_desktop.screensaver())
  66. def test_screensaver_error(self):
  67. '''
  68. Tests that an error is raised when cmd.run_all errors
  69. '''
  70. mock = MagicMock(return_value={'retcode': 1})
  71. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  72. self.assertRaises(CommandExecutionError,
  73. mac_desktop.screensaver)
  74. # 'lock' function tests: 2
  75. def test_lock(self):
  76. '''
  77. Test if it lock the desktop session
  78. '''
  79. mock = MagicMock(return_value={'retcode': 0})
  80. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  81. self.assertTrue(mac_desktop.lock())
  82. def test_lock_error(self):
  83. '''
  84. Tests that an error is raised when cmd.run_all errors
  85. '''
  86. mock = MagicMock(return_value={'retcode': 1})
  87. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  88. self.assertRaises(CommandExecutionError,
  89. mac_desktop.lock)
  90. # 'say' function tests: 2
  91. def test_say(self):
  92. '''
  93. Test if it says some words.
  94. '''
  95. mock = MagicMock(return_value={'retcode': 0})
  96. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  97. self.assertTrue(mac_desktop.say())
  98. def test_say_error(self):
  99. '''
  100. Tests that an error is raised when cmd.run_all errors
  101. '''
  102. mock = MagicMock(return_value={'retcode': 1})
  103. with patch.dict(mac_desktop.__salt__, {'cmd.run_all': mock}):
  104. self.assertRaises(CommandExecutionError,
  105. mac_desktop.say)