salt/tests/integration/modules/mac_power.py

284 lines
11 KiB
Python
Raw Normal View History

2016-03-18 21:55:57 +00:00
# -*- coding: utf-8 -*-
'''
integration tests for mac_power
'''
# Import python libs
from __future__ import absolute_import, print_function
2016-09-01 23:39:11 +00:00
from six import string_types
2016-03-18 21:55:57 +00:00
# Import Salt Testing libs
2016-08-22 07:11:01 +00:00
from salttesting import skipIf
2016-03-18 21:55:57 +00:00
from salttesting.helpers import ensure_in_syspath, destructiveTest
ensure_in_syspath('../../')
# Import salt libs
import integration
import salt.utils
2016-08-22 07:12:09 +00:00
@skipIf(not salt.utils.is_darwin()
2016-09-01 17:12:32 +00:00
or not salt.utils.which('systemsetup')
or salt.utils.get_uid(salt.utils.get_user()) != 0, 'Test requirements not met')
2016-03-18 23:11:35 +00:00
class MacPowerModuleTest(integration.ModuleCase):
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
Validate the mac_power module
2016-03-18 21:55:57 +00:00
'''
2016-09-01 23:28:32 +00:00
COMPUTER_SLEEP = None
DISPLAY_SLEEP = None
HARD_DISK_SLEEP = None
WAKE_ON_MODEM = None
WAKE_ON_NET = None
RESTART_POWER = None
SLEEP_ON_BUTTON = None
WAKE_ON_MOD_AVAIL = None
WAKE_ON_NET_AVAIL = None
RESTART_POW_AVAIL = None
SLEEP_ON_POW_AVAIL = None
2016-03-18 21:55:57 +00:00
2016-09-01 23:28:32 +00:00
def __init__(self, arg):
super(self.__class__, self).__init__(arg)
# Determine if these functions are available
# This is necessary because they may not be available if this test is run
# on a laptop vs a desktop or in a vm
if self.RESTART_POW_AVAIL is None:
self.RESTART_POW_AVAIL = True
2016-09-01 23:28:32 +00:00
ret = self.run_function('power.get_restart_power_failure')
2016-09-01 23:39:11 +00:00
if isinstance(ret, string_types) and 'Error' in ret:
self.RESTART_POW_AVAIL = False
if self.SLEEP_ON_POW_AVAIL is None:
self.SLEEP_ON_POW_AVAIL = True
2016-09-01 23:28:32 +00:00
ret = self.run_function('power.get_sleep_on_power_button')
2016-09-01 23:39:11 +00:00
if isinstance(ret, string_types) and 'Error' in ret:
self.SLEEP_ON_POW_AVAIL = False
if self.WAKE_ON_NET_AVAIL is None:
self.WAKE_ON_NET_AVAIL = True
ret = self.run_function('power.get_wake_on_network')
2016-09-01 23:39:11 +00:00
if isinstance(ret, string_types) and 'Error' in ret:
self.WAKE_ON_NET_AVAIL = False
if self.WAKE_ON_MOD_AVAIL is None:
self.WAKE_ON_MOD_AVAIL = True
ret = self.run_function('power.get_wake_on_modem')
2016-09-01 23:39:11 +00:00
if isinstance(ret, string_types) and 'Error' in ret:
self.WAKE_ON_MOD_AVAIL = False
2016-09-01 23:28:32 +00:00
def setUp(self):
'''
Get current settings
'''
# Get current settings
2016-09-01 23:28:32 +00:00
if self.COMPUTER_SLEEP is None:
self.COMPUTER_SLEEP = self.run_function('power.get_computer_sleep')
if self.DISPLAY_SLEEP is None:
self.DISPLAY_SLEEP = self.run_function('power.get_display_sleep')
if self.HARD_DISK_SLEEP is None:
self.HARD_DISK_SLEEP = self.run_function('power.get_harddisk_sleep')
if self.WAKE_ON_MOD_AVAIL:
2016-09-01 23:28:32 +00:00
if self.WAKE_ON_MODEM is None:
self.WAKE_ON_MODEM = self.run_function('power.get_wake_on_modem')
if self.WAKE_ON_NET_AVAIL:
2016-09-01 23:28:32 +00:00
if self.WAKE_ON_NET is None:
self.WAKE_ON_NET = self.run_function('power.get_wake_on_network')
if self.RESTART_POW_AVAIL:
2016-09-01 23:28:32 +00:00
if self.RESTART_POWER is None:
self.RESTART_POWER = self.run_function('power.get_restart_power_failure')
if self.SLEEP_ON_POW_AVAIL:
2016-09-01 23:28:32 +00:00
if self.SLEEP_ON_BUTTON is None:
self.SLEEP_ON_BUTTON = self.run_function('power.get_sleep_on_power_button')
2016-03-18 21:55:57 +00:00
def tearDown(self):
'''
Reset to original settings
'''
2016-03-23 20:52:48 +00:00
self.run_function('power.set_computer_sleep', [self.COMPUTER_SLEEP])
self.run_function('power.set_display_sleep', [self.DISPLAY_SLEEP])
self.run_function('power.set_harddisk_sleep', [self.HARD_DISK_SLEEP])
if self.WAKE_ON_MOD_AVAIL:
2016-09-01 17:12:32 +00:00
self.run_function('power.set_wake_on_modem', [self.WAKE_ON_MODEM])
if self.WAKE_ON_NET_AVAIL:
2016-09-01 17:12:32 +00:00
self.run_function('power.set_wake_on_network', [self.WAKE_ON_NET])
if self.RESTART_POW_AVAIL:
2016-09-01 17:12:32 +00:00
self.run_function('power.set_restart_power_failure',
[self.RESTART_POWER])
if self.SLEEP_ON_POW_AVAIL:
2016-09-01 17:12:32 +00:00
self.run_function('power.set_sleep_on_power_button',
[self.SLEEP_ON_BUTTON])
2016-03-18 21:55:57 +00:00
@destructiveTest
2016-03-18 23:11:35 +00:00
def test_computer_sleep(self):
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
Test power.get_computer_sleep
Test power.set_computer_sleep
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
# Normal Functionality
self.assertTrue(self.run_function('power.set_computer_sleep', [90]))
2016-03-18 23:59:07 +00:00
self.assertEqual(
self.run_function('power.get_computer_sleep'), 'after 90 minutes')
2016-03-18 23:11:35 +00:00
self.assertTrue(self.run_function('power.set_computer_sleep', ['Off']))
self.assertEqual(self.run_function('power.get_computer_sleep'), 'Never')
2016-03-18 21:55:57 +00:00
# Test invalid input
self.assertIn(
2016-03-18 23:11:35 +00:00
'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]))
2016-03-18 21:55:57 +00:00
@destructiveTest
2016-03-18 23:11:35 +00:00
def test_display_sleep(self):
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
Test power.get_display_sleep
Test power.set_display_sleep
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
# Normal Functionality
self.assertTrue(self.run_function('power.set_display_sleep', [90]))
2016-03-18 23:59:07 +00:00
self.assertEqual(
self.run_function('power.get_display_sleep'), 'after 90 minutes')
2016-03-18 23:11:35 +00:00
self.assertTrue(self.run_function('power.set_display_sleep', ['Off']))
self.assertEqual(self.run_function('power.get_display_sleep'), 'Never')
2016-03-18 21:55:57 +00:00
# Test invalid input
self.assertIn(
2016-03-18 23:11:35 +00:00
'Invalid String Value for Minutes',
self.run_function('power.set_display_sleep', ['spongebob']))
2016-03-18 21:55:57 +00:00
self.assertIn(
2016-03-18 23:11:35 +00:00
'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]))
2016-03-18 21:55:57 +00:00
self.assertIn(
2016-03-18 23:11:35 +00:00
'Invalid Boolean Value for Minutes',
self.run_function('power.set_display_sleep', [True]))
2016-03-18 21:55:57 +00:00
2016-03-18 23:36:49 +00:00
@destructiveTest
2016-03-18 23:11:35 +00:00
def test_harddisk_sleep(self):
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
Test power.get_harddisk_sleep
Test power.set_harddisk_sleep
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
# Normal Functionality
self.assertTrue(self.run_function('power.set_harddisk_sleep', [90]))
2016-03-18 23:59:07 +00:00
self.assertEqual(
self.run_function('power.get_harddisk_sleep'), 'after 90 minutes')
2016-03-18 23:11:35 +00:00
self.assertTrue(self.run_function('power.set_harddisk_sleep', ['Off']))
self.assertEqual(self.run_function('power.get_harddisk_sleep'), 'Never')
2016-03-18 21:55:57 +00:00
# Test invalid input
self.assertIn(
2016-03-18 23:11:35 +00:00
'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]))
2016-03-18 21:55:57 +00:00
2016-03-18 23:11:35 +00:00
def test_wake_on_modem(self):
2016-03-18 21:55:57 +00:00
'''
2016-03-18 23:11:35 +00:00
Test power.get_wake_on_modem
Test power.set_wake_on_modem
'''
# If available on this system, test it
if self.WAKE_ON_MOD_AVAIL:
2016-09-01 17:12:32 +00:00
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'))
else:
# Check for failure if not a desktop
ret = self.run_function('power.get_wake_on_modem')
2016-09-01 17:12:32 +00:00
self.assertIn('Error', ret)
2016-03-18 23:36:49 +00:00
def test_wake_on_network(self):
'''
Test power.get_wake_on_network
Test power.set_wake_on_network
'''
# If available on this system, test it
if self.WAKE_ON_NET_AVAIL:
2016-09-01 17:12:32 +00:00
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'))
else:
# Check for failure if not a desktop
ret = self.run_function('power.get_wake_on_network')
2016-09-01 17:12:32 +00:00
self.assertIn('Error', ret)
2016-03-18 23:36:49 +00:00
def test_restart_power_failure(self):
'''
Test power.get_restart_power_failure
Test power.set_restart_power_failure
'''
# If available on this system, test it
if self.RESTART_POW_AVAIL:
2016-09-01 17:12:32 +00:00
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'))
else:
# Check for failure if not a desktop
ret = self.run_function('power.get_restart_power_failure')
2016-09-01 17:12:32 +00:00
self.assertIn('Error', ret)
2016-03-18 23:36:49 +00:00
def test_restart_freeze(self):
'''
Test power.get_restart_freeze
Test power.set_restart_freeze
'''
# Normal Functionality
self.assertTrue(self.run_function('power.set_restart_freeze', ['on']))
self.assertTrue(self.run_function('power.get_restart_freeze'))
# This will return False because mac fails to actually make the change
self.assertFalse(
self.run_function('power.set_restart_freeze', ['off']))
# Even setting to off returns true, it actually is never set
# This is an apple bug
self.assertTrue(self.run_function('power.get_restart_freeze'))
2016-03-18 23:36:49 +00:00
def test_sleep_on_power_button(self):
'''
Test power.get_sleep_on_power_button
Test power.set_sleep_on_power_button
'''
# If available on this system, test it
if self.SLEEP_ON_POW_AVAIL:
2016-09-01 17:12:32 +00:00
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'))
else:
ret = self.run_function('power.get_sleep_on_power_button')
2016-09-01 17:12:32 +00:00
self.assertIn('Error', ret)
2016-03-18 21:55:57 +00:00
if __name__ == '__main__':
from integration import run_tests
2016-03-18 23:11:35 +00:00
run_tests(MacPowerModuleTest)