test_win_powercfg.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Libs
  5. import salt.states.win_powercfg as powercfg
  6. # Import Salt Testing Libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.unit import TestCase
  9. from tests.support.mock import (
  10. MagicMock,
  11. patch
  12. )
  13. class PowerCfgTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Validate the powercfg state
  16. '''
  17. def setup_loader_modules(self):
  18. return {powercfg: {}}
  19. def test_set_monitor(self):
  20. '''
  21. Test to make sure we can set the monitor timeout value
  22. '''
  23. ret = {
  24. 'changes': {
  25. 'monitor': {
  26. 'ac': {
  27. 'new': 0,
  28. 'old': 45
  29. }
  30. }
  31. },
  32. 'comment': 'Monitor timeout on AC power set to 0',
  33. 'name': 'monitor',
  34. 'result': True}
  35. get_monitor_side_effect = MagicMock(side_effect=[{"ac": 45, "dc": 22},
  36. {"ac": 0, "dc": 22}])
  37. with patch.dict(powercfg.__salt__,
  38. {"powercfg.get_monitor_timeout": get_monitor_side_effect,
  39. "powercfg.set_monitor_timeout": MagicMock(return_value=True)}):
  40. with patch.dict(powercfg.__opts__, {"test": False}):
  41. self.assertEqual(powercfg.set_timeout("monitor", 0), ret)
  42. def test_set_monitor_already_set(self):
  43. '''
  44. Test to make sure we can set the monitor timeout value
  45. '''
  46. ret = {'changes': {},
  47. 'comment': 'Monitor timeout on AC power is already set to 0',
  48. 'name': 'monitor',
  49. 'result': True}
  50. monitor_val = MagicMock(return_value={"ac": 0, "dc": 0})
  51. with patch.dict(powercfg.__salt__, {"powercfg.get_monitor_timeout": monitor_val}):
  52. self.assertEqual(powercfg.set_timeout("monitor", 0), ret)
  53. def test_set_monitor_test_true_with_change(self):
  54. '''
  55. Test to make sure set monitor works correctly with test=True with
  56. changes
  57. '''
  58. ret = {'changes': {},
  59. 'comment': 'Monitor timeout on AC power will be set to 0',
  60. 'name': 'monitor',
  61. 'result': None}
  62. get_monitor_return_value = MagicMock(return_value={"ac": 45, "dc": 22})
  63. with patch.dict(powercfg.__salt__, {"powercfg.get_monitor_timeout": get_monitor_return_value}):
  64. with patch.dict(powercfg.__opts__, {"test": True}):
  65. self.assertEqual(powercfg.set_timeout("monitor", 0), ret)
  66. def test_fail_invalid_setting(self):
  67. '''
  68. Test to make sure we can set the monitor timeout value
  69. '''
  70. ret = {'changes': {},
  71. 'comment': '"fakesetting" is not a valid setting',
  72. 'name': 'fakesetting',
  73. 'result': False}
  74. self.assertEqual(powercfg.set_timeout("fakesetting", 0), ret)
  75. def test_fail_invalid_power(self):
  76. '''
  77. Test to make sure we can set the monitor timeout value
  78. '''
  79. ret = {'changes': {},
  80. 'comment': '"fakepower" is not a power type',
  81. 'name': 'monitor',
  82. 'result': False}
  83. self.assertEqual(powercfg.set_timeout("monitor", 0, power="fakepower"),
  84. ret)