2016-03-09 23:17:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
integration tests for mac_system
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
2016-03-18 21:11:55 +00:00
|
|
|
from __future__ import absolute_import, print_function
|
2016-03-10 14:42:53 +00:00
|
|
|
import random
|
|
|
|
import string
|
2016-03-09 23:17:49 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf
|
2017-04-04 17:57:27 +00:00
|
|
|
from tests.support.helpers import destructiveTest, skip_if_not_root
|
2016-03-09 23:17:49 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
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.utils.path
|
|
|
|
import salt.utils.platform
|
2017-02-27 15:59:04 +00:00
|
|
|
from salt.ext.six.moves import range
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-10 17:16:06 +00:00
|
|
|
|
2016-03-10 14:42:53 +00:00
|
|
|
def __random_string(size=6):
|
|
|
|
'''
|
|
|
|
Generates a random username
|
|
|
|
'''
|
|
|
|
return 'RS-' + ''.join(
|
|
|
|
random.choice(string.ascii_uppercase + string.digits)
|
|
|
|
for x in range(size)
|
|
|
|
)
|
2016-03-09 23:17:49 +00:00
|
|
|
|
|
|
|
|
2016-03-17 21:51:05 +00:00
|
|
|
SET_COMPUTER_NAME = __random_string()
|
|
|
|
SET_SUBNET_NAME = __random_string()
|
|
|
|
|
|
|
|
|
2017-04-04 17:57:27 +00:00
|
|
|
@skip_if_not_root
|
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')
|
2017-04-03 16:04:09 +00:00
|
|
|
class MacSystemModuleTest(ModuleCase):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
|
|
|
Validate the mac_system module
|
|
|
|
'''
|
2016-03-23 21:28:54 +00:00
|
|
|
ATRUN_ENABLED = False
|
|
|
|
REMOTE_LOGIN_ENABLED = False
|
|
|
|
REMOTE_EVENTS_ENABLED = False
|
|
|
|
COMPUTER_NAME = ''
|
|
|
|
SUBNET_NAME = ''
|
|
|
|
KEYBOARD_DISABLED = False
|
2016-03-09 23:17:49 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
'''
|
|
|
|
Get current settings
|
|
|
|
'''
|
2016-03-23 21:28:54 +00:00
|
|
|
self.ATRUN_ENABLED = self.run_function('service.enabled', ['com.apple.atrun'])
|
|
|
|
self.REMOTE_LOGIN_ENABLED = self.run_function('system.get_remote_login')
|
|
|
|
self.REMOTE_EVENTS_ENABLED = self.run_function('system.get_remote_events')
|
|
|
|
self.COMPUTER_NAME = self.run_function('system.get_computer_name')
|
|
|
|
self.SUBNET_NAME = self.run_function('system.get_subnet_name')
|
|
|
|
self.KEYBOARD_DISABLED = self.run_function('system.get_disable_keyboard_on_lock')
|
2016-03-09 23:17:49 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
'''
|
|
|
|
Reset to original settings
|
|
|
|
'''
|
2016-03-23 21:28:54 +00:00
|
|
|
if not self.ATRUN_ENABLED:
|
2016-03-09 23:17:49 +00:00
|
|
|
atrun = '/System/Library/LaunchDaemons/com.apple.atrun.plist'
|
|
|
|
self.run_function('service.stop', [atrun])
|
|
|
|
|
2016-03-23 21:28:54 +00:00
|
|
|
self.run_function('system.set_remote_login', [self.REMOTE_LOGIN_ENABLED])
|
|
|
|
self.run_function('system.set_remote_events', [self.REMOTE_EVENTS_ENABLED])
|
|
|
|
self.run_function('system.set_computer_name', [self.COMPUTER_NAME])
|
|
|
|
self.run_function('system.set_subnet_name', [self.SUBNET_NAME])
|
2016-03-10 14:42:53 +00:00
|
|
|
self.run_function('system.set_disable_keyboard_on_lock',
|
2016-03-23 21:28:54 +00:00
|
|
|
[self.KEYBOARD_DISABLED])
|
2016-03-10 14:42:53 +00:00
|
|
|
|
|
|
|
@destructiveTest
|
2016-03-09 23:17:49 +00:00
|
|
|
def test_get_set_remote_login(self):
|
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_remote_login
|
|
|
|
Test system.set_remote_login
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
# Normal Functionality
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', [True]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertTrue(self.run_function('system.get_remote_login'))
|
2016-03-17 21:51:05 +00:00
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', [False]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertFalse(self.run_function('system.get_remote_login'))
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-17 21:51:05 +00:00
|
|
|
# Test valid input
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', [True]))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', [False]))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', ['yes']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', ['no']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', ['On']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', ['Off']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', [1]))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_login', [0]))
|
|
|
|
|
|
|
|
# Test invalid input
|
|
|
|
self.assertIn(
|
|
|
|
'Invalid String Value for Enabled',
|
|
|
|
self.run_function('system.set_remote_login', ['spongebob']))
|
|
|
|
|
2016-03-10 14:42:53 +00:00
|
|
|
@destructiveTest
|
|
|
|
def test_get_set_remote_events(self):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_remote_events
|
|
|
|
Test system.set_remote_events
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
# Normal Functionality
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', [True]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertTrue(self.run_function('system.get_remote_events'))
|
2016-03-17 21:51:05 +00:00
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', [False]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertFalse(self.run_function('system.get_remote_events'))
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-17 21:51:05 +00:00
|
|
|
# Test valid input
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', [True]))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', [False]))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', ['yes']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', ['no']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', ['On']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', ['Off']))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', [1]))
|
|
|
|
self.assertTrue(self.run_function('system.set_remote_events', [0]))
|
|
|
|
|
|
|
|
# Test invalid input
|
|
|
|
self.assertIn(
|
|
|
|
'Invalid String Value for Enabled',
|
|
|
|
self.run_function('system.set_remote_events', ['spongebob']))
|
|
|
|
|
2016-03-10 14:42:53 +00:00
|
|
|
@destructiveTest
|
|
|
|
def test_get_set_computer_name(self):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_computer_name
|
|
|
|
Test system.set_computer_name
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_computer_name', [SET_COMPUTER_NAME]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.run_function('system.get_computer_name'),
|
2016-03-17 21:51:05 +00:00
|
|
|
SET_COMPUTER_NAME)
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-10 14:42:53 +00:00
|
|
|
@destructiveTest
|
|
|
|
def test_get_set_subnet_name(self):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_subnet_name
|
|
|
|
Test system.set_subnet_name
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_subnet_name', [SET_SUBNET_NAME]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.run_function('system.get_subnet_name'),
|
2016-03-23 21:28:54 +00:00
|
|
|
SET_SUBNET_NAME)
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-10 14:42:53 +00:00
|
|
|
def test_get_list_startup_disk(self):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_startup_disk
|
|
|
|
Test system.list_startup_disks
|
|
|
|
Don't know how to test system.set_startup_disk as there's usually only
|
|
|
|
one startup disk available on a system
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
# Test list and get
|
2016-03-10 14:42:53 +00:00
|
|
|
ret = self.run_function('system.list_startup_disks')
|
2016-03-17 21:51:05 +00:00
|
|
|
self.assertIsInstance(ret, list)
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertIn(self.run_function('system.get_startup_disk'), ret)
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-17 21:51:05 +00:00
|
|
|
# Test passing set a bad disk
|
|
|
|
self.assertIn(
|
|
|
|
'Invalid value passed for path.',
|
|
|
|
self.run_function('system.set_startup_disk', ['spongebob']))
|
|
|
|
|
2016-09-07 18:01:38 +00:00
|
|
|
@skipIf(True, 'Skip this test until mac fixes it.')
|
2016-03-10 14:42:53 +00:00
|
|
|
def test_get_set_restart_delay(self):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_restart_delay
|
|
|
|
Test system.set_restart_delay
|
|
|
|
system.set_restart_delay does not work due to an apple bug, see docs
|
|
|
|
may need to disable this test as we can't control the delay value
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
# Normal Functionality
|
|
|
|
self.assertTrue(self.run_function('system.set_restart_delay', [90]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.run_function('system.get_restart_delay'),
|
2016-03-17 21:51:05 +00:00
|
|
|
'90 seconds')
|
|
|
|
|
|
|
|
# Pass set bad value for seconds
|
|
|
|
self.assertIn(
|
|
|
|
'Invalid value passed for seconds.',
|
2016-09-07 18:01:38 +00:00
|
|
|
self.run_function('system.set_restart_delay', [70]))
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-10 14:42:53 +00:00
|
|
|
def test_get_set_disable_keyboard_on_lock(self):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_disable_keyboard_on_lock
|
|
|
|
Test system.set_disable_keyboard_on_lock
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
# Normal Functionality
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertTrue(
|
2016-03-17 21:51:05 +00:00
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', [True]))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.get_disable_keyboard_on_lock'))
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-17 21:51:05 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', [False]))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertFalse(
|
2016-03-17 21:51:05 +00:00
|
|
|
self.run_function('system.get_disable_keyboard_on_lock'))
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-17 21:51:05 +00:00
|
|
|
# Test valid input
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', [True]))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', [False]))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', ['yes']))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', ['no']))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', ['On']))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', ['Off']))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', [1]))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock', [0]))
|
|
|
|
|
|
|
|
# Test invalid input
|
|
|
|
self.assertIn(
|
|
|
|
'Invalid String Value for Enabled',
|
|
|
|
self.run_function('system.set_disable_keyboard_on_lock',
|
|
|
|
['spongebob']))
|
|
|
|
|
2016-09-07 18:01:38 +00:00
|
|
|
@skipIf(True, 'Skip this test until mac fixes it.')
|
2016-03-10 14:42:53 +00:00
|
|
|
def test_get_set_boot_arch(self):
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-10 14:42:53 +00:00
|
|
|
Test system.get_boot_arch
|
|
|
|
Test system.set_boot_arch
|
2016-03-17 21:51:05 +00:00
|
|
|
system.set_boot_arch does not work due to an apple bug, see docs
|
2016-03-10 14:42:53 +00:00
|
|
|
may need to disable this test as we can't set the boot architecture
|
2016-03-09 23:17:49 +00:00
|
|
|
'''
|
2016-03-17 21:51:05 +00:00
|
|
|
# Normal Functionality
|
|
|
|
self.assertTrue(self.run_function('system.set_boot_arch', ['i386']))
|
|
|
|
self.assertEqual(self.run_function('system.get_boot_arch'), 'i386')
|
|
|
|
self.assertTrue(self.run_function('system.set_boot_arch', ['default']))
|
2016-03-10 14:42:53 +00:00
|
|
|
self.assertEqual(self.run_function('system.get_boot_arch'), 'default')
|
2016-03-09 23:17:49 +00:00
|
|
|
|
2016-03-17 21:51:05 +00:00
|
|
|
# Test invalid input
|
|
|
|
self.assertIn(
|
|
|
|
'Invalid value passed for arch',
|
|
|
|
self.run_function('system.set_boot_arch', ['spongebob']))
|