2014-10-29 15:40:15 +00:00
|
|
|
# coding: utf-8
|
2014-11-20 17:16:09 +00:00
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import Python Libs
|
|
|
|
from __future__ import absolute_import
|
2014-10-29 15:40:15 +00:00
|
|
|
import os
|
|
|
|
|
2014-11-20 03:00:20 +00:00
|
|
|
# Import Salt Testing Libs
|
|
|
|
from salttesting.unit import skipIf
|
2014-11-20 17:16:09 +00:00
|
|
|
from salttesting.case import TestCase
|
2014-11-20 03:00:20 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath
|
2014-11-20 17:16:09 +00:00
|
|
|
ensure_in_syspath('../../..')
|
2014-11-20 03:00:20 +00:00
|
|
|
|
|
|
|
# Import 3rd-party libs
|
|
|
|
# pylint: disable=import-error
|
|
|
|
try:
|
|
|
|
import tornado.testing
|
|
|
|
import tornado.concurrent
|
2014-11-20 17:35:42 +00:00
|
|
|
from tornado.testing import AsyncTestCase
|
2014-11-20 03:00:20 +00:00
|
|
|
HAS_TORNADO = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_TORNADO = False
|
|
|
|
|
|
|
|
# Let's create a fake AsyncHTTPTestCase so we can properly skip the test case
|
2014-11-20 17:35:42 +00:00
|
|
|
class AsyncTestCase(object):
|
2014-11-20 03:00:20 +00:00
|
|
|
pass
|
2014-11-21 20:34:46 +00:00
|
|
|
|
|
|
|
from salt.ext.six.moves import range # pylint: disable=redefined-builtin
|
2014-11-20 03:00:20 +00:00
|
|
|
# pylint: enable=import-error
|
|
|
|
|
|
|
|
try:
|
|
|
|
from salt.netapi.rest_tornado import saltnado
|
|
|
|
HAS_TORNADO = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_TORNADO = False
|
2014-10-29 15:40:15 +00:00
|
|
|
|
2014-11-20 17:16:09 +00:00
|
|
|
# Import utility lib from tests
|
2014-11-20 18:22:19 +00:00
|
|
|
from unit.utils.event_test import eventpublisher_process, event, SOCK_DIR # pylint: disable=import-error
|
2014-10-29 15:40:15 +00:00
|
|
|
|
|
|
|
|
2014-11-20 17:16:09 +00:00
|
|
|
@skipIf(HAS_TORNADO is False, 'The tornado package needs to be installed')
|
2014-10-29 15:40:15 +00:00
|
|
|
class TestUtils(TestCase):
|
|
|
|
def test_batching(self):
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertEqual(1, saltnado.get_batch_size('1', 10))
|
|
|
|
self.assertEqual(2, saltnado.get_batch_size('2', 10))
|
2014-10-29 15:40:15 +00:00
|
|
|
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertEqual(1, saltnado.get_batch_size('10%', 10))
|
2014-10-29 15:40:15 +00:00
|
|
|
# TODO: exception in this case? The core doesn't so we shouldn't
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertEqual(11, saltnado.get_batch_size('110%', 10))
|
2014-10-29 15:40:15 +00:00
|
|
|
|
|
|
|
|
2014-11-20 17:16:09 +00:00
|
|
|
@skipIf(HAS_TORNADO is False, 'The tornado package needs to be installed')
|
|
|
|
class TestSaltnadoUtils(AsyncTestCase):
|
2014-10-30 14:57:19 +00:00
|
|
|
def test_any_future(self):
|
|
|
|
'''
|
|
|
|
Test that the Any Future does what we think it does
|
|
|
|
'''
|
|
|
|
# create a few futures
|
|
|
|
futures = []
|
2014-11-21 20:34:46 +00:00
|
|
|
for x in range(0, 3):
|
2014-10-30 14:57:19 +00:00
|
|
|
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)
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertIs(any_.done(), False)
|
2014-10-30 14:57:19 +00:00
|
|
|
|
|
|
|
# finish one, lets see who finishes
|
|
|
|
futures[0].set_result('foo')
|
|
|
|
self.wait()
|
|
|
|
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertIs(any_.done(), True)
|
|
|
|
self.assertIs(futures[0].done(), True)
|
|
|
|
self.assertIs(futures[1].done(), False)
|
|
|
|
self.assertIs(futures[2].done(), False)
|
2014-10-30 14:57:19 +00:00
|
|
|
|
|
|
|
# make sure it returned the one that finished
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertEqual(any_.result(), futures[0])
|
2014-10-30 14:57:19 +00:00
|
|
|
|
2014-11-12 16:20:07 +00:00
|
|
|
futures = futures[1:]
|
|
|
|
# re-wait on some other futures
|
|
|
|
any_ = saltnado.Any(futures)
|
|
|
|
futures[0].set_result('foo')
|
2014-11-13 17:33:14 +00:00
|
|
|
self.wait()
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertIs(any_.done(), True)
|
|
|
|
self.assertIs(futures[0].done(), True)
|
|
|
|
self.assertIs(futures[1].done(), False)
|
2014-11-12 16:20:07 +00:00
|
|
|
|
2014-11-20 18:22:19 +00:00
|
|
|
|
2014-11-20 17:16:09 +00:00
|
|
|
@skipIf(HAS_TORNADO is False, 'The tornado package needs to be installed')
|
|
|
|
class TestEventListener(AsyncTestCase):
|
2014-10-30 15:05:57 +00:00
|
|
|
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 = event.MasterEvent(SOCK_DIR)
|
|
|
|
event_listener = saltnado.EventListener({}, # we don't use mod_opts, don't save?
|
|
|
|
{'sock_dir': SOCK_DIR,
|
|
|
|
'transport': 'zeromq'})
|
|
|
|
event_future = event_listener.get_event(1, '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
|
2014-11-20 16:17:12 +00:00
|
|
|
self.assertTrue(event_future.done())
|
|
|
|
self.assertEqual(event_future.result()['tag'], 'evt1')
|
|
|
|
self.assertEqual(event_future.result()['data']['data'], 'foo1')
|
2014-10-30 16:31:53 +00:00
|
|
|
|
2015-03-18 01:35:43 +00:00
|
|
|
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'})
|
|
|
|
event_future = event_listener.get_event(1,
|
|
|
|
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()
|
2014-10-30 15:05:57 +00:00
|
|
|
|
2014-10-29 15:40:15 +00:00
|
|
|
if __name__ == '__main__':
|
2014-11-20 18:22:19 +00:00
|
|
|
from integration import run_tests # pylint: disable=import-error
|
2014-10-29 15:40:15 +00:00
|
|
|
run_tests(TestUtils, needs_daemon=False)
|