2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2012-12-31 00:34:38 +00:00
|
|
|
import sys
|
2015-06-09 21:14:51 +00:00
|
|
|
import types
|
2012-12-31 00:34:38 +00:00
|
|
|
|
2015-06-11 21:39:39 +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
|
|
|
from salt.ext import six
|
2015-06-11 21:39:39 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import Salt Testing libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
2017-06-02 07:59:48 +00:00
|
|
|
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, Mock, patch, ANY
|
2012-12-31 00:34:38 +00:00
|
|
|
|
|
|
|
# wmi and pythoncom modules are platform specific...
|
2015-06-09 21:14:51 +00:00
|
|
|
wmi = types.ModuleType('wmi')
|
2012-12-31 00:34:38 +00:00
|
|
|
sys.modules['wmi'] = wmi
|
|
|
|
|
2015-06-09 21:14:51 +00:00
|
|
|
pythoncom = types.ModuleType('pythoncom')
|
2012-12-31 00:34:38 +00:00
|
|
|
sys.modules['pythoncom'] = pythoncom
|
|
|
|
|
2013-08-26 09:43:15 +00:00
|
|
|
if NO_MOCK is False:
|
2012-12-31 00:34:38 +00:00
|
|
|
WMI = Mock()
|
|
|
|
wmi.WMI = Mock(return_value=WMI)
|
|
|
|
pythoncom.CoInitialize = Mock()
|
2012-12-31 10:29:08 +00:00
|
|
|
pythoncom.CoUninitialize = Mock()
|
2012-12-31 00:34:38 +00:00
|
|
|
|
2014-04-30 19:06:27 +00:00
|
|
|
# This is imported late so mock can do its job
|
2012-12-31 00:34:38 +00:00
|
|
|
import salt.modules.win_status as status
|
|
|
|
|
2013-01-04 17:43:37 +00:00
|
|
|
|
2017-06-02 07:59:48 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
@skipIf(sys.stdin.encoding != 'UTF-8', 'UTF-8 encoding required for this test is not supported')
|
|
|
|
@skipIf(status.HAS_WMI is False, 'This test requires Windows')
|
2012-12-31 00:34:38 +00:00
|
|
|
class TestProcsBase(TestCase):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
TestCase.__init__(self, *args, **kwargs)
|
|
|
|
self.__processes = []
|
|
|
|
|
|
|
|
def add_process(
|
|
|
|
self,
|
|
|
|
pid=100,
|
|
|
|
cmd='cmd',
|
|
|
|
name='name',
|
|
|
|
user='user',
|
|
|
|
user_domain='domain',
|
|
|
|
get_owner_result=0):
|
|
|
|
process = Mock()
|
2013-06-24 22:53:59 +00:00
|
|
|
process.GetOwner = Mock(
|
|
|
|
return_value=(user_domain, get_owner_result, user)
|
|
|
|
)
|
2012-12-31 00:34:38 +00:00
|
|
|
process.ProcessId = pid
|
|
|
|
process.CommandLine = cmd
|
|
|
|
process.Name = name
|
|
|
|
self.__processes.append(process)
|
|
|
|
|
|
|
|
def call_procs(self):
|
|
|
|
WMI.win32_process = Mock(return_value=self.__processes)
|
|
|
|
self.result = status.procs()
|
|
|
|
|
|
|
|
|
|
|
|
class TestProcsCount(TestProcsBase):
|
|
|
|
def setUp(self):
|
|
|
|
self.add_process(pid=100)
|
|
|
|
self.add_process(pid=101)
|
|
|
|
self.call_procs()
|
|
|
|
|
|
|
|
def test_process_count(self):
|
|
|
|
self.assertEqual(len(self.result), 2)
|
|
|
|
|
|
|
|
def test_process_key_is_pid(self):
|
|
|
|
self.assertSetEqual(set(self.result.keys()), set([100, 101]))
|
|
|
|
|
|
|
|
|
|
|
|
class TestProcsAttributes(TestProcsBase):
|
|
|
|
def setUp(self):
|
|
|
|
self._expected_name = 'name'
|
|
|
|
self._expected_cmd = 'cmd'
|
|
|
|
self._expected_user = 'user'
|
|
|
|
self._expected_domain = 'domain'
|
|
|
|
pid = 100
|
|
|
|
self.add_process(
|
|
|
|
pid=pid,
|
|
|
|
cmd=self._expected_cmd,
|
|
|
|
user=self._expected_user,
|
|
|
|
user_domain=self._expected_domain,
|
|
|
|
get_owner_result=0)
|
|
|
|
self.call_procs()
|
|
|
|
self.proc = self.result[pid]
|
|
|
|
|
|
|
|
def test_process_cmd_is_set(self):
|
|
|
|
self.assertEqual(self.proc['cmd'], self._expected_cmd)
|
|
|
|
|
|
|
|
def test_process_name_is_set(self):
|
|
|
|
self.assertEqual(self.proc['name'], self._expected_name)
|
|
|
|
|
|
|
|
def test_process_user_is_set(self):
|
|
|
|
self.assertEqual(self.proc['user'], self._expected_user)
|
|
|
|
|
|
|
|
def test_process_user_domain_is_set(self):
|
|
|
|
self.assertEqual(self.proc['user_domain'], self._expected_domain)
|
|
|
|
|
|
|
|
|
|
|
|
class TestProcsUnicodeAttributes(TestProcsBase):
|
|
|
|
def setUp(self):
|
|
|
|
unicode_str = u'\xc1'
|
2015-06-11 21:39:39 +00:00
|
|
|
self.ustr = unicode_str.encode('utf8') if six.PY2 else unicode_str
|
2012-12-31 10:37:59 +00:00
|
|
|
pid = 100
|
2012-12-31 00:34:38 +00:00
|
|
|
self.add_process(
|
2012-12-31 10:37:59 +00:00
|
|
|
pid=pid,
|
2012-12-31 00:34:38 +00:00
|
|
|
user=unicode_str,
|
|
|
|
user_domain=unicode_str,
|
|
|
|
cmd=unicode_str,
|
|
|
|
name=unicode_str)
|
|
|
|
self.call_procs()
|
2012-12-31 10:37:59 +00:00
|
|
|
self.proc = self.result[pid]
|
2012-12-31 00:34:38 +00:00
|
|
|
|
|
|
|
def test_process_cmd_is_utf8(self):
|
2015-06-11 21:39:39 +00:00
|
|
|
self.assertEqual(self.proc['cmd'], self.ustr)
|
2012-12-31 00:34:38 +00:00
|
|
|
|
|
|
|
def test_process_name_is_utf8(self):
|
2015-06-11 21:39:39 +00:00
|
|
|
self.assertEqual(self.proc['name'], self.ustr)
|
2012-12-31 00:34:38 +00:00
|
|
|
|
|
|
|
def test_process_user_is_utf8(self):
|
2015-06-11 21:39:39 +00:00
|
|
|
self.assertEqual(self.proc['user'], self.ustr)
|
2012-12-31 00:34:38 +00:00
|
|
|
|
|
|
|
def test_process_user_domain_is_utf8(self):
|
2015-06-11 21:39:39 +00:00
|
|
|
self.assertEqual(self.proc['user_domain'], self.ustr)
|
2012-12-31 00:34:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestProcsWMIGetOwnerAccessDeniedWorkaround(TestProcsBase):
|
|
|
|
def setUp(self):
|
2013-06-24 22:53:59 +00:00
|
|
|
self.expected_user = 'SYSTEM'
|
|
|
|
self.expected_domain = 'NT AUTHORITY'
|
2012-12-31 00:34:38 +00:00
|
|
|
self.add_process(pid=0, get_owner_result=2)
|
|
|
|
self.add_process(pid=4, get_owner_result=2)
|
|
|
|
self.call_procs()
|
|
|
|
|
|
|
|
def test_user_is_set(self):
|
|
|
|
self.assertEqual(self.result[0]['user'], self.expected_user)
|
|
|
|
self.assertEqual(self.result[4]['user'], self.expected_user)
|
|
|
|
|
|
|
|
def test_process_user_domain_is_set(self):
|
|
|
|
self.assertEqual(self.result[0]['user_domain'], self.expected_domain)
|
|
|
|
self.assertEqual(self.result[4]['user_domain'], self.expected_domain)
|
|
|
|
|
|
|
|
|
|
|
|
class TestProcsWMIGetOwnerErrorsAreLogged(TestProcsBase):
|
|
|
|
def setUp(self):
|
|
|
|
self.expected_error_code = 8
|
|
|
|
self.add_process(get_owner_result=self.expected_error_code)
|
|
|
|
|
|
|
|
def test_error_logged_if_process_get_owner_fails(self):
|
|
|
|
with patch('salt.modules.win_status.log') as log:
|
|
|
|
self.call_procs()
|
|
|
|
log.warning.assert_called_once_with(ANY)
|
2013-06-24 22:53:59 +00:00
|
|
|
self.assertIn(
|
|
|
|
str(self.expected_error_code),
|
|
|
|
log.warning.call_args[0][0]
|
|
|
|
)
|
2012-12-31 00:34:38 +00:00
|
|
|
|
|
|
|
|
2012-12-31 10:16:50 +00:00
|
|
|
class TestEmptyCommandLine(TestProcsBase):
|
|
|
|
def setUp(self):
|
|
|
|
self.expected_error_code = 8
|
|
|
|
pid = 100
|
|
|
|
self.add_process(pid=pid, cmd=None)
|
|
|
|
self.call_procs()
|
|
|
|
self.proc = self.result[pid]
|
|
|
|
|
|
|
|
def test_cmd_is_empty_string(self):
|
|
|
|
self.assertEqual(self.proc['cmd'], '')
|
|
|
|
|
|
|
|
|
2013-02-17 12:05:11 +00:00
|
|
|
#class TestProcsComInitialization(TestProcsBase):
|
|
|
|
# def setUp(self):
|
|
|
|
# call_count = 5
|
|
|
|
# for _ in range(call_count):
|
|
|
|
# self.call_procs()
|
|
|
|
# self.expected_calls = [call()] * call_count
|
|
|
|
#
|
2013-05-01 23:06:17 +00:00
|
|
|
# def test_initialize_and_uninitialize_called(self):
|
2013-02-17 12:05:11 +00:00
|
|
|
# pythoncom.CoInitialize.assert_has_calls(self.expected_calls)
|
|
|
|
# pythoncom.CoUninitialize.assert_has_calls(self.expected_calls)
|