salt/tests/integration/modules/test_mac_power.py

337 lines
12 KiB
Python
Raw Normal View History

2016-03-18 21:55:57 +00:00
# -*- coding: utf-8 -*-
'''
integration tests for mac_power
'''
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
# Import Python libs
from __future__ import absolute_import, unicode_literals, print_function
2016-03-18 21:55:57 +00:00
# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.unit import skipIf
2017-06-06 18:19:45 +00:00
from tests.support.helpers import destructiveTest, skip_if_not_root, flaky
2016-03-18 21:55:57 +00:00
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
# Import Salt libs
import salt.utils.path
import salt.utils.platform
2016-03-18 21:55:57 +00:00
2016-09-02 15:02:28 +00:00
@skip_if_not_root
@flaky
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
@skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
@skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
class MacPowerModuleTest(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
def setUp(self):
'''
Get current settings
'''
# Get current settings
self.COMPUTER_SLEEP = self.run_function('power.get_computer_sleep')
self.DISPLAY_SLEEP = self.run_function('power.get_display_sleep')
self.HARD_DISK_SLEEP = self.run_function('power.get_harddisk_sleep')
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])
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
def test_restart_freeze(self):
2016-03-18 21:55:57 +00:00
'''
Test power.get_restart_freeze
Test power.set_restart_freeze
2016-03-18 23:11:35 +00:00
'''
# 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
@skip_if_not_root
@flaky
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
@skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
@skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
class MacPowerModuleTestSleepOnPowerButton(ModuleCase):
'''
Test power.get_sleep_on_power_button
Test power.set_sleep_on_power_button
'''
SLEEP_ON_BUTTON = None
def setUp(self):
'''
Check if function is available
Get existing value
'''
# Is the function available
ret = self.run_function('power.get_sleep_on_power_button')
if isinstance(ret, bool):
self.SLEEP_ON_BUTTON = self.run_function(
'power.get_sleep_on_power_button')
def tearDown(self):
'''
Reset to original value
'''
if self.SLEEP_ON_BUTTON is not None:
self.run_function(
'power.set_sleep_on_power_button', [self.SLEEP_ON_BUTTON])
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_BUTTON is None:
# Check for not available
ret = self.run_function('power.get_sleep_on_power_button')
self.assertIn('Error', ret)
else:
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'))
@skip_if_not_root
@flaky
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
@skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
@skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
class MacPowerModuleTestRestartPowerFailure(ModuleCase):
'''
Test power.get_restart_power_failure
Test power.set_restart_power_failure
'''
RESTART_POWER = None
def setUp(self):
'''
Check if function is available
Get existing value
'''
# Is the function available
ret = self.run_function('power.get_restart_power_failure')
if isinstance(ret, bool):
self.RESTART_POWER = ret
def tearDown(self):
'''
Reset to original value
'''
if self.RESTART_POWER is not None:
self.run_function(
'power.set_sleep_on_power_button', [self.SLEEP_ON_BUTTON])
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_POWER is None:
# Check for not available
ret = self.run_function('power.get_restart_power_failure')
self.assertIn('Error', ret)
else:
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'))
2016-03-18 23:36:49 +00:00
@skip_if_not_root
@flaky
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
@skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
@skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
class MacPowerModuleTestWakeOnNet(ModuleCase):
'''
Test power.get_wake_on_network
Test power.set_wake_on_network
'''
WAKE_ON_NET = None
def setUp(self):
2016-03-18 23:36:49 +00:00
'''
Check if function is available
Get existing value
2016-03-18 23:36:49 +00:00
'''
# Is the function available
ret = self.run_function('power.get_wake_on_network')
if isinstance(ret, bool):
self.WAKE_ON_NET = ret
2016-03-18 23:36:49 +00:00
def tearDown(self):
2016-03-18 23:36:49 +00:00
'''
Reset to original value
'''
if self.WAKE_ON_NET is not None:
self.run_function('power.set_wake_on_network', [self.WAKE_ON_NET])
def test_wake_on_network(self):
'''
Test power.get_wake_on_network
Test power.set_wake_on_network
2016-03-18 23:36:49 +00:00
'''
# If available on this system, test it
if self.WAKE_ON_NET is None:
# Check for not available
ret = self.run_function('power.get_wake_on_network')
self.assertIn('Error', ret)
else:
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'))
2016-09-01 17:12:32 +00:00
self.assertTrue(
self.run_function('power.set_wake_on_network', ['off']))
self.assertFalse(self.run_function('power.get_wake_on_network'))
@skip_if_not_root
@flaky
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
@skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
@skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
class MacPowerModuleTestWakeOnModem(ModuleCase):
'''
Test power.get_wake_on_modem
Test power.set_wake_on_modem
'''
WAKE_ON_MODEM = None
def setUp(self):
'''
Check if function is available
Get existing value
'''
# Is the function available
ret = self.run_function('power.get_wake_on_modem')
if isinstance(ret, bool):
self.WAKE_ON_MODEM = ret
def tearDown(self):
'''
Reset to original value
'''
if self.WAKE_ON_MODEM is not None:
self.run_function('power.set_wake_on_modem', [self.WAKE_ON_MODEM])
def test_wake_on_modem(self):
'''
Test power.get_wake_on_modem
Test power.set_wake_on_modem
'''
# If available on this system, test it
if self.WAKE_ON_MODEM is None:
# Check for not available
ret = self.run_function('power.get_wake_on_modem')
2016-09-01 17:12:32 +00:00
self.assertIn('Error', ret)
else:
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'))