Merge pull request #32000 from twangboy/mac_power_int

Integration tests for mac_power.py
This commit is contained in:
Mike Place 2016-03-21 14:18:57 -06:00
commit a496806fa2
3 changed files with 300 additions and 41 deletions

View File

@ -8,8 +8,7 @@ from __future__ import absolute_import
# Import salt libs
import salt.utils
from salt.utils.mac_utils import execute_return_result, \
execute_return_success, parse_return, validate_enabled
import salt.utils.mac_utils
from salt.exceptions import SaltInvocationError
from salt.ext.six.moves import range
@ -40,31 +39,31 @@ def _validate_sleep(minutes):
# Must be a value between 1 and 180 or Never/Off
if isinstance(minutes, str):
if minutes.lower() in ['never', 'off']:
return minutes.lower()
return 'Never'
else:
msg = '\nMac Power: Invalid String Value for Minutes.\n' \
msg = 'Invalid String Value for Minutes.\n' \
'String values must be "Never" or "Off".\n' \
'Passed: {0}'.format(minutes)
raise SaltInvocationError(msg)
elif isinstance(minutes, bool):
if minutes:
msg = '\nMac Power: Invalid Boolean Value for Minutes.\n' \
msg = 'Invalid Boolean Value for Minutes.\n' \
'Boolean value "On" or "True" is not allowed.\n' \
'Salt CLI converts "On" to boolean True.\n' \
'Passed: {0}'.format(minutes)
raise SaltInvocationError(msg)
else:
return 'never'
return 'Never'
elif isinstance(minutes, int):
if minutes in range(1, 181):
return minutes
else:
msg = '\nMac Power: Invalid Integer Value for Minutes.\n' \
msg = 'Invalid Integer Value for Minutes.\n' \
'Integer values must be between 1 and 180.\n' \
'Passed: {0}'.format(minutes)
raise SaltInvocationError(msg)
else:
msg = '\nMac Power: Unknown Variable Type Passed for Minutes.\n' \
msg = 'Unknown Variable Type Passed for Minutes.\n' \
'Passed: {0}'.format(minutes)
raise SaltInvocationError(msg)
@ -110,7 +109,10 @@ def set_sleep(minutes):
'''
value = _validate_sleep(minutes)
cmd = 'systemsetup -setsleep {0}'.format(value)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_sleep() == {'Computer': value,
'Display': value,
'Hard Disk': value}
def get_computer_sleep():
@ -126,8 +128,9 @@ def get_computer_sleep():
salt '*' power.get_computer_sleep
'''
ret = execute_return_result('systemsetup -getcomputersleep')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getcomputersleep')
return salt.utils.mac_utils.parse_return(ret)
def set_computer_sleep(minutes):
@ -150,7 +153,8 @@ def set_computer_sleep(minutes):
'''
value = _validate_sleep(minutes)
cmd = 'systemsetup -setcomputersleep {0}'.format(value)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return str(value) in get_computer_sleep()
def get_display_sleep():
@ -166,8 +170,9 @@ def get_display_sleep():
salt '*' power.get_display_sleep
'''
ret = execute_return_result('systemsetup -getdisplaysleep')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getdisplaysleep')
return salt.utils.mac_utils.parse_return(ret)
def set_display_sleep(minutes):
@ -190,7 +195,8 @@ def set_display_sleep(minutes):
'''
value = _validate_sleep(minutes)
cmd = 'systemsetup -setdisplaysleep {0}'.format(value)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return str(value) in get_display_sleep()
def get_harddisk_sleep():
@ -206,8 +212,9 @@ def get_harddisk_sleep():
salt '*' power.get_harddisk_sleep
'''
ret = execute_return_result('systemsetup -getharddisksleep')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getharddisksleep')
return salt.utils.mac_utils.parse_return(ret)
def set_harddisk_sleep(minutes):
@ -230,7 +237,8 @@ def set_harddisk_sleep(minutes):
'''
value = _validate_sleep(minutes)
cmd = 'systemsetup -setharddisksleep {0}'.format(value)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return str(value) in get_harddisk_sleep()
def get_wake_on_modem():
@ -246,8 +254,10 @@ def get_wake_on_modem():
salt '*' power.get_wake_on_modem
'''
ret = execute_return_result('systemsetup -getwakeonmodem')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getwakeonmodem')
return salt.utils.mac_utils.validate_enabled(
salt.utils.mac_utils.parse_return(ret)) == 'on'
def set_wake_on_modem(enabled):
@ -268,9 +278,10 @@ def set_wake_on_modem(enabled):
salt '*' power.set_wake_on_modem True
'''
state = validate_enabled(enabled)
state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setwakeonmodem {0}'.format(state)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_wake_on_modem() == state
def get_wake_on_network():
@ -286,8 +297,10 @@ def get_wake_on_network():
salt '*' power.get_wake_on_network
'''
ret = execute_return_result('systemsetup -getwakeonnetworkaccess')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getwakeonnetworkaccess')
return salt.utils.mac_utils.validate_enabled(
salt.utils.mac_utils.parse_return(ret)) == 'on'
def set_wake_on_network(enabled):
@ -308,9 +321,10 @@ def set_wake_on_network(enabled):
salt '*' power.set_wake_on_network True
'''
state = validate_enabled(enabled)
state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setwakeonnetworkaccess {0}'.format(state)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_wake_on_network() == state
def get_restart_power_failure():
@ -326,8 +340,10 @@ def get_restart_power_failure():
salt '*' power.get_restart_power_failure
'''
ret = execute_return_result('systemsetup -getrestartpowerfailure')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getrestartpowerfailure')
return salt.utils.mac_utils.validate_enabled(
salt.utils.mac_utils.parse_return(ret)) == 'on'
def set_restart_power_failure(enabled):
@ -348,9 +364,10 @@ def set_restart_power_failure(enabled):
salt '*' power.set_restart_power_failure True
'''
state = validate_enabled(enabled)
state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setrestartpowerfailure {0}'.format(state)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_restart_power_failure() == state
def get_restart_freeze():
@ -366,8 +383,10 @@ def get_restart_freeze():
salt '*' power.get_restart_freeze
'''
ret = execute_return_result('systemsetup -getrestartfreeze')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getrestartfreeze')
return salt.utils.mac_utils.validate_enabled(
salt.utils.mac_utils.parse_return(ret)) == 'on'
def set_restart_freeze(enabled):
@ -390,9 +409,10 @@ def set_restart_freeze(enabled):
salt '*' power.set_restart_freeze True
'''
state = validate_enabled(enabled)
state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setrestartfreeze {0}'.format(state)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_restart_freeze() == state
def get_sleep_on_power_button():
@ -410,8 +430,10 @@ def get_sleep_on_power_button():
salt '*' power.get_sleep_on_power_button
'''
ret = execute_return_result('systemsetup -getallowpowerbuttontosleepcomputer')
return parse_return(ret)
ret = salt.utils.mac_utils.execute_return_result(
'systemsetup -getallowpowerbuttontosleepcomputer')
return salt.utils.mac_utils.validate_enabled(
salt.utils.mac_utils.parse_return(ret)) == 'on'
def set_sleep_on_power_button(enabled):
@ -431,6 +453,7 @@ def set_sleep_on_power_button(enabled):
salt '*' power.set_sleep_on_power_button True
'''
state = validate_enabled(enabled)
state = salt.utils.mac_utils.validate_enabled(enabled)
cmd = 'systemsetup -setallowpowerbuttontosleepcomputer {0}'.format(state)
return execute_return_success(cmd)
salt.utils.mac_utils.execute_return_success(cmd)
return get_sleep_on_power_button() == state

View File

@ -0,0 +1,236 @@
# -*- coding: utf-8 -*-
'''
integration tests for mac_power
'''
# Import python libs
from __future__ import absolute_import, print_function
# Import Salt Testing libs
from salttesting.helpers import ensure_in_syspath, destructiveTest
ensure_in_syspath('../../')
# Import salt libs
import integration
import salt.utils
COMPUTER_SLEEP = 0
DISPLAY_SLEEP = 0
HARD_DISK_SLEEP = 0
WAKE_ON_MODEM = False
WAKE_ON_NET = False
RESTART_POWER = False
RESTART_FREEZE = False
SLEEP_ON_BUTTON = False
def disabled(f):
def _decorator(f):
print('{0} has been disabled'.format(f.__name__))
return _decorator(f)
class MacPowerModuleTest(integration.ModuleCase):
'''
Validate the mac_power module
'''
def setUp(self):
'''
Get current settings
'''
if not salt.utils.is_darwin():
self.skipTest('Test only available on Mac OS X')
if not salt.utils.which('systemsetup'):
self.skipTest('Test requires systemsetup binary')
if salt.utils.get_uid(salt.utils.get_user()) != 0:
self.skipTest('Test requires root')
COMPUTER_SLEEP = self.run_function('power.get_computer_sleep')
DISPLAY_SLEEP = self.run_function('power.get_display_sleep')
HARD_DISK_SLEEP = self.run_function('power.get_harddisk_sleep')
WAKE_ON_MODEM = self.run_function('power.get_wake_on_modem')
WAKE_ON_NET = self.run_function('power.get_wake_on_network')
RESTART_POWER = self.run_function('power.get_restart_power_failure')
RESTART_FREEZE = self.run_function('power.get_restart_freeze')
SLEEP_ON_BUTTON = self.run_function('power.get_sleep_on_power_button')
def tearDown(self):
'''
Reset to original settings
'''
self.run_function('power.set_computer_sleep', [COMPUTER_SLEEP])
self.run_function('power.set_display_sleep', [DISPLAY_SLEEP])
self.run_function('power.set_harddisk_sleep', [HARD_DISK_SLEEP])
self.run_function('power.set_wake_on_modem', [WAKE_ON_MODEM])
self.run_function('power.set_wake_on_network', [WAKE_ON_NET])
self.run_function('power.set_restart_power_failure', [RESTART_POWER])
self.run_function('power.set_restart_freeze', [RESTART_FREEZE])
self.run_function('power.set_sleep_on_power_button', [SLEEP_ON_BUTTON])
@destructiveTest
def test_computer_sleep(self):
'''
Test power.get_computer_sleep
Test power.set_computer_sleep
'''
# Normal Functionality
self.assertTrue(self.run_function('power.set_computer_sleep', [90]))
self.assertEqual(
self.run_function('power.get_computer_sleep'), 'after 90 minutes')
self.assertTrue(self.run_function('power.set_computer_sleep', ['Off']))
self.assertEqual(self.run_function('power.get_computer_sleep'), 'Never')
# Test invalid input
self.assertIn(
'Invalid String Value for Minutes',
self.run_function('power.set_computer_sleep', ['spongebob']))
self.assertIn(
'Invalid Integer Value for Minutes',
self.run_function('power.set_computer_sleep', [0]))
self.assertIn(
'Invalid Integer Value for Minutes',
self.run_function('power.set_computer_sleep', [181]))
self.assertIn(
'Invalid Boolean Value for Minutes',
self.run_function('power.set_computer_sleep', [True]))
@destructiveTest
def test_display_sleep(self):
'''
Test power.get_display_sleep
Test power.set_display_sleep
'''
# Normal Functionality
self.assertTrue(self.run_function('power.set_display_sleep', [90]))
self.assertEqual(
self.run_function('power.get_display_sleep'), 'after 90 minutes')
self.assertTrue(self.run_function('power.set_display_sleep', ['Off']))
self.assertEqual(self.run_function('power.get_display_sleep'), 'Never')
# Test invalid input
self.assertIn(
'Invalid String Value for Minutes',
self.run_function('power.set_display_sleep', ['spongebob']))
self.assertIn(
'Invalid Integer Value for Minutes',
self.run_function('power.set_display_sleep', [0]))
self.assertIn(
'Invalid Integer Value for Minutes',
self.run_function('power.set_display_sleep', [181]))
self.assertIn(
'Invalid Boolean Value for Minutes',
self.run_function('power.set_display_sleep', [True]))
@destructiveTest
def test_harddisk_sleep(self):
'''
Test power.get_harddisk_sleep
Test power.set_harddisk_sleep
'''
# Normal Functionality
self.assertTrue(self.run_function('power.set_harddisk_sleep', [90]))
self.assertEqual(
self.run_function('power.get_harddisk_sleep'), 'after 90 minutes')
self.assertTrue(self.run_function('power.set_harddisk_sleep', ['Off']))
self.assertEqual(self.run_function('power.get_harddisk_sleep'), 'Never')
# Test invalid input
self.assertIn(
'Invalid String Value for Minutes',
self.run_function('power.set_harddisk_sleep', ['spongebob']))
self.assertIn(
'Invalid Integer Value for Minutes',
self.run_function('power.set_harddisk_sleep', [0]))
self.assertIn(
'Invalid Integer Value for Minutes',
self.run_function('power.set_harddisk_sleep', [181]))
self.assertIn(
'Invalid Boolean Value for Minutes',
self.run_function('power.set_harddisk_sleep', [True]))
@disabled
def test_wake_on_modem(self):
'''
Test power.get_wake_on_modem
Test power.set_wake_on_modem
Commands don't seem to be supported on el capitan. Perhaps it works on
OS X Server or older versions
'''
self.assertTrue(self.run_function('power.set_wake_on_modem', ['on']))
self.assertTrue(self.run_function('power.get_wake_on_modem'))
self.assertTrue(self.run_function('power.set_wake_on_modem', ['off']))
self.assertFalse(self.run_function('power.get_wake_on_modem'))
@disabled
def test_wake_on_network(self):
'''
Test power.get_wake_on_network
Test power.set_wake_on_network
Commands don't seem to be supported on el capitan. Perhaps it works on
OS X Server or older versions
'''
self.assertTrue(self.run_function('power.set_wake_on_network', ['on']))
self.assertTrue(self.run_function('power.get_wake_on_network'))
self.assertTrue(self.run_function('power.set_wake_on_network', ['off']))
self.assertFalse(self.run_function('power.get_wake_on_network'))
@disabled
def test_restart_power_failure(self):
'''
Test power.get_restart_power_failure
Test power.set_restart_power_failure
Commands don't seem to be supported on el capitan. Perhaps it works on
OS X Server or older versions
'''
self.assertTrue(
self.run_function('power.set_restart_power_failure', ['on']))
self.assertTrue(self.run_function('power.get_restart_power_failure'))
self.assertTrue(
self.run_function('power.set_restart_power_failure', ['off']))
self.assertFalse(self.run_function('power.get_restart_power_failure'))
@disabled
def test_restart_freeze(self):
'''
Test power.get_restart_freeze
Test power.set_restart_freeze
Though the set command completes successfully, the setting isn't
actually changed
'''
# Normal Functionality
self.assertTrue(self.run_function('power.set_restart_freeze', ['on']))
self.assertTrue(self.run_function('power.get_restart_freeze'))
self.assertTrue(self.run_function('power.set_restart_freeze', ['off']))
self.assertFalse(self.run_function('power.get_restart_freeze'))
@disabled
def test_sleep_on_power_button(self):
'''
Test power.get_sleep_on_power_button
Test power.set_sleep_on_power_button
Commands don't seem to be supported on el capitan. Perhaps it works on
OS X Server or older versions
'''
# Normal Functionality
self.assertTrue(
self.run_function('power.set_sleep_on_power_button', ['on']))
self.assertTrue(self.run_function('power.get_sleep_on_power_button'))
self.assertTrue(
self.run_function('power.set_sleep_on_power_button', ['off']))
self.assertFalse(self.run_function('power.get_sleep_on_power_button'))
if __name__ == '__main__':
from integration import run_tests
run_tests(MacPowerModuleTest)

View File

@ -43,9 +43,9 @@ class MacPowerTestCase(TestCase):
test _validate_sleep function with valid string
'''
self.assertEqual(mac_power._validate_sleep('never'),
'never')
'Never')
self.assertEqual(mac_power._validate_sleep('off'),
'off')
'Never')
def test_validate_sleep_invalid_string(self):
'''
@ -68,7 +68,7 @@ class MacPowerTestCase(TestCase):
test _validate_sleep function with False
'''
self.assertEqual(mac_power._validate_sleep(False),
'never')
'Never')
def test_validate_sleep_unexpected(self):
'''