mirror of
https://github.com/valitydev/salt.git
synced 2024-11-09 01:36:48 +00:00
3184168365
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.
143 lines
5.4 KiB
Python
143 lines
5.4 KiB
Python
# coding: utf-8
|
|
|
|
# Import Python Libs
|
|
from __future__ import absolute_import
|
|
import os
|
|
|
|
# Import Salt Testing Libs
|
|
from tests.support.unit import skipIf
|
|
|
|
# Import 3rd-party libs
|
|
# pylint: disable=import-error
|
|
try:
|
|
import tornado.testing
|
|
import tornado.concurrent
|
|
from tornado.testing import AsyncTestCase
|
|
HAS_TORNADO = True
|
|
except ImportError:
|
|
HAS_TORNADO = False
|
|
|
|
# Let's create a fake AsyncHTTPTestCase so we can properly skip the test case
|
|
class AsyncTestCase(object):
|
|
pass
|
|
|
|
from salt.ext.six.moves import range # pylint: disable=redefined-builtin
|
|
# pylint: enable=import-error
|
|
|
|
try:
|
|
from salt.netapi.rest_tornado import saltnado
|
|
HAS_TORNADO = True
|
|
except ImportError:
|
|
HAS_TORNADO = False
|
|
|
|
# Import utility lib from tests
|
|
import salt.utils.event
|
|
from tests.unit.utils.test_event import eventpublisher_process, SOCK_DIR # pylint: disable=import-error
|
|
|
|
|
|
@skipIf(HAS_TORNADO is False, 'The tornado package needs to be installed')
|
|
class TestSaltnadoUtils(AsyncTestCase):
|
|
def test_any_future(self):
|
|
'''
|
|
Test that the Any Future does what we think it does
|
|
'''
|
|
# create a few futures
|
|
futures = []
|
|
for x in range(0, 3):
|
|
future = tornado.concurrent.Future()
|
|
future.add_done_callback(self.stop)
|
|
futures.append(future)
|
|
|
|
# create an any future, make sure it isn't immediately done
|
|
any_ = saltnado.Any(futures)
|
|
self.assertIs(any_.done(), False)
|
|
|
|
# finish one, lets see who finishes
|
|
futures[0].set_result('foo')
|
|
self.wait()
|
|
|
|
self.assertIs(any_.done(), True)
|
|
self.assertIs(futures[0].done(), True)
|
|
self.assertIs(futures[1].done(), False)
|
|
self.assertIs(futures[2].done(), False)
|
|
|
|
# make sure it returned the one that finished
|
|
self.assertEqual(any_.result(), futures[0])
|
|
|
|
futures = futures[1:]
|
|
# re-wait on some other futures
|
|
any_ = saltnado.Any(futures)
|
|
futures[0].set_result('foo')
|
|
self.wait()
|
|
self.assertIs(any_.done(), True)
|
|
self.assertIs(futures[0].done(), True)
|
|
self.assertIs(futures[1].done(), False)
|
|
|
|
|
|
@skipIf(HAS_TORNADO is False, 'The tornado package needs to be installed')
|
|
class TestEventListener(AsyncTestCase):
|
|
def setUp(self):
|
|
if not os.path.exists(SOCK_DIR):
|
|
os.makedirs(SOCK_DIR)
|
|
super(TestEventListener, self).setUp()
|
|
|
|
def test_simple(self):
|
|
'''
|
|
Test getting a few events
|
|
'''
|
|
with eventpublisher_process():
|
|
me = salt.utils.event.MasterEvent(SOCK_DIR)
|
|
event_listener = saltnado.EventListener({}, # we don't use mod_opts, don't save?
|
|
{'sock_dir': SOCK_DIR,
|
|
'transport': 'zeromq'})
|
|
self._finished = False # fit to event_listener's behavior
|
|
event_future = event_listener.get_event(self, 'evt1', self.stop) # get an event future
|
|
me.fire_event({'data': 'foo2'}, 'evt2') # fire an event we don't want
|
|
me.fire_event({'data': 'foo1'}, 'evt1') # fire an event we do want
|
|
self.wait() # wait for the future
|
|
|
|
# check that we got the event we wanted
|
|
self.assertTrue(event_future.done())
|
|
self.assertEqual(event_future.result()['tag'], 'evt1')
|
|
self.assertEqual(event_future.result()['data']['data'], 'foo1')
|
|
|
|
def test_set_event_handler(self):
|
|
'''
|
|
Test subscribing events using set_event_handler
|
|
'''
|
|
with eventpublisher_process():
|
|
me = salt.utils.event.MasterEvent(SOCK_DIR)
|
|
event_listener = saltnado.EventListener({}, # we don't use mod_opts, don't save?
|
|
{'sock_dir': SOCK_DIR,
|
|
'transport': 'zeromq'})
|
|
self._finished = False # fit to event_listener's behavior
|
|
event_future = event_listener.get_event(self,
|
|
tag='evt',
|
|
callback=self.stop,
|
|
timeout=1,
|
|
) # get an event future
|
|
me.fire_event({'data': 'foo'}, 'evt') # fire an event we do want
|
|
self.wait()
|
|
|
|
# check that we subscribed the event we wanted
|
|
self.assertEqual(len(event_listener.timeout_map), 0)
|
|
|
|
def test_timeout(self):
|
|
'''
|
|
Make sure timeouts work correctly
|
|
'''
|
|
with eventpublisher_process():
|
|
event_listener = saltnado.EventListener({}, # we don't use mod_opts, don't save?
|
|
{'sock_dir': SOCK_DIR,
|
|
'transport': 'zeromq'})
|
|
self._finished = False # fit to event_listener's behavior
|
|
event_future = event_listener.get_event(self,
|
|
tag='evt1',
|
|
callback=self.stop,
|
|
timeout=1,
|
|
) # get an event future
|
|
self.wait()
|
|
self.assertTrue(event_future.done())
|
|
with self.assertRaises(saltnado.TimeoutException):
|
|
event_future.result()
|